]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliTriggerRunScalers.cxx
Reverted back.
[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.
21//
22//
23//////////////////////////////////////////////////////////////////////////////
24
c180f65d 25#include <stdlib.h>
26
d7dd8a54 27#include <Riostream.h>
28#include <TObject.h>
29#include <TClass.h>
30#include <TArrayC.h>
31#include <TSystem.h>
32#include <TObjString.h>
33#include <TObjArray.h>
34#include <TSystem.h>
35#include <TFile.h>
36
37#include "AliLog.h"
38#include "AliTimeStamp.h"
39#include "AliTriggerScalers.h"
40#include "AliTriggerScalersRecord.h"
41#include "AliTriggerRunScalers.h"
e39e51f6 42#include "AliCDBEntry.h"
43#include "AliCDBManager.h"
44#include "AliCDBStorage.h"
d7dd8a54 45
46ClassImp( AliTriggerRunScalers )
47
48//_____________________________________________________________________________
49AliTriggerRunScalers::AliTriggerRunScalers():
50 TObject(),
51 fVersion(0),
52 fRunNumber(0),
53 fnClasses(0),
54 fClassIndex(),
55 fScalersRecord()
56{
57 // Default constructor
58}
59
60//_____________________________________________________________________________
61void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler )
62{
63 //
e39e51f6 64 fScalersRecord.AddLast( scaler );
65 if (!AliTriggerRunScalers::ConsistencyCheck()) AliErrorClass("Trigger counters not in the right order or decreasing!");
66// fScalersRecord.Sort();
d7dd8a54 67}
68
69//_____________________________________________________________________________
70AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
71{
d5aaf775 72 //Read scalers from text file(.cnt) provided by CTP for given run and convert it to root format
d7dd8a54 73 if( gSystem->AccessPathName( filename.Data() ) ) {
74 AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
75 return NULL;
76 }
77
78 ifstream *file = new ifstream ( filename.Data() );
79 if (!*file) {
80 AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
81 file->close();
82 delete file;
83 return NULL;
84 }
85
86 AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
87
88 TString strLine;
89 Bool_t verflag = kFALSE;
90 Bool_t classflag = kFALSE;
91 UChar_t nclass = 0;
92 while (strLine.ReadLine(*file)) {
93 if (strLine.BeginsWith("#")) continue;
94
95 TObjArray *tokens = strLine.Tokenize(" \t");
96 Int_t ntokens = tokens->GetEntriesFast();
97 // 1st line, version, it is one number,
98 if (!verflag) {
99 if (ntokens != 1) {
100 AliErrorClass( Form( "Error reading version number from (%s), line :%s\n",
101 filename.Data() , strLine.Data() ) );
102 return NULL;
103 }
104 // cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
105 rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
106 verflag = kTRUE;
107 delete tokens;
108 continue;
109 }
110
111 // 2nd line, run number , number of classes, list of classes used in this partition
112
113 if (!classflag) {
114 if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
115 AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() ));
116 }
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() ));
124 }
125 for (UChar_t i=0; i<nclass; ++i) {
126 rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
127 }
128 classflag = kTRUE;
129 delete tokens;
130 continue;
131 }
132
133 // Records
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, ....)
141 if (ntokens != 4) {
142 AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)",
143 filename.Data(), strLine.Data() ));
144 return NULL;
145 }
e39e51f6 146
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);
151
d7dd8a54 152 AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
153 rec->SetTimeStamp( orbit, period, seconds, microSecs );
d7dd8a54 154 TString strLine1;
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 ) {
29bdda4f 161 AliErrorClass( Form( "Error reading scalers from (%s): line (%s)",
162 filename.Data(), strLine1.Data() ));
163 delete rec;
164 return rScaler;
d7dd8a54 165 }
d5aaf775 166
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);
e39e51f6 173
d7dd8a54 174 rScaler->GetClass(i);
175 rec->AddTriggerScalers( rScaler->GetClass(i),
d5aaf775 176 lOCB, lOCA, l1CB,
177 l1CA, l2CB, l2CA );
178
d7dd8a54 179 delete tokens1;
180 }
181 rScaler->AddTriggerScalers( rec );
182
183 delete tokens;
184 }
185 file->close();
186 delete file;
187
188 return rScaler;
189}
190
191//_____________________________________________________________________________
192Int_t AliTriggerRunScalers::FindNearestScalersRecord( AliTimeStamp * stamp )
193{
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
198
199 Int_t base, position=-1, last, result = 0;
200 AliTimeStamp *op2 = NULL;
201
202 fScalersRecord.Sort();
203
204 base = 0;
205 last = fScalersRecord.GetEntriesFast();
206
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)
216 last = position-1;
217 else
218 base = position+1;
219 op2 = NULL;
220 }
221 if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() )
222 return -1; // out of range
223 else
224 return (result < 0 ) ? position-1 : position; // nearst < stamp
225}
e39e51f6 226//_____________________________________________________________________________
227Bool_t AliTriggerRunScalers::ConsistencyCheck()
228{
d5aaf775 229 //Check if counters are consistent(increase). Example: lOCB(n) < lOCB(n+1) and lOCB > lOCA
230UInt_t lOCBtwo, lOCAtwo, l1CBtwo, l1CAtwo, l2CBtwo, l2CAtwo, lOCBone, lOCAone, l1CBone, l1CAone, l2CBone, l2CAone;
35d1f53b 231Bool_t increase0B=0, increase0A=0, increase1B=0, increase1A=0, increase2B=0, increase2A=0;
232Bool_t overflow0B=0, overflow0A=0, overflow1B=0, overflow1A=0, overflow2B=0, overflow2A=0;
e39e51f6 233Int_t position = fScalersRecord.GetEntriesFast()-1;
e39e51f6 234if (position == 0) return 1;
235
d5aaf775 236AliTriggerScalersRecord* scalers2 = (AliTriggerScalersRecord*)fScalersRecord.At(position);
237AliTriggerScalersRecord* scalers1 = (AliTriggerScalersRecord*)fScalersRecord.At(position-1);
238if (scalers2->Compare((AliTriggerScalersRecord*)fScalersRecord.At(position-1)) == -1) return 0;
e39e51f6 239else for( Int_t i=0; i<fnClasses; ++i ){
240
d5aaf775 241TObjArray* scalersArray2 = (TObjArray*)scalers2->GetTriggerScalers();
242AliTriggerScalers* 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();
249
250TObjArray* scalersArray1 = (TObjArray*)scalers1->GetTriggerScalers();
251AliTriggerScalers* 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();
e39e51f6 258
35d1f53b 259UInt_t const max1 = 4294967295ul; //32bit counters overflow after 4294967295
260UInt_t const max2 = 1000000000ul; //when counters overflow they seem to be decreasing. Assume decrease cannot be smaller than max2.
e39e51f6 261
d5aaf775 262if ( lOCBtwo > lOCBone ) increase0B=1;
263else if ( lOCBtwo < lOCBone && (lOCBone - lOCBtwo) > max2) overflow0B=1;
e39e51f6 264else return 0;
265
d5aaf775 266if ( lOCAtwo > lOCAone ) increase0A=1;
267else if ( lOCAtwo < lOCAone && (lOCAone - lOCAtwo) > max2) overflow0A=1;
e39e51f6 268else return 0;
269
d5aaf775 270if ( l1CBtwo > l1CBone ) increase1B=1;
271else if ( l1CBtwo < l1CBone && (l1CBone - l1CBtwo) > max2) overflow1B=1;
e39e51f6 272else return 0;
d7dd8a54 273
d5aaf775 274if ( l1CAtwo > l1CAone ) increase1A=1;
275else if ( l1CAtwo < l1CAone && (l1CAone - l1CAtwo) > max2) overflow1A=1;
e39e51f6 276else return 0;
277
d5aaf775 278if ( l2CBtwo > l2CBone ) increase2B=1;
279else if ( l2CBtwo < l2CBone && (l2CBone - l2CBtwo) > max2) overflow2B=1;
e39e51f6 280else return 0;
281
d5aaf775 282if ( l2CAtwo > l2CAone ) increase2A=1;
283else if ( l2CAtwo < l2CAone && (l2CAone - l2CAtwo) > max2) overflow2A=1;
35d1f53b 284else return 0;
e39e51f6 285
e39e51f6 286
d5aaf775 287if ( (lOCBtwo - lOCBone) < (lOCAtwo - lOCAone) && increase0B && increase0A ) return 0;
288else if ( (max1 - lOCBone + lOCBtwo ) < (lOCAtwo - lOCAone) && overflow0B && increase0A ) return 0;
289else if ( (lOCBtwo - lOCBone) < (max1 - lOCAone + lOCAtwo) && increase0B && overflow0A ) return 0;
290else if ( (max1 - lOCBone + lOCBtwo ) < (max1 - lOCAone + lOCAtwo) && overflow0B && overflow0A ) return 0;
e39e51f6 291
d5aaf775 292if ( (lOCAtwo - lOCAone) < (l1CBtwo - l1CBone) && increase0A && increase1B ) return 0;
293else if ( (max1 - lOCAone + lOCAtwo ) < (l1CBtwo - l1CBone) && overflow0A && increase1B ) return 0;
294else if ( (lOCAtwo - lOCAone) < (max1 - l1CBone + l1CBtwo) && increase0A && overflow1B ) return 0;
295else if ( (max1 - lOCAone + lOCAtwo ) < (max1 - l1CBone + l1CBtwo) && overflow0A && overflow1B ) return 0;
e39e51f6 296
d5aaf775 297if ( (l1CBtwo - l1CBone) < (l1CAtwo - l1CAone) && increase1B && increase1A ) return 0;
298else if ( (max1 - l1CBone + l1CBtwo ) < (l1CAtwo - l1CAone) && overflow1B && increase1A ) return 0;
299else if ( (l1CBtwo - l1CBone) < (max1 - l1CAone + l1CAtwo) && increase1B && overflow1A ) return 0;
300else if ( (max1 - l1CBone + l1CBtwo ) < (max1 - l1CAone + l1CAtwo) && overflow1B && overflow1A ) return 0;
e39e51f6 301
d5aaf775 302if ( (l1CAtwo - l1CAone) < (l2CBtwo - l2CBone) && increase1A && increase2B ) return 0;
303else if ( (max1 - l1CAone + l1CAtwo ) < (l2CBtwo - l2CBone) && overflow1A && increase2B ) return 0;
304else if ( (l1CAtwo - l1CAone) < (max1 - l2CBone + l2CBtwo) && increase1A && overflow2B ) return 0;
305else if ( (max1 - l1CAone + l1CAtwo ) < (max1 - l2CBone + l2CBtwo) && overflow1A && overflow2B ) return 0;
e39e51f6 306
d5aaf775 307if ( (l2CBtwo - l2CBone) < (l2CAtwo - l2CAone) && increase2B && increase2A ) return 0;
308else if ( (max1 - l2CBone + l2CBtwo ) < (l2CAtwo - l2CAone) && overflow2B && increase2A ) return 0;
309else if ( (l2CBtwo - l2CBone) < (max1 - l2CAone + l2CAtwo) && increase2B && overflow2A ) return 0;
310else if ( (max1 - l2CBone + l2CBtwo ) < (max1 - l2CAone + l2CAtwo) && overflow2B && overflow2A ) return 0;
e39e51f6 311
e39e51f6 312}
313
314return 1;
315}
d7dd8a54 316//_____________________________________________________________________________
317void AliTriggerRunScalers::Print( const Option_t* ) const
318{
319 // Print
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];
327 cout << endl;
328
329 for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i )
330 ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();
331}
332