Bug fix in copy constructor of CTP scalers class. Bug #57162
[u/mrichter/AliRoot.git] / STEER / AliTriggerRunScalers.cxx
CommitLineData
e39e51f6 1/**************************************************************************
d7dd8a54 2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id: AliTriggerRunScalers.cxx 22322 2007-11-22 11:43:14Z cvetan $ */
17
18///////////////////////////////////////////////////////////////////////////////
19//
20// Class to define the ALICE Trigger Scalers Record per Run.
d07a9211 21// ReadScalers(): read the txt file (rXXXX.cnt) provided by CTP and creates array of records.
22// ConsistencyCheck(): checks if the last scaler added to record is consistent,
23// i.e. time increases and valued are not decreasing in time.
24//
d7dd8a54 25//
26//////////////////////////////////////////////////////////////////////////////
27
c180f65d 28#include <stdlib.h>
d7dd8a54 29#include <Riostream.h>
d07a9211 30
31//#include <TObject.h>
32//#include <TArrayC.h>
33//#include <TFile.h>
d7dd8a54 34#include <TClass.h>
d7dd8a54 35#include <TSystem.h>
36#include <TObjString.h>
37#include <TObjArray.h>
d7dd8a54 38
39#include "AliLog.h"
40#include "AliTimeStamp.h"
41#include "AliTriggerScalers.h"
6441c2a8 42#include "AliTriggerScalersESD.h"
d7dd8a54 43#include "AliTriggerScalersRecord.h"
6441c2a8 44#include "AliTriggerScalersRecordESD.h"
d7dd8a54 45#include "AliTriggerRunScalers.h"
46
47ClassImp( AliTriggerRunScalers )
48
49//_____________________________________________________________________________
50AliTriggerRunScalers::AliTriggerRunScalers():
51 TObject(),
52 fVersion(0),
53 fRunNumber(0),
54 fnClasses(0),
55 fClassIndex(),
bf7443cc 56 fScalersRecord(),
57 fScalersRecordESD()
d7dd8a54 58{
59 // Default constructor
60}
7eaf72df 61//______________________________________________________________________________
62AliTriggerRunScalers::~AliTriggerRunScalers()
63{
64 // Destructor
65 fScalersRecord.SetOwner();
66 fScalersRecord.Delete();
67 fScalersRecordESD.SetOwner();
68 fScalersRecordESD.Delete();
69}
d7dd8a54 70//_____________________________________________________________________________
71void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler )
72{
d07a9211 73 // Add scaler and check consistency
e39e51f6 74 fScalersRecord.AddLast( scaler );
a93ccff3 75 if (AliTriggerRunScalers::ConsistencyCheck(fScalersRecord.GetEntriesFast()-1,kFALSE)){
76 AliErrorClass("Trigger counters not in the right order or decreasing!");
77 // scaler->Print();
78 // fScalersRecord.Sort();
79 }
d7dd8a54 80}
bf7443cc 81//_____________________________________________________________________________
82
83AliTriggerRunScalers::AliTriggerRunScalers(const AliTriggerRunScalers &run) :
84 TObject(),
85 fVersion(run.fVersion),
86 fRunNumber(run.fRunNumber),
87 fnClasses(run.fnClasses),
88 fClassIndex(),
89 fScalersRecord(),
90 fScalersRecordESD()
91{
92// copy constructor
f379aa89 93 fClassIndex.Set(run.fClassIndex.GetSize());
bf7443cc 94for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
95 if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
96 }
97for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
98 if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
99 }
100for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
101 if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
102 }
103
104}
105//_____________________________________________________________________________
106AliTriggerRunScalers &AliTriggerRunScalers::operator=(const AliTriggerRunScalers& run)
107{
108// assignment operator
109if(&run == this) return *this;
110((TObject *)this)->operator=(run);
111
112fVersion = run.fVersion;
113fRunNumber = run.fRunNumber;
114fnClasses = run.fnClasses;
d7dd8a54 115
bf7443cc 116for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
117 if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
118 }
119for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
120 if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
121 }
122for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
123 if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
124 }
125return *this;
126}
d7dd8a54 127//_____________________________________________________________________________
128AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
129{
d07a9211 130 // Read scalers from text file(.cnt) provided by CTP
131 // for given run and convert it to root format
d7dd8a54 132 if( gSystem->AccessPathName( filename.Data() ) ) {
133 AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
134 return NULL;
135 }
136
137 ifstream *file = new ifstream ( filename.Data() );
138 if (!*file) {
139 AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
140 file->close();
141 delete file;
142 return NULL;
143 }
144
145 AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
146
147 TString strLine;
148 Bool_t verflag = kFALSE;
149 Bool_t classflag = kFALSE;
150 UChar_t nclass = 0;
151 while (strLine.ReadLine(*file)) {
152 if (strLine.BeginsWith("#")) continue;
153
154 TObjArray *tokens = strLine.Tokenize(" \t");
155 Int_t ntokens = tokens->GetEntriesFast();
156 // 1st line, version, it is one number,
157 if (!verflag) {
158 if (ntokens != 1) {
159 AliErrorClass( Form( "Error reading version number from (%s), line :%s\n",
160 filename.Data() , strLine.Data() ) );
161 return NULL;
162 }
163 // cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
164 rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
165 verflag = kTRUE;
166 delete tokens;
167 continue;
168 }
169
170 // 2nd line, run number , number of classes, list of classes used in this partition
171
172 if (!classflag) {
173 if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
174 AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() ));
175 }
176 // cout << "Run Number " << ((TObjString*)tokens->At(0))->String().Atoi() << endl;
177 rScaler->SetRunNumber( ((TObjString*)tokens->At(0))->String().Atoi() );
178 nclass = (UChar_t)((TObjString*)tokens->At(1))->String().Atoi();
179 // cout << "Number of classes " << nclass << endl;
180 rScaler->SetNumClasses( nclass );
181 if ( nclass != ntokens - 2 ) {
182 AliErrorClass( Form( "Error reading number of classes from (%s)\n", filename.Data() ));
183 }
184 for (UChar_t i=0; i<nclass; ++i) {
185 rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
186 }
187 classflag = kTRUE;
188 delete tokens;
189 continue;
190 }
191
192 // Records
193 // Each record consists of 1+(number of classes in partition) lines
194 // 1st line of record = time stamp (4 words)
195 // 1st word = ORBIT(24 bits)
196 // 2nd word = Period Counter (28 bit)
197 // 3rd word = seconds (32 bits) from epoch
198 // 4th word = microsecs (32 bits)
199 // other lines = 6 words of counters (L0 before,L0 after, ....)
200 if (ntokens != 4) {
201 AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)",
202 filename.Data(), strLine.Data() ));
203 return NULL;
204 }
e39e51f6 205
206 UInt_t orbit = strtoul(((TObjString*)tokens->At(0))->String(), NULL, 10);
207 UInt_t period = strtoul(((TObjString*)tokens->At(1))->String(), NULL, 10);
208 UInt_t seconds = strtoul(((TObjString*)tokens->At(2))->String(), NULL, 10);
209 UInt_t microSecs = strtoul(((TObjString*)tokens->At(3))->String(), NULL, 10);
210
d7dd8a54 211 AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
212 rec->SetTimeStamp( orbit, period, seconds, microSecs );
d7dd8a54 213 TString strLine1;
214 for (Int_t i=0; i<nclass; ++i) {
215 strLine1.ReadLine(*file);
216 if (strLine1.BeginsWith("#")) continue;
217 TObjArray *tokens1 = strLine1.Tokenize(" \t");
218 Int_t ntokens1 = tokens1->GetEntriesFast();
219 if( ntokens1 != 6 ) {
29bdda4f 220 AliErrorClass( Form( "Error reading scalers from (%s): line (%s)",
221 filename.Data(), strLine1.Data() ));
222 delete rec;
223 return rScaler;
d7dd8a54 224 }
d5aaf775 225
226 UInt_t lOCB = strtoul(((TObjString*)tokens1->At(0))->String(), NULL, 10);
227 UInt_t lOCA = strtoul(((TObjString*)tokens1->At(1))->String(), NULL, 10);
228 UInt_t l1CB = strtoul(((TObjString*)tokens1->At(2))->String(), NULL, 10);
229 UInt_t l1CA = strtoul(((TObjString*)tokens1->At(3))->String(), NULL, 10);
230 UInt_t l2CB = strtoul(((TObjString*)tokens1->At(4))->String(), NULL, 10);
231 UInt_t l2CA = strtoul(((TObjString*)tokens1->At(5))->String(), NULL, 10);
e39e51f6 232
d7dd8a54 233 rScaler->GetClass(i);
234 rec->AddTriggerScalers( rScaler->GetClass(i),
d5aaf775 235 lOCB, lOCA, l1CB,
236 l1CA, l2CB, l2CA );
237
d7dd8a54 238 delete tokens1;
239 }
240 rScaler->AddTriggerScalers( rec );
241
242 delete tokens;
243 }
244 file->close();
245 delete file;
246
247 return rScaler;
248}
249
250//_____________________________________________________________________________
033d0f8e 251Int_t AliTriggerRunScalers::FindNearestScalersRecord( const AliTimeStamp *stamp ) const
d7dd8a54 252{
253 // Find Trigger scaler record with the closest timestamp <= "stamp"
254 // using a binary search.
255 // return the index in the array of records, if the timestamp
256 // is out of range return -1
257
258 Int_t base, position=-1, last, result = 0;
5dd39685 259 Int_t op2 = 0;
d7dd8a54 260
033d0f8e 261 //fScalersRecord.Sort();
d7dd8a54 262
263 base = 0;
264 last = fScalersRecord.GetEntriesFast();
265
266 while (last >= base) {
267 position = (base+last) / 2;
767e5649 268 AliDebug(1, Form("position= %d base= %d last= %d ",position,base,last));
d7dd8a54 269 AliTriggerScalersRecord* rec = (AliTriggerScalersRecord*)fScalersRecord.At(position);
5dd39685 270 if( rec && rec->GetTimeStamp()) op2 = 1;
271 if( op2 && (result = stamp->Compare(rec->GetTimeStamp())) == 0 )
0367cade 272 return position; // exact match
d7dd8a54 273 if (!op2 || result < 0)
274 last = position-1;
275 else
276 base = position+1;
5dd39685 277 op2 = 0;
d7dd8a54 278 }
279 if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() )
280 return -1; // out of range
281 else
282 return (result < 0 ) ? position-1 : position; // nearst < stamp
283}
e39e51f6 284//_____________________________________________________________________________
6441c2a8 285Int_t AliTriggerRunScalers::ConsistencyCheck(Int_t position,Bool_t correctOverflow)
e39e51f6 286{
d07a9211 287 //Check if counters are consistent(increase). Example: lOCB(n) < lOCB(n+1) and lOCB > lOCA
6441c2a8 288 // scalers coding 0,1,2,3,4,5=0b,0a,1b,1a,2b,2a
47fde2cb 289 // returns:
290 // 1 = decresing time
291 // 2 = too big jump in scalers, looks like some readings are missing
292 // 3 = (level+1) > (level)
6441c2a8 293 if (position == 0){
a93ccff3 294 if(correctOverflow){
295 AliErrorClass("position=0\n");
296 return 1;
297 }else return 0; // to work correctlu in AddScalers
298 };
47fde2cb 299 UInt_t c2[6], c1[6];
6441c2a8 300 ULong64_t c64[6];
301 Bool_t increase[6], overflow[6];
47fde2cb 302 for(Int_t i=0;i<6;i++){increase[i]=0;overflow[i]=0;}
6441c2a8 303 ULong64_t const max1 = 4294967295ul; //32bit counters overflow after 4294967295
304 ULong64_t const max2 = 1000000000ul; //when counters overflow they seem to be decreasing. Assume decrease cannot be smaller than max2.
d07a9211 305
306 AliTriggerScalersRecord* scalers2 = (AliTriggerScalersRecord*)fScalersRecord.At(position);
307 AliTriggerScalersRecord* scalers1 = (AliTriggerScalersRecord*)fScalersRecord.At(position-1);
47fde2cb 308 if (scalers2->Compare((AliTriggerScalersRecord*)fScalersRecord.At(position-1)) == -1) return 1;
6441c2a8 309
310 AliTriggerScalersRecordESD* recESD = 0;
311 if(correctOverflow){
312 recESD = new AliTriggerScalersRecordESD();
268381f3 313 recESD->SetTimeStamp(scalers2->GetTimeStamp());
d07a9211 314 }
6441c2a8 315 for( Int_t ic=0; ic<fnClasses; ++ic ){
316 TObjArray* scalersArray2 = (TObjArray*)scalers2->GetTriggerScalers();
317 AliTriggerScalers* counters2 = (AliTriggerScalers*)scalersArray2->At(ic);
318 counters2->GetAllScalers(c2);
319 TObjArray* scalersArray1 = (TObjArray*)scalers1->GetTriggerScalers();
320 AliTriggerScalers* counters1 = (AliTriggerScalers*)scalersArray1->At(ic);
321 counters1->GetAllScalers(c1);
a93ccff3 322 for(Int_t i=0;i<5;i++){
323 if ( c2[i] >= c1[i] ) increase[i]=1;
6441c2a8 324 else if ( c2[i] < c1[i] && (c1[i] - c2[i]) > max2) overflow[i]=1;
325 else return 2;
326 }
327 for(Int_t i=0;i<5;i++){
a93ccff3 328 if ((c2[i] - c1[i]) < (c2[i+1] - c1[i+1]) && increase[i] && increase[i+1] ) {
329 if ( ((c2[i+1] - c1[i+1]) - (c2[i] - c1[i])) < 16 ) {AliWarningClass("Trigger scaler Level[i+1] > Level[i]. Diff < 16!");}
330 else return 3; }
331 else if ( (max1 - c1[i]+c2[i]) < (c2[i+1] - c1[i+1]) && overflow[i] && increase[i+1] ) {
332 if ( ((c2[i+1] - c1[i+1]) - (max1 - c1[i]+c2[i])) < 16 ) {AliWarningClass("Trigger scaler Level[i+1] > Level[i]. Diff < 16!");}
333 else return 3; }
334 else if ( (c2[i] - c1[i]) < (max1 - c1[i+1] + c2[i+1]) && increase[i] && overflow[i+1] ) {
335 if ( ((max1 - c1[i+1] + c2[i+1]) - (c2[i] - c1[i])) < 16 ) {AliWarningClass("Trigger scaler Level[i+1] > Level[i]. Diff < 16!");}
336 else return 3; }
337 else if ( (max1 - c1[i] + c2[i] ) < (max1 - c1[i+1] + c2[i+1]) && overflow[i] && overflow[i+1] ) {
338 if ( ((max1 - c1[i+1] + c2[i+1]) - (max1 - c1[i] + c2[i] )) < 16 ) {AliWarningClass("Trigger scaler Level[i+1] > Level[i]. Diff < 16!");}
339 else return 3; }
6441c2a8 340 }
a93ccff3 341 if(correctOverflow){
342 for(Int_t i=0;i<6;i++){ c64[i]=c2[i]+max1*overflow[i]; }
343 AliTriggerScalersESD* s= new AliTriggerScalersESD(fClassIndex[ic],c64);
6441c2a8 344 recESD->AddTriggerScalers(s);
a93ccff3 345 }
346
6441c2a8 347 }
348 if(correctOverflow)fScalersRecordESD.AddLast(recESD);
349 return 0;
350}
351//____________________________________________________________________________
352Int_t AliTriggerRunScalers::CorrectScalersOverflow()
353{
354 // Run over fScalersRecord, check overflow using CheckConsistency methos
355 // and save corrected result in fScalersRecordESD.
ab26241b 356
357 // Temporary fix for the OCDB entries written with v4-16-Release
358 // where the wrong sorting was used
359 fScalersRecord.Sort();
6441c2a8 360 UInt_t c1[6];
361 ULong64_t c64[6];
362 AliTriggerScalersRecordESD* recESD = new AliTriggerScalersRecordESD();
363 // add 0
364 AliTriggerScalersRecord* scalers = (AliTriggerScalersRecord*)fScalersRecord.At(0);
365 for( Int_t ic=0; ic<fnClasses; ++ic ){
366 TObjArray* scalersArray = (TObjArray*)scalers->GetTriggerScalers();
367 AliTriggerScalers* counters = (AliTriggerScalers*)scalersArray->At(ic);
368 counters->GetAllScalers(c1);
369 for(Int_t i=0; i<6; i++)c64[i]=c1[i];
a93ccff3 370 AliTriggerScalersESD* s= new AliTriggerScalersESD(fClassIndex[ic],c64);
6441c2a8 371 recESD->AddTriggerScalers(s);
372 }
373 fScalersRecordESD.AddLast(recESD);
374 for(Int_t i=1;i<fScalersRecord.GetEntriesFast(); i++){
375 if(ConsistencyCheck(i,kTRUE)){
a93ccff3 376 fScalersRecord.At(i)->Print();
377 fScalersRecord.At(i-1)->Print();
6441c2a8 378 fScalersRecordESD.SetOwner();
379 fScalersRecordESD.Delete();
380 AliErrorClass("Inconsistent scalers, they will not be provided.\n");
381 return 1;
382 }
383 }
384 if(fScalersRecordESD.GetEntriesFast() != fScalersRecord.GetEntriesFast()){
385 AliErrorClass("Internal error: #scalers ESD != #scalers \n");
386 return 1;
387 }
388 return 0;
389}
390//_____________________________________________________________________________
391AliTriggerScalersESD* AliTriggerRunScalers::GetScalersForEventClass(const AliTimeStamp* stamp,const Int_t classIndex) const
392{
393 // Find scalers for event for class in fScalersRecordESD
394 // Assumes that fScalerRecord = fScalerRecordESD
395 Int_t position = FindNearestScalersRecord(stamp);
396 if ( position == -1 ) {
397 AliErrorClass("Event AliTimeStamp out of range!");
398 return 0;
399 }
400 // check also position=max
401 AliTriggerScalersRecordESD* scalrec1 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(position);
402 AliTriggerScalersRecordESD* scalrec2 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(position+1);
403 TObjArray* scalers1 = (TObjArray*)scalrec1->GetTriggerScalers();
404 TObjArray* scalers2 = (TObjArray*)scalrec2->GetTriggerScalers();
0367cade 405
6441c2a8 406 if(scalers1->GetEntriesFast() != fnClasses){
407 AliErrorClass("Internal error: #classes in RecordESD != fnClasses\n");
408 return 0;
409 }
a93ccff3 410
6441c2a8 411 AliTriggerScalersESD *s1,*s2;
a93ccff3 412 for ( Int_t ic=0; ic < (Int_t)fnClasses; ++ic ){
413
6441c2a8 414 s1 = (AliTriggerScalersESD*)scalers1->At(ic);
415 s2 = (AliTriggerScalersESD*)scalers2->At(ic);
a93ccff3 416
6441c2a8 417 Bool_t classfound = (s1->GetClassIndex() == classIndex) && (s2->GetClassIndex() == classIndex);
418 if(classfound){
419 ULong64_t max = 4294967295ul;
420 AliTriggerScalersRecordESD* scalrec0 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(0);
421 TObjArray* scalers0 = (TObjArray*)scalrec0->GetTriggerScalers();
422 AliTriggerScalersESD *s0 = (AliTriggerScalersESD*)scalers0->At(ic);
423 ULong64_t base[6],c1[6],c2[6],cint[6];
424 ULong64_t orbit = max*(stamp->GetPeriod()) + stamp->GetOrbit();
425 s0->GetAllScalers(base);
426 s1->GetAllScalers(c1);
427 s2->GetAllScalers(c2);
428 ULong64_t orbit1 = max*(scalrec1->GetTimeStamp()->GetPeriod())+scalrec1->GetTimeStamp()->GetOrbit();
429 ULong64_t orbit2 = max*(scalrec2->GetTimeStamp()->GetPeriod())+scalrec2->GetTimeStamp()->GetOrbit();
430 for(Int_t i=0;i<6;i++){
431 Double_t slope=Double_t(c2[i]-c1[i])/Double_t(orbit2-orbit1);
432 cint[i]=ULong64_t(slope*(orbit-orbit1)) +c1[i] -base[i];
433 }
434 AliTriggerScalersESD* result = new AliTriggerScalersESD(classIndex,cint);
435 return result;
436 }
47fde2cb 437 }
6441c2a8 438 AliErrorClass(Form("Classindex %i not found.\n",classIndex));
47fde2cb 439 return 0;
e39e51f6 440}
d7dd8a54 441//_____________________________________________________________________________
442void AliTriggerRunScalers::Print( const Option_t* ) const
443{
444 // Print
445 cout << "Trigger Scalers Record per Run: " << endl;
446 cout << " File version :" << fVersion << endl;
447 cout << " Run Number :" << fRunNumber << endl;
448 cout << " Number of Classes :" << (Int_t)fnClasses << endl;
449 cout << " Classes ID:";
450 for( Int_t i=0; i<fnClasses; ++i )
451 cout << " " << (Int_t)fClassIndex[i];
452 cout << endl;
453
454 for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i )
455 ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();
456}
457