1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 /* $Id: AliTriggerRunScalers.cxx 22322 2007-11-22 11:43:14Z cvetan $ */
18 ///////////////////////////////////////////////////////////////////////////////
20 // Class to define the ALICE Trigger Scalers Record per Run.
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.
26 //////////////////////////////////////////////////////////////////////////////
29 #include <Riostream.h>
31 //#include <TObject.h>
32 //#include <TArrayC.h>
36 #include <TObjString.h>
37 #include <TObjArray.h>
40 #include "AliTimeStamp.h"
41 #include "AliTriggerScalers.h"
42 #include "AliTriggerScalersESD.h"
43 #include "AliTriggerScalersRecord.h"
44 #include "AliTriggerScalersRecordESD.h"
45 #include "AliTriggerRunScalers.h"
47 ClassImp( AliTriggerRunScalers )
49 //_____________________________________________________________________________
50 AliTriggerRunScalers::AliTriggerRunScalers():
59 // Default constructor
61 //______________________________________________________________________________
62 AliTriggerRunScalers::~AliTriggerRunScalers()
65 fScalersRecord.SetOwner();
66 fScalersRecord.Delete();
67 fScalersRecordESD.SetOwner();
68 fScalersRecordESD.Delete();
70 //_____________________________________________________________________________
71 void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler )
73 // Add scaler and check consistency
74 fScalersRecord.AddLast( scaler );
75 if (AliTriggerRunScalers::ConsistencyCheck(fScalersRecord.GetEntriesFast()-1,kFALSE)){
76 AliErrorClass("Trigger counters not in the right order or decreasing!");
78 // fScalersRecord.Sort();
81 //_____________________________________________________________________________
83 AliTriggerRunScalers::AliTriggerRunScalers(const AliTriggerRunScalers &run) :
85 fVersion(run.fVersion),
86 fRunNumber(run.fRunNumber),
87 fnClasses(run.fnClasses),
93 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
94 if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
96 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
97 if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
99 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
100 if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
104 //_____________________________________________________________________________
105 AliTriggerRunScalers &AliTriggerRunScalers::operator=(const AliTriggerRunScalers& run)
107 // assignment operator
108 if(&run == this) return *this;
109 ((TObject *)this)->operator=(run);
111 fVersion = run.fVersion;
112 fRunNumber = run.fRunNumber;
113 fnClasses = run.fnClasses;
115 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
116 if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
118 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
119 if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
121 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
122 if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
126 //_____________________________________________________________________________
127 AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
129 // Read scalers from text file(.cnt) provided by CTP
130 // for given run and convert it to root format
131 if( gSystem->AccessPathName( filename.Data() ) ) {
132 AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
136 ifstream *file = new ifstream ( filename.Data() );
138 AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
144 AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
147 Bool_t verflag = kFALSE;
148 Bool_t classflag = kFALSE;
150 while (strLine.ReadLine(*file)) {
151 if (strLine.BeginsWith("#")) continue;
153 TObjArray *tokens = strLine.Tokenize(" \t");
154 Int_t ntokens = tokens->GetEntriesFast();
155 // 1st line, version, it is one number,
158 AliErrorClass( Form( "Error reading version number from (%s), line :%s\n",
159 filename.Data() , strLine.Data() ) );
162 // cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
163 rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
169 // 2nd line, run number , number of classes, list of classes used in this partition
172 if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
173 AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() ));
175 // cout << "Run Number " << ((TObjString*)tokens->At(0))->String().Atoi() << endl;
176 rScaler->SetRunNumber( ((TObjString*)tokens->At(0))->String().Atoi() );
177 nclass = (UChar_t)((TObjString*)tokens->At(1))->String().Atoi();
178 // cout << "Number of classes " << nclass << endl;
179 rScaler->SetNumClasses( nclass );
180 if ( nclass != ntokens - 2 ) {
181 AliErrorClass( Form( "Error reading number of classes from (%s)\n", filename.Data() ));
183 for (UChar_t i=0; i<nclass; ++i) {
184 rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
192 // Each record consists of 1+(number of classes in partition) lines
193 // 1st line of record = time stamp (4 words)
194 // 1st word = ORBIT(24 bits)
195 // 2nd word = Period Counter (28 bit)
196 // 3rd word = seconds (32 bits) from epoch
197 // 4th word = microsecs (32 bits)
198 // other lines = 6 words of counters (L0 before,L0 after, ....)
200 AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)",
201 filename.Data(), strLine.Data() ));
205 UInt_t orbit = strtoul(((TObjString*)tokens->At(0))->String(), NULL, 10);
206 UInt_t period = strtoul(((TObjString*)tokens->At(1))->String(), NULL, 10);
207 UInt_t seconds = strtoul(((TObjString*)tokens->At(2))->String(), NULL, 10);
208 UInt_t microSecs = strtoul(((TObjString*)tokens->At(3))->String(), NULL, 10);
210 AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
211 rec->SetTimeStamp( orbit, period, seconds, microSecs );
213 for (Int_t i=0; i<nclass; ++i) {
214 strLine1.ReadLine(*file);
215 if (strLine1.BeginsWith("#")) continue;
216 TObjArray *tokens1 = strLine1.Tokenize(" \t");
217 Int_t ntokens1 = tokens1->GetEntriesFast();
218 if( ntokens1 != 6 ) {
219 AliErrorClass( Form( "Error reading scalers from (%s): line (%s)",
220 filename.Data(), strLine1.Data() ));
225 UInt_t lOCB = strtoul(((TObjString*)tokens1->At(0))->String(), NULL, 10);
226 UInt_t lOCA = strtoul(((TObjString*)tokens1->At(1))->String(), NULL, 10);
227 UInt_t l1CB = strtoul(((TObjString*)tokens1->At(2))->String(), NULL, 10);
228 UInt_t l1CA = strtoul(((TObjString*)tokens1->At(3))->String(), NULL, 10);
229 UInt_t l2CB = strtoul(((TObjString*)tokens1->At(4))->String(), NULL, 10);
230 UInt_t l2CA = strtoul(((TObjString*)tokens1->At(5))->String(), NULL, 10);
232 rScaler->GetClass(i);
233 rec->AddTriggerScalers( rScaler->GetClass(i),
239 rScaler->AddTriggerScalers( rec );
249 //_____________________________________________________________________________
250 Int_t AliTriggerRunScalers::FindNearestScalersRecord( const AliTimeStamp *stamp ) const
252 // Find Trigger scaler record with the closest timestamp <= "stamp"
253 // using a binary search.
254 // return the index in the array of records, if the timestamp
255 // is out of range return -1
257 Int_t base, position=-1, last, result = 0;
260 //fScalersRecord.Sort();
263 last = fScalersRecord.GetEntriesFast();
265 while (last >= base) {
266 position = (base+last) / 2;
267 cout << "pos " << position<< " base " << base << "last " << last << endl;
268 AliTriggerScalersRecord* rec = (AliTriggerScalersRecord*)fScalersRecord.At(position);
269 if( rec && rec->GetTimeStamp()) op2 = 1;
270 if( op2 && (result = stamp->Compare(rec->GetTimeStamp())) == 0 )
271 return position; // exact match
272 cout << "result " << result << " op2 " << op2 << " rec "<< rec << endl;
273 if (!op2 || result < 0)
279 if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() )
280 return -1; // out of range
282 return (result < 0 ) ? position-1 : position; // nearst < stamp
284 //_____________________________________________________________________________
285 Int_t AliTriggerRunScalers::ConsistencyCheck(Int_t position,Bool_t correctOverflow)
287 //Check if counters are consistent(increase). Example: lOCB(n) < lOCB(n+1) and lOCB > lOCA
288 // scalers coding 0,1,2,3,4,5=0b,0a,1b,1a,2b,2a
290 // 1 = decresing time
291 // 2 = too big jump in scalers, looks like some readings are missing
292 // 3 = (level+1) > (level)
295 AliErrorClass("position=0\n");
297 }else return 0; // to work correctlu in AddScalers
301 Bool_t increase[6], overflow[6];
302 for(Int_t i=0;i<6;i++){increase[i]=0;overflow[i]=0;}
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.
306 AliTriggerScalersRecord* scalers2 = (AliTriggerScalersRecord*)fScalersRecord.At(position);
307 AliTriggerScalersRecord* scalers1 = (AliTriggerScalersRecord*)fScalersRecord.At(position-1);
308 if (scalers2->Compare((AliTriggerScalersRecord*)fScalersRecord.At(position-1)) == -1) return 1;
310 AliTriggerScalersRecordESD* recESD = 0;
312 recESD = new AliTriggerScalersRecordESD();
313 recESD->SetTimeStamp(scalers2->GetTimeStamp());
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);
322 for(Int_t i=0;i<5;i++){
323 if ( c2[i] >= c1[i] ) increase[i]=1;
324 else if ( c2[i] < c1[i] && (c1[i] - c2[i]) > max2) overflow[i]=1;
327 for(Int_t i=0;i<5;i++){
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!");}
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!");}
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!");}
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!");}
342 for(Int_t i=0;i<6;i++){ c64[i]=c2[i]+max1*overflow[i]; }
343 AliTriggerScalersESD* s= new AliTriggerScalersESD(fClassIndex[ic],c64);
344 recESD->AddTriggerScalers(s);
348 if(correctOverflow)fScalersRecordESD.AddLast(recESD);
351 //____________________________________________________________________________
352 Int_t AliTriggerRunScalers::CorrectScalersOverflow()
354 // Run over fScalersRecord, check overflow using CheckConsistency methos
355 // and save corrected result in fScalersRecordESD.
357 // Temporary fix for the OCDB entries written with v4-16-Release
358 // where the wrong sorting was used
359 fScalersRecord.Sort();
362 AliTriggerScalersRecordESD* recESD = new AliTriggerScalersRecordESD();
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];
370 AliTriggerScalersESD* s= new AliTriggerScalersESD(fClassIndex[ic],c64);
371 recESD->AddTriggerScalers(s);
373 fScalersRecordESD.AddLast(recESD);
374 for(Int_t i=1;i<fScalersRecord.GetEntriesFast(); i++){
375 if(ConsistencyCheck(i,kTRUE)){
376 fScalersRecord.At(i)->Print();
377 fScalersRecord.At(i-1)->Print();
378 fScalersRecordESD.SetOwner();
379 fScalersRecordESD.Delete();
380 AliErrorClass("Inconsistent scalers, they will not be provided.\n");
384 if(fScalersRecordESD.GetEntriesFast() != fScalersRecord.GetEntriesFast()){
385 AliErrorClass("Internal error: #scalers ESD != #scalers \n");
390 //_____________________________________________________________________________
391 AliTriggerScalersESD* AliTriggerRunScalers::GetScalersForEventClass(const AliTimeStamp* stamp,const Int_t classIndex) const
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!");
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();
405 cout << " Position = " << position << endl;
406 if(scalers1->GetEntriesFast() != fnClasses){
407 AliErrorClass("Internal error: #classes in RecordESD != fnClasses\n");
411 AliTriggerScalersESD *s1,*s2;
412 for ( Int_t ic=0; ic < (Int_t)fnClasses; ++ic ){
414 s1 = (AliTriggerScalersESD*)scalers1->At(ic);
415 s2 = (AliTriggerScalersESD*)scalers2->At(ic);
417 Bool_t classfound = (s1->GetClassIndex() == classIndex) && (s2->GetClassIndex() == classIndex);
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];
434 AliTriggerScalersESD* result = new AliTriggerScalersESD(classIndex,cint);
438 AliErrorClass(Form("Classindex %i not found.\n",classIndex));
441 //_____________________________________________________________________________
442 void AliTriggerRunScalers::Print( const Option_t* ) const
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];
454 for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i )
455 ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();