BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnMSCMatrix.h
Go to the documentation of this file.
1#ifndef BMNMSCMATRIX_H
2#define BMNMSCMATRIX_H
3
4#include <chrono>
5#include <map>
6#include <vector>
7// Boost
8#include <boost/dynamic_bitset.hpp>
9// #include <boost/numeric/ublas/matrix_sparse.hpp>
10// ROOT
11// #include <TMatrixTSparse.h>
12#include <TObject.h>
13// FairRoot
14#include <FairLogger.h>
15// BmnRoot
16#include "BmnAliases.h"
17#include "BmnFunctionSet.h"
18
19using SysClock = std::chrono::system_clock;
20using SysPoint = std::chrono::time_point<SysClock>;
21
22using std::string;
23using std::vector;
24
25// namespace ublas = boost::numeric::ublas;
26
27// template class TMatrixTSparse<uint8_t>;
28// template class ublas::compressed_matrix<uint8_t>;
29
30/**
31 * Storage for all MSC counters of the spill
32 * @param times_count
33 * @param scaler_count
34 * it is assumed that time points entered by SetTime(...) are chronologically ordered
35 */
36template<typename IntType = uint8_t>
37class BmnMSCMatrix : public TObject
38{
39 typedef std::pair<SysPoint, SysPoint> TimeRange; // [begin,end) range
40 typedef vector<TimeRange> TimeRangeList;
41 // typedef map<SysPoint, IntType> TimeLine;
42 typedef std::pair<SysPoint, IntType> TimeLineEntry; // <timeslice start, count>
43 typedef vector<TimeLineEntry> TimeLine;
44 template<typename TimeLocationPair> // for TimeRange, TimeLineEntry
46 {
47 bool operator()(const TimeLocationPair& entry, const SysPoint& key) const { return entry.first < key; }
48 bool operator()(const SysPoint& key, const TimeLocationPair& entry) const { return key < entry.first; }
49 };
50 template<typename TimeLocationPair>
52 {
53 bool operator()(const TimeLocationPair& entry, const SysPoint& key) const { return entry.second < key; }
54 bool operator()(const SysPoint& key, const TimeLocationPair& entry) const { return key < entry.second; }
55 };
56
57 typedef std::pair<SysPoint, size_t> TimeMapEntry;
58 // typedef vector<TimeMapEntry> Time2Pos;
59 typedef map<SysPoint, size_t> Time2Pos;
60 typedef map<string, TimeLine> Scaler2TimeDist;
61
62 typedef std::map<uint8_t, TimeLine> ScalerIdx2TimeDist;
63
64 public:
65 BmnMSCMatrix(size_t times_count = 0,
66 // uint16_t scaler_count = 0,
67 NameChMap scalerName2Index = NameChMap{})
68 : fNRecords{times_count}
69 // , fNScalers{scalerName2Index.size()} // , fVector(fNRecords * fNScalers, 0)
70 // , fMatrix{fNRecords, fNScalers, false}
71 // , fMatrix{(int32_t)fNRecords, fNScalers}
72 , fScalerName2index{scalerName2Index}
75 // , fBitVecSpill(fNRecords, false)
76 // , fBitVecBusy(fNRecords, false)
77 {}
78
79 // IntType& Counts(size_t i_record, uint16_t i_scaler) { return fVector[i_record * fNScalers + i_scaler]; }
80 // auto& Counts() { return fMatrix; }
81
82 // /**
83 // * map (time point -> vector position)
84 // * @return
85 // */
86 // Time2Pos& TimeMap() { return fTimeMap; }
87
88 const IntType GetCount(size_t record_id, const string& scaler_name)
89 {
90 if (record_id >= GetNRecords())
91 return 0;
92 TimeLine& row = ScalerTimeLine(scaler_name);
93 SysPoint& time = fTimes[record_id];
94 return GetCntByTime(row, time);
95 }
96 const IntType GetCount(size_t record_id, uint16_t scaler_id)
97 {
98 if (record_id >= GetNRecords())
99 return 0;
100 TimeLine& row = ScalerTimeLine(scaler_id);
101 SysPoint& time = fTimes[record_id];
102 return GetCntByTime(row, time);
103 // return fMatrix(record_id, scaler_id);
104 }
105 const IntType GetCount(const SysPoint& time, const string& scaler_name)
106 {
107 auto it = fScalerName2index.find(scaler_name);
108 if (it == fScalerName2index.end()) {
109 throw std::out_of_range("Scaler not found");
110 }
111 return GetCount(time, it->second);
112 }
113 const IntType GetCount(const SysPoint& time, const uint16_t scaler_id)
114 {
115 TimeLine& row = ScalerTimeLine(scaler_id);
116 return GetCntByTime(row, time);
117 }
118
119 void SetCount(size_t record_id, uint16_t scaler_id, IntType val)
120 {
121 TimeLine& row = ScalerTimeLine(scaler_id);
122 SetCount(record_id, row, val);
123 // row[time] = val;
124 // fMatrix(record_id, scaler_id) = val;
125 }
126
127 void SetCount(size_t record_id, const string& scaler_name, IntType val)
128 {
129 TimeLine& row = ScalerTimeLine(scaler_name);
130 SetCount(record_id, row, val);
131 // row[time] = val;
132 }
133
134 SysPoint& GetTime(const size_t& record_id) { return fTimes[record_id]; }
135 TTimeStamp GetTS(const size_t& record_id) { return BmnFunctionSet::TimePoint2TS(fTimes[record_id]); }
136
137 /** we assume the user is filling the timestamps in the chronological order
138 * on the basis of preliminary created time map */
139 void SetTime(const size_t& record_id, const SysPoint& time)
140 {
141 fTimes[record_id] = time;
142 // fTimeMap[time] = record_id;
143 }
144 void SetTS(const size_t& record_id, const TTimeStamp& ts) { SetTime(record_id, BmnFunctionSet::TimeStamp2TP(ts)); }
145
147 // NameChMap& ScalerName2indexMap() { return fScalerName2index; }
148
149 TimeLine& ScalerTimeLine(const string& scaler_name)
150 {
151 auto it = fScalerName2index.find(scaler_name);
152 if (it == fScalerName2index.end()) {
153 throw std::out_of_range("Scaler name not found");
154 }
155 return ScalerTimeLine(it->second);
156 }
157 TimeLine& ScalerTimeLine(const uint16_t& scaler_id)
158 {
159 if (scaler_id >= fScalerName2index.size())
160 throw std::out_of_range("Scaler index not found");
161 return fMapVector[scaler_id];
162 }
163 auto GetScalerRange(const string& scaler_name,
164 const SysPoint& timeBegin = SysPoint::min(),
165 const SysPoint& timeEnd = SysPoint::max())
166 {
167 TimeLine& row = ScalerTimeLine(scaler_name);
168 auto itStart = std::lower_bound(row.begin(), row.end(), timeBegin, LessThanForFirst<TimeLineEntry>{});
169 auto itFinish = std::upper_bound(row.begin(), row.end(), timeEnd, LessThanForFirst<TimeLineEntry>{});
170 return make_pair(itStart, itFinish);
171 }
172 /**
173 * Returns the [begin,end) iterators for range of time point pairs representing
174 * begin and end of a spill time interval overlapping with the given interval
175 * @param timeBegin start time point
176 * @param timeEnd end time point
177 * @return
178 */
179 auto GetSpillRanges(const SysPoint& timeBegin = SysPoint::min(), const SysPoint& timeEnd = SysPoint::max())
180 {
181 return GetRanges(fSpillRangeVec, timeBegin, timeEnd);
182 }
183 /**
184 * Returns the [begin,end) iterators for range of time point pairs representing
185 * begin and end of a busy time interval overlapping with the given interval
186 * @param timeBegin start time point
187 * @param timeEnd end time point
188 * @return
189 */
190 auto GetBusyRanges(const SysPoint& timeBegin = SysPoint::min(), const SysPoint& timeEnd = SysPoint::max())
191 {
192 return GetRanges(fBusyRangeVec, timeBegin, timeEnd);
193 }
194 // Scaler count between time points
195 uint64_t GetCntInRange(const string& scaler_name,
196 const SysPoint& timeBegin = SysPoint::min(),
197 const SysPoint& timeEnd = SysPoint::max())
198 {
199 return GetCntInRange(ScalerTimeLine(scaler_name), timeBegin, timeEnd);
200 }
201
202 size_t GetRecordIdByTime(const SysPoint& timePoint)
203 {
204 auto it = std::lower_bound(fTimes.begin(), fTimes.end(), timePoint);
205 if (it == fTimes.end())
206 throw std::out_of_range("Time point not found");
207 return std::distance(it, fTimes.begin());
208 // auto it = fTimeMap.find(timePoint);
209 // if (it == fTimeMap.end())
210 // throw std::out_of_range("Time point not found");
211 // return it->second;
212 }
213
214 bool GetIsSpill(const SysPoint& time)
215 {
216 return IsInRange(fSpillRangeVec, time);
217 // size_t& record_id = GetRecordIdByTime(timePoint);
218 // return fBitVecSpill[record_id];
219 }
220
221 bool GetIsBusy(const SysPoint& time)
222 {
223 return IsInRange(fBusyRangeVec, time);
224 // size_t& record_id = GetRecordIdByTime(timePoint);
225 // return fBitVecBusy[record_id];
226 }
227
228 auto& SpillRangeVec() { return fSpillRangeVec; }
229
230 auto& BusyRangeVec() { return fBusyRangeVec; }
231
232 // auto& IsSpillBitVec() { return fBitVecSpill; }
233 //
234 // auto& IsBusyBitVec() { return fBitVecBusy; }
235
236 const size_t GetNRecords() { return fNRecords; }
237
238 const uint16_t GetNScalers() { return fScalerName2index.size(); }
239 // const uint16_t GetNScalers() { return fNScalers; }
240
241 uint32_t GetMissingHits() { return fMissingHits; }
242
243 void SetMissingHits(uint32_t val) { fMissingHits = val; }
244
245 /** Destructor **/
246 virtual ~BmnMSCMatrix() {}
247
248 private:
249 static bool IsInRange(const TimeRangeList& rangeList, const SysPoint& time)
250 {
251 auto it = std::lower_bound(
252 rangeList.begin(), rangeList.end(), time,
253 [](const TimeRange& rangePair, const SysPoint& timePoint) { return rangePair.second < timePoint; });
254 if (it == rangeList.end())
255 return false;
256 // printf("time \n\t%s \n range\n\t%s\n\t%s\n\t%d\n",
257 // BmnFunctionSet::TimePoint2String(time).data(),
258 // BmnFunctionSet::TimePoint2String(it->first).data(),
259 // BmnFunctionSet::TimePoint2String(it->second).data(),
260 // ((time >= it->first)&&(time < it->second)));
261 return ((time >= it->first) && (time < it->second));
262 }
263 /**
264 * Returns the [begin,end) iterators for range of time point pairs representing
265 * begin and end of a specific time interval overlapping with the given interval
266 * @param rangeList the vector of time point pairs to work with
267 * @param timeBegin start time point
268 * @param timeEnd end time point
269 * @return
270 */
271 static auto GetRanges(const TimeRangeList& rangeList,
272 const SysPoint& timeBegin = SysPoint::min(),
273 const SysPoint& timeEnd = SysPoint::max())
274 {
275 auto itBeginByFirsts =
276 std::lower_bound(rangeList.begin(), rangeList.end(), timeBegin, LessThanForFirst<TimeRange>{});
277 auto itBeginBySeconds =
278 std::lower_bound(rangeList.begin(), rangeList.end(), timeBegin, LessThanForSecond<TimeRange>{});
279 auto& itBegin = std::distance(itBeginByFirsts, itBeginBySeconds) > 0 ? itBeginByFirsts : itBeginBySeconds;
280
281 auto itEndByFirsts =
282 std::upper_bound(rangeList.begin(), rangeList.end(), timeEnd, LessThanForFirst<TimeRange>{});
283 auto itEndBySeconds =
284 std::upper_bound(rangeList.begin(), rangeList.end(), timeEnd, LessThanForSecond<TimeRange>{});
285 auto& itEnd = std::distance(itEndByFirsts, itEndBySeconds) > 0 ? itEndBySeconds : itEndByFirsts;
286 return make_pair(itBegin, itEnd);
287 }
288 static IntType GetCntByTime(const TimeLine& row, const SysPoint& time)
289 {
290 auto it = std::lower_bound(row.begin(), row.end(), time, LessThanForFirst<TimeLineEntry>{});
291 if (it->first != time) {
292 LOGF(error, "Time point not found: %s", BmnFunctionSet::TimePoint2String(time).data());
293 return 0;
294 }
295 // auto it = row.find(time);
296 if (it == row.end())
297 return 0;
298 return it->second;
299 }
300 static uint64_t GetCntInRange(const TimeLine& row,
301 const SysPoint& timeBegin = SysPoint::min(),
302 const SysPoint& timeEnd = SysPoint::max())
303 {
304 if (timeEnd < timeBegin)
305 return 0;
306 auto it = row.begin()->first > timeBegin
307 ? row.begin()
308 : std::lower_bound(row.begin(), row.end(), timeBegin, LessThanForFirst<TimeLineEntry>{});
309 // row.lower_bound(timeBegin);
310 if (it == row.end())
311 return 0;
312 auto itFinish = std::upper_bound(row.begin(), row.end(), timeEnd, LessThanForFirst<TimeLineEntry>{});
313 // auto itFinish = row.upper_bound(timeEnd);
314 uint64_t sum{0};
315 while ((it != itFinish))
316 sum += (it++)->second;
317 return sum;
318 }
319
320 void SetCount(const size_t& record_id, TimeLine& row, const IntType& val)
321 {
322 SysPoint& time = fTimes[record_id];
323 auto it = std::lower_bound(row.begin(), row.end(), time, LessThanForFirst<TimeLineEntry>{});
324 if (it == row.end())
325 row.push_back(make_pair(time, val));
326 else
327 it->second = val;
328 }
329
330 const size_t fNRecords;
331 // const uint16_t fNScalers;
333 vector<TimeLine> fMapVector;
334 // std::map<IntType> fVector;
335 // std::vector<IntType> fVector;
336 // Time2Pos fTimeMap; // (time -> position in the fTimes vector) map
337 // ublas::compressed_matrix<IntType> fMatrix; // WTF ROOT cannot work with it in a macro
338 // TMatrixTSparse<uint8_t> fMatrix; // "template" only supports a couple of prebuilt types
339 vector<SysPoint> fTimes;
340
343 // boost::dynamic_bitset<> fBitVecSpill;
344 // boost::dynamic_bitset<> fBitVecBusy;
345 uint32_t fMissingHits;
346
347 // Scaler2TimeDist scalers;
348 // TimeDist ext_cond;
349
351};
352
353#endif /* BMNMSCMATRIX_H */
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition P4_F32vec4.h:30
#define ClassDef(name, id)
std::chrono::system_clock SysClock
std::chrono::time_point< SysClock > SysPoint
std::unordered_map< std::string, uint16_t > NameChMap
Definition BmnAliases.h:7
static std::string TimePoint2String(SysPoint p)
static SysPoint TimeStamp2TP(TTimeStamp p)
static TTimeStamp TimePoint2TS(SysPoint p)
map< string, TimeLine > Scaler2TimeDist
virtual ~BmnMSCMatrix()
Destructor.
std::map< uint8_t, TimeLine > ScalerIdx2TimeDist
static uint64_t GetCntInRange(const TimeLine &row, const SysPoint &timeBegin=SysPoint::min(), const SysPoint &timeEnd=SysPoint::max())
auto & SpillRangeVec()
TimeRangeList fSpillRangeVec
uint32_t GetMissingHits()
static bool IsInRange(const TimeRangeList &rangeList, const SysPoint &time)
const IntType GetCount(size_t record_id, uint16_t scaler_id)
TimeLine & ScalerTimeLine(const uint16_t &scaler_id)
auto GetBusyRanges(const SysPoint &timeBegin=SysPoint::min(), const SysPoint &timeEnd=SysPoint::max())
Returns the [begin,end) iterators for range of time point pairs representing begin and end of a busy ...
BmnMSCMatrix(size_t times_count=0, NameChMap scalerName2Index=NameChMap{})
static auto GetRanges(const TimeRangeList &rangeList, const SysPoint &timeBegin=SysPoint::min(), const SysPoint &timeEnd=SysPoint::max())
Returns the [begin,end) iterators for range of time point pairs representing begin and end of a speci...
std::pair< SysPoint, size_t > TimeMapEntry
size_t GetRecordIdByTime(const SysPoint &timePoint)
const size_t GetNRecords()
const IntType GetCount(const SysPoint &time, const string &scaler_name)
auto GetSpillRanges(const SysPoint &timeBegin=SysPoint::min(), const SysPoint &timeEnd=SysPoint::max())
Returns the [begin,end) iterators for range of time point pairs representing begin and end of a spill...
void SetTime(const size_t &record_id, const SysPoint &time)
we assume the user is filling the timestamps in the chronological order on the basis of preliminary c...
uint32_t fMissingHits
NameChMap & GetScalerName2indexMap()
vector< TimeLineEntry > TimeLine
TTimeStamp GetTS(const size_t &record_id)
void SetMissingHits(uint32_t val)
bool GetIsSpill(const SysPoint &time)
NameChMap fScalerName2index
TimeRangeList fBusyRangeVec
const size_t fNRecords
void SetCount(const size_t &record_id, TimeLine &row, const IntType &val)
auto GetScalerRange(const string &scaler_name, const SysPoint &timeBegin=SysPoint::min(), const SysPoint &timeEnd=SysPoint::max())
void SetCount(size_t record_id, uint16_t scaler_id, IntType val)
TimeLine & ScalerTimeLine(const string &scaler_name)
const uint16_t GetNScalers()
vector< SysPoint > fTimes
bool GetIsBusy(const SysPoint &time)
static IntType GetCntByTime(const TimeLine &row, const SysPoint &time)
vector< TimeRange > TimeRangeList
uint64_t GetCntInRange(const string &scaler_name, const SysPoint &timeBegin=SysPoint::min(), const SysPoint &timeEnd=SysPoint::max())
std::pair< SysPoint, IntType > TimeLineEntry
vector< TimeLine > fMapVector
void SetTS(const size_t &record_id, const TTimeStamp &ts)
std::pair< SysPoint, SysPoint > TimeRange
SysPoint & GetTime(const size_t &record_id)
const IntType GetCount(size_t record_id, const string &scaler_name)
map< SysPoint, size_t > Time2Pos
void SetCount(size_t record_id, const string &scaler_name, IntType val)
const IntType GetCount(const SysPoint &time, const uint16_t scaler_id)
auto & BusyRangeVec()
@ IntType
std::chrono::time_point< SysClock > SysPoint
bool operator()(const SysPoint &key, const TimeLocationPair &entry) const
bool operator()(const TimeLocationPair &entry, const SysPoint &key) const
bool operator()(const SysPoint &key, const TimeLocationPair &entry) const
bool operator()(const TimeLocationPair &entry, const SysPoint &key) const