CbmRoot
Loading...
Searching...
No Matches
CbmMCEventList.cxx
Go to the documentation of this file.
1/* Copyright (C) 2015-2020 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Volker Friese [committer] */
4
9#include "CbmMCEventList.h"
10
11#include <Logger.h> // for Logger, LOG
12
13#include <TNamed.h> // for TNamed
14#include <TString.h> // for operator<<, TString
15
16#include <algorithm> // for lower_bound, sort
17#include <cassert> // for assert
18#include <cstring> // for strcmp, size_t
19#include <iostream> // for operator<<, basic_ostream, stringstream
20#include <string> // for char_traits
21
22using std::lower_bound;
23using std::string;
24using std::stringstream;
25using std::vector;
26
27// ----- Constructor ------------------------------------------------------
28CbmMCEventList::CbmMCEventList() : TNamed("MCEventList", "List of MC events"), fEvents(), fIsSorted(false) {}
29// ----------------------------------------------------------------------------
30
31
32// ----- Destructor -------------------------------------------------------
34// ----------------------------------------------------------------------------
35
36
37// ----- Check double occurrences of events -------------------------------
39{
40
41 int32_t lastFile = -1;
42 int32_t lastEvent = -1;
43 double lastTime = 0.;
44 int32_t thisFile = -1;
45 int32_t thisEvent = -1;
46 double thisTime = 0.;
47
48 for (auto& eventInfo : fEvents) {
49 thisFile = eventInfo.GetFileId();
50 thisEvent = eventInfo.GetEventId();
51 thisTime = eventInfo.GetTime();
52 if (thisFile == lastFile && thisEvent == lastEvent) {
53 LOG(error) << fName << ": double entry for event " << thisEvent << ", file " << thisFile << ", first time "
54 << lastTime << ", second time " << thisTime;
55 return false;
56 }
57 lastFile = thisFile;
58 lastEvent = thisEvent;
59 lastTime = thisTime;
60 }
61 return true;
62}
63// ----------------------------------------------------------------------------
64
65
66// ----- Find an event in the list ----------------------------------------
67vector<CbmMCEventInfo>::iterator CbmMCEventList::Find(uint32_t file, uint32_t event)
68{
69 if (!fIsSorted) Sort();
70 auto it = lower_bound(fEvents.begin(), fEvents.end(), CbmMCEventInfo(file, event, -1.));
71 if (it->GetFileId() != int32_t(file)) return fEvents.end();
72 if (it->GetEventId() != int32_t(event)) return fEvents.end();
73 return (it);
74}
75// ----------------------------------------------------------------------------
76
77
78// ----- Get event number for event at index in list ----------------------
79int32_t CbmMCEventList::GetEventIdByIndex(uint32_t index)
80{
81 if (!fIsSorted) Sort();
82 if (index >= GetNofEvents()) return -1;
83 return fEvents[index].GetEventId();
84}
85// ----------------------------------------------------------------------------
86
87
88// ----- Get event time of a MC event -------------------------------------
89double CbmMCEventList::GetEventTime(uint32_t eventId, uint32_t fileId)
90{
91 if (!fIsSorted) Sort();
92 auto it = Find(fileId, eventId);
93 if (it == fEvents.end()) return -1.;
94 return it->GetTime();
95}
96// ----------------------------------------------------------------------------
97
98
99// ----- Get event index in the list -------------------------------------
100Int_t CbmMCEventList::GetEventIndex(UInt_t eventId, UInt_t fileId)
101{
102 if (!fIsSorted) Sort();
103 auto it = Find(fileId, eventId);
104 if (it == fEvents.end()) return -1.;
105 return (it - fEvents.begin());
106}
107// ----------------------------------------------------------------------------
108
109
110// ----- Get event time for event at index in list ------------------------
112{
113 if (!fIsSorted) Sort();
114 if (index >= GetNofEvents()) return -1.;
115 auto info = fEvents[index];
116 return fEvents[index].GetTime();
117}
118// ----------------------------------------------------------------------------
119
120
121// ----- Get file number for event at index in list -----------------------
122int32_t CbmMCEventList::GetFileIdByIndex(uint32_t index)
123{
124 if (!fIsSorted) Sort();
125 if (index >= GetNofEvents()) return -1;
126 auto info = fEvents[index];
127 return fEvents[index].GetFileId();
128}
129// ----------------------------------------------------------------------------
130
131// ----- Get file number for event at index in list -----------------------
133{
134 if (!fIsSorted) Sort();
135 if (index >= GetNofEvents()) return CbmLink();
136 return fEvents[index].GetCbmLink();
137}
138// ----------------------------------------------------------------------------
139
140
141// ----- Insert an event --------------------------------------------------
142bool CbmMCEventList::Insert(uint32_t event, uint32_t file, double time)
143{
144 if (time < 0.) return false;
145 fEvents.push_back(CbmMCEventInfo(file, event, time));
146 fIsSorted = false;
147 return true;
148}
149// ----------------------------------------------------------------------------
150
151
152// ----- Print to screen --------------------------------------------------
153void CbmMCEventList::Print(Option_t* opt) const { std::cout << ToString(opt) << std::endl; }
154// ----------------------------------------------------------------------------
155
156
157// ----- Sort events ------------------------------------------------------
159{
160 if (fIsSorted) return;
161 std::sort(fEvents.begin(), fEvents.end());
162 assert(Check());
163 fIsSorted = true;
164}
165// ----------------------------------------------------------------------------
166
167
168// ----- Status info ------------------------------------------------------
169string CbmMCEventList::ToString(const char* option) const
170{
171 stringstream ss;
172 ss << fName << ": " << GetNofEvents() << " MC events in list\n";
173 if (!strcmp(option, "long"))
174 for (std::size_t index = 0; index < GetNofEvents(); index++)
175 ss << fEvents[index].ToString() << "\n";
176 return ss.str();
177}
178// ----------------------------------------------------------------------------
179
180
ClassImp(CbmConverterManager)
Allows to access an MC event in the source file.
Container class for MC events with number, file and start time.
std::vector< CbmMCEventInfo > fEvents
CbmLink GetEventLinkByIndex(uint32_t index)
Event file and event indices as CbmLink.
int32_t GetFileIdByIndex(uint32_t index)
File number by index @value File number for event at given index in list.
std::string ToString(const char *option="") const
bool Check()
Check for double occurrences of events in list @value true is no double occurrences,...
virtual ~CbmMCEventList()
Destructor.
virtual void Print(Option_t *opt="") const
double GetEventTimeByIndex(uint32_t index)
Event time by index @value Event time for event at given index in list.
void Sort()
Sort the list.
int32_t GetEventIdByIndex(uint32_t index)
Event number by index @value Event number for event at given index in list.
Int_t GetEventIndex(UInt_t event, UInt_t file)
Event index.
std::size_t GetNofEvents() const
Number of events in the list @value Number of events.
double GetEventTime(uint32_t event, uint32_t file)
Event start time.
CbmMCEventList()
Standard constructor.
std::vector< CbmMCEventInfo >::iterator Find(uint32_t file, uint32_t event)
Find an element in the list.
bool Insert(uint32_t event, uint32_t file, double time)