1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 /* $Id: AliTriggerRunScalers.cxx 22322 2007-11-22 11:43:14Z cvetan $ */
18 ///////////////////////////////////////////////////////////////////////////////
20 // Class to define the ALICE Trigger Scalers Record per Run.
23 //////////////////////////////////////////////////////////////////////////////
25 #include <Riostream.h>
30 #include <TObjString.h>
31 #include <TObjArray.h>
36 #include "AliTimeStamp.h"
37 #include "AliTriggerScalers.h"
38 #include "AliTriggerScalersRecord.h"
39 #include "AliTriggerRunScalers.h"
41 ClassImp( AliTriggerRunScalers )
43 //_____________________________________________________________________________
44 AliTriggerRunScalers::AliTriggerRunScalers():
52 // Default constructor
55 //_____________________________________________________________________________
56 void AliTriggerRunScalers::AddTriggerScalers( AliTriggerScalersRecord* scaler )
59 fScalersRecord.AddLast( scaler );
60 fScalersRecord.Sort();
63 //_____________________________________________________________________________
64 AliTriggerRunScalers* AliTriggerRunScalers::ReadScalers( TString & filename )
67 if( gSystem->AccessPathName( filename.Data() ) ) {
68 AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
72 ifstream *file = new ifstream ( filename.Data() );
74 AliErrorClass(Form("Error opening file (%s) !\n",filename.Data()));
80 AliTriggerRunScalers* rScaler = new AliTriggerRunScalers();
83 Bool_t verflag = kFALSE;
84 Bool_t classflag = kFALSE;
86 while (strLine.ReadLine(*file)) {
87 if (strLine.BeginsWith("#")) continue;
89 TObjArray *tokens = strLine.Tokenize(" \t");
90 Int_t ntokens = tokens->GetEntriesFast();
91 // 1st line, version, it is one number,
94 AliErrorClass( Form( "Error reading version number from (%s), line :%s\n",
95 filename.Data() , strLine.Data() ) );
98 // cout << "Version "<< ((TObjString*)tokens->At(0))->String().Atoi() << endl;
99 rScaler->SetVersion( ((TObjString*)tokens->At(0))->String().Atoi() );
105 // 2nd line, run number , number of classes, list of classes used in this partition
108 if ( !((TObjString*)tokens->At(1))->String().IsDigit() ) {
109 AliErrorClass( Form( "Error reading Run number from (%s)\n", filename.Data() ));
111 // cout << "Run Number " << ((TObjString*)tokens->At(0))->String().Atoi() << endl;
112 rScaler->SetRunNumber( ((TObjString*)tokens->At(0))->String().Atoi() );
113 nclass = (UChar_t)((TObjString*)tokens->At(1))->String().Atoi();
114 // cout << "Number of classes " << nclass << endl;
115 rScaler->SetNumClasses( nclass );
116 if ( nclass != ntokens - 2 ) {
117 AliErrorClass( Form( "Error reading number of classes from (%s)\n", filename.Data() ));
119 for (UChar_t i=0; i<nclass; ++i) {
120 rScaler->SetClass( i, (Char_t)((TObjString*)tokens->At(2+i))->String().Atoi() );
128 // Each record consists of 1+(number of classes in partition) lines
129 // 1st line of record = time stamp (4 words)
130 // 1st word = ORBIT(24 bits)
131 // 2nd word = Period Counter (28 bit)
132 // 3rd word = seconds (32 bits) from epoch
133 // 4th word = microsecs (32 bits)
134 // other lines = 6 words of counters (L0 before,L0 after, ....)
136 AliErrorClass( Form( "Error reading timestamp from (%s): line (%s)",
137 filename.Data(), strLine.Data() ));
140 UInt_t orbit = ((TObjString*)tokens->At(0))->String().Atoi();
141 UInt_t period = ((TObjString*)tokens->At(1))->String().Atoi();
142 UInt_t seconds = ((TObjString*)tokens->At(2))->String().Atoi();
143 UInt_t microSecs = ((TObjString*)tokens->At(3))->String().Atoi();
144 AliTriggerScalersRecord * rec = new AliTriggerScalersRecord();
145 rec->SetTimeStamp( orbit, period, seconds, microSecs );
148 for (Int_t i=0; i<nclass; ++i) {
149 strLine1.ReadLine(*file);
150 if (strLine1.BeginsWith("#")) continue;
151 TObjArray *tokens1 = strLine1.Tokenize(" \t");
152 Int_t ntokens1 = tokens1->GetEntriesFast();
153 if( ntokens1 != 6 ) {
154 AliErrorClass( Form( "Error reading scalers from (%s): line (%s)",
155 filename.Data(), strLine1.Data() ));
159 UInt_t LOCB = ((TObjString*)tokens1->At(0))->String().Atoi();
160 UInt_t LOCA = ((TObjString*)tokens1->At(1))->String().Atoi();
161 UInt_t L1CB = ((TObjString*)tokens1->At(2))->String().Atoi();
162 UInt_t L1CA = ((TObjString*)tokens1->At(3))->String().Atoi();
163 UInt_t L2CB = ((TObjString*)tokens1->At(4))->String().Atoi();
164 UInt_t L2CA = ((TObjString*)tokens1->At(5))->String().Atoi();
165 rScaler->GetClass(i);
166 rec->AddTriggerScalers( rScaler->GetClass(i),
171 rScaler->AddTriggerScalers( rec );
181 //_____________________________________________________________________________
182 Int_t AliTriggerRunScalers::FindNearestScalersRecord( AliTimeStamp * stamp )
184 // Find Trigger scaler record with the closest timestamp <= "stamp"
185 // using a binary search.
186 // return the index in the array of records, if the timestamp
187 // is out of range return -1
189 Int_t base, position=-1, last, result = 0;
190 AliTimeStamp *op2 = NULL;
192 fScalersRecord.Sort();
195 last = fScalersRecord.GetEntriesFast();
197 while (last >= base) {
198 position = (base+last) / 2;
199 cout << "pos " << position<< " base " << base << "last " << last << endl;
200 AliTriggerScalersRecord* rec = (AliTriggerScalersRecord*)fScalersRecord.At(position);
201 if( rec ) op2 = rec->GetTimeStamp();
202 if( op2 && (result = stamp->Compare(op2)) == 0 )
203 return position; // exact match
204 cout << "result " << result << " op2 " << op2 << " rec "<< rec << endl;
205 if (!op2 || result < 0)
211 if( (position == 0 && result < 0) || position >= fScalersRecord.GetEntriesFast() )
212 return -1; // out of range
214 return (result < 0 ) ? position-1 : position; // nearst < stamp
217 //_____________________________________________________________________________
218 void AliTriggerRunScalers::Print( const Option_t* ) const
221 cout << "Trigger Scalers Record per Run: " << endl;
222 cout << " File version :" << fVersion << endl;
223 cout << " Run Number :" << fRunNumber << endl;
224 cout << " Number of Classes :" << (Int_t)fnClasses << endl;
225 cout << " Classes ID:";
226 for( Int_t i=0; i<fnClasses; ++i )
227 cout << " " << (Int_t)fClassIndex[i];
230 for( Int_t i=0; i<fScalersRecord.GetEntriesFast(); ++i )
231 ((AliTriggerScalersRecord*)fScalersRecord.At(i))->Print();