Diana Software
MTestNeighbours.cc
Go to the documentation of this file.
1 #include "MTestNeighbours.hh"
2 #include "QEvent.hh"
3 #include "QEventList.hh"
4 #include "QRawEvent.hh"
5 #include "QBool.hh"
6 #include <sstream>
7 #include <cmath>
8 
9 using namespace Diana;
10 
11 std::map<unsigned int, std::vector<int> > MTestNeighbours::fNeighMap;
12 
14 
15 using std::stringstream;
16 #define ASSERT_EQUAL(x,y)\
17 if(x==y) fTestOK++;\
18 else {\
19 fTestKO++;\
20 stringstream difference;\
21 difference<<x<<" vs "<<y;\
22 Error("Test failed: file %s; line %d: %s", __FILE__ , __LINE__,difference.str().c_str());\
23 }
24 
25 
26 
28 {
29  // called before event loop
30 
31  if(GetIteration() == 1) {
32  fTestOK=0ULL;
33  fTestKO=0ULL;
34  }
35 
36  if(GetSequence().GetName() == "TestNeigh") {
37  ev.Add<QBool>("UpdateNeighbour");
38 
39  } else if (GetSequence().GetName() == "TestNeighFilter" && GetOccurrence() == 1){
40  if(GetIteration() == 1) {
41  bool present = true;
42  try {
43  ev.RequireByLabel<QBool>("TestNeighbours@VariableIsPresent");
44  } catch (...) {
45  present = false;
46  }
47  ASSERT_EQUAL(present,false);
48  ev.Add<QBool>("VariableIsPresent");
49  SetRunAgain(true);
50  } else {
51  ev.RequireByLabel<QBool>("TestNeighbours@VariableIsPresent");
52  ASSERT_EQUAL(1,1);
53  }
54  }
55  fTime = 0ULL;
56  fPrevTime = 0ULL;
57  ev.Require<QPulseInfo>("DAQ","PulseInfo");
58 
59 
60 }
61 
62 void MTestNeighbours::Do(QEvent& ev, const QEventList& neighbours)
63 {
64  if(GetSequence().GetName() == "TestNeighFilter") {
65  static int passed = 0;
66  if(GetOccurrence() == 0) {
67  passed = 0;
68  for(size_t i = 0 ; i < neighbours.Size(); i++) {
69  const QEvent& other = neighbours[i];
70  const QPulseInfo& pInfo = other.Get<QPulseInfo>("","PulseInfo");
71  bool IsNoise = pInfo.GetIsNoise();
72  if(IsNoise) passed++;
73  }
74  return;
75  }
76  int newPassed = 0;
77  for(size_t i = 0 ; i < neighbours.Size(); i++) {
78  newPassed++;
79  const QEvent& other = neighbours[i];
80  const QPulseInfo& pInfo = other.Get<QPulseInfo>("DAQ","PulseInfo");
81  bool IsNoise = pInfo.GetIsNoise();
82  ASSERT_EQUAL(IsNoise,true);
83 
84  }
85  ASSERT_EQUAL(passed,newPassed);
86 
87  // Check that objects are disabled
88  ReadHandle<QPulse> readHandle("Pulse");
89  ev.Get("DAQ",readHandle);
90  return;
91  }
92  // called event by event
93  const QHeader& header = ev.Get<QHeader>("DAQ","Header");
94  const int eventnumber = ev.GetReadNumber();
95  const unsigned long long time = header.GetTime().GetFromStartRunNs();
96  const int hevnum = header.GetEventNumber();
97  if(GetIteration() == 2) {
98  fEventMap[eventnumber].fEventNumber = hevnum;
99  fEventMap[eventnumber].fTime = time;
100  return;
101  } else if(GetSequence().GetName() == "TimeSort") {
102  return;
103  } else if(GetSequence().GetName() == "TestNeigh") {
104  std::vector<int> thNeigh = fNeighMap[hevnum];
105  ASSERT_EQUAL(thNeigh.size(),neighbours.Size());
106  std::vector<int> neigh;
107  for(size_t n = 0; n < neighbours.Size(); n++) {
108  const QEvent& other = neighbours[n];
109  const QHeader& o_header = other.Get<QHeader>("DAQ","Header");
110  neigh.push_back(o_header.GetEventNumber());
111  }
112  std::sort(thNeigh.begin(),thNeigh.end());
113  std::sort(neigh.begin(),neigh.end());
114  ASSERT_EQUAL(thNeigh.size(),neigh.size());
115  for(size_t n = 0; n < neigh.size() && n<thNeigh.size(); n++) {
116  ASSERT_EQUAL(neigh[n],thNeigh[n]);
117  }
118  }
119 
120  ev.Get<QBool>("UpdateNeighbour") = true;
121 
122  if(fTime != 0ULL) {
123  ASSERT_EQUAL(time,fTime);
124  }
125  for(size_t i = 0 ; i < neighbours.Size(); i++) {
126  const QEvent& other = neighbours[i];
127  const QHeader& o_header = other.Get<QHeader>("DAQ","Header");
128  const unsigned long long o_time = o_header.GetTime().GetFromStartRunNs();
129  const int o_eventnumber = other.GetReadNumber();
130  ReadHandle<QBool> un("UpdateNeighbour");
131  other.Get<QBool>(GetName().c_str(),un);
132  double diff;
133  if(o_eventnumber < eventnumber) {
134  ASSERT_EQUAL(un.IsValid(),true);
135  ASSERT_EQUAL((o_time <= time),true);
136  diff = time - o_time;
137  fTime = 0ULL;
138  if(o_eventnumber == (eventnumber - 1)) {
139  ASSERT_EQUAL(o_time,fPrevTime);
140  }
141  } else if(o_eventnumber > eventnumber) {
142  if(o_eventnumber == (eventnumber + 1)) {
143  fTime = o_time;
144  }
145  ASSERT_EQUAL(un.IsValid(),false);
146  ASSERT_EQUAL((o_time >= time),true);
147  diff = o_time - time;
148  } else {
149  diff = 0;
150  ASSERT_EQUAL(0,1);
151  }
152  diff /= -1.e09;
153 
154  ASSERT_EQUAL((fabs(diff) < 1.000),true);
155  }
156 
157  fPrevTime = time;
158 }
159 
161 {
162 
163  if(GetIteration() == 2 && GetSequence().GetName() == "TestNeighFilter" && GetOccurrence() == 1) {
164  Info("----TEST SUMMARY-----");
165  Info("OK: %d", fTestOK);
166  if(fTestKO >0 ) Error("KO: %d", fTestKO);
167  if(fTestOK != 18689*2) Panic("Expected test %d vs %d",18689*2,fTestOK);
168  }
169 
170  if(GetIteration() == 2) {
171  std::map<unsigned int, EventInfo>::const_iterator iter = fEventMap.begin();
172  while(iter != fEventMap.end()) {
173  const double thisTime = iter->second.fTime;
174  const unsigned int thisEventNumber = iter->second.fEventNumber;
175 
176  std::map<unsigned int, EventInfo>::const_iterator oiter = fEventMap.begin();
177  fNeighMap[thisEventNumber].clear();
178  while(oiter != fEventMap.end()) {
179  const double otherTime = oiter->second.fTime;
180  const unsigned int otherEventNumber = oiter->second.fEventNumber;
181  if(fabs(thisTime-otherTime)/1.e09 < 1.000 && thisEventNumber != otherEventNumber) {
182  fNeighMap[thisEventNumber].push_back(otherEventNumber);
183  }
184  oiter++;
185  }
186  iter++;
187 
188  }
189  return;
190  } else if(GetSequence().GetName() == "TestNeigh" && GetOccurrence() == 0) {
191  Info("----TEST SUMMARY-----");
192  Info("OK: %d", fTestOK);
193  if(fTestKO > 0 ) Error("KO: %d", fTestKO);
194  if(fTestOK != 142632) Panic("Expected test %d vs %d",142632,fTestOK);
195  } else if( GetSequence().GetName() == "TestNeighFilter" && GetOccurrence() == 1) {
196  Info("----TEST SUMMARY-----");
197  Info("OK: %d", fTestOK);
198  if(fTestKO > 0 ) Error("KO: %d", fTestKO);
199  if(fTestOK != 18689) Panic("Expected test %d vs %d",18689,fTestOK);
200  }
201  // called at the end of the event loop
202 }
203 
double diff
Definition: CheckOFShape.C:173
#define ASSERT_EQUAL(x, y)
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
one-line description of your module
void Init(Diana::QEvent &ev)
Init method.
void Done()
Done method.
void Do(Diana::QEvent &ev, const Diana::QEventList &neighbours)
Do method.
static std::map< unsigned int, std::vector< int > > fNeighMap
bool wrapped into a QObject
Definition: QBool.hh:17
list of references to const QEvent (s)
Definition: QEventList.hh:21
size_t Size() const
number of QEvent (s)
Definition: QEventList.hh:36
diana event
Definition: QEvent.hh:46
void RequireByLabel(const QEventLabel &label) const
notify the QEvent that we need a QObject, if not found an exception is thrown
Definition: QEvent.hh:242
void Require(const std::string &owner, const std::string &name) const
notify the QEvent that we need a QObject, if not found an exception is thrown
Definition: QEvent.hh:232
void Get(const char *owner, ReadHandle< Q > &handle) const
Get a QObject Handle in read mode.
Definition: QEvent.hh:74
unsigned int GetReadNumber() const
return the event number as read by the current reader.
Definition: QEvent.hh:60
void Add(WriteHandle< Q > &handle)
Add a QObject to the event.
Definition: QEvent.hh:193
virtual bool IsValid() const
Check object validity.
Definition: QHandle.hh:34
Raw event: basic information like run number and time.
Definition: QHeader.hh:16
const Diana::QTime & GetTime() const
get time
Definition: QHeader.hh:28
int GetEventNumber() const
get EventNumber
Definition: QHeader.hh:25
Raw event: bolometer channel, trigger positions and types.
Definition: QPulseInfo.hh:18
bool GetIsNoise() const
Get IsNoise.
Definition: QPulseInfo.hh:54
read handle to access QEvent QObject's.
Definition: QHandle.hh:126
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...