]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerRunScalers.cxx
Array of records with 64 bit scalers added, copy constructor,=operator
[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 //_____________________________________________________________________________
61 void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler ) 
62
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(); 
67 }
68 //_____________________________________________________________________________
69
70 AliTriggerRunScalers::AliTriggerRunScalers(const AliTriggerRunScalers &run) :
71  TObject(),
72  fVersion(run.fVersion),
73  fRunNumber(run.fRunNumber),
74  fnClasses(run.fnClasses),
75  fClassIndex(),                    
76  fScalersRecord(),
77  fScalersRecordESD()
78 {
79 // copy constructor
80 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
81     if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
82   }
83 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
84     if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
85   }
86 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
87     if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
88   }
89
90 }
91 //_____________________________________________________________________________
92 AliTriggerRunScalers &AliTriggerRunScalers::operator=(const AliTriggerRunScalers& run)
93 {
94 // assignment operator
95 if(&run == this) return *this;
96 ((TObject *)this)->operator=(run);
97
98 fVersion = run.fVersion;
99 fRunNumber = run.fRunNumber;
100 fnClasses = run.fnClasses;
101
102 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
103     if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
104   }
105 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
106     if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
107   }
108 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
109     if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
110   }
111 return *this;
112
113 //_____________________________________________________________________________
114 AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
115 {
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() ) );
120      return NULL;
121   }
122
123   ifstream *file = new ifstream ( filename.Data() );
124   if (!*file) {
125     AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
126     file->close();
127     delete file;
128     return NULL;
129   }
130   
131   AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
132   
133   TString strLine;
134   Bool_t verflag = kFALSE;
135   Bool_t classflag = kFALSE;
136   UChar_t nclass = 0;
137   while (strLine.ReadLine(*file)) {
138     if (strLine.BeginsWith("#")) continue;
139     
140     TObjArray *tokens = strLine.Tokenize(" \t");
141     Int_t ntokens = tokens->GetEntriesFast();
142     // 1st line, version, it is one number, 
143     if (!verflag) {
144       if (ntokens != 1) { 
145         AliErrorClass( Form( "Error reading version number from (%s), line :%s\n", 
146                               filename.Data() , strLine.Data() ) );  
147         return NULL;
148       }
149   //    cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
150       rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
151       verflag = kTRUE;
152       delete tokens;
153       continue;
154     }
155    
156     // 2nd line, run number , number of classes, list of classes used in this partition
157
158     if (!classflag) {
159       if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
160         AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() )); 
161       }
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() )); 
169       }
170       for (UChar_t i=0; i<nclass; ++i) {
171         rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
172       }
173       classflag = kTRUE;
174       delete tokens;
175       continue;
176     }
177     
178     // Records
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, ....)
186     if (ntokens != 4) { 
187       AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)", 
188                             filename.Data(), strLine.Data() )); 
189       return NULL;
190     }
191
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);
196
197     AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
198     rec->SetTimeStamp( orbit, period, seconds, microSecs );
199     TString strLine1;
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() ));
208         delete rec;
209         return rScaler;
210       }
211
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);
218
219       rScaler->GetClass(i);
220       rec->AddTriggerScalers( rScaler->GetClass(i),
221                               lOCB, lOCA, l1CB,
222                               l1CA, l2CB, l2CA );
223
224       delete tokens1;
225     } 
226     rScaler->AddTriggerScalers( rec );
227     
228     delete tokens;     
229   }
230   file->close();
231   delete file;
232
233   return  rScaler; 
234 }
235   
236 //_____________________________________________________________________________
237 Int_t  AliTriggerRunScalers::FindNearestScalersRecord( const AliTimeStamp *stamp ) const
238 {
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
243
244    Int_t   base, position=-1, last, result = 0;
245    Int_t op2 = 0;
246    
247    //fScalersRecord.Sort();
248
249    base = 0;
250    last = fScalersRecord.GetEntriesFast();
251
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)
261          last = position-1;
262       else
263          base = position+1;
264       op2 = 0;  
265    }
266    if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() ) 
267     return -1;  // out of range
268    else 
269     return (result < 0 ) ? position-1 : position; // nearst < stamp   
270 }
271 //_____________________________________________________________________________
272 Bool_t AliTriggerRunScalers::ConsistencyCheck() const
273 {
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;
280
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 ){
285
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();
294
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();
303
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.
306
307    if ( lOCBtwo > lOCBone ) increase0B=1;
308    else if ( lOCBtwo < lOCBone && (lOCBone - lOCBtwo) > max2) overflow0B=1;
309    else return 0;
310
311    if ( lOCAtwo > lOCAone ) increase0A=1;
312    else if ( lOCAtwo < lOCAone && (lOCAone - lOCAtwo) > max2) overflow0A=1;
313    else return 0;
314
315    if ( l1CBtwo > l1CBone ) increase1B=1;
316    else if ( l1CBtwo < l1CBone && (l1CBone - l1CBtwo) > max2) overflow1B=1;
317    else return 0;
318
319    if ( l1CAtwo > l1CAone ) increase1A=1;
320    else if ( l1CAtwo < l1CAone && (l1CAone - l1CAtwo) > max2) overflow1A=1;
321    else return 0;
322
323    if ( l2CBtwo > l2CBone ) increase2B=1;
324    else if ( l2CBtwo < l2CBone && (l2CBone - l2CBtwo) > max2) overflow2B=1;
325    else return 0;
326
327    if ( l2CAtwo > l2CAone ) increase2A=1;
328    else if ( l2CAtwo < l2CAone && (l2CAone - l2CAtwo) > max2) overflow2A=1;
329    else return 0;
330
331
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;
336  
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;
341
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;
346
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;
351
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;
356
357    }
358
359    return 1;
360 }
361 //_____________________________________________________________________________
362 void AliTriggerRunScalers::Print( const Option_t* ) const
363 {
364    // Print
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];       
372   cout << endl; 
373     
374   for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i ) 
375      ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();
376 }
377