]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerRunScalers.cxx
4e0c2e555a75bdd32df2a280cab9c858dcaee754
[u/mrichter/AliRoot.git] / STEER / AliTriggerRunScalers.cxx
1 /**************************************************************************
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 // 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 // 
25 //
26 //////////////////////////////////////////////////////////////////////////////
27
28 #include <stdlib.h>
29 #include <Riostream.h>
30
31 //#include <TObject.h>
32 //#include <TArrayC.h>
33 //#include <TFile.h>
34 #include <TClass.h>
35 #include <TSystem.h>
36 #include <TObjString.h>
37 #include <TObjArray.h>
38
39 #include "AliLog.h"  
40 #include "AliTimeStamp.h"
41 #include "AliTriggerScalers.h"
42 #include "AliTriggerScalersRecord.h"
43 #include "AliTriggerRunScalers.h"
44
45 ClassImp( AliTriggerRunScalers )
46
47 //_____________________________________________________________________________
48 AliTriggerRunScalers::AliTriggerRunScalers():
49   TObject(),
50   fVersion(0),
51   fRunNumber(0),
52   fnClasses(0),
53   fClassIndex(),                    
54   fScalersRecord(),
55   fScalersRecordESD()
56 {
57   // Default constructor
58 }
59 //______________________________________________________________________________
60 AliTriggerRunScalers::~AliTriggerRunScalers() 
61 {
62  // Destructor
63  fScalersRecord.SetOwner(); 
64  fScalersRecord.Delete(); 
65  fScalersRecordESD.SetOwner(); 
66  fScalersRecordESD.Delete(); 
67 }
68 //_____________________________________________________________________________
69 void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler ) 
70
71   // Add scaler and check consistency
72   fScalersRecord.AddLast( scaler );
73   if (!AliTriggerRunScalers::ConsistencyCheck()) AliErrorClass("Trigger counters not in the right order or decreasing!");
74 //  fScalersRecord.Sort(); 
75 }
76 //_____________________________________________________________________________
77
78 AliTriggerRunScalers::AliTriggerRunScalers(const AliTriggerRunScalers &run) :
79  TObject(),
80  fVersion(run.fVersion),
81  fRunNumber(run.fRunNumber),
82  fnClasses(run.fnClasses),
83  fClassIndex(),                    
84  fScalersRecord(),
85  fScalersRecordESD()
86 {
87 // copy constructor
88 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
89     if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
90   }
91 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
92     if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
93   }
94 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
95     if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
96   }
97
98 }
99 //_____________________________________________________________________________
100 AliTriggerRunScalers &AliTriggerRunScalers::operator=(const AliTriggerRunScalers& run)
101 {
102 // assignment operator
103 if(&run == this) return *this;
104 ((TObject *)this)->operator=(run);
105
106 fVersion = run.fVersion;
107 fRunNumber = run.fRunNumber;
108 fnClasses = run.fnClasses;
109
110 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
111     if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
112   }
113 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
114     if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
115   }
116 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
117     if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
118   }
119 return *this;
120
121 //_____________________________________________________________________________
122 AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
123 {
124   // Read scalers from text file(.cnt) provided by CTP 
125   // for given run and convert it to root format 
126   if( gSystem->AccessPathName( filename.Data() ) ) {
127      AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
128      return NULL;
129   }
130
131   ifstream *file = new ifstream ( filename.Data() );
132   if (!*file) {
133     AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
134     file->close();
135     delete file;
136     return NULL;
137   }
138   
139   AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
140   
141   TString strLine;
142   Bool_t verflag = kFALSE;
143   Bool_t classflag = kFALSE;
144   UChar_t nclass = 0;
145   while (strLine.ReadLine(*file)) {
146     if (strLine.BeginsWith("#")) continue;
147     
148     TObjArray *tokens = strLine.Tokenize(" \t");
149     Int_t ntokens = tokens->GetEntriesFast();
150     // 1st line, version, it is one number, 
151     if (!verflag) {
152       if (ntokens != 1) { 
153         AliErrorClass( Form( "Error reading version number from (%s), line :%s\n", 
154                               filename.Data() , strLine.Data() ) );  
155         return NULL;
156       }
157   //    cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
158       rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
159       verflag = kTRUE;
160       delete tokens;
161       continue;
162     }
163    
164     // 2nd line, run number , number of classes, list of classes used in this partition
165
166     if (!classflag) {
167       if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
168         AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() )); 
169       }
170   //    cout << "Run Number " << ((TObjString*)tokens->At(0))->String().Atoi() << endl;
171       rScaler->SetRunNumber( ((TObjString*)tokens->At(0))->String().Atoi() );
172       nclass = (UChar_t)((TObjString*)tokens->At(1))->String().Atoi();
173   //    cout << "Number of classes " << nclass << endl;
174       rScaler->SetNumClasses( nclass );
175       if ( nclass != ntokens - 2 ) {
176         AliErrorClass( Form( "Error reading number of classes from (%s)\n", filename.Data() )); 
177       }
178       for (UChar_t i=0; i<nclass; ++i) {
179         rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
180       }
181       classflag = kTRUE;
182       delete tokens;
183       continue;
184     }
185     
186     // Records
187     // Each record consists of 1+(number of classes in partition) lines
188     //  1st line of record = time stamp (4 words)
189     //                        1st word = ORBIT(24 bits)
190     //                        2nd word = Period Counter (28 bit)
191     //                        3rd word = seconds (32 bits) from epoch
192     //                        4th word = microsecs (32 bits)
193     //  other lines = 6 words of counters (L0 before,L0 after, ....)
194     if (ntokens != 4) { 
195       AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)", 
196                             filename.Data(), strLine.Data() )); 
197       return NULL;
198     }
199
200     UInt_t orbit     = strtoul(((TObjString*)tokens->At(0))->String(), NULL, 10);
201     UInt_t period    = strtoul(((TObjString*)tokens->At(1))->String(), NULL, 10);
202     UInt_t seconds   = strtoul(((TObjString*)tokens->At(2))->String(), NULL, 10);
203     UInt_t microSecs = strtoul(((TObjString*)tokens->At(3))->String(), NULL, 10);
204
205     AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
206     rec->SetTimeStamp( orbit, period, seconds, microSecs );
207     TString strLine1;
208     for (Int_t i=0; i<nclass; ++i) {
209       strLine1.ReadLine(*file);
210       if (strLine1.BeginsWith("#")) continue;
211       TObjArray *tokens1 = strLine1.Tokenize(" \t");
212       Int_t ntokens1 = tokens1->GetEntriesFast();
213       if( ntokens1 != 6 ) {
214         AliErrorClass( Form( "Error reading scalers from (%s): line (%s)", 
215                              filename.Data(), strLine1.Data() ));
216         delete rec;
217         return rScaler;
218       }
219
220       UInt_t lOCB = strtoul(((TObjString*)tokens1->At(0))->String(), NULL, 10);
221       UInt_t lOCA = strtoul(((TObjString*)tokens1->At(1))->String(), NULL, 10);
222       UInt_t l1CB = strtoul(((TObjString*)tokens1->At(2))->String(), NULL, 10);
223       UInt_t l1CA = strtoul(((TObjString*)tokens1->At(3))->String(), NULL, 10);
224       UInt_t l2CB = strtoul(((TObjString*)tokens1->At(4))->String(), NULL, 10);
225       UInt_t l2CA = strtoul(((TObjString*)tokens1->At(5))->String(), NULL, 10);
226
227       rScaler->GetClass(i);
228       rec->AddTriggerScalers( rScaler->GetClass(i),
229                               lOCB, lOCA, l1CB,
230                               l1CA, l2CB, l2CA );
231
232       delete tokens1;
233     } 
234     rScaler->AddTriggerScalers( rec );
235     
236     delete tokens;     
237   }
238   file->close();
239   delete file;
240
241   return  rScaler; 
242 }
243   
244 //_____________________________________________________________________________
245 Int_t  AliTriggerRunScalers::FindNearestScalersRecord( const AliTimeStamp *stamp ) const
246 {
247    // Find Trigger scaler record with the closest timestamp <= "stamp"
248    // using a binary search. 
249    // return the index in the array of records, if the timestamp 
250    // is out of range return -1
251
252    Int_t   base, position=-1, last, result = 0;
253    Int_t op2 = 0;
254    
255    //fScalersRecord.Sort();
256
257    base = 0;
258    last = fScalersRecord.GetEntriesFast();
259
260    while (last >= base) {
261       position = (base+last) / 2;
262       cout << "pos " <<   position<< " base " <<   base << "last " <<   last << endl;
263       AliTriggerScalersRecord* rec = (AliTriggerScalersRecord*)fScalersRecord.At(position);
264       if( rec && rec->GetTimeStamp()) op2 = 1;
265       if( op2 && (result = stamp->Compare(rec->GetTimeStamp())) == 0  )
266          return position;  // exact match
267       cout << "result " <<   result << " op2 " << op2 << " rec "<< rec << endl;
268       if (!op2 || result < 0)
269          last = position-1;
270       else
271          base = position+1;
272       op2 = 0;  
273    }
274    if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() ) 
275     return -1;  // out of range
276    else 
277     return (result < 0 ) ? position-1 : position; // nearst < stamp   
278 }
279 //_____________________________________________________________________________
280 Bool_t AliTriggerRunScalers::ConsistencyCheck() const
281 {
282    //Check if counters are consistent(increase). Example: lOCB(n) < lOCB(n+1) and lOCB > lOCA
283    UInt_t lOCBtwo, lOCAtwo, l1CBtwo, l1CAtwo, l2CBtwo, l2CAtwo, lOCBone, lOCAone, l1CBone, l1CAone, l2CBone, l2CAone;
284    Bool_t increase0B=0, increase0A=0, increase1B=0,  increase1A=0, increase2B=0, increase2A=0;
285    Bool_t overflow0B=0, overflow0A=0, overflow1B=0,  overflow1A=0, overflow2B=0, overflow2A=0;
286    Int_t position = fScalersRecord.GetEntriesFast()-1;
287    if (position == 0) return 1;
288
289    AliTriggerScalersRecord* scalers2 = (AliTriggerScalersRecord*)fScalersRecord.At(position);
290    AliTriggerScalersRecord* scalers1 = (AliTriggerScalersRecord*)fScalersRecord.At(position-1);
291    if (scalers2->Compare((AliTriggerScalersRecord*)fScalersRecord.At(position-1)) == -1) return 0;
292    else for( Int_t i=0; i<fnClasses; ++i ){
293
294    TObjArray* scalersArray2 = (TObjArray*)scalers2->GetTriggerScalers();
295    AliTriggerScalers* counters2 = (AliTriggerScalers*)scalersArray2->At(i);
296         lOCBtwo = counters2->GetLOCB();
297         lOCAtwo = counters2->GetLOCA();
298         l1CBtwo = counters2->GetL1CB();
299         l1CAtwo = counters2->GetL1CA();
300         l2CBtwo = counters2->GetL2CB();
301         l2CAtwo = counters2->GetL2CA();
302
303    TObjArray* scalersArray1 = (TObjArray*)scalers1->GetTriggerScalers();
304    AliTriggerScalers* counters1 = (AliTriggerScalers*)scalersArray1->At(i);
305         lOCBone = counters1->GetLOCB();
306         lOCAone = counters1->GetLOCA();
307         l1CBone = counters1->GetL1CB();
308         l1CAone = counters1->GetL1CA();
309         l2CBone = counters1->GetL2CB();
310         l2CAone = counters1->GetL2CA();
311
312    UInt_t const max1 = 4294967295ul;  //32bit counters overflow after 4294967295
313    UInt_t const max2 = 1000000000ul;  //when counters overflow they seem to be decreasing. Assume decrease cannot be smaller than max2.
314
315    if ( lOCBtwo > lOCBone ) increase0B=1;
316    else if ( lOCBtwo < lOCBone && (lOCBone - lOCBtwo) > max2) overflow0B=1;
317    else return 0;
318
319    if ( lOCAtwo > lOCAone ) increase0A=1;
320    else if ( lOCAtwo < lOCAone && (lOCAone - lOCAtwo) > max2) overflow0A=1;
321    else return 0;
322
323    if ( l1CBtwo > l1CBone ) increase1B=1;
324    else if ( l1CBtwo < l1CBone && (l1CBone - l1CBtwo) > max2) overflow1B=1;
325    else return 0;
326
327    if ( l1CAtwo > l1CAone ) increase1A=1;
328    else if ( l1CAtwo < l1CAone && (l1CAone - l1CAtwo) > max2) overflow1A=1;
329    else return 0;
330
331    if ( l2CBtwo > l2CBone ) increase2B=1;
332    else if ( l2CBtwo < l2CBone && (l2CBone - l2CBtwo) > max2) overflow2B=1;
333    else return 0;
334
335    if ( l2CAtwo > l2CAone ) increase2A=1;
336    else if ( l2CAtwo < l2CAone && (l2CAone - l2CAtwo) > max2) overflow2A=1;
337    else return 0;
338
339
340    if ( (lOCBtwo - lOCBone) < (lOCAtwo - lOCAone) && increase0B && increase0A ) return 0;
341    else if ( (max1 - lOCBone + lOCBtwo ) < (lOCAtwo - lOCAone) && overflow0B && increase0A ) return 0;
342    else if ( (lOCBtwo - lOCBone) < (max1 - lOCAone + lOCAtwo) && increase0B && overflow0A ) return 0;
343    else if ( (max1 - lOCBone + lOCBtwo ) < (max1 - lOCAone + lOCAtwo) && overflow0B && overflow0A ) return 0;
344  
345    if ( (lOCAtwo - lOCAone) < (l1CBtwo - l1CBone) && increase0A && increase1B ) return 0;
346    else if ( (max1 - lOCAone + lOCAtwo ) < (l1CBtwo - l1CBone) && overflow0A && increase1B ) return 0;
347    else if ( (lOCAtwo - lOCAone) < (max1 - l1CBone + l1CBtwo) && increase0A && overflow1B ) return 0;
348    else if ( (max1 - lOCAone + lOCAtwo ) < (max1 - l1CBone + l1CBtwo) && overflow0A && overflow1B ) return 0;
349
350    if ( (l1CBtwo - l1CBone) < (l1CAtwo - l1CAone) && increase1B && increase1A ) return 0;
351    else if ( (max1 - l1CBone + l1CBtwo ) < (l1CAtwo - l1CAone) && overflow1B && increase1A ) return 0;
352    else if ( (l1CBtwo - l1CBone) < (max1 - l1CAone + l1CAtwo) && increase1B && overflow1A ) return 0;
353    else if ( (max1 - l1CBone + l1CBtwo ) < (max1 - l1CAone + l1CAtwo) && overflow1B && overflow1A ) return 0;
354
355    if ( (l1CAtwo - l1CAone) < (l2CBtwo - l2CBone) && increase1A && increase2B ) return 0;
356    else if ( (max1 - l1CAone + l1CAtwo ) < (l2CBtwo - l2CBone) && overflow1A && increase2B ) return 0;
357    else if ( (l1CAtwo - l1CAone) < (max1 - l2CBone + l2CBtwo) && increase1A && overflow2B ) return 0;
358    else if ( (max1 - l1CAone + l1CAtwo ) < (max1 - l2CBone + l2CBtwo) && overflow1A && overflow2B ) return 0;
359
360    if ( (l2CBtwo - l2CBone) < (l2CAtwo - l2CAone) && increase2B && increase2A ) return 0;
361    else if ( (max1 - l2CBone + l2CBtwo ) < (l2CAtwo - l2CAone) && overflow2B && increase2A ) return 0;
362    else if ( (l2CBtwo - l2CBone) < (max1 - l2CAone + l2CAtwo) && increase2B && overflow2A ) return 0;
363    else if ( (max1 - l2CBone + l2CBtwo ) < (max1 - l2CAone + l2CAtwo) && overflow2B && overflow2A ) return 0;
364
365    }
366
367    return 1;
368 }
369 //_____________________________________________________________________________
370 void AliTriggerRunScalers::Print( const Option_t* ) const
371 {
372    // Print
373   cout << "Trigger Scalers Record per Run: " << endl;
374   cout << "  File version :" <<  fVersion << endl;            
375   cout << "  Run Number :" <<  fRunNumber << endl;          
376   cout << "  Number of Classes :" <<  (Int_t)fnClasses << endl;          
377   cout << "    Classes ID:";
378   for( Int_t i=0; i<fnClasses; ++i ) 
379     cout << "  " << (Int_t)fClassIndex[i];       
380   cout << endl; 
381     
382   for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i ) 
383      ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();
384 }
385