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 "AliTriggerScalersRecord.h"
43 #include "AliTriggerRunScalers.h"
45 ClassImp( AliTriggerRunScalers )
47 //_____________________________________________________________________________
48 AliTriggerRunScalers::AliTriggerRunScalers():
57 // Default constructor
60 //_____________________________________________________________________________
61 void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler )
63 // Add scaler and check consistency
64 fScalersRecord.AddLast( scaler );
65 if (!AliTriggerRunScalers::ConsistencyCheck()) AliErrorClass("Trigger counters not in the right order or decreasing!");
66 // fScalersRecord.Sort();
68 //_____________________________________________________________________________
70 AliTriggerRunScalers::AliTriggerRunScalers(const AliTriggerRunScalers &run) :
72 fVersion(run.fVersion),
73 fRunNumber(run.fRunNumber),
74 fnClasses(run.fnClasses),
80 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
81 if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
83 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
84 if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
86 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
87 if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
91 //_____________________________________________________________________________
92 AliTriggerRunScalers &AliTriggerRunScalers::operator=(const AliTriggerRunScalers& run)
94 // assignment operator
95 if(&run == this) return *this;
96 ((TObject *)this)->operator=(run);
98 fVersion = run.fVersion;
99 fRunNumber = run.fRunNumber;
100 fnClasses = run.fnClasses;
102 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
103 if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
105 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
106 if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
108 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
109 if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
113 //_____________________________________________________________________________
114 AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
116 // Read scalers from text file(.cnt) provided by CTP
117 // for given run and convert it to root format
118 if( gSystem->AccessPathName( filename.Data() ) ) {
119 AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
123 ifstream *file = new ifstream ( filename.Data() );
125 AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
131 AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
134 Bool_t verflag = kFALSE;
135 Bool_t classflag = kFALSE;
137 while (strLine.ReadLine(*file)) {
138 if (strLine.BeginsWith("#")) continue;
140 TObjArray *tokens = strLine.Tokenize(" \t");
141 Int_t ntokens = tokens->GetEntriesFast();
142 // 1st line, version, it is one number,
145 AliErrorClass( Form( "Error reading version number from (%s), line :%s\n",
146 filename.Data() , strLine.Data() ) );
149 // cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
150 rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
156 // 2nd line, run number , number of classes, list of classes used in this partition
159 if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
160 AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() ));
162 // cout << "Run Number " << ((TObjString*)tokens->At(0))->String().Atoi() << endl;
163 rScaler->SetRunNumber( ((TObjString*)tokens->At(0))->String().Atoi() );
164 nclass = (UChar_t)((TObjString*)tokens->At(1))->String().Atoi();
165 // cout << "Number of classes " << nclass << endl;
166 rScaler->SetNumClasses( nclass );
167 if ( nclass != ntokens - 2 ) {
168 AliErrorClass( Form( "Error reading number of classes from (%s)\n", filename.Data() ));
170 for (UChar_t i=0; i<nclass; ++i) {
171 rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
179 // Each record consists of 1+(number of classes in partition) lines
180 // 1st line of record = time stamp (4 words)
181 // 1st word = ORBIT(24 bits)
182 // 2nd word = Period Counter (28 bit)
183 // 3rd word = seconds (32 bits) from epoch
184 // 4th word = microsecs (32 bits)
185 // other lines = 6 words of counters (L0 before,L0 after, ....)
187 AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)",
188 filename.Data(), strLine.Data() ));
192 UInt_t orbit = strtoul(((TObjString*)tokens->At(0))->String(), NULL, 10);
193 UInt_t period = strtoul(((TObjString*)tokens->At(1))->String(), NULL, 10);
194 UInt_t seconds = strtoul(((TObjString*)tokens->At(2))->String(), NULL, 10);
195 UInt_t microSecs = strtoul(((TObjString*)tokens->At(3))->String(), NULL, 10);
197 AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
198 rec->SetTimeStamp( orbit, period, seconds, microSecs );
200 for (Int_t i=0; i<nclass; ++i) {
201 strLine1.ReadLine(*file);
202 if (strLine1.BeginsWith("#")) continue;
203 TObjArray *tokens1 = strLine1.Tokenize(" \t");
204 Int_t ntokens1 = tokens1->GetEntriesFast();
205 if( ntokens1 != 6 ) {
206 AliErrorClass( Form( "Error reading scalers from (%s): line (%s)",
207 filename.Data(), strLine1.Data() ));
212 UInt_t lOCB = strtoul(((TObjString*)tokens1->At(0))->String(), NULL, 10);
213 UInt_t lOCA = strtoul(((TObjString*)tokens1->At(1))->String(), NULL, 10);
214 UInt_t l1CB = strtoul(((TObjString*)tokens1->At(2))->String(), NULL, 10);
215 UInt_t l1CA = strtoul(((TObjString*)tokens1->At(3))->String(), NULL, 10);
216 UInt_t l2CB = strtoul(((TObjString*)tokens1->At(4))->String(), NULL, 10);
217 UInt_t l2CA = strtoul(((TObjString*)tokens1->At(5))->String(), NULL, 10);
219 rScaler->GetClass(i);
220 rec->AddTriggerScalers( rScaler->GetClass(i),
226 rScaler->AddTriggerScalers( rec );
236 //_____________________________________________________________________________
237 Int_t AliTriggerRunScalers::FindNearestScalersRecord( const AliTimeStamp *stamp ) const
239 // Find Trigger scaler record with the closest timestamp <= "stamp"
240 // using a binary search.
241 // return the index in the array of records, if the timestamp
242 // is out of range return -1
244 Int_t base, position=-1, last, result = 0;
247 //fScalersRecord.Sort();
250 last = fScalersRecord.GetEntriesFast();
252 while (last >= base) {
253 position = (base+last) / 2;
254 cout << "pos " << position<< " base " << base << "last " << last << endl;
255 AliTriggerScalersRecord* rec = (AliTriggerScalersRecord*)fScalersRecord.At(position);
256 if( rec && rec->GetTimeStamp()) op2 = 1;
257 if( op2 && (result = stamp->Compare(rec->GetTimeStamp())) == 0 )
258 return position; // exact match
259 cout << "result " << result << " op2 " << op2 << " rec "<< rec << endl;
260 if (!op2 || result < 0)
266 if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() )
267 return -1; // out of range
269 return (result < 0 ) ? position-1 : position; // nearst < stamp
271 //_____________________________________________________________________________
272 Bool_t AliTriggerRunScalers::ConsistencyCheck() const
274 //Check if counters are consistent(increase). Example: lOCB(n) < lOCB(n+1) and lOCB > lOCA
275 UInt_t lOCBtwo, lOCAtwo, l1CBtwo, l1CAtwo, l2CBtwo, l2CAtwo, lOCBone, lOCAone, l1CBone, l1CAone, l2CBone, l2CAone;
276 Bool_t increase0B=0, increase0A=0, increase1B=0, increase1A=0, increase2B=0, increase2A=0;
277 Bool_t overflow0B=0, overflow0A=0, overflow1B=0, overflow1A=0, overflow2B=0, overflow2A=0;
278 Int_t position = fScalersRecord.GetEntriesFast()-1;
279 if (position == 0) return 1;
281 AliTriggerScalersRecord* scalers2 = (AliTriggerScalersRecord*)fScalersRecord.At(position);
282 AliTriggerScalersRecord* scalers1 = (AliTriggerScalersRecord*)fScalersRecord.At(position-1);
283 if (scalers2->Compare((AliTriggerScalersRecord*)fScalersRecord.At(position-1)) == -1) return 0;
284 else for( Int_t i=0; i<fnClasses; ++i ){
286 TObjArray* scalersArray2 = (TObjArray*)scalers2->GetTriggerScalers();
287 AliTriggerScalers* counters2 = (AliTriggerScalers*)scalersArray2->At(i);
288 lOCBtwo = counters2->GetLOCB();
289 lOCAtwo = counters2->GetLOCA();
290 l1CBtwo = counters2->GetL1CB();
291 l1CAtwo = counters2->GetL1CA();
292 l2CBtwo = counters2->GetL2CB();
293 l2CAtwo = counters2->GetL2CA();
295 TObjArray* scalersArray1 = (TObjArray*)scalers1->GetTriggerScalers();
296 AliTriggerScalers* counters1 = (AliTriggerScalers*)scalersArray1->At(i);
297 lOCBone = counters1->GetLOCB();
298 lOCAone = counters1->GetLOCA();
299 l1CBone = counters1->GetL1CB();
300 l1CAone = counters1->GetL1CA();
301 l2CBone = counters1->GetL2CB();
302 l2CAone = counters1->GetL2CA();
304 UInt_t const max1 = 4294967295ul; //32bit counters overflow after 4294967295
305 UInt_t const max2 = 1000000000ul; //when counters overflow they seem to be decreasing. Assume decrease cannot be smaller than max2.
307 if ( lOCBtwo > lOCBone ) increase0B=1;
308 else if ( lOCBtwo < lOCBone && (lOCBone - lOCBtwo) > max2) overflow0B=1;
311 if ( lOCAtwo > lOCAone ) increase0A=1;
312 else if ( lOCAtwo < lOCAone && (lOCAone - lOCAtwo) > max2) overflow0A=1;
315 if ( l1CBtwo > l1CBone ) increase1B=1;
316 else if ( l1CBtwo < l1CBone && (l1CBone - l1CBtwo) > max2) overflow1B=1;
319 if ( l1CAtwo > l1CAone ) increase1A=1;
320 else if ( l1CAtwo < l1CAone && (l1CAone - l1CAtwo) > max2) overflow1A=1;
323 if ( l2CBtwo > l2CBone ) increase2B=1;
324 else if ( l2CBtwo < l2CBone && (l2CBone - l2CBtwo) > max2) overflow2B=1;
327 if ( l2CAtwo > l2CAone ) increase2A=1;
328 else if ( l2CAtwo < l2CAone && (l2CAone - l2CAtwo) > max2) overflow2A=1;
332 if ( (lOCBtwo - lOCBone) < (lOCAtwo - lOCAone) && increase0B && increase0A ) return 0;
333 else if ( (max1 - lOCBone + lOCBtwo ) < (lOCAtwo - lOCAone) && overflow0B && increase0A ) return 0;
334 else if ( (lOCBtwo - lOCBone) < (max1 - lOCAone + lOCAtwo) && increase0B && overflow0A ) return 0;
335 else if ( (max1 - lOCBone + lOCBtwo ) < (max1 - lOCAone + lOCAtwo) && overflow0B && overflow0A ) return 0;
337 if ( (lOCAtwo - lOCAone) < (l1CBtwo - l1CBone) && increase0A && increase1B ) return 0;
338 else if ( (max1 - lOCAone + lOCAtwo ) < (l1CBtwo - l1CBone) && overflow0A && increase1B ) return 0;
339 else if ( (lOCAtwo - lOCAone) < (max1 - l1CBone + l1CBtwo) && increase0A && overflow1B ) return 0;
340 else if ( (max1 - lOCAone + lOCAtwo ) < (max1 - l1CBone + l1CBtwo) && overflow0A && overflow1B ) return 0;
342 if ( (l1CBtwo - l1CBone) < (l1CAtwo - l1CAone) && increase1B && increase1A ) return 0;
343 else if ( (max1 - l1CBone + l1CBtwo ) < (l1CAtwo - l1CAone) && overflow1B && increase1A ) return 0;
344 else if ( (l1CBtwo - l1CBone) < (max1 - l1CAone + l1CAtwo) && increase1B && overflow1A ) return 0;
345 else if ( (max1 - l1CBone + l1CBtwo ) < (max1 - l1CAone + l1CAtwo) && overflow1B && overflow1A ) return 0;
347 if ( (l1CAtwo - l1CAone) < (l2CBtwo - l2CBone) && increase1A && increase2B ) return 0;
348 else if ( (max1 - l1CAone + l1CAtwo ) < (l2CBtwo - l2CBone) && overflow1A && increase2B ) return 0;
349 else if ( (l1CAtwo - l1CAone) < (max1 - l2CBone + l2CBtwo) && increase1A && overflow2B ) return 0;
350 else if ( (max1 - l1CAone + l1CAtwo ) < (max1 - l2CBone + l2CBtwo) && overflow1A && overflow2B ) return 0;
352 if ( (l2CBtwo - l2CBone) < (l2CAtwo - l2CAone) && increase2B && increase2A ) return 0;
353 else if ( (max1 - l2CBone + l2CBtwo ) < (l2CAtwo - l2CAone) && overflow2B && increase2A ) return 0;
354 else if ( (l2CBtwo - l2CBone) < (max1 - l2CAone + l2CAtwo) && increase2B && overflow2A ) return 0;
355 else if ( (max1 - l2CBone + l2CBtwo ) < (max1 - l2CAone + l2CAtwo) && overflow2B && overflow2A ) return 0;
361 //_____________________________________________________________________________
362 void AliTriggerRunScalers::Print( const Option_t* ) const
365 cout << "Trigger Scalers Record per Run: " << endl;
366 cout << " File version :" << fVersion << endl;
367 cout << " Run Number :" << fRunNumber << endl;
368 cout << " Number of Classes :" << (Int_t)fnClasses << endl;
369 cout << " Classes ID:";
370 for( Int_t i=0; i<fnClasses; ++i )
371 cout << " " << (Int_t)fClassIndex[i];
374 for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i )
375 ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();