]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerRunScalers.cxx
GC2,RS1,RS3 rules applied
[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 {
56   // Default constructor
57 }
58
59 //_____________________________________________________________________________
60 void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler ) 
61
62   // Add scaler and check consistency
63   fScalersRecord.AddLast( scaler );
64   if (!AliTriggerRunScalers::ConsistencyCheck()) AliErrorClass("Trigger counters not in the right order or decreasing!");
65 //  fScalersRecord.Sort(); 
66 }
67
68 //_____________________________________________________________________________
69 AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
70 {
71   // Read scalers from text file(.cnt) provided by CTP 
72   // for given run and convert it to root format 
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     }
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
152     AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
153     rec->SetTimeStamp( orbit, period, seconds, microSecs );
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 ) {
161         AliErrorClass( Form( "Error reading scalers from (%s): line (%s)", 
162                              filename.Data(), strLine1.Data() ));
163         delete rec;
164         return rScaler;
165       }
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);
173
174       rScaler->GetClass(i);
175       rec->AddTriggerScalers( rScaler->GetClass(i),
176                               lOCB, lOCA, l1CB,
177                               l1CA, l2CB, l2CA );
178
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 //_____________________________________________________________________________
192 Int_t  AliTriggerRunScalers::FindNearestScalersRecord( const AliTimeStamp *stamp ) const
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 }
226 //_____________________________________________________________________________
227 Bool_t AliTriggerRunScalers::ConsistencyCheck()
228 {
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;
235
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 ){
240
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();
249
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();
258
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.
261
262    if ( lOCBtwo > lOCBone ) increase0B=1;
263    else if ( lOCBtwo < lOCBone && (lOCBone - lOCBtwo) > max2) overflow0B=1;
264    else return 0;
265
266    if ( lOCAtwo > lOCAone ) increase0A=1;
267    else if ( lOCAtwo < lOCAone && (lOCAone - lOCAtwo) > max2) overflow0A=1;
268    else return 0;
269
270    if ( l1CBtwo > l1CBone ) increase1B=1;
271    else if ( l1CBtwo < l1CBone && (l1CBone - l1CBtwo) > max2) overflow1B=1;
272    else return 0;
273
274    if ( l1CAtwo > l1CAone ) increase1A=1;
275    else if ( l1CAtwo < l1CAone && (l1CAone - l1CAtwo) > max2) overflow1A=1;
276    else return 0;
277
278    if ( l2CBtwo > l2CBone ) increase2B=1;
279    else if ( l2CBtwo < l2CBone && (l2CBone - l2CBtwo) > max2) overflow2B=1;
280    else return 0;
281
282    if ( l2CAtwo > l2CAone ) increase2A=1;
283    else if ( l2CAtwo < l2CAone && (l2CAone - l2CAtwo) > max2) overflow2A=1;
284    else return 0;
285
286
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;
291  
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;
296
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;
301
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;
306
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;
311
312    }
313
314    return 1;
315 }
316 //_____________________________________________________________________________
317 void 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