]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerRunScalers.cxx
Bugs in CheckConsistency, more warnings (with PLamen)
[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 "AliTriggerScalersESD.h"
43 #include "AliTriggerScalersRecord.h"
44 #include "AliTriggerScalersRecordESD.h"
45 #include "AliTriggerRunScalers.h"
46
47 ClassImp( AliTriggerRunScalers )
48
49 //_____________________________________________________________________________
50 AliTriggerRunScalers::AliTriggerRunScalers():
51   TObject(),
52   fVersion(0),
53   fRunNumber(0),
54   fnClasses(0),
55   fClassIndex(),                    
56   fScalersRecord(),
57   fScalersRecordESD()
58 {
59   // Default constructor
60 }
61 //______________________________________________________________________________
62 AliTriggerRunScalers::~AliTriggerRunScalers() 
63 {
64  // Destructor
65  fScalersRecord.SetOwner(); 
66  fScalersRecord.Delete(); 
67  fScalersRecordESD.SetOwner(); 
68  fScalersRecordESD.Delete(); 
69 }
70 //_____________________________________________________________________________
71 void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler ) 
72
73   // Add scaler and check consistency
74   fScalersRecord.AddLast( scaler );
75   if (AliTriggerRunScalers::ConsistencyCheck(fScalersRecord.GetEntriesFast()-1,kFALSE)) AliErrorClass("Trigger counters not in the right order or decreasing!");
76 //  fScalersRecord.Sort(); 
77 }
78 //_____________________________________________________________________________
79
80 AliTriggerRunScalers::AliTriggerRunScalers(const AliTriggerRunScalers &run) :
81  TObject(),
82  fVersion(run.fVersion),
83  fRunNumber(run.fRunNumber),
84  fnClasses(run.fnClasses),
85  fClassIndex(),                    
86  fScalersRecord(),
87  fScalersRecordESD()
88 {
89 // copy constructor
90 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
91     if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
92   }
93 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
94     if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
95   }
96 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
97     if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
98   }
99
100 }
101 //_____________________________________________________________________________
102 AliTriggerRunScalers &AliTriggerRunScalers::operator=(const AliTriggerRunScalers& run)
103 {
104 // assignment operator
105 if(&run == this) return *this;
106 ((TObject *)this)->operator=(run);
107
108 fVersion = run.fVersion;
109 fRunNumber = run.fRunNumber;
110 fnClasses = run.fnClasses;
111
112 for (Int_t i = 0; i < run.fClassIndex.GetSize(); i++) {
113     if (run.fClassIndex[i]) fClassIndex.AddAt(run.fClassIndex[i], i);
114   }
115 for (Int_t i = 0; i < run.fScalersRecord.GetEntriesFast(); i++) {
116     if (run.fScalersRecord[i]) fScalersRecord.Add(run.fScalersRecord[i]->Clone());
117   }
118 for (Int_t i = 0; i < run.fScalersRecordESD.GetEntriesFast(); i++) {
119     if (run.fScalersRecordESD[i]) fScalersRecordESD.Add(run.fScalersRecordESD[i]->Clone());
120   }
121 return *this;
122
123 //_____________________________________________________________________________
124 AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
125 {
126   // Read scalers from text file(.cnt) provided by CTP 
127   // for given run and convert it to root format 
128   if( gSystem->AccessPathName( filename.Data() ) ) {
129      AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
130      return NULL;
131   }
132
133   ifstream *file = new ifstream ( filename.Data() );
134   if (!*file) {
135     AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
136     file->close();
137     delete file;
138     return NULL;
139   }
140   
141   AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
142   
143   TString strLine;
144   Bool_t verflag = kFALSE;
145   Bool_t classflag = kFALSE;
146   UChar_t nclass = 0;
147   while (strLine.ReadLine(*file)) {
148     if (strLine.BeginsWith("#")) continue;
149     
150     TObjArray *tokens = strLine.Tokenize(" \t");
151     Int_t ntokens = tokens->GetEntriesFast();
152     // 1st line, version, it is one number, 
153     if (!verflag) {
154       if (ntokens != 1) { 
155         AliErrorClass( Form( "Error reading version number from (%s), line :%s\n", 
156                               filename.Data() , strLine.Data() ) );  
157         return NULL;
158       }
159   //    cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
160       rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
161       verflag = kTRUE;
162       delete tokens;
163       continue;
164     }
165    
166     // 2nd line, run number , number of classes, list of classes used in this partition
167
168     if (!classflag) {
169       if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
170         AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() )); 
171       }
172   //    cout << "Run Number " << ((TObjString*)tokens->At(0))->String().Atoi() << endl;
173       rScaler->SetRunNumber( ((TObjString*)tokens->At(0))->String().Atoi() );
174       nclass = (UChar_t)((TObjString*)tokens->At(1))->String().Atoi();
175   //    cout << "Number of classes " << nclass << endl;
176       rScaler->SetNumClasses( nclass );
177       if ( nclass != ntokens - 2 ) {
178         AliErrorClass( Form( "Error reading number of classes from (%s)\n", filename.Data() )); 
179       }
180       for (UChar_t i=0; i<nclass; ++i) {
181         rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
182       }
183       classflag = kTRUE;
184       delete tokens;
185       continue;
186     }
187     
188     // Records
189     // Each record consists of 1+(number of classes in partition) lines
190     //  1st line of record = time stamp (4 words)
191     //                        1st word = ORBIT(24 bits)
192     //                        2nd word = Period Counter (28 bit)
193     //                        3rd word = seconds (32 bits) from epoch
194     //                        4th word = microsecs (32 bits)
195     //  other lines = 6 words of counters (L0 before,L0 after, ....)
196     if (ntokens != 4) { 
197       AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)", 
198                             filename.Data(), strLine.Data() )); 
199       return NULL;
200     }
201
202     UInt_t orbit     = strtoul(((TObjString*)tokens->At(0))->String(), NULL, 10);
203     UInt_t period    = strtoul(((TObjString*)tokens->At(1))->String(), NULL, 10);
204     UInt_t seconds   = strtoul(((TObjString*)tokens->At(2))->String(), NULL, 10);
205     UInt_t microSecs = strtoul(((TObjString*)tokens->At(3))->String(), NULL, 10);
206
207     AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
208     rec->SetTimeStamp( orbit, period, seconds, microSecs );
209     TString strLine1;
210     for (Int_t i=0; i<nclass; ++i) {
211       strLine1.ReadLine(*file);
212       if (strLine1.BeginsWith("#")) continue;
213       TObjArray *tokens1 = strLine1.Tokenize(" \t");
214       Int_t ntokens1 = tokens1->GetEntriesFast();
215       if( ntokens1 != 6 ) {
216         AliErrorClass( Form( "Error reading scalers from (%s): line (%s)", 
217                              filename.Data(), strLine1.Data() ));
218         delete rec;
219         return rScaler;
220       }
221
222       UInt_t lOCB = strtoul(((TObjString*)tokens1->At(0))->String(), NULL, 10);
223       UInt_t lOCA = strtoul(((TObjString*)tokens1->At(1))->String(), NULL, 10);
224       UInt_t l1CB = strtoul(((TObjString*)tokens1->At(2))->String(), NULL, 10);
225       UInt_t l1CA = strtoul(((TObjString*)tokens1->At(3))->String(), NULL, 10);
226       UInt_t l2CB = strtoul(((TObjString*)tokens1->At(4))->String(), NULL, 10);
227       UInt_t l2CA = strtoul(((TObjString*)tokens1->At(5))->String(), NULL, 10);
228
229       rScaler->GetClass(i);
230       rec->AddTriggerScalers( rScaler->GetClass(i),
231                               lOCB, lOCA, l1CB,
232                               l1CA, l2CB, l2CA );
233
234       delete tokens1;
235     } 
236     rScaler->AddTriggerScalers( rec );
237     
238     delete tokens;     
239   }
240   file->close();
241   delete file;
242
243   return  rScaler; 
244 }
245   
246 //_____________________________________________________________________________
247 Int_t  AliTriggerRunScalers::FindNearestScalersRecord( const AliTimeStamp *stamp ) const
248 {
249    // Find Trigger scaler record with the closest timestamp <= "stamp"
250    // using a binary search. 
251    // return the index in the array of records, if the timestamp 
252    // is out of range return -1
253
254    Int_t   base, position=-1, last, result = 0;
255    Int_t op2 = 0;
256    
257    //fScalersRecord.Sort();
258
259    base = 0;
260    last = fScalersRecord.GetEntriesFast();
261
262    while (last >= base) {
263       position = (base+last) / 2;
264       cout << "pos " <<   position<< " base " <<   base << "last " <<   last << endl;
265       AliTriggerScalersRecord* rec = (AliTriggerScalersRecord*)fScalersRecord.At(position);
266       if( rec && rec->GetTimeStamp()) op2 = 1;
267       if( op2 && (result = stamp->Compare(rec->GetTimeStamp())) == 0  )
268          return position;  // exact match
269       cout << "result " <<   result << " op2 " << op2 << " rec "<< rec << endl;
270       if (!op2 || result < 0)
271          last = position-1;
272       else
273          base = position+1;
274       op2 = 0;  
275    }
276    if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() ) 
277     return -1;  // out of range
278    else 
279     return (result < 0 ) ? position-1 : position; // nearst < stamp   
280 }
281 //_____________________________________________________________________________
282 Int_t AliTriggerRunScalers::ConsistencyCheck(Int_t position,Bool_t correctOverflow)
283 {
284    //Check if counters are consistent(increase). Example: lOCB(n) < lOCB(n+1) and lOCB > lOCA
285    // scalers coding 0,1,2,3,4,5=0b,0a,1b,1a,2b,2a
286    // returns: 
287    //         1 = decresing time 
288    //         2 = too big jump in scalers, looks like some readings are missing
289    //         3 = (level+1) > (level)
290    if (position == 0){
291       AliErrorClass("position=0\n");
292       return 1;
293    }
294    UInt_t c2[6], c1[6];
295    ULong64_t c64[6]; 
296    Bool_t increase[6], overflow[6];  
297    for(Int_t i=0;i<6;i++){increase[i]=0;overflow[i]=0;}
298    ULong64_t const max1 = 4294967295ul;  //32bit counters overflow after 4294967295
299    ULong64_t const max2 = 1000000000ul;  //when counters overflow they seem to be decreasing. Assume decrease cannot be smaller than max2.
300
301    AliTriggerScalersRecord* scalers2 = (AliTriggerScalersRecord*)fScalersRecord.At(position);
302    AliTriggerScalersRecord* scalers1 = (AliTriggerScalersRecord*)fScalersRecord.At(position-1);
303    if (scalers2->Compare((AliTriggerScalersRecord*)fScalersRecord.At(position-1)) == -1) return 1;
304    
305    AliTriggerScalersRecordESD* recESD = 0;
306    if(correctOverflow){
307      recESD = new AliTriggerScalersRecordESD();
308      recESD->SetTimeStamp(scalers2->GetTimeStamp());
309    }
310    for( Int_t ic=0; ic<fnClasses; ++ic ){
311       TObjArray* scalersArray2 = (TObjArray*)scalers2->GetTriggerScalers();
312       AliTriggerScalers* counters2 = (AliTriggerScalers*)scalersArray2->At(ic);
313       counters2->GetAllScalers(c2);
314       TObjArray* scalersArray1 = (TObjArray*)scalers1->GetTriggerScalers();
315       AliTriggerScalers* counters1 = (AliTriggerScalers*)scalersArray1->At(ic);
316       counters1->GetAllScalers(c1);
317
318       for(Int_t i=0;i<6;i++){
319          if ( c2[i] > c1[i] ) increase[i]=1;
320          else if ( c2[i] < c1[i] && (c1[i] - c2[i]) > max2) overflow[i]=1;
321          else return 2;
322       }
323       for(Int_t i=0;i<5;i++){
324          if ((c2[i] - c1[i]) < (c2[i+1] - c1[i+1]) && increase[i] && increase[i+1] ) return 3;
325          else if ( (max1 - c1[i]+c2[i]  ) < (c2[i+1] - c1[i+1]) && overflow[i] && increase[i+1] ) return 3;
326          else if ( (c2[i] - c1[i]) < (max1 - c1[i+1] + c2[i+1]) && increase[i] && overflow[i+1] ) return 3;
327          else if ( (max1 - c1[i] + c2[i] ) < (max1 - c1[i+1] + c2[i+1]) && overflow[i] && overflow[i+1] ) return 3;
328       }
329       if(correctOverflow)for(Int_t i=0;i<6;i++){
330         c64[i]=c2[i]+max1*overflow[i];
331         AliTriggerScalersESD* s= new AliTriggerScalersESD(ic,c64);
332         recESD->AddTriggerScalers(s);
333      }
334  }
335  if(correctOverflow)fScalersRecordESD.AddLast(recESD);
336  return 0;
337 }
338 //____________________________________________________________________________
339 Int_t AliTriggerRunScalers::CorrectScalersOverflow()
340 {
341  // Run over fScalersRecord, check overflow using CheckConsistency methos
342  // and save corrected result in fScalersRecordESD.
343  UInt_t c1[6];
344  ULong64_t c64[6];
345  AliTriggerScalersRecordESD* recESD = new AliTriggerScalersRecordESD();
346  // add 0
347  AliTriggerScalersRecord* scalers = (AliTriggerScalersRecord*)fScalersRecord.At(0);
348  for( Int_t ic=0; ic<fnClasses; ++ic ){
349     TObjArray* scalersArray = (TObjArray*)scalers->GetTriggerScalers();
350     AliTriggerScalers* counters = (AliTriggerScalers*)scalersArray->At(ic);
351     counters->GetAllScalers(c1);
352     for(Int_t i=0; i<6; i++)c64[i]=c1[i];
353     AliTriggerScalersESD* s= new AliTriggerScalersESD(ic,c64);
354     recESD->AddTriggerScalers(s);
355  }
356  fScalersRecordESD.AddLast(recESD);
357  for(Int_t i=1;i<fScalersRecord.GetEntriesFast(); i++){
358   if(ConsistencyCheck(i,kTRUE)){
359     fScalersRecordESD.SetOwner(); 
360     fScalersRecordESD.Delete(); 
361     AliErrorClass("Inconsistent scalers, they will not be provided.\n");
362     return 1;
363   }
364  }
365  if(fScalersRecordESD.GetEntriesFast() != fScalersRecord.GetEntriesFast()){
366     AliErrorClass("Internal error: #scalers ESD != #scalers \n");
367     return 1;
368  }
369  return 0;
370 }
371 //_____________________________________________________________________________
372 AliTriggerScalersESD* AliTriggerRunScalers::GetScalersForEventClass(const AliTimeStamp* stamp,const Int_t classIndex) const
373 {
374  // Find scalers for event for class in fScalersRecordESD
375  // Assumes that fScalerRecord = fScalerRecordESD
376  Int_t position = FindNearestScalersRecord(stamp);
377  if ( position == -1 ) { 
378   AliErrorClass("Event AliTimeStamp out of range!");
379   return 0; 
380  }
381  // check also position=max
382  AliTriggerScalersRecordESD* scalrec1 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(position);
383  AliTriggerScalersRecordESD* scalrec2 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(position+1);
384  TObjArray* scalers1 = (TObjArray*)scalrec1->GetTriggerScalers();
385  TObjArray* scalers2 = (TObjArray*)scalrec2->GetTriggerScalers();
386  if(scalers1->GetEntriesFast() != fnClasses){
387   AliErrorClass("Internal error: #classes in RecordESD != fnClasses\n");
388   return 0; 
389  }
390  AliTriggerScalersESD *s1,*s2;
391  for ( Int_t ic=0; ic < fnClasses; ++ic ){
392       s1 = (AliTriggerScalersESD*)scalers1->At(ic);
393       s2 = (AliTriggerScalersESD*)scalers2->At(ic);
394       Bool_t classfound = (s1->GetClassIndex() == classIndex) && (s2->GetClassIndex() == classIndex);
395       if(classfound){
396         ULong64_t max = 4294967295ul;
397         AliTriggerScalersRecordESD* scalrec0 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(0);
398         TObjArray* scalers0 = (TObjArray*)scalrec0->GetTriggerScalers();
399         AliTriggerScalersESD *s0 = (AliTriggerScalersESD*)scalers0->At(ic);
400         ULong64_t base[6],c1[6],c2[6],cint[6];
401         ULong64_t orbit = max*(stamp->GetPeriod()) + stamp->GetOrbit();
402         s0->GetAllScalers(base);
403         s1->GetAllScalers(c1);
404         s2->GetAllScalers(c2);
405         ULong64_t orbit1 = max*(scalrec1->GetTimeStamp()->GetPeriod())+scalrec1->GetTimeStamp()->GetOrbit();
406         ULong64_t orbit2 = max*(scalrec2->GetTimeStamp()->GetPeriod())+scalrec2->GetTimeStamp()->GetOrbit();
407         for(Int_t i=0;i<6;i++){
408            Double_t slope=Double_t(c2[i]-c1[i])/Double_t(orbit2-orbit1);
409            cint[i]=ULong64_t(slope*(orbit-orbit1)) +c1[i] -base[i];
410         }
411         AliTriggerScalersESD* result = new AliTriggerScalersESD(classIndex,cint);
412         return result;
413       }
414  }
415  AliErrorClass(Form("Classindex %i not found.\n",classIndex));
416  return 0;
417 }
418 //_____________________________________________________________________________
419 void AliTriggerRunScalers::Print( const Option_t* ) const
420 {
421    // Print
422   cout << "Trigger Scalers Record per Run: " << endl;
423   cout << "  File version :" <<  fVersion << endl;            
424   cout << "  Run Number :" <<  fRunNumber << endl;          
425   cout << "  Number of Classes :" <<  (Int_t)fnClasses << endl;          
426   cout << "    Classes ID:";
427   for( Int_t i=0; i<fnClasses; ++i ) 
428     cout << "  " << (Int_t)fClassIndex[i];       
429   cout << endl; 
430     
431   for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i ) 
432      ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();
433 }
434