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.
23 //////////////////////////////////////////////////////////////////////////////
27 #include <Riostream.h>
32 #include <TObjString.h>
33 #include <TObjArray.h>
38 #include "AliTimeStamp.h"
39 #include "AliTriggerScalers.h"
40 #include "AliTriggerScalersRecord.h"
41 #include "AliTriggerRunScalers.h"
42 #include "AliCDBEntry.h"
43 #include "AliCDBManager.h"
44 #include "AliCDBStorage.h"
46 ClassImp( AliTriggerRunScalers )
48 //_____________________________________________________________________________
49 AliTriggerRunScalers::AliTriggerRunScalers():
57 // Default constructor
60 //_____________________________________________________________________________
61 void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler )
64 fScalersRecord.AddLast( scaler );
65 if (!AliTriggerRunScalers::ConsistencyCheck()) AliErrorClass("Trigger counters not in the right order or decreasing!");
66 // fScalersRecord.Sort();
69 //_____________________________________________________________________________
70 AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
72 //Read scalers from text file(.cnt) provided by CTP for given run and convert it to root format
73 if( gSystem->AccessPathName( filename.Data() ) ) {
74 AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
78 ifstream *file = new ifstream ( filename.Data() );
80 AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
86 AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
89 Bool_t verflag = kFALSE;
90 Bool_t classflag = kFALSE;
92 while (strLine.ReadLine(*file)) {
93 if (strLine.BeginsWith("#")) continue;
95 TObjArray *tokens = strLine.Tokenize(" \t");
96 Int_t ntokens = tokens->GetEntriesFast();
97 // 1st line, version, it is one number,
100 AliErrorClass( Form( "Error reading version number from (%s), line :%s\n",
101 filename.Data() , strLine.Data() ) );
104 // cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
105 rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
111 // 2nd line, run number , number of classes, list of classes used in this partition
114 if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
115 AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() ));
117 // cout << "Run Number " << ((TObjString*)tokens->At(0))->String().Atoi() << endl;
118 rScaler->SetRunNumber( ((TObjString*)tokens->At(0))->String().Atoi() );
119 nclass = (UChar_t)((TObjString*)tokens->At(1))->String().Atoi();
120 // cout << "Number of classes " << nclass << endl;
121 rScaler->SetNumClasses( nclass );
122 if ( nclass != ntokens - 2 ) {
123 AliErrorClass( Form( "Error reading number of classes from (%s)\n", filename.Data() ));
125 for (UChar_t i=0; i<nclass; ++i) {
126 rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
134 // Each record consists of 1+(number of classes in partition) lines
135 // 1st line of record = time stamp (4 words)
136 // 1st word = ORBIT(24 bits)
137 // 2nd word = Period Counter (28 bit)
138 // 3rd word = seconds (32 bits) from epoch
139 // 4th word = microsecs (32 bits)
140 // other lines = 6 words of counters (L0 before,L0 after, ....)
142 AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)",
143 filename.Data(), strLine.Data() ));
147 UInt_t orbit = strtoul(((TObjString*)tokens->At(0))->String(), NULL, 10);
148 UInt_t period = strtoul(((TObjString*)tokens->At(1))->String(), NULL, 10);
149 UInt_t seconds = strtoul(((TObjString*)tokens->At(2))->String(), NULL, 10);
150 UInt_t microSecs = strtoul(((TObjString*)tokens->At(3))->String(), NULL, 10);
152 AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
153 rec->SetTimeStamp( orbit, period, seconds, microSecs );
155 for (Int_t i=0; i<nclass; ++i) {
156 strLine1.ReadLine(*file);
157 if (strLine1.BeginsWith("#")) continue;
158 TObjArray *tokens1 = strLine1.Tokenize(" \t");
159 Int_t ntokens1 = tokens1->GetEntriesFast();
160 if( ntokens1 != 6 ) {
161 AliErrorClass( Form( "Error reading scalers from (%s): line (%s)",
162 filename.Data(), strLine1.Data() ));
167 UInt_t lOCB = strtoul(((TObjString*)tokens1->At(0))->String(), NULL, 10);
168 UInt_t lOCA = strtoul(((TObjString*)tokens1->At(1))->String(), NULL, 10);
169 UInt_t l1CB = strtoul(((TObjString*)tokens1->At(2))->String(), NULL, 10);
170 UInt_t l1CA = strtoul(((TObjString*)tokens1->At(3))->String(), NULL, 10);
171 UInt_t l2CB = strtoul(((TObjString*)tokens1->At(4))->String(), NULL, 10);
172 UInt_t l2CA = strtoul(((TObjString*)tokens1->At(5))->String(), NULL, 10);
174 rScaler->GetClass(i);
175 rec->AddTriggerScalers( rScaler->GetClass(i),
181 rScaler->AddTriggerScalers( rec );
191 //_____________________________________________________________________________
192 Int_t AliTriggerRunScalers::FindNearestScalersRecord( const AliTimeStamp *stamp ) const
194 // Find Trigger scaler record with the closest timestamp <= "stamp"
195 // using a binary search.
196 // return the index in the array of records, if the timestamp
197 // is out of range return -1
199 Int_t base, position=-1, last, result = 0;
200 AliTimeStamp *op2 = NULL;
202 //fScalersRecord.Sort();
205 last = fScalersRecord.GetEntriesFast();
207 while (last >= base) {
208 position = (base+last) / 2;
209 cout << "pos " << position<< " base " << base << "last " << last << endl;
210 AliTriggerScalersRecord* rec = (AliTriggerScalersRecord*)fScalersRecord.At(position);
211 if( rec ) op2 = rec->GetTimeStamp();
212 if( op2 && (result = stamp->Compare(op2)) == 0 )
213 return position; // exact match
214 cout << "result " << result << " op2 " << op2 << " rec "<< rec << endl;
215 if (!op2 || result < 0)
221 if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() )
222 return -1; // out of range
224 return (result < 0 ) ? position-1 : position; // nearst < stamp
226 //_____________________________________________________________________________
227 Bool_t AliTriggerRunScalers::ConsistencyCheck()
229 //Check if counters are consistent(increase). Example: lOCB(n) < lOCB(n+1) and lOCB > lOCA
230 UInt_t lOCBtwo, lOCAtwo, l1CBtwo, l1CAtwo, l2CBtwo, l2CAtwo, lOCBone, lOCAone, l1CBone, l1CAone, l2CBone, l2CAone;
231 Bool_t increase0B=0, increase0A=0, increase1B=0, increase1A=0, increase2B=0, increase2A=0;
232 Bool_t overflow0B=0, overflow0A=0, overflow1B=0, overflow1A=0, overflow2B=0, overflow2A=0;
233 Int_t position = fScalersRecord.GetEntriesFast()-1;
234 if (position == 0) return 1;
236 AliTriggerScalersRecord* scalers2 = (AliTriggerScalersRecord*)fScalersRecord.At(position);
237 AliTriggerScalersRecord* scalers1 = (AliTriggerScalersRecord*)fScalersRecord.At(position-1);
238 if (scalers2->Compare((AliTriggerScalersRecord*)fScalersRecord.At(position-1)) == -1) return 0;
239 else for( Int_t i=0; i<fnClasses; ++i ){
241 TObjArray* scalersArray2 = (TObjArray*)scalers2->GetTriggerScalers();
242 AliTriggerScalers* counters2 = (AliTriggerScalers*)scalersArray2->At(i);
243 lOCBtwo = counters2->GetLOCB();
244 lOCAtwo = counters2->GetLOCA();
245 l1CBtwo = counters2->GetL1CB();
246 l1CAtwo = counters2->GetL1CA();
247 l2CBtwo = counters2->GetL2CB();
248 l2CAtwo = counters2->GetL2CA();
250 TObjArray* scalersArray1 = (TObjArray*)scalers1->GetTriggerScalers();
251 AliTriggerScalers* counters1 = (AliTriggerScalers*)scalersArray1->At(i);
252 lOCBone = counters1->GetLOCB();
253 lOCAone = counters1->GetLOCA();
254 l1CBone = counters1->GetL1CB();
255 l1CAone = counters1->GetL1CA();
256 l2CBone = counters1->GetL2CB();
257 l2CAone = counters1->GetL2CA();
259 UInt_t const max1 = 4294967295ul; //32bit counters overflow after 4294967295
260 UInt_t const max2 = 1000000000ul; //when counters overflow they seem to be decreasing. Assume decrease cannot be smaller than max2.
262 if ( lOCBtwo > lOCBone ) increase0B=1;
263 else if ( lOCBtwo < lOCBone && (lOCBone - lOCBtwo) > max2) overflow0B=1;
266 if ( lOCAtwo > lOCAone ) increase0A=1;
267 else if ( lOCAtwo < lOCAone && (lOCAone - lOCAtwo) > max2) overflow0A=1;
270 if ( l1CBtwo > l1CBone ) increase1B=1;
271 else if ( l1CBtwo < l1CBone && (l1CBone - l1CBtwo) > max2) overflow1B=1;
274 if ( l1CAtwo > l1CAone ) increase1A=1;
275 else if ( l1CAtwo < l1CAone && (l1CAone - l1CAtwo) > max2) overflow1A=1;
278 if ( l2CBtwo > l2CBone ) increase2B=1;
279 else if ( l2CBtwo < l2CBone && (l2CBone - l2CBtwo) > max2) overflow2B=1;
282 if ( l2CAtwo > l2CAone ) increase2A=1;
283 else if ( l2CAtwo < l2CAone && (l2CAone - l2CAtwo) > max2) overflow2A=1;
287 if ( (lOCBtwo - lOCBone) < (lOCAtwo - lOCAone) && increase0B && increase0A ) return 0;
288 else if ( (max1 - lOCBone + lOCBtwo ) < (lOCAtwo - lOCAone) && overflow0B && increase0A ) return 0;
289 else if ( (lOCBtwo - lOCBone) < (max1 - lOCAone + lOCAtwo) && increase0B && overflow0A ) return 0;
290 else if ( (max1 - lOCBone + lOCBtwo ) < (max1 - lOCAone + lOCAtwo) && overflow0B && overflow0A ) return 0;
292 if ( (lOCAtwo - lOCAone) < (l1CBtwo - l1CBone) && increase0A && increase1B ) return 0;
293 else if ( (max1 - lOCAone + lOCAtwo ) < (l1CBtwo - l1CBone) && overflow0A && increase1B ) return 0;
294 else if ( (lOCAtwo - lOCAone) < (max1 - l1CBone + l1CBtwo) && increase0A && overflow1B ) return 0;
295 else if ( (max1 - lOCAone + lOCAtwo ) < (max1 - l1CBone + l1CBtwo) && overflow0A && overflow1B ) return 0;
297 if ( (l1CBtwo - l1CBone) < (l1CAtwo - l1CAone) && increase1B && increase1A ) return 0;
298 else if ( (max1 - l1CBone + l1CBtwo ) < (l1CAtwo - l1CAone) && overflow1B && increase1A ) return 0;
299 else if ( (l1CBtwo - l1CBone) < (max1 - l1CAone + l1CAtwo) && increase1B && overflow1A ) return 0;
300 else if ( (max1 - l1CBone + l1CBtwo ) < (max1 - l1CAone + l1CAtwo) && overflow1B && overflow1A ) return 0;
302 if ( (l1CAtwo - l1CAone) < (l2CBtwo - l2CBone) && increase1A && increase2B ) return 0;
303 else if ( (max1 - l1CAone + l1CAtwo ) < (l2CBtwo - l2CBone) && overflow1A && increase2B ) return 0;
304 else if ( (l1CAtwo - l1CAone) < (max1 - l2CBone + l2CBtwo) && increase1A && overflow2B ) return 0;
305 else if ( (max1 - l1CAone + l1CAtwo ) < (max1 - l2CBone + l2CBtwo) && overflow1A && overflow2B ) return 0;
307 if ( (l2CBtwo - l2CBone) < (l2CAtwo - l2CAone) && increase2B && increase2A ) return 0;
308 else if ( (max1 - l2CBone + l2CBtwo ) < (l2CAtwo - l2CAone) && overflow2B && increase2A ) return 0;
309 else if ( (l2CBtwo - l2CBone) < (max1 - l2CAone + l2CAtwo) && increase2B && overflow2A ) return 0;
310 else if ( (max1 - l2CBone + l2CBtwo ) < (max1 - l2CAone + l2CAtwo) && overflow2B && overflow2A ) return 0;
316 //_____________________________________________________________________________
317 void AliTriggerRunScalers::Print( const Option_t* ) const
320 cout << "Trigger Scalers Record per Run: " << endl;
321 cout << " File version :" << fVersion << endl;
322 cout << " Run Number :" << fRunNumber << endl;
323 cout << " Number of Classes :" << (Int_t)fnClasses << endl;
324 cout << " Classes ID:";
325 for( Int_t i=0; i<fnClasses; ++i )
326 cout << " " << (Int_t)fClassIndex[i];
329 for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i )
330 ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();