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

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