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