source: trunk/MagicSoft/Mars/mreport/MReportFileRead.cc@ 3832

Last change on this file since 3832 was 3568, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 8.7 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 11/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MReportFileRead
28//
29// Task to read the central control report file. For more information see
30// the base class of all reports MReport.
31//
32// To add a report which should be read use AddToList.
33//
34// eg. AddToList("Drive") will assume the existance of a class called
35// MReportDrive. It will create such an object automatically. It will
36// send all lines starting with 'MReportDrive::fIndetifier-REPORT'
37// to this class.
38//
39//////////////////////////////////////////////////////////////////////////////
40#include "MReportFileRead.h"
41
42#include <errno.h>
43#include <fstream>
44
45#include <TRegexp.h>
46#include <THashTable.h>
47
48#include "MLog.h"
49#include "MLogManip.h"
50
51#include "MParList.h"
52#include "MReportHelp.h"
53
54ClassImp(MReportFileRead);
55
56using namespace std;
57
58const TString MReportFileRead::gsReportHeader ="[CC Report File]";
59const TString MReportFileRead::gsVersionPrefix="Arehucas Version Number";
60
61// --------------------------------------------------------------------------
62//
63// Default constructor. It tries to open the given file and creates a
64// THashTable which allows faster access to the MReport* objects.
65//
66MReportFileRead::MReportFileRead(const char *fname, const char *name, const char *title)
67 : fFileName(fname), fIn(NULL)
68{
69 fName = name ? name : "MReportFileRead";
70 fTitle = title ? title : "Read task to read Central Control report files";
71
72 fIn = new ifstream;
73 fList = new THashTable(1,1);
74 fList->SetOwner();
75}
76
77// --------------------------------------------------------------------------
78//
79// Destructor. Delete input stream and hash table.
80//
81MReportFileRead::~MReportFileRead()
82{
83 delete fIn;
84 delete fList;
85}
86
87// --------------------------------------------------------------------------
88//
89// Wrapper. Returns the MReportHelp with the given identifier from the
90// hash table.
91//
92MReportHelp *MReportFileRead::GetReportHelp(const TString &str) const
93{
94 return (MReportHelp*)fList->FindObject(str);
95}
96
97// --------------------------------------------------------------------------
98//
99// Wrapper. Returns the MReport stored in the given MReportHelp
100//
101MReport *MReportFileRead::GetReport(MReportHelp *help) const
102{
103 return help ? help->GetReport() : 0;
104}
105
106// --------------------------------------------------------------------------
107//
108// Wrapper. Returns the MReport stored in the MReportHelp given by its
109// identifier.
110//
111MReport *MReportFileRead::GetReport(const TString &str) const
112{
113 return GetReport(GetReportHelp(str));
114}
115
116// --------------------------------------------------------------------------
117//
118// Add a new MReport* to the list (eg 'Drive' will add MReportDrive)
119// For convinience the object is created as a MReportHelp object.
120//
121Bool_t MReportFileRead::AddToList(const char *name) const
122{
123 MReportHelp *help = new MReportHelp(name, fLog);
124
125 if (!help->GetReport())
126 return kFALSE;
127
128 if (GetReport(help->GetName()))
129 {
130 *fLog << warn << "WARNING - Report with Identifier '";
131 *fLog << help->GetName() << "' already added to the list... ";
132 *fLog << "ignored." << endl;
133 delete help;
134 return kFALSE;
135 }
136
137 fList->Add(help);
138 return kTRUE;
139}
140
141// --------------------------------------------------------------------------
142//
143// Check whether the file header corresponds to a central control file
144// header and check for the existance of a correct version number.
145// The version number may later be used to be able to read different
146// file versions
147//
148Int_t MReportFileRead::CheckFileHeader() const
149{
150 Int_t line = 0;
151
152 TString str;
153 str.ReadLine(*fIn); // Read to EOF or newline
154 if (str != gsReportHeader)
155 {
156 *fLog << err << "ERROR - First line doesn't match '" << gsReportHeader <<"' ";
157 *fLog << "in file '" << fFileName << "'"<<endl;
158 return line;
159 }
160 line++;
161
162 str.ReadLine(*fIn); // Read to EOF or newline
163 if (!str.BeginsWith(gsVersionPrefix))
164 {
165 *fLog << err << "ERROR - Version prefix '" << gsVersionPrefix <<"' ";
166 *fLog << "not found in second line of file '" << fFileName << "'"<<endl;
167 return line;
168 }
169 line++;
170
171 str.Remove(0, gsVersionPrefix.Length());
172 str = str.Strip(TString::kBoth);
173
174 TString ver = str(TRegexp("^[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]$"));
175 if (ver.IsNull())
176 {
177 *fLog << err << "ERROR - Version string '" << str <<"' doesn't ";
178 *fLog << "match regular expression." << endl;
179 return line;
180 }
181
182 *fLog << dbg << "Report File version: <" << ver << ">" << endl;
183
184 return line;
185}
186
187// --------------------------------------------------------------------------
188//
189// Call SetupReading for all MReportHelp objects scheduled.
190// Try to open the file and check the file header.
191//
192Int_t MReportFileRead::PreProcess(MParList *pList)
193{
194 //MTime *time = (MTime*)pList->FindCreateObj("MTime");
195 //if (!time)
196 // return kFALSE;
197 fNumLine = 0;
198
199 TIter Next(fList);
200 MReportHelp *help=0;
201 while ((help=(MReportHelp*)Next()))
202 if (!help->SetupReading(*pList))
203 return kFALSE;
204
205 fList->ForEach(MReportHelp, AddToList)(*pList);
206
207 //
208 // open the input stream
209 // first of all check if opening the file in the constructor was
210 // successfull
211 //
212 fIn->open(fFileName);
213 if (!(*fIn))
214 {
215 *fLog << err << "Cannot open file " << fFileName << ": ";
216 *fLog << strerror(errno) << endl;
217 return kFALSE;
218 }
219
220 if (TestBit(kHasNoHeader))
221 return kTRUE;
222
223 const Int_t n = CheckFileHeader();
224 fNumLine += n;
225 return n==2;
226}
227
228// --------------------------------------------------------------------------
229//
230// Read the file line by line as long as a matching MReport* class is found.
231// In this case call its interpreter (Interprete()) and remove the identifier
232// first (XYZ-REPORT)
233//
234Int_t MReportFileRead::Process()
235{
236 TString str;
237
238 MReportHelp *rep=NULL;
239 while (!GetReport(rep))
240 {
241 str.ReadLine(*fIn);
242 if (!*fIn)
243 {
244 *fLog << dbg << "EOF detected." << endl;
245 return kFALSE;
246 }
247
248 fNumLine++;
249
250 const Int_t pos = str.First(' ');
251 if (pos<=0)
252 continue;
253
254 rep = GetReportHelp(str(0,pos));
255 if (GetReport(rep))
256 str.Remove(0, pos);
257 }
258
259 const Int_t rc = rep->Interprete(str, fStart, fStop);
260
261 switch (rc)
262 {
263 case kFALSE:
264 *fLog << err << "ERROR - Interpretation of '" << rep->GetName() << "' failed (Line #" << fNumLine << ")... abort." << endl;
265 break;
266 case kCONTINUE:
267 *fLog << warn << "WARNING - Interpretation of '" << rep->GetName() << "' failed (Line #" << fNumLine << ")... skipped." << endl;
268 break;
269 case -1: // This is the special case: out of time limit
270 return kCONTINUE;
271 }
272
273 return rc;
274}
275
276// --------------------------------------------------------------------------
277//
278// Close the file and print some execution statistics
279//
280Int_t MReportFileRead::PostProcess()
281{
282 fIn->close();
283
284 if (!GetNumExecutions())
285 return kTRUE;
286
287 *fLog << inf << endl;
288 *fLog << GetDescriptor() << " statistics:" << endl;
289 *fLog << dec << setfill(' ');
290
291 TIter Next(fList);
292 MReportHelp *rep=0;
293
294 while ((rep=(MReportHelp*)Next()))
295 {
296 *fLog << inf;
297 *fLog << " " << setw(7) << rep->GetNumReports() << " (";
298 *fLog << setw(3) << (int)(100.*rep->GetNumReports()/GetNumExecutions());
299 *fLog << "%): " << rep->GetName() << endl;
300
301 if (rep->GetNumSkipped()==0)
302 continue;
303
304 *fLog << warn;
305 *fLog << " " << setw(7) << rep->GetNumSkipped() << " (";
306 *fLog << setw(3) << (int)(100.*rep->GetNumSkipped()/GetNumExecutions());
307 *fLog << "%): " << rep->GetName() << " skipped!" << endl;
308 }
309
310 return kTRUE;
311}
Note: See TracBrowser for help on using the repository browser.