]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCConfigParser.cxx
Corrected makrolon rod length
[u/mrichter/AliRoot.git] / TPC / AliTPCConfigParser.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 //                                                                           //
18 //  Class for Parsing simple text configuration files                        //
19 //  It produces a TList for the TObjArrays with the name of the Key
20 //    the TObjArray contain the Values, split from kommas, as found in the   //
21 //  Configutation file.                                                      //
22 //                                                                           //
23 // The configuration file has a simple structure:                            //
24 // * Lines starting with a # or empty lines are ignored                      //
25 // * Key and Value are separated either by a <tab> or <space>es              //
26 //                                                                           //
27 //  Currently the class is used in the TPC DAs to allow an adjustment of     //
28 //  the most relevant parameters without recompiling the DAs                 //
29 ///////////////////////////////////////////////////////////////////////////////
30
31
32 #include <fstream>
33 //Root includes
34 #include <TObjString.h>
35 #include <TObjArray.h>
36 #include <TString.h>
37 #include <TIterator.h>
38 #include <TList.h>
39 #include <TSystem.h>
40 //AliRoot includes
41
42 //header
43 #include "AliTPCConfigParser.h"
44
45
46 AliTPCConfigParser::AliTPCConfigParser() :
47 TObject(),
48 fConfigMap(new TList),
49 fKeyIter(0),
50 fValIter(0)
51 {
52  //
53  // default constructor
54  //
55   fConfigMap->SetOwner();
56
57 //_____________________________________________________________________
58 AliTPCConfigParser::AliTPCConfigParser(const AliTPCConfigParser &cfg) :
59 TObject(),
60 fConfigMap((TList*)cfg.fConfigMap->Clone()),
61 fKeyIter(0),
62 fValIter(0)
63 {
64   //
65   // copy constructor
66   //
67   fConfigMap->SetOwner();
68 }
69
70 //_____________________________________________________________________
71 AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
72 TObject(),
73 fConfigMap(new TList),
74 fKeyIter(0),
75 fValIter(0)
76 {
77   //
78   // default constructor using the config file name as input parameter
79   //
80   fConfigMap->SetOwner();
81   if ( !cfgfile ) return;
82   ParseConfigFileTxt(cfgfile);
83 }
84 //_____________________________________________________________________
85 AliTPCConfigParser& AliTPCConfigParser::operator = (const  AliTPCConfigParser &source)
86 {
87   //
88   // assignment operator
89   //
90   if (&source == this) return *this;
91   new (this) AliTPCConfigParser(source);
92   
93   return *this;
94 }
95 //_____________________________________________________________________
96 AliTPCConfigParser::~AliTPCConfigParser()
97 {
98  //
99  // dtor
100  //
101   delete fConfigMap;
102   delete fKeyIter;
103   delete fValIter;
104 }
105 //_____________________________________________________________________
106 Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
107 {
108  //
109  // Function to parse a configuration file
110  //
111   ResetMap();
112   ifstream file(gSystem->ExpandPathName(cfgfile));
113   if ( !file.is_open() ){
114     Error("ParseConfigFileTxt","File '%s' could not be opened!", cfgfile);
115     return 1;
116   }
117   TString strFile;
118   strFile.ReadFile(file);
119   TObjArray *arr=strFile.Tokenize("\n");
120   if ( !arr ) {
121     file.close();
122     return 2;
123   }
124   TIter nextLine(arr);
125   while (TObject *l=nextLine()){
126     TString line(((TObjString*)l)->GetString());
127   //remove whitespcaces
128     line.Remove(TString::kBoth,' ');
129     line.Remove(TString::kBoth,'\t');
130     if ( line.BeginsWith("#") || line=="" ) continue;
131     line.ReplaceAll(", ",",");
132     TObjArray *arrValues=line.Tokenize(" \t");
133   //currently only name => Value is supported
134   //and            name => 'nothing'
135   //value may be a comma separated list, in which case a TObjArray
136   //of the list will be created and stored as the value
137     Int_t nentries=arrValues->GetEntries();
138     if (nentries>2){
139       Error("AliTPCConfigParser","ParseConfigFileTxt: Cannot parse line '%s'\n",line.Data());
140       delete arrValues;
141       continue;
142     }
143     TObjArray  *objArr=0x0;
144     if (nentries==2){
145       TObject *objVal=arrValues->At(1);
146       const TString &str=((TObjString*)objVal)->GetString();
147       if (str.Contains(","))
148         objArr=str.Tokenize(",");
149       else{
150         objArr=new TObjArray;
151         objArr->Add(objVal->Clone());
152       }
153       objArr->SetOwner(kTRUE);
154     } else {
155       objArr=new TObjArray;
156     }
157     objArr->SetName(arrValues->At(0)->GetName());
158     fConfigMap->AddLast(objArr);
159     delete arrValues;
160   }
161   
162   delete arr;
163   return 0;
164 }
165 //_____________________________________________________________________
166 Float_t AliTPCConfigParser::GetValue(const char *key, UInt_t position)
167 {
168   //
169   //Get value for the speciefied key
170   //
171   TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
172   if ( !val ) return -999.;
173   TString sval(((TObjString*)val)->GetString());
174   return sval.Atof();
175 }
176 //_____________________________________________________________________
177 const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
178 {
179   //
180   //Get value for the speciefied key
181   //
182   TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
183   if (position>=(UInt_t)(arr->GetEntries())) return "";
184   TObject *val=arr->At(position);
185   if ( !val ) return "";
186   return (((TObjString*)val)->GetString()).Data();
187 }
188 //_____________________________________________________________________
189 Float_t AliTPCConfigParser::GetValue(const TObject *key, UInt_t position)
190 {
191   //
192   //Get value for the speciefied key
193   //
194   TObject *val=((TObjArray*)fConfigMap->FindObject(key))->At(position);
195   if ( !val ) return -999.;
196   TString sval(((TObjString*)val)->GetString());
197   return sval.Atof();
198 }
199 //_____________________________________________________________________
200 const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
201 {
202   //
203   //Get value for the speciefied key
204   //
205   TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
206   if (position>=((UInt_t)arr->GetEntries())) return "";
207   TObject *val=arr->At(position);
208   if ( !val ) return "";
209   return (((TObjString*)val)->GetString()).Data();
210 }
211 //_____________________________________________________________________
212 Int_t AliTPCConfigParser::GetNumberOfValues(const char* key) const
213 {
214   //
215   // return the number of values for key
216   //
217   return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
218 }
219 //_____________________________________________________________________
220 Int_t AliTPCConfigParser::GetNumberOfValues(TObject* key) const
221 {
222   //
223   // return the number of values for key
224   //
225   return ((TObjArray*)fConfigMap->FindObject(key))->GetEntries();
226 }
227 //_____________________________________________________________________
228 TObject* AliTPCConfigParser::NextKey(){
229   if (!fKeyIter) fKeyIter=fConfigMap->MakeIterator();
230   TObject *obj=fKeyIter->Next();
231   if (!obj) {
232     delete fKeyIter;
233     fKeyIter=0;
234   }
235   return obj;
236 }
237 //_____________________________________________________________________
238 TObject* AliTPCConfigParser::NextValue(const char *key){
239   return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
240 }
241 //_____________________________________________________________________
242 TObject* AliTPCConfigParser::NextValue(TObject *key){
243   return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
244 }
245 //_____________________________________________________________________
246 TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
247   if (!key) return 0;
248   //check if the collection has changed
249   if (fValIter && key!=fValIter->GetCollection()) delete fValIter;
250   if (!fValIter) fValIter=key->MakeIterator();
251   TObject *value=fValIter->Next();
252   if (!value) {
253     delete fValIter;
254     fValIter=0;
255   }
256   return value;
257 }
258 //_____________________________________________________________________
259 void AliTPCConfigParser::ResetMap()
260 {
261   //
262   // Reset the map with the configuration values
263   //
264   fConfigMap->Delete();
265 }