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