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

Last change on this file since 2563 was 2556, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 9.1 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//////////////////////////////////////////////////////////////////////////////
30#include "MReportFileRead.h"
31
32#include <fstream>
33
34#include <TClass.h>
35#include <TRegexp.h>
36#include <THashTable.h>
37
38#include "MLog.h"
39#include "MLogManip.h"
40
41#include "MReport.h"
42#include "MParList.h"
43
44ClassImp(MReportFileRead);
45
46using namespace std;
47
48const TString MReportFileRead::gsReportHeader ="[CC Report File]";
49const TString MReportFileRead::gsVersionPrefix="Arehucas Version Number";
50
51class MReportHelp : public TObject
52{
53private:
54 MReport *fReport;
55 ULong_t fNumReports;
56
57public:
58 MReportHelp(const char *name, MLog *fLog) : fReport(NULL), fNumReports(0)
59 {
60 TClass *cls = gROOT->GetClass(name);
61 Int_t rc = 0;
62 if (!cls)
63 rc =1;
64 else
65 {
66 if (!cls->Property())
67 rc = 5;
68 if (!cls->Size())
69 rc = 4;
70 if (!cls->IsLoaded())
71 rc = 3;
72 if (!cls->HasDefaultConstructor())
73 rc = 2;
74 }
75
76 if (rc)
77 {
78 *fLog << err << dbginf << "Cannot create new instance of class '" << name << "': ";
79 switch (rc)
80 {
81 case 1:
82 *fLog << "gROOT->GetClass() returned NULL." << endl;
83 return;
84 case 2:
85 *fLog << "no default constructor." << endl;
86 return;
87 case 3:
88 *fLog << "not loaded." << endl;
89 return;
90 case 4:
91 *fLog << "zero size." << endl;
92 return;
93 case 5:
94 *fLog << "no property." << endl;
95 return;
96 }
97 }
98
99 //
100 // create the parameter container of the the given class type
101 //
102 fReport = (MReport*)cls->New();
103 }
104 ~MReportHelp() { if (fReport) delete fReport; }
105
106 const char *GetName() const { return fReport->GetIdentifier(); }
107 ULong_t GetNumReports() const { return fNumReports; }
108 ULong_t Hash() const { return fReport->GetIdentifier().Hash(); }
109 MReport *GetReport() { return fReport; }
110 //void SetTime(MTime *t) { fReport->SetTime(t); }
111 Bool_t Interprete(TString &str)
112 {
113 if (!fReport->Interprete(str))
114 return kFALSE;
115
116 fNumReports++;
117 return kTRUE;
118 }
119 Bool_t SetupReading(MParList &plist) { return fReport->SetupReading(plist); }
120 void AddToList(MParList &plist) { plist.AddToList(fReport); }
121};
122
123// --------------------------------------------------------------------------
124//
125// Default constructor. It tries to open the given file.
126//
127MReportFileRead::MReportFileRead(const char *fname, const char *name, const char *title)
128 : fFileName(fname), fIn(NULL)
129{
130 fName = name ? name : "MReportFileRead";
131 fTitle = title ? title : "Read task to read Central Control report files";
132
133 fIn = new ifstream;
134 fList = new THashTable(1,1);
135 fList->SetOwner();
136}
137
138// --------------------------------------------------------------------------
139//
140// Destructor. Delete input stream.
141//
142MReportFileRead::~MReportFileRead()
143{
144 delete fIn;
145 delete fList;
146}
147
148MReportHelp *MReportFileRead::GetReportHelp(const TString &str) const
149{
150 return (MReportHelp*)fList->FindObject(str);
151}
152
153MReport *MReportFileRead::GetReport(MReportHelp *help) const
154{
155 return help ? help->GetReport() : 0;
156}
157
158MReport *MReportFileRead::GetReport(const TString &str) const
159{
160 return GetReport(GetReportHelp(str));
161}
162
163Bool_t MReportFileRead::AddToList(const char *name) const
164{
165 MReportHelp *help = new MReportHelp(name, fLog);
166
167 MReport *rep = NULL;
168 if (!(rep=help->GetReport()))
169 return kFALSE;
170
171 if (GetReport(rep->GetIdentifier()))
172 {
173 *fLog << warn << "WARNING - Report with Identifier '";
174 *fLog << rep->GetIdentifier() << "' already added to the list... ";
175 *fLog << "ignored." << endl;
176 delete help;
177 return kFALSE;
178 }
179
180 fList->Add(help);
181 return kTRUE;
182}
183
184Bool_t MReportFileRead::CheckFileHeader() const
185{
186 TString str;
187 str.ReadLine(*fIn); // Read to EOF or newline
188 if (str != gsReportHeader)
189 {
190 *fLog << err << "ERROR - First line doesn't match '" << gsReportHeader <<"' ";
191 *fLog << "in file '" << fFileName << "'"<<endl;
192 return kFALSE;
193 }
194
195 str.ReadLine(*fIn); // Read to EOF or newline
196 if (!str.BeginsWith(gsVersionPrefix))
197 {
198 *fLog << err << "ERROR - Version prefix '" << gsVersionPrefix <<"' ";
199 *fLog << "not found in second line of file '" << fFileName << "'"<<endl;
200 return kFALSE;
201 }
202
203 str.Remove(0, gsVersionPrefix.Length());
204 str = str.Strip(TString::kBoth);
205
206 TString ver = str(TRegexp("^[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]$"));
207 if (ver.IsNull())
208 {
209 *fLog << err << "ERROR - Version string '" << str <<"' doesn't ";
210 *fLog << "match regular expression." << endl;
211 return kFALSE;
212 }
213
214 *fLog << dbg << "Report File version: <" << ver << ">" << endl;
215
216 return kTRUE;
217}
218
219// --------------------------------------------------------------------------
220//
221// The PreProcess of this task checks for the following containers in the
222// list:
223// MRawRunHeader <output> if not found it is created
224// MRawEvtHeader <output> if not found it is created
225// MRawEvtData <output> if not found it is created
226// MRawCrateArray <output> if not found it is created
227// MRawEvtTime <output> if not found it is created (MTime)
228//
229// If all containers are found or created the run header is read from the
230// binary file and printed. If the Magic-Number (file identification)
231// doesn't match we stop the eventloop.
232//
233// Now the EvtHeader and EvtData containers are initialized.
234//
235Int_t MReportFileRead::PreProcess(MParList *pList)
236{
237 //MTime *time = (MTime*)pList->FindCreateObj("MTime");
238 //if (!time)
239 // return kFALSE;
240
241 TIter Next(fList);
242 MReportHelp *help=0;
243 while ((help=(MReportHelp*)Next()))
244 if (!help->SetupReading(*pList))
245 return kFALSE;
246
247 fList->ForEach(MReportHelp, AddToList)(*pList);
248
249 //
250 // open the input stream
251 // first of all check if opening the file in the constructor was
252 // successfull
253 //
254 fIn->open(fFileName);
255 if (!(*fIn))
256 {
257 *fLog << err << "Error: Cannot open file '" << fFileName << "'" << endl;
258 return kFALSE;
259 }
260
261 return CheckFileHeader();
262}
263
264// --------------------------------------------------------------------------
265//
266// The Process reads one event from the binary file:
267// - The event header is read
268// - the run header is read
269// - all crate information is read
270// - the raw data information of one event is read
271//
272Int_t MReportFileRead::Process()
273{
274 TString str;
275
276 MReportHelp *rep=NULL;
277 while (!GetReport(rep))
278 {
279 str.ReadLine(*fIn);
280 if (!*fIn)
281 {
282 *fLog << dbg << "EOF detected." << endl;
283 return kFALSE;
284 }
285
286 const Int_t pos = str.First(' ');
287 if (pos<=0)
288 continue;
289
290 rep = GetReportHelp(str(0,pos));
291 if (GetReport(rep))
292 str.Remove(0, pos);
293 }
294
295 if (!rep->Interprete(str))
296 {
297 *fLog << err << "ERROR - Interpretation of '" << rep->GetName() << "' failed." << endl;
298 return kFALSE;
299 }
300
301 return kTRUE;
302}
303
304// --------------------------------------------------------------------------
305//
306// Close the file. Check whether the number of read events differs from
307// the number the file should containe (MRawRunHeader). Prints a warning
308// if it doesn't match.
309//
310Int_t MReportFileRead::PostProcess()
311{
312 fIn->close();
313
314 if (!GetNumExecutions())
315 return kTRUE;
316
317 *fLog << inf << endl;
318 *fLog << GetDescriptor() << " statistics:" << endl;
319 *fLog << dec << setfill(' ');
320
321 TIter Next(fList);
322 MReportHelp *rep=0;
323
324 while ((rep=(MReportHelp*)Next()))
325 {
326 *fLog << " " << setw(7) << rep->GetNumReports() << " (";
327 *fLog << setw(3) << (int)(100.*rep->GetNumReports()/GetNumExecutions());
328 *fLog << "%): " << rep->GetName() << endl;
329 }
330
331 return kTRUE;
332}
Note: See TracBrowser for help on using the repository browser.