Corrected protection.
[u/mrichter/AliRoot.git] / STEER / AliCTPTimeParams.cxx
1 /*************************************************************************
2 * Copyright(c) 1998-2008, 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
17 #include <Riostream.h>
18
19 #include <TObjArray.h>
20 #include <TObjString.h>
21 #include <TObject.h>
22 #include <TString.h>
23 #include <TSystem.h>
24 #include <TFile.h>
25
26 #include "AliLog.h"
27 #include "AliCTPTimeParams.h"
28 #include "AliCTPInputTimeParams.h"
29
30 ClassImp(AliCTPTimeParams)
31
32 //______________________________________________________________________________
33 AliCTPTimeParams::AliCTPTimeParams():
34 TNamed(),
35 fDelayL1L0(0),
36 fDelayL2L0(0),
37 fCTPInputTimeParams()
38 {
39 //Default constructor
40 }
41
42 //______________________________________________________________________________
43 AliCTPTimeParams::AliCTPTimeParams(const AliCTPTimeParams &timeparams):
44  TNamed(),
45  fDelayL1L0(timeparams.fDelayL1L0),
46  fDelayL2L0(timeparams.fDelayL2L0),
47  fCTPInputTimeParams()
48 {
49  for (Int_t i = 0; i < timeparams.fCTPInputTimeParams.GetSize(); i++) {
50  if ( timeparams.fCTPInputTimeParams[i] ) fCTPInputTimeParams.Add(timeparams.fCTPInputTimeParams[i]->Clone());
51  }
52  // copy constructor
53 }
54
55
56 //______________________________________________________________________________
57 AliCTPTimeParams& AliCTPTimeParams::operator=(const AliCTPTimeParams &timeparams)
58 {
59  // assignment operator
60  if(this==&timeparams) return *this;
61  ((TNamed *)this)->operator=(timeparams);
62  fDelayL1L0=timeparams.fDelayL1L0;
63  fDelayL2L0=timeparams.fDelayL2L0;
64  
65  for (Int_t i = 0; i < timeparams.fCTPInputTimeParams.GetSize(); i++) {
66  if ( timeparams.fCTPInputTimeParams[i] ) fCTPInputTimeParams.Add(timeparams.fCTPInputTimeParams[i]->Clone());
67  }
68
69  return *this;
70 }
71
72 //______________________________________________________________________________
73 AliCTPTimeParams::~AliCTPTimeParams()
74 {
75  //Destructor
76
77  fCTPInputTimeParams.SetOwner();
78  fCTPInputTimeParams.Delete();
79 }
80
81 //______________________________________________________________________________
82 void AliCTPTimeParams::AddInput( TString& inputName, UInt_t& inputLevel, UInt_t inputDelay, TString inputEdge, UInt_t deltamin, UInt_t deltamax )
83 {
84  fCTPInputTimeParams.AddLast( new AliCTPInputTimeParams(inputName, inputLevel, inputDelay, inputEdge, deltamin, deltamax ));
85 }
86
87 //______________________________________________________________________________
88 void AliCTPTimeParams::AddDelayL0L1L2(Int_t delayL1L0, UInt_t delayL2L0)
89 {
90  fDelayL1L0 = delayL1L0;
91  fDelayL2L0 = delayL2L0;
92 }
93 //______________________________________________________________________________
94 AliCTPInputTimeParams* AliCTPTimeParams::GetTimeParamsForInput( TString inputname)
95 {
96  // Get AliCTPInputTimeParams for input name
97 Int_t ninputs = fCTPInputTimeParams.GetEntriesFast();
98 for ( Int_t i=0; i < ninputs; i++ )
99  {
100   AliCTPInputTimeParams* ctpinputtime = (AliCTPInputTimeParams*)fCTPInputTimeParams.At(i);
101   if (inputname == ctpinputtime->GetInputName() ) return ctpinputtime;
102  }
103 cout << "Input: " << inputname << " not found." << endl;
104 return NULL;
105 }
106 //______________________________________________________________________________
107 Int_t AliCTPTimeParams::GetDeltasforClass(TString classname,Int_t& deltamin,Int_t& deltamax)
108 {
109  // Get deltamin and deltamax for given class
110  // Assumes that descriptor = DINPU
111  // ret: 0=OK, 1= input doea not exist
112  TString input(classname(1,4)); 
113  AliCTPInputTimeParams* tprm = GetTimeParamsForInput(input);
114  if(tprm){
115   deltamin=tprm->GetDeltaMin();
116   deltamax=tprm->GetDeltaMax();
117   return 0;
118  }
119  return 1;
120 }
121 //______________________________________________________________________________
122 AliCTPTimeParams* AliCTPTimeParams::LoadCTPTimeParams(TString filename)
123 {
124  // Load pre-created CTP time parameters from database/file
125  // By default files are stored in GRP/CTP folder
126  // The filename is constructed as GRP/CTP/<file>.cfg
127   if( gSystem->AccessPathName( filename.Data() )) {
128  // AliError( Form( "File (%s) not found!", filename.Data()));
129   return NULL;
130  }
131
132  ifstream *file = new ifstream( filename.Data() );
133  if(!*file) {
134   //AliErrorClass( Form( "Error opening file (%s) !", filename.Data()));
135   file->close();
136   delete file;
137   return NULL;
138  }
139
140  AliCTPTimeParams *ctptime = new AliCTPTimeParams();
141
142  TString strline;
143  
144  while (strline.ReadLine(*file)) {
145     if (ctptime->ProcessCTPTimeParamsLine(strline) == kFALSE) {
146      delete ctptime;
147      break;
148     }
149  }
150
151  file->close();
152  delete file;
153
154  return ctptime;
155 }
156
157 //______________________________________________________________________________
158 AliCTPTimeParams* AliCTPTimeParams::LoadCTPTimeParamsFromString(const char* timeparams)
159 {
160
161  // Loads configuration from string
162
163    if (!timeparams)
164      return 0;
165
166    AliCTPTimeParams *ctptime = new AliCTPTimeParams();
167
168    TObjArray* tokens = TString(timeparams).Tokenize("\n");
169    for (Int_t i=0; i<tokens->GetEntries(); i++)
170    {
171      TObjString* string = dynamic_cast<TObjString*>(tokens->At(i));
172      if (!string)
173        continue;
174
175      if (ctptime->ProcessCTPTimeParamsLine(string->String()) == kFALSE)
176      {
177         ctptime  = 0x0;
178         break;
179      }
180    }
181
182    delete tokens;
183    if (ctptime) return ctptime;
184    else return NULL;
185 }
186
187 //______________________________________________________________________________
188 Bool_t AliCTPTimeParams::ProcessCTPTimeParamsLine(const char* line)
189 {
190  UInt_t level = 0;
191  TString strline(line);
192   if (strline.BeginsWith("L012")) {
193    strline.ReplaceAll("L012", "");
194    TObjArray *tokens = strline.Tokenize(" \t");
195       
196
197    AddDelayL0L1L2(((TObjString*)tokens->At(0))->String().Atoi(),((TObjString*)tokens->At(1))->String().Atoi());
198    delete tokens;
199   }
200   else {
201     if (strline.BeginsWith("0")) { level = 0; }   // determine the input level (0, 1 or 2)
202     else if (strline.BeginsWith("1")) { level = 1; }
203     else if (strline.BeginsWith("2")) { level = 2; } 
204     else return kFALSE; // file not in the right format!
205    
206     TObjArray *tokens = strline.Tokenize(" \t");
207     Int_t ntokens = tokens->GetEntriesFast();
208     if (ntokens == 5)  AddInput(((TObjString*)tokens->At(0))->String(), level, ((TObjString*)tokens->At(2))->String().Atoi(), ((TObjString*)tokens->At(1))->String(),  ((TObjString*)tokens->At(3))->String().Atoi(), ((TObjString*)tokens->At(4))->String().Atoi());
209     else if (ntokens == 3) AddInput(((TObjString*)tokens->At(0))->String(), level, ((TObjString*)tokens->At(2))->String().Atoi(), ((TObjString*)tokens->At(1))->String(),  0, 0); //the old format is used - no DeltaMin & DeltaMax!
210          else return kFALSE; // file not in the right format!
211   }
212 return kTRUE;
213 }
214
215 //______________________________________________________________________________
216 void AliCTPTimeParams::Print(const Option_t*) const
217 {
218   //Print
219  cout << "Delay L0 - L1 = " << fDelayL1L0 << endl;
220  cout << "Delay L0 - L2 = " << fDelayL2L0 << endl;
221  fCTPInputTimeParams.Print();
222  cout << ""<<endl;
223 }
224 //______________________________________________________________________________