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