KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVINDRARunListReader.cpp
Go to the documentation of this file.
1 /*
2 $Id: KVINDRARunListReader.cpp,v 1.8 2007/04/26 16:38:57 franklan Exp $
3 */
4 
5 #include <KVINDRARunListReader.h>
6 #include <TClass.h>
7 #include <TError.h>
8 #include <KVRunListLine.h>
9 
10 
11 using namespace std;
12 
14 
15 
16 
21 {
22  //Default ctor.
23  //We set default values for recognising comment lines ('#') and the separator character ('|')
24 
25  fLineReader = new KVRunListLine;
26  fComment = '#';
27  fSeparator = '|';
28  fTrigger = -1;
29  fNewRunList = kFALSE;
30  fVersion = 0;
31 }
32 
33 
34 
38 
39 KVINDRARunListReader::~KVINDRARunListReader()
40 {
41  //Dtor.
42  //Delete KVRunListLine object if one exists; close runlist file if open
43  CloseRLFile();
44  if (fLineReader) {
45  delete fLineReader;
46  fLineReader = 0;
47  }
48 }
49 
50 
51 
56 
58 {
59  //Open file 'filename' for reading.
60  //Returns kTRUE if file can be opened (info message), kFALSE if not (error message)
61  //Reset fTrigger to -1 (in case we already read a different file)
62 
63  fRunsStream.open(filename);
64  if (fRunsStream.is_open()) {
65  fRunsFile = filename;
66  Info(Form("%s::OpenRLFile", IsA()->GetName()),
67  "Opened file %s for reading", filename);
68  fTrigger = -1;
69  return kTRUE;
70  }
71  Error(Form("%s::OpenRLFile", IsA()->GetName()),
72  "Cannot open file %s for reading", filename);
73  //reset ifstream ready to try opening another file
74  fRunsStream.close();
75  fRunsStream.clear();
76  return kFALSE;
77 }
78 
79 
80 
84 
86 {
87  //Close current runlist file. Another file can be opened after this.
88  //If no file is open, do nothing.
89  if (fRunsStream.is_open()) {
90  fRunsStream.close();
91  fRunsStream.clear();
92  fRunsFile = "";
93  }
94 }
95 
96 
97 
104 
106 {
107  //Read a line from current runlist file. If not a comment line (i.e. does not begin with 'fComment' character)
108  //then analyse it with fLineReader object.
109  //Returns kFALSE if no file is open or if file is not good for i/o
110  //Returns kFALSE if fLineReader has not been initialised
111  //Returns kFALSE if line cannot be read / end of file is reached
112  if (!fRunsStream.is_open()) {
113  Error(Form("%s::ReadRLLine", IsA()->GetName()),
114  "No file open for reading");
115  return kFALSE;
116  }
117  if (!fRunsStream.good()) {
118  Error(Form("%s::ReadRLLine", IsA()->GetName()),
119  "File %s is not good for reading", fRunsFile.Data());
120  return kFALSE;
121  }
122  fLine.ReadLine(fRunsStream);
123 
124  if (!fRunsStream.good())
125  return kFALSE;
126 
127  if (fLine.BeginsWith("Version")) {
128  //read version number in file
129  if (sscanf(fLine.Data(), "Version=%d", &fVersion) == 1) {
130  //set flag to force reading runlist with new method
131  fNewRunList = kTRUE;
132  return kFALSE;
133  }
134  }
135  else if (!fLine.BeginsWith(fComment)) {
136  fLineReader->SetLine(fLine);
137  fLineReader->BreakLineIntoFields(fSeparator);
138  }
139  else {
140  //comment line - reset line reader
141  fLineReader->Clear();
142  }
143  return kTRUE;
144 }
145 
146 
147 
155 
157 {
158  //This method will open the runlist file 't' and read its entire contents, calling user-overridable method "GoodRunLine(KVRunListLine*)"
159  //every time a line defining a new run is read from the file.
160  //If 't' is not given, we assume that the name of the run list file was passed to the constructor, and so the file should
161  //already be open in this case.
162  //Before calling this method, you should call SetRunListLineType() to define the class used to read each line, and
163  //SetRLCommentChar()/SetRLSeparatorChar() if you want to override the default comment/separator characters.
164 
165  if (!strcmp(t, "")) {
166  //no filename given - file should have been opened by OpenRLFile - check !
167  if (!fRunsStream.is_open()) {
168  Error(Form("%s::ReadRunList", IsA()->GetName()),
169  "No runlist file open for reading");
170  return;
171  }
172  }
173  else {
174  //open new file - check OK
175  //close any previously opened file
176  CloseRLFile();
177  if (!OpenRLFile(t))
178  return; //error message will be given by OpenRLFile
179  }
180  //reset last trigger read
181  fTrigger = -1;
182  fCurrentLine = 1;
183  while (ReadRLLine()) { // read lines in file while we can
184 
185  if (fLineReader->GoodRunLine()) {
186  //call user's function
187  GoodRunLine();
188  }
189  //reset line reader
190  fLineReader->Clear();
191  fCurrentLine++;
192  }
193 
194  //close run file
195  CloseRLFile();
196 }
197 
198 
199 
204 
206 {
207  //This method should be overridden in derived class by the user.
208  //It is called every time that the fLineReader finds a line which is a "good run line" (see KVRunListLine::GoodRunLine()).
209  //The default behaviour is just to print the breakdown of the line in question on the screen.
210 
211  fLineReader->Print();
212 }
213 
214 
215 
223 
225  const Char_t* fmt)
226 {
227  //Get trigger multiplicity for current line.
228  //Give field name which contains trigger information and format string 'fmt' containing e.g. "M>=%d"
229  //if trigger field info is written in form "M>=4" etc. etc.
230  //The last value read is kept (fTrigger), so that, if no trigger value is given for the current line,
231  //but one was given on a previous line, we assume that the previous value is valid until a new one is given.
232  //A value "-1" means no trigger has been set for any line in the file so far.
233 
234  Int_t trig = fLineReader->GetTrigger(field, fmt);
235  if (trig < 0 && fLineReader->HasFieldValue(field))
236  cout << "...apparently on line " << GetRLLineNumber() << endl;
237  return (trig < 0 ? fTrigger : fTrigger = trig);
238 }
239 
240 
int Int_t
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
char * Form(const char *fmt,...)
Utitlity base class for reading INDRA runlist files.
Bool_t OpenRLFile(const Char_t *)
void ReadRunList(const Char_t *name="")
Int_t GetRunListTrigger(const Char_t *field, const Char_t *fmt)
Base class for reading runlists for experiments ,.
Definition: KVRunListLine.h:25
void Info(const char *location, const char *va_(fmt),...)
void Error(const char *location, const char *va_(fmt),...)