CbmRoot
CbmMCEventList.cxx
Go to the documentation of this file.
1 
5 #include "CbmMCEventList.h"
6 
7 #include <FairLogger.h> // for Logger, LOG
8 
9 #include <TNamed.h> // for TNamed
10 #include <TString.h> // for operator<<, TString
11 
12 #include <algorithm> // for lower_bound, sort
13 #include <cassert> // for assert
14 #include <iostream> // for operator<<, basic_ostream, stringstream
15 #include <string.h> // for strcmp, size_t
16 #include <string> // for char_traits
17 
18 using std::lower_bound;
19 using std::string;
20 using std::stringstream;
21 using std::vector;
22 
23 // ----- Constructor ------------------------------------------------------
25  : TNamed("MCEventList", "List of MC events"), fEvents(), fIsSorted(kFALSE) {}
26 // ----------------------------------------------------------------------------
27 
28 
29 // ----- Destructor -------------------------------------------------------
31 // ----------------------------------------------------------------------------
32 
33 
34 // ----- Check double occurrences of events -------------------------------
36 
37  Int_t lastFile = -1;
38  Int_t lastEvent = -1;
39  Double_t lastTime = 0.;
40  Int_t thisFile = -1;
41  Int_t thisEvent = -1;
42  Double_t thisTime = 0.;
43 
44  for (auto& eventInfo : fEvents) {
45  thisFile = eventInfo.GetFileId();
46  thisEvent = eventInfo.GetEventId();
47  thisTime = eventInfo.GetTime();
48  if (thisFile == lastFile && thisEvent == lastEvent) {
49  LOG(error) << fName << ": double entry for event " << thisEvent
50  << ", file " << thisFile << ", first time " << lastTime
51  << ", second time " << thisTime;
52  return kFALSE;
53  }
54  lastFile = thisFile;
55  lastEvent = thisEvent;
56  lastTime = thisTime;
57  }
58  return kTRUE;
59 }
60 // ----------------------------------------------------------------------------
61 
62 
63 // ----- Find an event in the list ----------------------------------------
64 vector<CbmMCEventInfo>::iterator CbmMCEventList::Find(UInt_t file,
65  UInt_t event) {
66  if (!fIsSorted) Sort();
67  auto it = lower_bound(
68  fEvents.begin(), fEvents.end(), CbmMCEventInfo(file, event, -1.));
69  if (it->GetFileId() != Int_t(file)) return fEvents.end();
70  if (it->GetEventId() != Int_t(event)) return fEvents.end();
71  return (it);
72 }
73 // ----------------------------------------------------------------------------
74 
75 
76 // ----- Get event number for event at index in list ----------------------
77 Int_t CbmMCEventList::GetEventIdByIndex(UInt_t index) {
78  if (!fIsSorted) Sort();
79  if (index >= GetNofEvents()) return -1;
80  return fEvents[index].GetEventId();
81 }
82 // ----------------------------------------------------------------------------
83 
84 
85 // ----- Get event time of a MC event -------------------------------------
86 Double_t CbmMCEventList::GetEventTime(UInt_t eventId, UInt_t fileId) {
87  if (!fIsSorted) Sort();
88  auto it = Find(fileId, eventId);
89  if (it == fEvents.end()) return -1.;
90  return it->GetTime();
91 }
92 // ----------------------------------------------------------------------------
93 
94 
95 // ----- Get event time for event at index in list ------------------------
96 Double_t CbmMCEventList::GetEventTimeByIndex(UInt_t index) {
97  if (!fIsSorted) Sort();
98  if (index >= GetNofEvents()) return -1.;
99  auto info = fEvents[index];
100  return fEvents[index].GetTime();
101 }
102 // ----------------------------------------------------------------------------
103 
104 
105 // ----- Get file number for event at index in list -----------------------
106 Int_t CbmMCEventList::GetFileIdByIndex(UInt_t index) {
107  if (!fIsSorted) Sort();
108  if (index >= GetNofEvents()) return -1;
109  auto info = fEvents[index];
110  return fEvents[index].GetFileId();
111 }
112 // ----------------------------------------------------------------------------
113 
114 
115 // ----- Insert an event --------------------------------------------------
116 Bool_t CbmMCEventList::Insert(UInt_t event, UInt_t file, Double_t time) {
117  if (time < 0.) return kFALSE;
118  fEvents.push_back(CbmMCEventInfo(file, event, time));
119  fIsSorted = kFALSE;
120  return kTRUE;
121 }
122 // ----------------------------------------------------------------------------
123 
124 
125 // ----- Print to screen --------------------------------------------------
126 void CbmMCEventList::Print(Option_t* opt) const {
127  std::cout << ToString(opt) << std::endl;
128 }
129 // ----------------------------------------------------------------------------
130 
131 
132 // ----- Sort events ------------------------------------------------------
134  if (fIsSorted) return;
135  std::sort(fEvents.begin(), fEvents.end());
136  assert(Check());
137  fIsSorted = kTRUE;
138 }
139 // ----------------------------------------------------------------------------
140 
141 
142 // ----- Status info ------------------------------------------------------
143 string CbmMCEventList::ToString(const char* option) const {
144  stringstream ss;
145  ss << fName << ": " << GetNofEvents() << " MC events in list\n";
146  if (!strcmp(option, "long"))
147  for (std::size_t index = 0; index < GetNofEvents(); index++)
148  ss << fEvents[index].ToString() << "\n";
149  return ss.str();
150 }
151 // ----------------------------------------------------------------------------
152 
153 
CbmMCEventList::GetFileIdByIndex
Int_t GetFileIdByIndex(UInt_t index)
File number by index @value File number for event at given index in list.
Definition: CbmMCEventList.cxx:106
CbmMCEventList::Sort
void Sort()
Sort the list.
Definition: CbmMCEventList.cxx:133
CbmMCEventList::Insert
Bool_t Insert(UInt_t event, UInt_t file, Double_t time)
Definition: CbmMCEventList.cxx:116
CbmMCEventList::fIsSorted
Bool_t fIsSorted
Definition: CbmMCEventList.h:131
CbmMCEventList::GetEventTimeByIndex
Double_t GetEventTimeByIndex(UInt_t index)
Event time by index @value Event time for event at given index in list.
Definition: CbmMCEventList.cxx:96
CbmMCEventInfo
Allows to access an MC event in the source file.
Definition: CbmMCEventInfo.h:27
CbmMCEventList::fEvents
std::vector< CbmMCEventInfo > fEvents
Definition: CbmMCEventList.h:128
CbmMCEventList::GetNofEvents
std::size_t GetNofEvents() const
Number of events in the list @value Number of events.
Definition: CbmMCEventList.h:90
CbmMCEventList::GetEventTime
Double_t GetEventTime(UInt_t event, UInt_t file)
Event start time.
Definition: CbmMCEventList.cxx:86
CbmMCEventList::ToString
std::string ToString(const char *option="") const
Definition: CbmMCEventList.cxx:143
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmMCEventList
Container class for MC events with number, file and start time.
Definition: CbmMCEventList.h:38
CbmMCEventList::CbmMCEventList
CbmMCEventList()
Standard constructor.
Definition: CbmMCEventList.cxx:24
CbmMCEventList.h
CbmMCEventList::Find
std::vector< CbmMCEventInfo >::iterator Find(UInt_t file, UInt_t event)
Find an element in the list.
Definition: CbmMCEventList.cxx:64
CbmMCEventList::GetEventIdByIndex
Int_t GetEventIdByIndex(UInt_t index)
Event number by index @value Event number for event at given index in list.
Definition: CbmMCEventList.cxx:77
CbmMCEventList::~CbmMCEventList
virtual ~CbmMCEventList()
Destructor.
Definition: CbmMCEventList.cxx:30
CbmMCEventList::Print
virtual void Print(Option_t *opt="") const
Definition: CbmMCEventList.cxx:126
CbmMCEventList::Check
Bool_t Check()
Check for double occurrences of events in list @value kTRUE is no double occurrences,...
Definition: CbmMCEventList.cxx:35