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 "QEvent.hh"
11 #include "QRawEvent.hh"
12 #include "QError.hh"
13 #include <vector>
14 #include <algorithm>
15 #include <utility>
16 
18 
19 using namespace std;
20 
21 bool CompareTimes(const std::pair<int, unsigned long long> &EventAndTime1,
22  const std::pair<int, unsigned long long> &EventAndTime2)
23 {
24  return EventAndTime1.second < EventAndTime2.second;
25 }
26 
27 // ctor
28 MTimeSort::MTimeSort(QSequence *s) : QModule("TimeSort",s) {
29 }
30 
31 // dtor
33 }
34 
35 // Init method is called before event loop
37 
39 
40  if(fIteration == 1)
41  {
42  fOutputFilename = GetString("OutputFilename", "");
43  if(fOutputFilename != "")
44  {
45  bool notroot = (fOutputFilename.find(".root") == string::npos);
46  bool nottxt = (fOutputFilename.find(".txt") == string::npos);
47  if(notroot && nottxt)
48  {
49  Panic("Invalid filename for temporary data. Filename must end in .root or .txt");
50  }
51  }
52  }
53  else
54  {
55  fIndex = 0;
56  JumpToEvent((fEventVector[fIndex]).first);
57  fIndex++;
58  }
59 
60  fStartTime=0;
61 }
62 
63 // Doit method is called for each event, getting the event as argument
65 
66  if(fIteration==1)
67  {
68  // We will make a vector containing the tree index and time for each event
69  std::pair<int, unsigned long long> EventAndTime;
70  EventAndTime.first = ev->GetReadNumber(); // This returns the tree index
71  const QRawEvent& raw = ev->GetRawEvent();
72  QTime t = raw.GetTime();
73  int chan = raw.GetPulse().GetChannelId();
74  EventAndTime.second = t.GetFromStartRunNs();
75  fEventVector.push_back(EventAndTime);
76  fChannelVector.push_back(chan);
77  if(fStartTime == 0)
79  }
80  else
81  {
83  {
84  QError err = JumpToEvent((fEventVector[fIndex]).first);
85  if(err != QERR_SUCCESS) Error("%s: %s",err.ToString().c_str(), err.GetDescription().c_str());
86  fIndex++;
87  }
88  else
89  {
90  // this will stop the diana loop, otherwise it will continue til the last event in file
91  QError err = JumpToEvent(fNumberOfEvents);
92  if(err != QERR_SUCCESS) Error("%s: %s",err.ToString().c_str(), err.GetDescription().c_str());
93  }
94  }
95  return ev;
96 }
97 
98 // Done method is called after event loop
99 void MTimeSort::Done() {
100 
101  if(fIteration == 1)
102  {
103  sort(fEventVector.begin(), fEventVector.end(), CompareTimes);
104  std::pair<int, unsigned long long> EventAndTime;
105  fNumberOfEvents = fEventVector.size();
106 
107  if(fOutputFilename != "")
108  {
109  QVector EventList = QVector(fNumberOfEvents);
110  QVector TimeList = QVector(fNumberOfEvents); // Time in ms
111  QVector ChannelList = QVector(fNumberOfEvents);
112  for(int i=0; i<fNumberOfEvents; i++)
113  {
114  EventAndTime = fEventVector[i];
115  EventList[i] = (double)EventAndTime.first;
116  TimeList[i] = (double)EventAndTime.second/1.0e6;
117  ChannelList[i] = (double)fChannelVector[i];
118  }
119  SeqAuxData().SetInt("RunStartTime", fStartTime, fOutputFilename);
120  SeqAuxData().SetQObject("EventList", &EventList, fOutputFilename);
121  SeqAuxData().SetQObject("TimeMs", &TimeList, fOutputFilename);
122  SeqAuxData().SetQObject("Channel", &ChannelList, fOutputFilename);
123  }
124  if(GetBool("SortInPlace", true)) SetRunAgain(true);
125  }
126  else
127  SetRunAgain(false);
128  return;
129 }
130 
err
Definition: CheckOF.C:114
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
@ QERR_SUCCESS
Definition: QError.hh:27
Module for sorting events by time.
Definition: MTimeSort.hh:42
virtual ~MTimeSort()
Definition: MTimeSort.cc:32
int fNumberOfEvents
Number of sorted events.
Definition: MTimeSort.hh:68
virtual void Init()
Definition: MTimeSort.cc:36
std::map< int, unsigned int > fStartTime
Start time of run.
Definition: MTimeSort.hh:71
virtual Action Do(Diana::QEventAssembler &ev)
MTimeSort(QSequence *s)
Definition: MTimeSort.cc:28
int fIteration
What iteration are we on?
Definition: MTimeSort.hh:65
std::map< int, std::vector< int > > fChannelVector
Vector of channel numbers.
Definition: MTimeSort.hh:74
std::string fOutputFilename
this is the name of the file where the sorted list is kept
Definition: MTimeSort.hh:55
std::map< int, std::vector< std::pair< int, unsigned long long > > > fEventVector
this is the name of the file where the sorted list is kept
Definition: MTimeSort.hh:59
virtual Action Done()
Done method is called after event loop.
Definition: MTimeSort.cc:118
int fIndex
An index to keep track of event.
Definition: MTimeSort.hh:62
const std::string & GetString(const std::string &parname, const std::string &defVal, bool warnCfg=true) const
Get a string parameter from config file ( see GetDouble() )
Definition: QBaseModule.cc:297
unsigned int GetIteration() const
Get Current sequence iteration.
Definition: QBaseModule.hh:122
void Error(const char *descr,...) const
Send an error message (an error that the framework cannot recover) with printf syntax.
Definition: QBaseModule.hh:238
void Panic(const char *descr,...) const
Send a panic message (stops the framework) with printf syntax.
Definition: QBaseModule.hh:248
bool GetBool(const std::string &parname, bool defVal, bool warnCfg=true) const
Get a bool parameter from config file ( see GetDouble() )
Definition: QBaseModule.cc:256
void SetRunAgain(bool b)
Set that the sequence will be reiterated.
Definition: QBaseModule.hh:120
error class with error type and description
Definition: QError.hh:115
diana event
Definition: QEvent.hh:46
unsigned int GetReadNumber() const
return the event number as read by the current reader.
Definition: QEvent.hh:60
Base class for diana modules.
Definition: QModule.hh:54
Diana Reconstruction program.
Definition: QSequence.hh:40
Diana time.
Definition: QTime.hh:17
unsigned long long GetFromStartRunNs() const
retrieve time from start of run in ns
Definition: QTime.hh:38
time_t GetStartRunUnix() const
retrieve time of start of run in seconds from 1/1/1970
Definition: QTime.hh:41