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