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