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

Last change on this file since 2837 was 2814, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 8.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// 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 <fstream>
43
44#include <TRegexp.h>
45#include <THashTable.h>
46
47#include "MLog.h"
48#include "MLogManip.h"
49
50#include "MParList.h"
51#include "MReportHelp.h"
52
53ClassImp(MReportFileRead);
54
55using namespace std;
56
57const TString MReportFileRead::gsReportHeader ="[CC Report File]";
58const TString MReportFileRead::gsVersionPrefix="Arehucas Version Number";
59
60// --------------------------------------------------------------------------
61//
62// Default constructor. It tries to open the given file and creates a
63// THashTable which allows faster access to the MReport* objects.
64//
65MReportFileRead::MReportFileRead(const char *fname, const char *name, const char *title)
66 : fFileName(fname), fIn(NULL)
67{
68 fName = name ? name : "MReportFileRead";
69 fTitle = title ? title : "Read task to read Central Control report files";
70
71 fIn = new ifstream;
72 fList = new THashTable(1,1);
73 fList->SetOwner();
74}
75
76// --------------------------------------------------------------------------
77//
78// Destructor. Delete input stream and hash table.
79//
80MReportFileRead::~MReportFileRead()
81{
82 delete fIn;
83 delete fList;
84}
85
86// --------------------------------------------------------------------------
87//
88// Wrapper. Returns the MReportHelp with the given identifier from the
89// hash table.
90//
91MReportHelp *MReportFileRead::GetReportHelp(const TString &str) const
92{
93 return (MReportHelp*)fList->FindObject(str);
94}
95
96// --------------------------------------------------------------------------
97//
98// Wrapper. Returns the MReport stored in the given MReportHelp
99//
100MReport *MReportFileRead::GetReport(MReportHelp *help) const
101{
102 return help ? help->GetReport() : 0;
103}
104
105// --------------------------------------------------------------------------
106//
107// Wrapper. Returns the MReport stored in the MReportHelp given by its
108// identifier.
109//
110MReport *MReportFileRead::GetReport(const TString &str) const
111{
112 return GetReport(GetReportHelp(str));
113}
114
115// --------------------------------------------------------------------------
116//
117// Add a new MReport* to the list (eg 'Drive' will add MReportDrive)
118// For convinience the object is created as a MReportHelp object.
119//
120Bool_t MReportFileRead::AddToList(const char *name) const
121{
122 MReportHelp *help = new MReportHelp(name, fLog);
123
124 if (!help->GetReport())
125 return kFALSE;
126
127 if (GetReport(help->GetName()))
128 {
129 *fLog << warn << "WARNING - Report with Identifier '";
130 *fLog << help->GetName() << "' already added to the list... ";
131 *fLog << "ignored." << endl;
132 delete help;
133 return kFALSE;
134 }
135
136 fList->Add(help);
137 return kTRUE;
138}
139
140// --------------------------------------------------------------------------
141//
142// Check whether the file header corresponds to a central control file
143// header and check for the existance of a correct version number.
144// The version number may later be used to be able to read different
145// file versions
146//
147Int_t MReportFileRead::CheckFileHeader() const
148{
149 Int_t line = 0;
150
151 TString str;
152 str.ReadLine(*fIn); // Read to EOF or newline
153 if (str != gsReportHeader)
154 {
155 *fLog << err << "ERROR - First line doesn't match '" << gsReportHeader <<"' ";
156 *fLog << "in file '" << fFileName << "'"<<endl;
157 return line;
158 }
159 line++;
160
161 str.ReadLine(*fIn); // Read to EOF or newline
162 if (!str.BeginsWith(gsVersionPrefix))
163 {
164 *fLog << err << "ERROR - Version prefix '" << gsVersionPrefix <<"' ";
165 *fLog << "not found in second line of file '" << fFileName << "'"<<endl;
166 return line;
167 }
168 line++;
169
170 str.Remove(0, gsVersionPrefix.Length());
171 str = str.Strip(TString::kBoth);
172
173 TString ver = str(TRegexp("^[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]$"));
174 if (ver.IsNull())
175 {
176 *fLog << err << "ERROR - Version string '" << str <<"' doesn't ";
177 *fLog << "match regular expression." << endl;
178 return line;
179 }
180
181 *fLog << dbg << "Report File version: <" << ver << ">" << endl;
182
183 return line;
184}
185
186// --------------------------------------------------------------------------
187//
188// Call SetupReading for all MReportHelp objects scheduled.
189// Try to open the file and check the file header.
190//
191Int_t MReportFileRead::PreProcess(MParList *pList)
192{
193 //MTime *time = (MTime*)pList->FindCreateObj("MTime");
194 //if (!time)
195 // return kFALSE;
196 fNumLine = 0;
197
198 TIter Next(fList);
199 MReportHelp *help=0;
200 while ((help=(MReportHelp*)Next()))
201 if (!help->SetupReading(*pList))
202 return kFALSE;
203
204 fList->ForEach(MReportHelp, AddToList)(*pList);
205
206 //
207 // open the input stream
208 // first of all check if opening the file in the constructor was
209 // successfull
210 //
211 fIn->open(fFileName);
212 if (!(*fIn))
213 {
214 *fLog << err << "Error: Cannot open file '" << fFileName << "'" << endl;
215 return kFALSE;
216 }
217 if (TestBit(kHasNoHeader))
218 return kTRUE;
219
220 const Int_t n = CheckFileHeader();
221 fNumLine += n;
222 return n==2;
223}
224
225// --------------------------------------------------------------------------
226//
227// Read the file line by line as long as a matching MReport* class is found.
228// In this case call its interpreter (Interprete()) and remove the identifier
229// first (XYZ-REPORT)
230//
231Int_t MReportFileRead::Process()
232{
233 TString str;
234
235 MReportHelp *rep=NULL;
236 while (!GetReport(rep))
237 {
238 str.ReadLine(*fIn);
239 if (!*fIn)
240 {
241 *fLog << dbg << "EOF detected." << endl;
242 return kFALSE;
243 }
244
245 fNumLine++;
246
247 const Int_t pos = str.First(' ');
248 if (pos<=0)
249 continue;
250
251 rep = GetReportHelp(str(0,pos));
252 if (GetReport(rep))
253 str.Remove(0, pos);
254 }
255
256 const Int_t rc = rep->Interprete(str, fStart, fStop);
257 if (rc==kFALSE)
258 {
259 *fLog << err << "ERROR - Interpretation of '" << rep->GetName() << "' failed (Line #" << fNumLine << ")" << endl;
260 return kFALSE;
261 }
262
263 return rc;
264}
265
266// --------------------------------------------------------------------------
267//
268// Close the file and print some execution statistics
269//
270Int_t MReportFileRead::PostProcess()
271{
272 fIn->close();
273
274 if (!GetNumExecutions())
275 return kTRUE;
276
277 *fLog << inf << endl;
278 *fLog << GetDescriptor() << " statistics:" << endl;
279 *fLog << dec << setfill(' ');
280
281 TIter Next(fList);
282 MReportHelp *rep=0;
283
284 while ((rep=(MReportHelp*)Next()))
285 {
286 *fLog << " " << setw(7) << rep->GetNumReports() << " (";
287 *fLog << setw(3) << (int)(100.*rep->GetNumReports()/GetNumExecutions());
288 *fLog << "%): " << rep->GetName() << endl;
289 }
290
291 return kTRUE;
292}
Note: See TracBrowser for help on using the repository browser.