PAASS
Software suite to Acquire and Analyze Data from Pixie16
utilities.h
Go to the documentation of this file.
1 // utilities.h
2 // August 2010, DTM
3 
4 #ifndef __UTILITIES_H_
5 #define __UTILITIES_H_ 1
6 
7 #include <functional>
8 
9 #include "PixieInterface.h"
10 
11 template<typename T=int>
12 struct PixieFunctionParms
13 {
15  unsigned int mod;
16  unsigned int ch;
17  T par;
18 
19  PixieFunctionParms(PixieInterface &p, T x) : pif(p) {par=x;}
20 };
21 
22 template<typename T=int>
23 class PixieFunction :
24 public std::unary_function<bool, struct PixieFunctionParms<T> >
25 {
26  public:
27  virtual bool operator()(struct PixieFunctionParms<T> &par) = 0;
28  virtual ~PixieFunction() {};
29 };
30 
31 template<typename T>
32 bool forModule(PixieInterface &pif, int mod,
33  PixieFunction<T> &f, T par = T() );
34 template<typename T>
35 bool forChannel(PixieInterface &pif, int mod, int ch,
36  PixieFunction<T> &f, T par = T() );
37 
38 // implementation follows
39 // perform the function for the specified module and channel
40 // except if the channel < 0, do for all channels
41 // or if the module < 0, do for all modules
42 
43 template<typename T>
44 bool forChannel(PixieInterface &pif, int mod, int ch,
45  PixieFunction<T> &f, T par)
46 {
47  PixieFunctionParms<T> parms(pif, par);
48 
49  bool hadError = false;
50 
51  if (mod < 0) {
52  for (parms.mod = 0; parms.mod < pif.GetNumberCards(); parms.mod++) {
53  if (ch < 0) {
54  for (parms.ch = 0; parms.ch < pif.GetNumberChannels(); parms.ch++) {
55  if (!f(parms))
56  hadError = true;
57  }
58  } else {
59  parms.ch = ch;
60  if (!f(parms))
61  hadError = true;
62  }
63  }
64  } else {
65  parms.mod = mod;
66  if (ch < 0) {
67  for (parms.ch = 0; parms.ch < pif.GetNumberChannels(); parms.ch++) {
68  if (!f(parms))
69  hadError = true;
70  }
71  } else {
72  parms.ch = ch;
73  hadError = !f(parms);
74  }
75  }
76 
77  return !hadError;
78 }
79 
80 template<typename T>
82 {
83  PixieFunctionParms<T> parms(pif, par);
84  bool hadError = false;
85 
86  if (mod < 0) {
87  for (parms.mod = 0; parms.mod < pif.GetNumberCards(); parms.mod++) {
88  if (!f(parms))
89  hadError = true;
90  }
91  } else {
92  parms.mod = mod;
93  hadError = !f(parms);
94  }
95 
96  return !hadError;
97 }
98 
99 #endif // __UTILITIES_H_
unsigned int ch
Definition: PixieSupport.h:17
static size_t GetNumberChannels(void)
unsigned int mod
Definition: PixieSupport.h:16
PixieInterface * pif
Definition: PixieSupport.h:15
PixieInterface & pif
Definition: utilities.h:14
bool forModule(PixieInterface &pif, int mod, PixieFunction< T > &f, T par=T())
Definition: utilities.h:81
virtual ~PixieFunction()
Definition: utilities.h:28
PixieFunctionParms(PixieInterface &p, T x)
Definition: utilities.h:19
bool forChannel(PixieInterface &pif, int mod, int ch, PixieFunction< T > &f, T par=T())
Definition: utilities.h:44
unsigned short GetNumberCards(void) const