]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGCF/Correlations/JCORRAN/AliJBaseCard.cxx
Adding TOF calib task for calibration of problematic channels
[u/mrichter/AliRoot.git] / PWGCF / Correlations / JCORRAN / AliJBaseCard.cxx
1 //====================================
2 //last modified FK 6.NOV 2009
3 //====================================
4 //blah
5 // blah
6
7 #include <TPRegexp.h>
8 #include "AliJConst.h"
9 #include "AliJBaseCard.h"
10
11 //ClassImp(AliJBaseCard);
12
13 AliJBaseCard::AliJBaseCard() :
14   fnentry(0),
15   fKeyWordVector(0),
16   fValuesVector(0),
17   fValueString(0),
18   fKeyTable(0)
19 {   
20   //constructor
21 }
22
23 AliJBaseCard::AliJBaseCard(const char *filename) :
24   fnentry(0),
25   fKeyWordVector(0),
26   fValuesVector(0),
27   fValueString(0),
28   fKeyTable(0)
29 {  
30   //constructor
31
32   strcpy(fcardname, filename);  //needed in PrintOut()
33
34   if( strlen( filename ) > 0 ){
35
36     //----  r e a d   t h e   c a r d ----
37     ReadInputCard();//read config file fill Tvectors
38   }
39 }
40
41
42 AliJBaseCard& AliJBaseCard::operator=(const AliJBaseCard& obj){
43   // equal operator
44   JUNUSED(obj);
45   return *this;
46 }
47
48 AliJBaseCard::~AliJBaseCard(){
49   // destructor
50 }
51
52
53 unsigned int AliJBaseCard::GetTVectorIndex(TString keyword, int tol){
54
55   //   //returns findex of a TVector according to its position in std::hash_map 
56   //   std::hash_map< TString, unsigned int >::iterator iter = MapKeyWordToTVector.begin();
57   //   iter = MapKeyWordToTVector.find(keyword);
58   //   if(iter != MapKeyWordToTVector.end()){
59   //     return (unsigned int) iter->second;
60   //   }else{
61   //     cout << "ERROR: \""<<keyword.Data()<<"\" must be defined  "<< endl;
62   //     exit(1);
63   //   }
64
65   UInt_t i;
66   Int_t ind;
67   i = 0;
68   ind = -1;
69
70   //cout<<"ALIJBASECARD_SEARCH_MODE_HASHLIST"<<endl;
71   TNamed * ko = (TNamed*)fKeyTable.FindObject( keyword.Data() ); 
72   //cout<<ko<<endl;
73   if(ko) ind = ko->GetUniqueID();
74   if( ind == -1 ){
75     for( UInt_t ii=0;ii<fKeyWordVector.size();ii++ ){
76       if( fKeyWordVector[ii] == keyword ) return i;
77     }
78     if( tol == 0 ){
79       cout << "ERROR: \""<<keyword.Data()<<"\" must be defined  "<< endl;
80       exit(1);
81     }else if ( tol == 1 ){
82       cout << "Warning: \""<<keyword.Data()<<"\" is not exist. return default value  "<< endl;
83       return -1;
84     }else{
85         return -1;
86     }
87   }
88
89   return ind;
90 }
91
92 int AliJBaseCard::GetN(TString keyword){
93   //returns size of TVector
94   unsigned int findex = GetTVectorIndex(keyword);
95   return (int) fValuesVector[findex].GetNrows();
96 }
97
98 TVector *  AliJBaseCard::GetVector(TString keyword ){
99   int findex = GetTVectorIndex(keyword);
100   return &fValuesVector[findex];
101 }
102
103 float AliJBaseCard::Get(TString keyword, int VectorComponent){
104   //returns VectorComponent Component of  fValuesVector TVector for given keyword
105   int findex = GetTVectorIndex(keyword);
106   if(0<=VectorComponent && VectorComponent<GetNwithIndex(findex)){
107     return fValuesVector[findex](VectorComponent+1);
108   }else{
109     cout<<"ERROR: fValuesVector findex out of range "<<keyword.Data()<<endl;
110     cout << "   Max findex: " << GetN(keyword) -  1<< " Asked: " <<  VectorComponent << endl;
111     exit(1);
112   }
113 }
114
115 TString AliJBaseCard::GetStr(TString keyword ){
116   int findex = GetTVectorIndex(keyword, 1);
117   if( findex < 0  ) return TString("");
118   return fValueString[findex];
119 }
120
121
122 void AliJBaseCard::InitCard(){
123   // set the length of fIndexVector and disable all indices
124 }
125
126 void AliJBaseCard::FinishCard(){
127   // recompute fast idices
128 }
129
130 void AliJBaseCard::ReadInputCard(){
131   // read card
132
133   char buffer[kMaxDimBuffer];
134   ifstream incard;
135
136   cout << "Reading fcard from file: " << fcardname << endl;
137   incard.open(fcardname,ios::in);
138
139   if(!incard){
140     cout<<"ERROR: Config file <"<<fcardname<<"> not found!"<<endl;
141     exit(1);
142   }
143
144   InitCard();
145
146   while(!incard.eof()){ //loop over the input fcard
147
148     incard.getline(buffer,kMaxDimBuffer); //read a line
149
150     if(fnentry > 1000){//is the file reasonably long?
151       cout<<"Maximum number of 1000 lines reached in AliJBaseCard.C"<<endl;
152       exit(1);
153     }
154
155     ReadInputLine( buffer );
156
157     fnentry++;
158   }//while eof
159
160   FinishCard();
161
162   return;
163 }
164
165 void AliJBaseCard::ReadLine( const char * buffer ){
166     TString tstr(buffer);
167     TPMERegexp rsp(";");
168     TPMERegexp csp1("=");
169     TPMERegexp csp2(",");
170     int nrow = rsp.Split( tstr );
171     for( int i=0;i<nrow;i++ ){
172         TString row = rsp[i];
173         int nst = csp1.Split(row);
174         if( nst!=2 ) continue; // TODO Error or warning
175         TString key = csp1[0];
176         TString val = csp1[1];
177         int nc = csp2.Split( val );
178
179         vector< float > items;//auxiliary vector
180
181         for(int j=0; j<nc; j++){ //loop over the numbers 
182             TString token = csp2[j];//read a string
183
184             if(token.IsFloat()){
185                 items.push_back(token.Atof());//if string is float number store it to vector
186             }else{
187                 items.push_back(0);
188                 // cout<<"ERROR: char "<<token.Data()<<" among numbers"<<endl;
189                 // exit(1);
190             }
191         }//end of the for loop
192
193
194         //Fill TVectors and Map 
195         int index =  GetTVectorIndex( key, 2 );
196         if(  index > -1 ){
197             //fKeyWordVector[index] = key;
198             //fValuesVector[index] = TVector( 1, items.size(), &items[0]);
199             fValuesVector[index].ResizeTo( 1, items.size()) ;
200             //fValuesVector[index] = TVector( 1, items.size(), &items[0]);
201             //fValuesVector[index].SetElements( &items[0] );
202             for( unsigned int ii=0;ii< items.size(); ii ++ ){
203                 fValuesVector[index][ii+1] = items[ii];
204             }
205
206
207             fValueString[index] = val;
208         }else{
209             fKeyWordVector.push_back( key.Data() );//put the new keyword at the end of the array
210
211
212             fValuesVector.push_back( TVector( 1, items.size(), &items[0]) );//store TVector to array
213             fValueString.push_back( val );
214             //       MapKeyWordToTVector.insert(pair<TString, unsigned int>(entryname.Data(),fKeyWordVector.size()-1)); 
215             AddToKeyTable( key, fValuesVector.size()-1 ); 
216         }
217
218     }
219
220 }
221
222 void AliJBaseCard::ReadInputLine( const char *buffer ){
223     // parse a line
224
225
226
227     TString tstr(buffer); //convert the line in the buffer to TString
228
229     if( tstr.BeginsWith("#") ) return;//skipp comments
230     tstr.ReplaceAll("\t"," ");//get rid of tabelators
231
232     //remove comment in line
233     Ssiz_t startOFcomment = tstr.First('#');
234     if(startOFcomment>0){
235         tstr.Remove(startOFcomment,tstr.Length() - startOFcomment);
236     }
237
238     //remove white spaces from the begining
239     if(tstr.BeginsWith(" ")){
240         Ssiz_t startOFkeyword = 0;
241         while(1){
242             TString s = tstr[startOFkeyword];
243             if(s.CompareTo(" ")) break;
244             startOFkeyword++;
245         }
246         tstr.Replace(0,startOFkeyword,"",0);
247     }
248
249     //separate inputs 
250     TObjArray *lineContents = tstr.Tokenize(" ");
251
252     if(lineContents->GetEntriesFast() < 1) return;//skipp empty lines
253
254     //----- Read a keyword -----
255     TString entryname = ((TObjString*)(lineContents->At(0)))->String(); //read a key word
256
257     if(lineContents->GetEntriesFast() == 1){
258         cout<<"WARNING: single keyword "<<entryname.Data()<<" on line"<<endl;
259     }else{
260
261
262         //----- Read parameters -----
263         vector< float > items;//auxiliary vector
264
265         for(int i=1; i<lineContents->GetEntriesFast(); i++){ //loop over the numbers 
266             TString token = ((TObjString*)(lineContents->At(i)))->String();//read a string
267
268             if(token.IsFloat()){
269                 items.push_back(token.Atof());//if string is float number store it to vector
270             }else{
271                 items.push_back(0);
272                 // cout<<"ERROR: char "<<token.Data()<<" among numbers"<<endl;
273                 // exit(1);
274             }
275         }//end of the for loop
276
277
278         //Fill TVectors and Map 
279         fKeyWordVector.push_back( entryname.Data() );//put the new keyword at the end of the array
280
281
282         fValuesVector.push_back( TVector( 1, items.size(), &items[0]) );//store TVector to array
283         fValueString.push_back( ((TObjString*)(lineContents->At(1)))->String() );
284
285         //       MapKeyWordToTVector.insert(pair<TString, unsigned int>(entryname.Data(),fKeyWordVector.size()-1)); 
286         AddToKeyTable( entryname, fValuesVector.size()-1 ); 
287
288
289     }//else
290
291     lineContents->~TObjArray();//remove array from heap
292 }
293
294 void AliJBaseCard::PrintOut(){
295     // echo
296     cout<<endl<<"======== "<<fcardname<<" ========="<<endl;
297     for(unsigned int i=0; i<fValuesVector.size();i++){
298         cout<<Form("%15s",fKeyWordVector[i].Data());//print keyword
299         cout<<" (dim ="<<fValuesVector[i].GetNrows()<<") ";//print size of TVector
300         for(int j=1; j<=fValuesVector[i].GetNrows(); j++){
301             cout<<fValuesVector[i][j]<<" ";//TVector components
302         }
303         cout<<endl;
304     }
305 }
306
307
308 void AliJBaseCard::WriteCard(TDirectory *file){
309     // write
310     cout<<endl<<"====== Writing into file ========="<<endl;
311
312     if(!file->GetDirectory("JCard")) {
313         file->mkdir("JCard");//directory to store input parameters
314     }
315     file->cd("JCard");
316     for(unsigned int i=0;i<fValuesVector.size();i++){ 
317         fValuesVector[i].Write(fKeyWordVector[i]);
318     }
319 }
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339