1 //====================================
2 //last modified FK 6.NOV 2009
3 //====================================
9 #include "AliJBaseCard.h"
11 //ClassImp(AliJBaseCard);
13 AliJBaseCard::AliJBaseCard() :
23 AliJBaseCard::AliJBaseCard(const char *filename) :
32 strcpy(fcardname, filename); //needed in PrintOut()
34 if( strlen( filename ) > 0 ){
36 //---- r e a d t h e c a r d ----
37 ReadInputCard();//read config file fill Tvectors
42 AliJBaseCard& AliJBaseCard::operator=(const AliJBaseCard& obj){
48 AliJBaseCard::~AliJBaseCard(){
53 unsigned int AliJBaseCard::GetTVectorIndex(TString keyword, int tol){
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;
61 // cout << "ERROR: \""<<keyword.Data()<<"\" must be defined "<< endl;
70 //cout<<"ALIJBASECARD_SEARCH_MODE_HASHLIST"<<endl;
71 TNamed * ko = (TNamed*)fKeyTable.FindObject( keyword.Data() );
73 if(ko) ind = ko->GetUniqueID();
75 for( UInt_t ii=0;ii<fKeyWordVector.size();ii++ ){
76 if( fKeyWordVector[ii] == keyword ) return i;
79 cout << "ERROR: \""<<keyword.Data()<<"\" must be defined "<< endl;
81 }else if ( tol == 1 ){
82 cout << "Warning: \""<<keyword.Data()<<"\" is not exist. return default value "<< endl;
92 int AliJBaseCard::GetN(TString keyword){
93 //returns size of TVector
94 unsigned int findex = GetTVectorIndex(keyword);
95 return (int) fValuesVector[findex].GetNrows();
98 TVector * AliJBaseCard::GetVector(TString keyword ){
99 int findex = GetTVectorIndex(keyword);
100 return &fValuesVector[findex];
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);
109 cout<<"ERROR: fValuesVector findex out of range "<<keyword.Data()<<endl;
110 cout << " Max findex: " << GetN(keyword) - 1<< " Asked: " << VectorComponent << endl;
115 TString AliJBaseCard::GetStr(TString keyword ){
116 int findex = GetTVectorIndex(keyword, 1);
117 if( findex < 0 ) return TString("");
118 return fValueString[findex];
122 void AliJBaseCard::InitCard(){
123 // set the length of fIndexVector and disable all indices
126 void AliJBaseCard::FinishCard(){
127 // recompute fast idices
130 void AliJBaseCard::ReadInputCard(){
133 char buffer[kMaxDimBuffer];
136 cout << "Reading fcard from file: " << fcardname << endl;
137 incard.open(fcardname,ios::in);
140 cout<<"ERROR: Config file <"<<fcardname<<"> not found!"<<endl;
146 while(!incard.eof()){ //loop over the input fcard
148 incard.getline(buffer,kMaxDimBuffer); //read a line
150 if(fnentry > 1000){//is the file reasonably long?
151 cout<<"Maximum number of 1000 lines reached in AliJBaseCard.C"<<endl;
155 ReadInputLine( buffer );
165 void AliJBaseCard::ReadLine( const char * buffer ){
166 TString tstr(buffer);
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 );
179 vector< float > items;//auxiliary vector
181 for(int j=0; j<nc; j++){ //loop over the numbers
182 TString token = csp2[j];//read a string
185 items.push_back(token.Atof());//if string is float number store it to vector
188 // cout<<"ERROR: char "<<token.Data()<<" among numbers"<<endl;
191 }//end of the for loop
194 //Fill TVectors and Map
195 int index = GetTVectorIndex( key, 2 );
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];
207 fValueString[index] = val;
209 fKeyWordVector.push_back( key.Data() );//put the new keyword at the end of the array
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 );
222 void AliJBaseCard::ReadInputLine( const char *buffer ){
227 TString tstr(buffer); //convert the line in the buffer to TString
229 if( tstr.BeginsWith("#") ) return;//skipp comments
230 tstr.ReplaceAll("\t"," ");//get rid of tabelators
232 //remove comment in line
233 Ssiz_t startOFcomment = tstr.First('#');
234 if(startOFcomment>0){
235 tstr.Remove(startOFcomment,tstr.Length() - startOFcomment);
238 //remove white spaces from the begining
239 if(tstr.BeginsWith(" ")){
240 Ssiz_t startOFkeyword = 0;
242 TString s = tstr[startOFkeyword];
243 if(s.CompareTo(" ")) break;
246 tstr.Replace(0,startOFkeyword,"",0);
250 TObjArray *lineContents = tstr.Tokenize(" ");
252 if(lineContents->GetEntriesFast() < 1) return;//skipp empty lines
254 //----- Read a keyword -----
255 TString entryname = ((TObjString*)(lineContents->At(0)))->String(); //read a key word
257 if(lineContents->GetEntriesFast() == 1){
258 cout<<"WARNING: single keyword "<<entryname.Data()<<" on line"<<endl;
262 //----- Read parameters -----
263 vector< float > items;//auxiliary vector
265 for(int i=1; i<lineContents->GetEntriesFast(); i++){ //loop over the numbers
266 TString token = ((TObjString*)(lineContents->At(i)))->String();//read a string
269 items.push_back(token.Atof());//if string is float number store it to vector
272 // cout<<"ERROR: char "<<token.Data()<<" among numbers"<<endl;
275 }//end of the for loop
278 //Fill TVectors and Map
279 fKeyWordVector.push_back( entryname.Data() );//put the new keyword at the end of the array
282 fValuesVector.push_back( TVector( 1, items.size(), &items[0]) );//store TVector to array
283 fValueString.push_back( ((TObjString*)(lineContents->At(1)))->String() );
285 // MapKeyWordToTVector.insert(pair<TString, unsigned int>(entryname.Data(),fKeyWordVector.size()-1));
286 AddToKeyTable( entryname, fValuesVector.size()-1 );
291 lineContents->~TObjArray();//remove array from heap
294 void AliJBaseCard::PrintOut(){
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
308 void AliJBaseCard::WriteCard(TDirectory *file){
310 cout<<endl<<"====== Writing into file ========="<<endl;
312 if(!file->GetDirectory("JCard")) {
313 file->mkdir("JCard");//directory to store input parameters
316 for(unsigned int i=0;i<fValuesVector.size();i++){
317 fValuesVector[i].Write(fKeyWordVector[i]);