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

Last change on this file since 2804 was 2800, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 7.6 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
86MReportHelp *MReportFileRead::GetReportHelp(const TString &str) const
87{
88 return (MReportHelp*)fList->FindObject(str);
89}
90
91MReport *MReportFileRead::GetReport(MReportHelp *help) const
92{
93 return help ? help->GetReport() : 0;
94}
95
96MReport *MReportFileRead::GetReport(const TString &str) const
97{
98 return GetReport(GetReportHelp(str));
99}
100
101// --------------------------------------------------------------------------
102//
103// Add a new MReport* to the list (eg 'Drive' will add MReportDrive)
104// For convinience the object is created as a MReportHelp object.
105//
106Bool_t MReportFileRead::AddToList(const char *name) const
107{
108 MReportHelp *help = new MReportHelp(name, fLog);
109
110 if (!help->GetReport())
111 return kFALSE;
112
113 if (GetReport(help->GetName()))
114 {
115 *fLog << warn << "WARNING - Report with Identifier '";
116 *fLog << help->GetName() << "' already added to the list... ";
117 *fLog << "ignored." << endl;
118 delete help;
119 return kFALSE;
120 }
121
122 fList->Add(help);
123 return kTRUE;
124}
125
126// --------------------------------------------------------------------------
127//
128// Check whether the file header corresponds to a central control file
129// header and check for the existance of a correct version number.
130// The version number may later be used to be able to read different
131// file versions
132//
133Int_t MReportFileRead::CheckFileHeader() const
134{
135 Int_t line = 0;
136
137 TString str;
138 str.ReadLine(*fIn); // Read to EOF or newline
139 if (str != gsReportHeader)
140 {
141 *fLog << err << "ERROR - First line doesn't match '" << gsReportHeader <<"' ";
142 *fLog << "in file '" << fFileName << "'"<<endl;
143 return line;
144 }
145 line++;
146
147 str.ReadLine(*fIn); // Read to EOF or newline
148 if (!str.BeginsWith(gsVersionPrefix))
149 {
150 *fLog << err << "ERROR - Version prefix '" << gsVersionPrefix <<"' ";
151 *fLog << "not found in second line of file '" << fFileName << "'"<<endl;
152 return line;
153 }
154 line++;
155
156 str.Remove(0, gsVersionPrefix.Length());
157 str = str.Strip(TString::kBoth);
158
159 TString ver = str(TRegexp("^[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]$"));
160 if (ver.IsNull())
161 {
162 *fLog << err << "ERROR - Version string '" << str <<"' doesn't ";
163 *fLog << "match regular expression." << endl;
164 return line;
165 }
166
167 *fLog << dbg << "Report File version: <" << ver << ">" << endl;
168
169 return line;
170}
171
172// --------------------------------------------------------------------------
173//
174// Call SetupReading for all MReportHelp objects scheduled.
175// Try to open the file and check the file header.
176//
177Int_t MReportFileRead::PreProcess(MParList *pList)
178{
179 //MTime *time = (MTime*)pList->FindCreateObj("MTime");
180 //if (!time)
181 // return kFALSE;
182 fNumLine = 0;
183
184 TIter Next(fList);
185 MReportHelp *help=0;
186 while ((help=(MReportHelp*)Next()))
187 if (!help->SetupReading(*pList))
188 return kFALSE;
189
190 fList->ForEach(MReportHelp, AddToList)(*pList);
191
192 //
193 // open the input stream
194 // first of all check if opening the file in the constructor was
195 // successfull
196 //
197 fIn->open(fFileName);
198 if (!(*fIn))
199 {
200 *fLog << err << "Error: Cannot open file '" << fFileName << "'" << endl;
201 return kFALSE;
202 }
203 if (TestBit(kHasNoHeader))
204 return kTRUE;
205
206 const Int_t n = CheckFileHeader();
207 fNumLine += n;
208 return n==2;
209}
210
211// --------------------------------------------------------------------------
212//
213// Read the file line by line as long as a matching MReport* class is found.
214// In this case call its interpreter (Interprete()) and remove the identifier
215// first (XYZ-REPORT)
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 fNumLine++;
232
233 const Int_t pos = str.First(' ');
234 if (pos<=0)
235 continue;
236
237 rep = GetReportHelp(str(0,pos));
238 if (GetReport(rep))
239 str.Remove(0, pos);
240 }
241
242 const Int_t rc = rep->Interprete(str, fStart, fStop);
243 if (rc==kFALSE)
244 {
245 *fLog << err << "ERROR - Interpretation of '" << rep->GetName() << "' failed (Line #" << fNumLine << ")" << endl;
246 return kFALSE;
247 }
248
249 return rc;
250}
251
252// --------------------------------------------------------------------------
253//
254// Close the file and print some execution statistics
255//
256Int_t MReportFileRead::PostProcess()
257{
258 fIn->close();
259
260 if (!GetNumExecutions())
261 return kTRUE;
262
263 *fLog << inf << endl;
264 *fLog << GetDescriptor() << " statistics:" << endl;
265 *fLog << dec << setfill(' ');
266
267 TIter Next(fList);
268 MReportHelp *rep=0;
269
270 while ((rep=(MReportHelp*)Next()))
271 {
272 *fLog << " " << setw(7) << rep->GetNumReports() << " (";
273 *fLog << setw(3) << (int)(100.*rep->GetNumReports()/GetNumExecutions());
274 *fLog << "%): " << rep->GetName() << endl;
275 }
276
277 return kTRUE;
278}
Note: See TracBrowser for help on using the repository browser.