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  fSortByRun = GetBool("SortByRun",true,false);
37 
38  Action act;
39  act.fActionId = ACT_NEXTEV;
40 
41  eva.Add<QInt>("TimeSort","SortedIndex");
42 
43  if(fIteration >1){
44  //Go to first event in the first run
45  fIndex = 0;
46  act.fActionId = ACT_GOTOEV;
47  act.fEventNumber = (fEventVector.begin()->second[fIndex]).first;
48  fIndex++;
49  }
50 
51  fStartTime.clear();
52 
53  ev.Require<QHeader>("DAQ","Header");
54  ev.Require<QPulseInfo>("DAQ","PulseInfo");
55  return act;
56 }
57 
58 // Doit method is called for each event, getting the event as argument
60 {
61  Action act;
62  act.fActionId = ACT_NEXTEV;
63 
64  QEvent& ev = eva.GetEvent();
65  const QHeader& header = ev.Get<QHeader>("DAQ","Header");
66  int run=1;
67  if(fSortByRun){
68  run = header.GetRun();
69  }
70 
71  if(fIteration==1){
72  // We will make a vector containing the tree index and time for each event
73  std::pair<int, unsigned long long> EventAndTime;
74  EventAndTime.first = ev.GetReadNumber(); // This returns the tree index
75  const QPulseInfo& pulseInfo = ev.Get<QPulseInfo>("DAQ","PulseInfo");
76  const QTime& t = header.GetTime();
77  int chan = pulseInfo.GetChannelId();
78  EventAndTime.second = t.GetFromStartRunNs();
79  if(fEventVector.find(run) == fEventVector.end()){
80  //If run not in the list create it
81  fEventVector[run] = std::vector< std::pair<int, unsigned long long>>();
82  fChannelVector[run] = std::vector<int>();
83  fStartTime[run] = 0;
84  }
85  fEventVector[run].push_back(EventAndTime);
86  fChannelVector[run].push_back(chan);
87  if(fStartTime[run] == 0) fStartTime[run] = t.GetStartRunUnix();
88 
89  }else{
90  void * vtmp = (void*) &header;
91  QHeader * tmph = (QHeader*) vtmp;
92  QTime& t = tmph->GetTime();
93  t.SetStartRunUnix(fStartTime[run]);
94 
95  eva.Get<QInt>("TimeSort","SortedIndex") = fIndex;
96  if(fIndex<fNumberOfEvents){
97  act.fActionId = ACT_GOTOEV;
98  size_t index=fIndex;
99 
100  std::map<int,std::vector< std::pair<int, unsigned long long>>>::iterator it;
101  for(it=fEventVector.begin();it!=fEventVector.end();it++){
102  if(index>=it->second.size()){
103  index -= it->second.size();
104  }
105  }
106 
107  act.fEventNumber = (fEventVector[run][index]).first;
108  fIndex++;
109  }else{
110  // this will stop the diana loop, otherwise it will continue til the last event in file
111  act.fActionId = ACT_QUIT;
112  }
113  }
114  return act;
115 }
116 
117 // Done method is called after event loop
119 
120  Action act;
121  act = ACT_NONE;
122  if(fIteration == 1){
123  std::map<int,std::vector< std::pair<int, unsigned long long>>>::iterator it;
124  fNumberOfEvents=0;
125  for(it=fEventVector.begin();it!=fEventVector.end();it++){
126  sort(it->second.begin(), it->second.end(), CompareTimes);
127  fNumberOfEvents+=it->second.size();
128  }
129  if(GetBool("SortInPlace", true)) act = ACT_RERUN;
130  }
131  return act;
132 }
133 
bool CompareTimes(const std::pair< int, unsigned long long > &EventAndTime1, const std::pair< int, unsigned long long > &EventAndTime2)
Definition: MTimeSort.cc:25
#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
base types wrapped into a QObject. Currently implemented types are QInt QDouble and QFloat....
Definition: QBaseType.hh:17
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.
void Get(const char *owner, WriteHandle< Q > &handle)
Get QObject from the event in write mode. This method has to be called in the event loop,...
void Add(const char *owner, WriteHandle< Q > &handle)
Add QObject to the event. This method has to be called before the event loop, e.g....
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
int GetRun() const
destructor
Definition: QHeader.hh:22
Raw event: bolometer channel, trigger positions and types.
Definition: QPulseInfo.hh:18
const int & GetChannelId() const
Get ChannelId.
Definition: QPulseInfo.hh:22
Diana time.
Definition: QTime.hh:17
void SetStartRunUnix(unsigned int time)
set time of start of run in seconds from 1/1/1970
Definition: QTime.hh:32
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...