Diana Software
MTimeSort.cc
Go to the documentation of this file.
1 /*
2  * Author: Laura Kogler <lkogler@berkeley.edu>
3  *
4  * Class MTimeSort: Sorts events by time
5  *
6 */
7 
8 
9 #include "MTimeSort.hh"
10 #include "QEventAssembler.hh"
11 #include "QRawEvent.hh"
12 #include "QVector.hh"
13 #include "QError.hh"
14 #include "QBaseType.hh"
15 #include <vector>
16 #include <algorithm>
17 #include <utility>
18 
19 using namespace Diana;
20 
22 
23 using namespace std;
24 
25 bool CompareTimes(const std::pair<int, unsigned long long> &EventAndTime1,
26  const std::pair<int, unsigned long long> &EventAndTime2)
27 {
28  return EventAndTime1.second < EventAndTime2.second;
29 }
30 
31 
32 // Init method is called before event loop
34  QEvent& ev = eva.GetEvent();
35  fIteration = GetIteration();
36  Action act;
37  act.fActionId = ACT_NEXTEV;
38 
39  if(fIteration == 1)
40  {
41  fOutputFilename = GetString("OutputFilename", "");
42  if(fOutputFilename != "")
43  {
44  bool notroot = (fOutputFilename.find(".root") == string::npos);
45  bool nottxt = (fOutputFilename.find(".txt") == string::npos);
46  if(notroot && nottxt)
47  {
48  Panic("Invalid filename for temporary data. Filename must end in .root or .txt");
49  }
50  }
51  }
52  else
53  {
54  fIndex = 0;
55  act.fActionId = ACT_GOTOEV;
56  act.fEventNumber = (fEventVector[fIndex]).first;
57  fIndex++;
58  }
59 
60  fStartTime=0;
61 
62  ev.Require<QHeader>("DAQ","Header");
63  ev.Require<QPulseInfo>("DAQ","PulseInfo");
64  return act;
65 }
66 
67 // Doit method is called for each event, getting the event as argument
69 {
70  Action act;
71  act.fActionId = ACT_NEXTEV;
72 
73  QEvent& ev = eva.GetEvent();
74 
75  if(fIteration==1)
76  {
77  // We will make a vector containing the tree index and time for each event
78  std::pair<int, unsigned long long> EventAndTime;
79  EventAndTime.first = ev.GetReadNumber(); // This returns the tree index
80  const QHeader& header = ev.Get<QHeader>("DAQ","Header");
81  const QPulseInfo& pulseInfo = ev.Get<QPulseInfo>("DAQ","PulseInfo");
82  const QTime& t = header.GetTime();
83  int chan = pulseInfo.GetChannelId();
84  EventAndTime.second = t.GetFromStartRunNs();
85  fEventVector.push_back(EventAndTime);
86  fChannelVector.push_back(chan);
87  if(fStartTime == 0)
88  fStartTime = t.GetStartRunUnix();
89  }
90  else
91  {
92  if(fIndex<fNumberOfEvents)
93  {
94 
95  act.fActionId = ACT_GOTOEV;
96  act.fEventNumber = (fEventVector[fIndex]).first;
97  fIndex++;
98  }
99  else
100  {
101  // this will stop the diana loop, otherwise it will continue til the last event in file
102  act.fActionId = ACT_GOTOEV;
103  act.fEventNumber = fNumberOfEvents;
104  }
105  }
106  return act;
107 }
108 
109 // Done method is called after event loop
111 
112  Action act;
113  act = ACT_NONE;
114  if(fIteration == 1)
115  {
116  sort(fEventVector.begin(), fEventVector.end(), CompareTimes);
117  std::pair<int, unsigned long long> EventAndTime;
118  fNumberOfEvents = fEventVector.size();
119 
120  if(fOutputFilename != "")
121  {
122  QVector EventList = QVector(fNumberOfEvents);
123  QVector TimeList = QVector(fNumberOfEvents); // Time in ms
124  QVector ChannelList = QVector(fNumberOfEvents);
125  for(int i=0; i<fNumberOfEvents; i++)
126  {
127  EventAndTime = fEventVector[i];
128  EventList[i] = (double)EventAndTime.first;
129  TimeList[i] = (double)EventAndTime.second/1.0e6;
130  ChannelList[i] = (double)fChannelVector[i];
131  }
132  GlobalData().Set("RunStartTime", QInt(fStartTime), fOutputFilename);
133  GlobalData().Set("EventList", EventList, fOutputFilename);
134  GlobalData().Set("TimeMs", TimeList, fOutputFilename);
135  GlobalData().Set("Channel", ChannelList, fOutputFilename);
136  }
137  if(GetBool("SortInPlace", true)) act = ACT_RERUN;
138  }
139  return act;
140 }
141 
bool CompareTimes(const std::pair< int, unsigned long long > &EventAndTime1, const std::pair< int, unsigned long long > &EventAndTime2)
Definition: MTimeSort.cc:25
QBaseType< int > QInt
int wrapped in a QObject
Definition: QBaseType.hh:174
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
Module for sorting events by time.
Definition: MTimeSort.hh:42
virtual void Init()
Definition: MTimeSort.cc:36
virtual Action Do(Diana::QEventAssembler &ev)
virtual Action Done()
Done method is called after event loop.
Definition: MTimeSort.cc:118
class to store ActionId and fEventNumber (in case fActionId=ACT_GOTOEV)
Definition: QDriver.hh:59
Visitor class of QEvent that provides full handling of QEvent.
QEvent & GetEvent()
Get the QEvent.
diana event
Definition: QEvent.hh:46
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
Raw event: basic information like run number and time.
Definition: QHeader.hh:16
const Diana::QTime & GetTime() const
get time
Definition: QHeader.hh:28
Raw event: bolometer channel, trigger positions and types.
Definition: QPulseInfo.hh:18
Diana time.
Definition: QTime.hh:17
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...