]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerRunScalers.cxx
more cout removed (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)){
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       AliDebug(1, Form("position= %d   base= %d    last= %d  ",position,base,last));
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       if (!op2 || result < 0)
273          last = position-1;
274       else
275          base = position+1;
276       op2 = 0;  
277    }
278    if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() ) 
279     return -1;  // out of range
280    else 
281     return (result < 0 ) ? position-1 : position; // nearst < stamp   
282 }
283 //_____________________________________________________________________________
284 Int_t AliTriggerRunScalers::ConsistencyCheck(Int_t position,Bool_t correctOverflow)
285 {
286    //Check if counters are consistent(increase). Example: lOCB(n) < lOCB(n+1) and lOCB > lOCA
287    // scalers coding 0,1,2,3,4,5=0b,0a,1b,1a,2b,2a
288    // returns: 
289    //         1 = decresing time 
290    //         2 = too big jump in scalers, looks like some readings are missing
291    //         3 = (level+1) > (level)
292    if (position == 0){
293       if(correctOverflow){
294         AliErrorClass("position=0\n");
295         return 1;
296       }else return 0; // to work correctlu in AddScalers
297    };
298    UInt_t c2[6], c1[6];
299    ULong64_t c64[6]; 
300    Bool_t increase[6], overflow[6];  
301    for(Int_t i=0;i<6;i++){increase[i]=0;overflow[i]=0;}
302    ULong64_t const max1 = 4294967295ul;  //32bit counters overflow after 4294967295
303    ULong64_t const max2 = 1000000000ul;  //when counters overflow they seem to be decreasing. Assume decrease cannot be smaller than max2.
304
305    AliTriggerScalersRecord* scalers2 = (AliTriggerScalersRecord*)fScalersRecord.At(position);
306    AliTriggerScalersRecord* scalers1 = (AliTriggerScalersRecord*)fScalersRecord.At(position-1);
307    if (scalers2->Compare((AliTriggerScalersRecord*)fScalersRecord.At(position-1)) == -1) return 1;
308    
309    AliTriggerScalersRecordESD* recESD = 0;
310    if(correctOverflow){
311      recESD = new AliTriggerScalersRecordESD();
312      recESD->SetTimeStamp(scalers2->GetTimeStamp());
313    }
314    for( Int_t ic=0; ic<fnClasses; ++ic ){
315       TObjArray* scalersArray2 = (TObjArray*)scalers2->GetTriggerScalers();
316       AliTriggerScalers* counters2 = (AliTriggerScalers*)scalersArray2->At(ic);
317       counters2->GetAllScalers(c2);
318       TObjArray* scalersArray1 = (TObjArray*)scalers1->GetTriggerScalers();
319       AliTriggerScalers* counters1 = (AliTriggerScalers*)scalersArray1->At(ic);
320       counters1->GetAllScalers(c1);
321       for(Int_t i=0;i<5;i++){
322          if ( c2[i] >= c1[i] ) increase[i]=1;
323          else if ( c2[i] < c1[i] && (c1[i] - c2[i]) > max2) overflow[i]=1;
324          else return 2;
325       }
326       for(Int_t i=0;i<5;i++){
327          if ((c2[i] - c1[i]) < (c2[i+1] - c1[i+1]) && increase[i] && increase[i+1] ) {
328                  if ( ((c2[i+1] - c1[i+1]) - (c2[i] - c1[i])) < 16 ) {AliWarningClass("Trigger scaler Level[i+1] > Level[i]. Diff < 16!");}
329                  else return 3; }
330          else if ( (max1 - c1[i]+c2[i]) < (c2[i+1] - c1[i+1]) && overflow[i] && increase[i+1] ) {
331                  if ( ((c2[i+1] - c1[i+1]) - (max1 - c1[i]+c2[i])) < 16 ) {AliWarningClass("Trigger scaler Level[i+1] > Level[i]. Diff < 16!");}
332                  else return 3; }
333          else if ( (c2[i] - c1[i]) < (max1 - c1[i+1] + c2[i+1]) && increase[i] && overflow[i+1] ) {
334                  if ( ((max1 - c1[i+1] + c2[i+1]) - (c2[i] - c1[i])) < 16 ) {AliWarningClass("Trigger scaler Level[i+1] > Level[i]. Diff < 16!");}
335                  else return 3; }
336          else if ( (max1 - c1[i] + c2[i] ) < (max1 - c1[i+1] + c2[i+1]) && overflow[i] && overflow[i+1] ) {
337                  if ( ((max1 - c1[i+1] + c2[i+1]) - (max1 - c1[i] + c2[i] )) < 16 ) {AliWarningClass("Trigger scaler Level[i+1] > Level[i]. Diff < 16!");}
338                  else return 3; }
339       }
340       if(correctOverflow){ 
341         for(Int_t i=0;i<6;i++){ c64[i]=c2[i]+max1*overflow[i]; }
342         AliTriggerScalersESD* s= new AliTriggerScalersESD(fClassIndex[ic],c64);
343         recESD->AddTriggerScalers(s);
344          }
345
346  }
347  if(correctOverflow)fScalersRecordESD.AddLast(recESD);
348  return 0;
349 }
350 //____________________________________________________________________________
351 Int_t AliTriggerRunScalers::CorrectScalersOverflow()
352 {
353  // Run over fScalersRecord, check overflow using CheckConsistency methos
354  // and save corrected result in fScalersRecordESD.
355
356  // Temporary fix for the OCDB entries written with v4-16-Release
357  // where the wrong sorting was used
358  fScalersRecord.Sort();
359  UInt_t c1[6];
360  ULong64_t c64[6];
361  AliTriggerScalersRecordESD* recESD = new AliTriggerScalersRecordESD();
362  // add 0
363  AliTriggerScalersRecord* scalers = (AliTriggerScalersRecord*)fScalersRecord.At(0);
364  for( Int_t ic=0; ic<fnClasses; ++ic ){
365     TObjArray* scalersArray = (TObjArray*)scalers->GetTriggerScalers();
366     AliTriggerScalers* counters = (AliTriggerScalers*)scalersArray->At(ic);
367     counters->GetAllScalers(c1);
368     for(Int_t i=0; i<6; i++)c64[i]=c1[i];
369     AliTriggerScalersESD* s= new AliTriggerScalersESD(fClassIndex[ic],c64);
370     recESD->AddTriggerScalers(s);
371  }
372  fScalersRecordESD.AddLast(recESD);
373  for(Int_t i=1;i<fScalersRecord.GetEntriesFast(); i++){
374   if(ConsistencyCheck(i,kTRUE)){
375     fScalersRecord.At(i)->Print();
376     fScalersRecord.At(i-1)->Print();
377     fScalersRecordESD.SetOwner(); 
378     fScalersRecordESD.Delete(); 
379     AliErrorClass("Inconsistent scalers, they will not be provided.\n");
380     return 1;
381   }
382  }
383  if(fScalersRecordESD.GetEntriesFast() != fScalersRecord.GetEntriesFast()){
384     AliErrorClass("Internal error: #scalers ESD != #scalers \n");
385     return 1;
386  }
387  return 0;
388 }
389 //_____________________________________________________________________________
390 AliTriggerScalersESD* AliTriggerRunScalers::GetScalersForEventClass(const AliTimeStamp* stamp,const Int_t classIndex) const
391 {
392  // Find scalers for event for class in fScalersRecordESD
393  // Assumes that fScalerRecord = fScalerRecordESD
394  Int_t position = FindNearestScalersRecord(stamp);
395  if ( position == -1 ) { 
396   AliErrorClass("Event AliTimeStamp out of range!");
397   return 0; 
398  }
399  // check also position=max
400  AliTriggerScalersRecordESD* scalrec1 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(position);
401  AliTriggerScalersRecordESD* scalrec2 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(position+1);
402  TObjArray* scalers1 = (TObjArray*)scalrec1->GetTriggerScalers();
403  TObjArray* scalers2 = (TObjArray*)scalrec2->GetTriggerScalers();
404
405  if(scalers1->GetEntriesFast() != fnClasses){
406   AliErrorClass("Internal error: #classes in RecordESD != fnClasses\n");
407   return 0; 
408  }
409
410  AliTriggerScalersESD *s1,*s2;
411  for ( Int_t ic=0; ic < (Int_t)fnClasses; ++ic ){
412
413       s1 = (AliTriggerScalersESD*)scalers1->At(ic);
414       s2 = (AliTriggerScalersESD*)scalers2->At(ic);
415
416       Bool_t classfound = (s1->GetClassIndex() == classIndex) && (s2->GetClassIndex() == classIndex);
417       if(classfound){
418         ULong64_t max = 4294967295ul;
419         AliTriggerScalersRecordESD* scalrec0 = (AliTriggerScalersRecordESD*)fScalersRecordESD.At(0);
420         TObjArray* scalers0 = (TObjArray*)scalrec0->GetTriggerScalers();
421         AliTriggerScalersESD *s0 = (AliTriggerScalersESD*)scalers0->At(ic);
422         ULong64_t base[6],c1[6],c2[6],cint[6];
423         ULong64_t orbit = max*(stamp->GetPeriod()) + stamp->GetOrbit();
424         s0->GetAllScalers(base);
425         s1->GetAllScalers(c1);
426         s2->GetAllScalers(c2);
427         ULong64_t orbit1 = max*(scalrec1->GetTimeStamp()->GetPeriod())+scalrec1->GetTimeStamp()->GetOrbit();
428         ULong64_t orbit2 = max*(scalrec2->GetTimeStamp()->GetPeriod())+scalrec2->GetTimeStamp()->GetOrbit();
429         for(Int_t i=0;i<6;i++){
430            Double_t slope=Double_t(c2[i]-c1[i])/Double_t(orbit2-orbit1);
431            cint[i]=ULong64_t(slope*(orbit-orbit1)) +c1[i] -base[i];
432         }
433         AliTriggerScalersESD* result = new AliTriggerScalersESD(classIndex,cint);
434         return result;
435       }
436  }
437  AliErrorClass(Form("Classindex %i not found.\n",classIndex));
438  return 0;
439 }
440 //_____________________________________________________________________________
441 void AliTriggerRunScalers::Print( const Option_t* ) const
442 {
443    // Print
444   cout << "Trigger Scalers Record per Run: " << endl;
445   cout << "  File version :" <<  fVersion << endl;            
446   cout << "  Run Number :" <<  fRunNumber << endl;          
447   cout << "  Number of Classes :" <<  (Int_t)fnClasses << endl;          
448   cout << "    Classes ID:";
449   for( Int_t i=0; i<fnClasses; ++i ) 
450     cout << "  " << (Int_t)fClassIndex[i];       
451   cout << endl; 
452     
453   for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i ) 
454      ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();
455 }
456