]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TPC/AliTPCConfigParser.cxx
one more tpcrefit added
[u/mrichter/AliRoot.git] / TPC / 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
45
46AliTPCConfigParser::AliTPCConfigParser() :
47TObject(),
48fConfigMap(new TList),
49fKeyIter(0),
50fValIter(0)
51{
52 //
53 // default constructor
54 //
55 fConfigMap->SetOwner();
56}
57//_____________________________________________________________________
58AliTPCConfigParser::AliTPCConfigParser(const AliTPCConfigParser &cfg) :
59TObject(),
60fConfigMap((TList*)cfg.fConfigMap->Clone()),
61fKeyIter(0),
62fValIter(0)
63{
64 //
65 // copy constructor
66 //
67 fConfigMap->SetOwner();
68}
69
70//_____________________________________________________________________
71AliTPCConfigParser::AliTPCConfigParser(const char* cfgfile) :
72TObject(),
73fConfigMap(new TList),
74fKeyIter(0),
75fValIter(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//_____________________________________________________________________
85AliTPCConfigParser& 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//_____________________________________________________________________
96AliTPCConfigParser::~AliTPCConfigParser()
97{
98 //
99 // dtor
100 //
101 delete fConfigMap;
102 delete fKeyIter;
103 delete fValIter;
104}
105//_____________________________________________________________________
106Int_t AliTPCConfigParser::ParseConfigFileTxt(const char* cfgfile)
107{
108 //
109 // Function to parse a configuration file
110 //
6e7d7dc4 111 ResetMap();
112 ifstream file(gSystem->ExpandPathName(cfgfile));
5312f439 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//_____________________________________________________________________
166Float_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//_____________________________________________________________________
6fb51ca4 177const char* AliTPCConfigParser::GetData(const char *key, UInt_t position)
178{
179 //
180 //Get value for the speciefied key
181 //
7390f655 182 TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
be1765e4 183 if (position>=(UInt_t)(arr->GetEntries())) return "";
7390f655 184 TObject *val=arr->At(position);
185 if ( !val ) return "";
6fb51ca4 186 return (((TObjString*)val)->GetString()).Data();
187}
188//_____________________________________________________________________
189Float_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//_____________________________________________________________________
200const char* AliTPCConfigParser::GetData(const TObject *key, UInt_t position)
201{
202 //
203 //Get value for the speciefied key
204 //
7390f655 205 TObjArray *arr=((TObjArray*)fConfigMap->FindObject(key));
be1765e4 206 if (position>=((UInt_t)arr->GetEntries())) return "";
7390f655 207 TObject *val=arr->At(position);
208 if ( !val ) return "";
6fb51ca4 209 return (((TObjString*)val)->GetString()).Data();
210}
211//_____________________________________________________________________
212Int_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//_____________________________________________________________________
220Int_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//_____________________________________________________________________
5312f439 228TObject* 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//_____________________________________________________________________
238TObject* AliTPCConfigParser::NextValue(const char *key){
239 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
240}
241//_____________________________________________________________________
242TObject* AliTPCConfigParser::NextValue(TObject *key){
243 return NextValueIter((TObjArray*)fConfigMap->FindObject(key));
244}
245//_____________________________________________________________________
246TObject* AliTPCConfigParser::NextValueIter(TObjArray *key){
247 if (!key) return 0;
248 //check if the collection has changed
5dbad769 249 if (fValIter && key!=fValIter->GetCollection()) {
250 delete fValIter;
251 fValIter=0x0;
252 }
5312f439 253 if (!fValIter) fValIter=key->MakeIterator();
254 TObject *value=fValIter->Next();
255 if (!value) {
256 delete fValIter;
257 fValIter=0;
258 }
259 return value;
260}
261//_____________________________________________________________________
262void AliTPCConfigParser::ResetMap()
263{
264 //
265 // Reset the map with the configuration values
266 //
267 fConfigMap->Delete();
268}