KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVRunListLine.cpp
Go to the documentation of this file.
1 /*
2 $Id: KVRunListLine.cpp,v 1.6 2006/10/19 14:32:43 franklan Exp $
3 */
4 
5 #include "KVRunListLine.h"
6 #include "RVersion.h"
7 #include "KVString.h"
8 #include "TError.h"
9 #include <TClass.h>
10 
11 using namespace std;
12 
14 
15 
16 
21 {
22  //Default ctor
23  //Initial size of TObjArray (fMaxNFields) is 5. This will be increased if necessary in BreakLineIntoFields().
24 
25  fMaxNFields = 5;
26  fFields = new TObjArray(fMaxNFields);
27  fFields->SetOwner();
28  fFieldsSet = kFALSE;
29  calling_field_keys = false;
30  calling_run_keys = false;
31 }
32 
33 
34 
36 
37 KVRunListLine::~KVRunListLine()
38 {
39  fFields->Delete();
40  delete fFields;
41  fFields = 0;
42  fFieldKeys.Clear();
43  fIndexList.Clear();
44 }
45 
46 
47 
48 
54 
56 {
57  //Break down "line" into fields separated by the delimiter "_delim". Fields are stored in TObjArray of TObjString s (including empty fields
58  //i.e. 2 consecutive delimiters in "line" with nothing between are translated as a field whose content is "". TString::Tokenize() simply skips over such
59  //cases, so we can't use it with our field idxing system). Remove leading/trailing white space from each field.
60  //Return the total number of fields found (including empty fields).
61 
62  Int_t allfields = 0;
63  Int_t start = 0;
64  TString delim(_delim);
65  Int_t end = fLine.Index(delim);
66  TObjArray& tca = *fFields;
67  while (end > -1) {
68 
69  tca[allfields] = new TObjString();
70 
71  if (start < end) {
72  TString _dummy = fLine(start, end - start);
73  KVString dummy(_dummy);
74 
75  //remove trailing/leading tabs
76  dummy.Remove(TString::kBoth, '\t');
77  //remove trailing/leading whitespace
78  dummy.Remove(TString::kBoth, ' ');
79 
80 #if ROOT_VERSION_CODE >= ROOT_VERSION(3,5,0)
81  //since ROOT v3.05 SetString(const char *)
82  ((TObjString*) tca[allfields])->SetString(dummy.Data());
83 #else
84  //before ROOT v3.05 SetString(char *)
85  ((TObjString*) tca[allfields])->SetString((char*) dummy.Data());
86 #endif
87 
88  }
89  allfields++;
90  //check size of TObjArray fFields - if too many fields are found, double size of array
91  if (allfields == fMaxNFields) {
92  fMaxNFields *= 2;
93  tca.Expand(fMaxNFields);
94  }
95  start = end + 1;
96  end = fLine.Index(delim, end + 1);
97  //if last column does not have a delimiter on the right, we take rest of line up to end
98  if (end == -1 && start < fLine.Length())
99  end = fLine.Length();
100  }
101  //does this line define the field names ?
102  if (IsFieldHeader())
103  SetFields();
104  return allfields;
105 }
106 
107 
108 
109 
116 
118 {
119  //set field idxes based on field header line in file identified by IsFieldHeader().
120  //this method is called automatically by BreakLineIntoFields().
121  //we check that at least one index has been defined at the end
122  //previous definitions are forgotten
123 
124  //clear index list
125  fIndexList.Clear();
126  //reset flag
127  fFieldsSet = kFALSE;
128 
129  TIter next_field(fFields);
130  TObjString* field = 0;
131  Int_t idx = 0;
132  while ((field = (TObjString*) next_field())) {
133 
134  if (field->GetString().IsAscii() && field->GetString().Length()) {
135  //non-null ASCII string ? use it as column header
136  fIndexList.SetValue(field->GetString().Data(), idx);
137  }
138  idx++;
139 
140  }
141 
142  if (!fIndexList.GetNpar()) {
143  //no indices set - something's wrong
144  Error(Form("%s::SetFields", IsA()->GetName()), "No indices set");
145  return;
146  }
147  //set flag
148  fFieldsSet = kTRUE;
149 }
150 
151 
152 
153 
157 
159 {
160  //returns a TObjString containing the field in the line corresponding to column title "fname".
161  //Check that field indices are set and field is known otherwise return 0
162 
163  Int_t idx = GetFieldIndex(fname);
164  if (idx < 0)
165  return 0;
166  TObjArray& tca = *fFields;
167  return (TObjString*) tca[idx];
168 }
169 
170 
171 
172 
176 
177 const Char_t* KVRunListLine::GetField(const Char_t* fname) const
178 {
179  //returns a string containing the field in the line corresponding to column title "fname".
180  //returns 0 if field not found
181 
182  TObjString* tmp = GetFieldString(fname);
183  if (tmp)
184  return tmp->String().Data();
185  return 0;
186 }
187 
188 
189 
190 
195 
197 {
198  //Returns the integer translation of the field in the line corresponding to column title "fname".
199  //We check the field is present in current line and that all characters in the field are numbers (KVString::IsDigit).
200  //If not, 0 is returned.
201 
202  TObjString* tmp = GetFieldString(fname);
203  if (!tmp)
204  return 0;
205  KVString kvs(tmp->String());
206  if (HasFieldValue(fname) && kvs.IsDigit())
207  return kvs.Atoi();
208  return 0;
209 }
210 
211 
212 
213 
218 
220 {
221  //returns the floating-point translation of the field in the line corresponding to column title "fname".
222  //We check that the field is a floating point number (IsFloat=kTRUE)
223  //If not, 0 is returned.
224 
225  TObjString* tmp = GetFieldString(fname);
226  if (!tmp)
227  return 0;
228  KVString kvs(tmp->String());
229  if (IsFloat(fname))
230  return kvs.Atof();
231  return 0.0;
232 }
233 
234 
235 
236 
241 
243 {
244  //Print information on this reader object.
245  //If fields have been set, we show which fields have been found.
246  //Break down of current line is shown.
247 
248  cout << IsA()->GetName() << " object" << endl;
249  cout << "Current line : " << endl << fLine << endl;
250 
251  if (!FieldsSet())
252  return;
253 
254  //show all defined fields with current values
255  cout << "Field name : current value" << endl;
256  cout << "==========================" << endl;
257 
258  for (Int_t j = 0; j < fIndexList.GetNpar(); j++) {
259  PrintFieldValue(fIndexList.GetParameter(j)->GetName());
260  }
261 }
262 
263 
264 
265 
273 
275 {
276  //Returns kTRUE if field "name" contains a floating point number
277  //Examples are:
278  // 64320
279  // 6.4320
280  // 6.43e20 6.43E20
281  // 6.43e-20 6.43E-20
282 
283  if (HasFieldValue(name)) { //check field is present
284 
285  TObjString* tmp = GetFieldString(name);
286  if (!tmp)
287  return 0;
288  KVString kvs(tmp->String());
289  return kvs.IsFloat();
290  }
291  return kFALSE;
292 }
293 
294 
295 
296 
299 
301  const Char_t* string) const
302 {
303  //Returns true if "field" exists, has been filled, and contains "string" as part or all of its value
304  if (!HasFieldValue(field))
305  return kFALSE;
306  TObjString* tmp = GetFieldString(field);
307  if (!tmp)
308  return kFALSE;
309  KVString kvs(tmp->String());
310  return kvs.Contains(string);
311 }
312 
313 
314 
315 
321 
323  const Char_t* fmt)
324 {
325  //Returns value of "Trigger" field, assuming it is written with format "fmt" i.e. if fmt = "M>=%d" (default) we expect "M>=1", "M>=4" etc.
326  //The actual field name for the trigger is given as first argument (default value = "Trigger")
327  //If trigger field is not present, we return -1 (but no error)
328  //If format is not respected, an error message is printed and -1 is returned.
329 
330  if (!HasFieldValue(field_name))
331  return -1;
332  Int_t trigger = -1;
333  if (sscanf(GetField(field_name), fmt, &trigger) != 1) {
334  Warning(Form("%s::GetTrigger", IsA()->GetName()),
335  "trigger field - %s - unreadable", GetField(field_name));
336  }
337  return trigger;
338 }
339 
340 
341 
342 
343 
344 
345 
346 
347 
348 
351 
353 {
354  //Returns kTRUE if and only if current line contains ALL of the keywords defined by SetFieldKeys()
355 
356  if (!fFieldKeys.GetSize()) {
357  //no keywords set - can't recognize header
358  return kFALSE;
359  }
360  //test all keywords in list - first one not contained in "line" causes us to return kFALSE
361  TIter next_kw(&fFieldKeys);
362  TObjString* os;
363  while ((os = (TObjString*) next_kw())) {
364  if (!fLine.Contains(os->GetString())) {
365  return kFALSE;
366  }
367  }
368  return kTRUE;
369 }
370 
371 
372 
373 
376 
378 {
379  //Returns kTRUE if and only if current line contains integer values for ALL the fields defined by SetRunKeys
380 
381  if (!fRunKeys.GetSize()) {
382  //no fields set
383  return kFALSE;
384  }
385  //test all fields in list - first one without a value in line causes us to return kFALSE
386  TIter next_kw(&fRunKeys);
387  TObjString* os;
388  while ((os = (TObjString*) next_kw())) {
389  if (!HasFieldValue(os->GetString())) {
390  return kFALSE;
391  }
392  KVString tmp(GetField(os->GetString()));
393  if (!tmp.IsDigit()) {
394  return kFALSE;
395  }
396  }
397  return kTRUE;
398 }
399 
400 
int Int_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
bool Bool_t
float Float_t
const Bool_t kTRUE
char * Form(const char *fmt,...)
Base class for reading runlists for experiments ,.
Definition: KVRunListLine.h:25
Int_t BreakLineIntoFields(const char)
Bool_t FieldContains(const Char_t *, const Char_t *) const
Returns true if "field" exists, has been filled, and contains "string" as part or all of its value.
virtual void SetFields()
Float_t GetFloatField(const Char_t *)
virtual Bool_t IsFieldHeader()
Returns kTRUE if and only if current line contains ALL of the keywords defined by SetFieldKeys()
virtual void Print() const
Bool_t IsFloat(const Char_t *name)
virtual Bool_t GoodRunLine()
Returns kTRUE if and only if current line contains integer values for ALL the fields defined by SetRu...
TObjString * GetFieldString(const Char_t *) const
virtual Int_t GetTrigger(const Char_t *field_name="Trigger", const Char_t *fmt="M>=%d")
Int_t GetIntField(const Char_t *)
virtual const Char_t * GetField(const Char_t *) const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
virtual void Expand(Int_t newSize)
const TString & GetString() const
TString & String()
Ssiz_t Length() const
Int_t Atoi() const
Double_t Atof() const
Bool_t IsFloat() const
Bool_t IsDigit() const
Bool_t IsAscii() const
const char * Data() const
TString & Remove(EStripType s, char c)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
void Error(const char *location, const char *va_(fmt),...)
void Warning(const char *location, const char *va_(fmt),...)