1414
1515#include " DataFormatsCTP/Scalers.h"
1616#include < iostream>
17+ #include < iomanip>
1718#include " CommonUtils/StringUtils.h"
1819#include " FairLogger.h"
1920
2021using namespace o2 ::ctp;
2122
2223void CTPScalerRaw::printStream (std::ostream& stream) const
2324{
24- stream << " Class:" << classIndex << " RAW LMB:" << lmBefore << " LMA:" << lmAfter;
25- stream << " LOB:" << l0Before << " L0A:" << l0After;
26- stream << " L1B:" << l1Before << " L1A:" << l1After << std::endl;
25+ stream << " Class:" << std::setw (2 ) << classIndex << " RAW" ;
26+ stream << " LMB:" << std::setw (10 ) << lmBefore << " LMA:" << std::setw (10 ) << lmAfter;
27+ stream << " LOB:" << std::setw (10 ) << l0Before << " L0A:" << std::setw (10 ) << l0After;
28+ stream << " L1B:" << std::setw (10 ) << l1Before << " L1A:" << std::setw (10 ) << l1After << std::endl;
2729}
2830//
29- void CTPScalerO2::createCTPScalerO2FromRaw (CTPScalerRaw& raw, std::vector <uint32_t >& overflow)
31+ void CTPScalerO2::createCTPScalerO2FromRaw (CTPScalerRaw raw, std::array <uint32_t , 6 >& overflow)
3032{
3133 classIndex = raw.classIndex ;
32- lmBefore = raw.lmBefore + 0xffffffff * overflow[0 ];
33- lmAfter = raw.lmAfter + 0xffffffff * overflow[1 ];
34- l0Before = raw.l0Before + 0xffffffff * overflow[2 ];
35- l0After = raw.l0After + 0xffffffff * overflow[3 ];
36- l1Before = raw.l1Before + 0xffffffff * overflow[4 ];
37- l1After = raw.l1After + 0xffffffff * overflow[5 ];
34+ lmBefore = (uint64_t )(raw.lmBefore ) + 0xffffffffull * (uint64_t )(overflow[0 ]);
35+ lmAfter = (uint64_t )(raw.lmAfter ) + 0xffffffffull * (uint64_t )(overflow[1 ]);
36+ l0Before = (uint64_t )(raw.l0Before ) + 0xffffffffull * (uint64_t )(overflow[2 ]);
37+ l0After = (uint64_t )(raw.l0After ) + 0xffffffffull * (uint64_t )(overflow[3 ]);
38+ l1Before = (uint64_t )(raw.l1Before ) + 0xffffffffull * (uint64_t )(overflow[4 ]);
39+ l1After = (uint64_t )(raw.l1After ) + 0xffffffffull * (uint64_t )(overflow[5 ]);
40+ // std::cout << "lmb overflow:" << overflow[0] << " lmb:" << lmBefore << " raw:" << raw.lmBefore << std::endl;
41+
3842}
3943void CTPScalerO2::printStream (std::ostream& stream) const
4044{
41- stream << " Class:" << classIndex << " O2 LMB:" << lmBefore << " LMA:" << lmAfter;
42- stream << " LOB:" << l0Before << " L0A:" << l0After;
43- stream << " L1B:" << l1Before << " L1A:" << l1After << std::endl;
45+ stream << " Class:" << std::setw (2 ) << classIndex << " O2" ;
46+ stream << " LMB:" << std::setw (10 ) << lmBefore << " LMA:" << std::setw (10 ) << lmAfter;
47+ stream << " LOB:" << std::setw (10 ) << l0Before << " L0A:" << std::setw (10 ) << l0After;
48+ stream << " L1B:" << std::setw (10 ) << l1Before << " L1A:" << std::setw (10 ) << l1After << std::endl;
4449}
4550void CTPScalerRecordRaw::printStream (std::ostream& stream) const
4651{
@@ -58,13 +63,20 @@ void CTPScalerRecordO2::printStream(std::ostream& stream) const
5863 cnts.printStream (stream);
5964 }
6065}
66+ //
67+ // CTPRunScalers
68+ //
6169void CTPRunScalers::printStream (std::ostream& stream) const
6270{
6371 stream << " CTP Scalers (version:" << mVersion << " ) Run:" << mRunNumber << std::endl;
6472 printClasses (stream);
6573 for (auto const & rec : mScalerRecordRaw ) {
6674 rec.printStream (stream);
6775 }
76+ stream << " O2 Counters:" << std::endl;
77+ for (auto const & rec : mScalerRecordO2 ) {
78+ rec.printStream (stream);
79+ }
6880}
6981void CTPRunScalers::printClasses (std::ostream& stream) const
7082{
@@ -76,7 +88,7 @@ void CTPRunScalers::printClasses(std::ostream& stream) const
7688 }
7789 stream << std::endl;
7890}
79- std::vector<uint32_t > CTPRunScalers::getClassIndexes ()
91+ std::vector<uint32_t > CTPRunScalers::getClassIndexes () const
8092{
8193 std::vector<uint32_t > indexes;
8294 for (uint32_t i = 0 ; i < CTP_NCLASSES ; i++) {
@@ -94,11 +106,17 @@ int CTPRunScalers::readScalers(const std::string& rawscalers)
94106 int level = 0 ;
95107 std::string line;
96108 int nclasses = 0 ;
109+ int nlines = 0 ;
97110 while (std::getline (iss, line)) {
98111 o2::utils::Str::trim (line);
99112 if ((ret = processScalerLine (line, level, nclasses)) != 0 ) {
100113 return ret;
101114 }
115+ nlines++;
116+ }
117+ if (nlines < 4 ) {
118+ LOG (ERROR ) << " Input string seems too small:\n " << rawscalers;
119+ return 6 ;
102120 }
103121 if (nclasses != 0 ) {
104122 LOG (ERROR ) << " Wrong number of classes in final record" ;
@@ -108,6 +126,7 @@ int CTPRunScalers::readScalers(const std::string& rawscalers)
108126}
109127int CTPRunScalers::processScalerLine (std::string& line, int & level, int & nclasses)
110128{
129+ // std::cout << "Processing line" << std::endl;
111130 if (line.size () == 0 ) {
112131 return 0 ;
113132 }
@@ -119,7 +138,7 @@ int CTPRunScalers::processScalerLine(std::string& line, int& level, int& nclasse
119138 if (ntokens == 0 ) {
120139 return 0 ;
121140 }
122- std::cout << line << " level in::" << level << std::endl;
141+ // std::cout << line << " level in::" << level << std::endl;
123142 // Version
124143 if (level == 0 ) {
125144 if (ntokens != 1 ) {
@@ -174,9 +193,9 @@ int CTPRunScalers::processScalerLine(std::string& line, int& level, int& nclasse
174193 LOG (ERROR ) << " Wrong syntax of counters line in CTP scalers" ;
175194 return 5 ;
176195 } else {
177- std::cout << " nclasses:" << nclasses << std::endl;
196+ // std::cout << "nclasses:" << nclasses << std::endl;
178197 CTPScalerRaw scaler;
179- scaler.classIndex = getClassIndexes ()[level - 3 ];
198+ scaler.classIndex = getClassIndexes ()[nclasses ];
180199 scaler.lmBefore = std::stol (tokens[0 ]);
181200 scaler.lmAfter = std::stol (tokens[1 ]);
182201 scaler.l0Before = std::stol (tokens[2 ]);
@@ -205,13 +224,140 @@ int CTPRunScalers::convertRawToO2()
205224 overflows[i] = {0 , 0 , 0 , 0 , 0 , 0 };
206225 }
207226 }
227+ // 1st o2 rec is just copy
208228 CTPScalerRecordO2 o2rec;
209- o2rec.intRecord = mScalerRecordRaw [0 ].intRecord ;
210- o2rec.seconds = mScalerRecordRaw [0 ].seconds ;
211- o2rec.microSeconds = mScalerRecordRaw [0 ].microSeconds ;
212- for (int i = 1 ; i < mScalerRecordRaw .size (); i++) {
213- // CTPScalerRecordRaw& prev = mScalerRecordRaw[i-1];
214- // CTPScalerRecordRaw& curr = mScalerRecordRaw[i];
229+ copyRawToO2ScalerRecord (mScalerRecordRaw [0 ],o2rec,overflows);
230+ mScalerRecordO2 .push_back (o2rec);
231+ for ( int i = 1 ; i < mScalerRecordRaw .size (); i++) {
232+ // update overflows
233+ updateOverflows (mScalerRecordRaw [i-1 ],mScalerRecordRaw [i], overflows);
234+ //
235+ CTPScalerRecordO2 o2rec;
236+ copyRawToO2ScalerRecord (mScalerRecordRaw [i],o2rec,overflows);
237+ mScalerRecordO2 .push_back (o2rec);
238+ // Check consistency
239+ checkConsistency (mScalerRecordO2 [i-1 ], mScalerRecordO2 [i]);
240+ }
241+ return 0 ;
242+ }
243+ int CTPRunScalers::copyRawToO2ScalerRecord (const CTPScalerRecordRaw& rawrec,CTPScalerRecordO2& o2rec, overflows_t & classesoverflows)
244+ {
245+ if (rawrec.scalers .size () != (mClassMask .count ())) {
246+ LOG (ERROR ) << " Inconsistent scaler record size:" << rawrec.scalers .size () << " Expected:" << mClassMask .count ();
247+ return 1 ;
248+ }
249+ o2rec.scalers .clear ();
250+ o2rec.intRecord = rawrec.intRecord ;
251+ o2rec.seconds = rawrec.seconds ;
252+ o2rec.microSeconds = rawrec.microSeconds ;
253+ for (int i = 0 ; i < rawrec.scalers .size (); i++ ) {
254+ CTPScalerRaw rawscal = rawrec.scalers [i];
255+ CTPScalerO2 o2scal;
256+ int k = (getClassIndexes ())[i];
257+ o2scal.createCTPScalerO2FromRaw (rawscal,classesoverflows[k]);
258+ o2rec.scalers .push_back (o2scal);
259+ }
260+ return 0 ;
261+ }
262+ int CTPRunScalers::checkConsistency (const CTPScalerO2& scal0, const CTPScalerO2& scal1) const
263+ {
264+ int ret=0 ;
265+ // Scaler should never decrease
266+ if (scal0.lmBefore > scal1.lmBefore ) {
267+ LOG (ERROR ) << " Scaler decreasing: Class:" << scal0.classIndex << " lmBefore 0:" << scal0.lmBefore << " lmBefore :" << scal1.lmBefore ;
268+ ret++;
269+ }
270+ if (scal0.l0Before > scal1.l0Before ) {
271+ LOG (ERROR ) << " Scaler decreasing: Class:" << scal0.classIndex << " lmBefore 0:" << scal0.l0Before << " lmBefore :" << scal1.l0Before ;
272+ ret++;
273+ }
274+ if (scal0.l1Before > scal1.l1Before ) {
275+ LOG (ERROR ) << " Scaler decreasing: Class:" << scal0.classIndex << " lmBefore 0:" << scal0.l1Before << " lmBefore :" << scal1.l1Before ;
276+ ret++;
277+ }
278+ if (scal0.lmAfter > scal1.lmAfter ) {
279+ LOG (ERROR ) << " Scaler decreasing: Class:" << scal0.classIndex << " lmAfter 0:" << scal0.lmAfter << " lmAfter :" << scal1.lmAfter ;
280+ ret++;
281+ }
282+ if (scal0.l0After > scal1.l0After ) {
283+ LOG (ERROR ) << " Scaler decreasing: Class:" << scal0.classIndex << " lmAfter 0:" << scal0.l0After << " lmAfter :" << scal1.l0After ;
284+ ret++;
285+ }
286+ if (scal0.l1After > scal1.l1After ) {
287+ LOG (ERROR ) << " Scaler decreasing: Class:" << scal0.classIndex << " lmAfter 0:" << scal0.l1After << " lmAfter :" << scal1.l1After ;
288+ ret++;
289+ }
290+ //
291+ // LMB >= LMA >= L0B >= L0A >= L1B >= L1A: 5 relations
292+ //
293+ if ((scal1.lmAfter - scal0.lmAfter ) > (scal1.lmBefore - scal0.lmBefore )) {
294+ LOG (ERROR ) << " LMA > LMB eerror" ;
295+ ret++;
296+ }
297+ if ((scal1.l0After - scal0.l0After ) > (scal1.l0Before - scal0.l0Before )) {
298+ LOG (ERROR ) << " L0A > L0B error" ;
299+ ret++;
300+ }
301+ if ((scal1.l1After - scal0.l1After ) > (scal1.l1Before - scal0.l1Before )) {
302+ LOG (ERROR ) << " L1A > L1B error" ;
303+ ret++;
304+ }
305+ if ((scal1.l0Before - scal0.l0Before ) > (scal1.lmAfter - scal0.lmAfter )) {
306+ LOG (ERROR ) << " L0B > LMA error." ;
307+ ret++;
308+ }
309+ if ((scal1.l1Before - scal0.l1Before ) > (scal1.l0After - scal0.l0After )) {
310+ LOG (ERROR ) << " L1B > L0A Before error." ;
311+ ret++;
312+ }
313+ //
314+ if (ret) {
315+ scal0.printStream (std::cout);
316+ scal1.printStream (std::cout);
317+ }
318+ return ret;
319+ }
320+ int CTPRunScalers::updateOverflows (const CTPScalerRecordRaw& rec0, const CTPScalerRecordRaw& rec1, overflows_t & classesoverflows) const
321+ {
322+ if (rec1.scalers .size () != mClassMask .count ()) {
323+ LOG (ERROR ) << " Inconsistent scaler record size:" << rec1.scalers .size () << " Expected:" << mClassMask .count ();
324+ return 1 ;
325+ }
326+ for (int i = 0 ; i < rec0.scalers .size () ; i++) {
327+ int k = (getClassIndexes ())[i];
328+ updateOverflows (rec0.scalers [i],rec1.scalers [i],classesoverflows[k]);
329+ }
330+ return 0 ;
331+ }
332+ int CTPRunScalers::checkConsistency (const CTPScalerRecordO2& rec0, const CTPScalerRecordO2& rec1) const
333+ {
334+ for (int i = 0 ; i < rec0.scalers .size (); i++) {
335+ checkConsistency (rec0.scalers [i],rec1.scalers [i]);
336+ }
337+ return 0 ;
338+ }
339+ int CTPRunScalers::updateOverflows (const CTPScalerRaw& scal0, const CTPScalerRaw& scal1, std::array<uint32_t , 6 >& overflow) const
340+ {
341+ if (scal0.lmBefore > scal1.lmBefore ) {
342+ overflow[0 ]+=1 ;
343+ }
344+ if (scal0.lmAfter > scal1.lmAfter ) {
345+ overflow[1 ]+=1 ;
346+ }
347+ if (scal0.l0Before > scal1.l0Before ) {
348+ overflow[2 ]+=1 ;
349+ }
350+ if (scal0.l0After > scal1.l0After ) {
351+ overflow[3 ]+=1 ;
352+ }
353+ if (scal0.l1Before > scal1.l1Before ) {
354+ overflow[4 ]+=1 ;
355+ }
356+ if (scal0.l1After > scal1.l1After ) {
357+ overflow[5 ]+=1 ;
215358 }
359+ // std::cout << "lmB0:" << scal0.lmBefore << " lmB1:" << scal1.lmBefore << " over:" << overflow[0] << std::endl;
360+ // for(int i = 0; i < 6; i++)std::cout << overflow[i] << " ";
361+ // std::cout << std::endl;
216362 return 0 ;
217363}
0 commit comments