source: trunk/Mars/mreport/MReportFileRead.cc@ 19727

Last change on this file since 19727 was 10281, checked in by tbretz, 14 years ago
File size: 7.4 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
58// --------------------------------------------------------------------------
59//
60// Default constructor. It tries to open the given file and creates a
61// THashTable which allows faster access to the MReport* objects.
62//
63MReportFileRead::MReportFileRead(const char *fname, const char *name, const char *title)
64 : fFileName(fname), fVersion(-1), fIn(NULL)
65{
66 fName = name ? name : "MReportFileRead";
67 fTitle = title ? title : "Read task to read general report files";
68
69 fIn = new ifstream;
70
71 fList = new THashTable(1,1);
72 fList->SetOwner();
73}
74
75// --------------------------------------------------------------------------
76//
77// Destructor. Delete input stream and hash table.
78//
79MReportFileRead::~MReportFileRead()
80{
81 delete fIn;
82 delete fList;
83}
84
85// --------------------------------------------------------------------------
86//
87// Wrapper. Returns the MReportHelp with the given identifier from the
88// hash table.
89//
90MReportHelp *MReportFileRead::GetReportHelp(const TString &str) const
91{
92 return static_cast<MReportHelp*>(fList->FindObject(str));
93}
94
95// --------------------------------------------------------------------------
96//
97// Wrapper. Returns the MReport stored in the given MReportHelp
98//
99MReport *MReportFileRead::GetReport(MReportHelp *help) const
100{
101 return help ? help->GetReport() : 0;
102}
103
104// --------------------------------------------------------------------------
105//
106// Wrapper. Returns the MReport stored in the MReportHelp given by its
107// identifier.
108//
109MReport *MReportFileRead::GetReport(const TString &str) const
110{
111 return GetReport(GetReportHelp(str));
112}
113
114// --------------------------------------------------------------------------
115//
116// Add a new MReport* to the list. The name is the class name (eg MReportDrive)
117// For convinience the object is created as a MReportHelp object.
118//
119Bool_t MReportFileRead::AddToList(const char *name) const
120{
121 MReportHelp *help = new MReportHelp(name, fLog);
122
123 if (!help->GetReport())
124 {
125 delete help;
126 return kFALSE;
127 }
128
129 if (GetReport(help->GetName()))
130 {
131 *fLog << warn << "WARNING - Report with Identifier '";
132 *fLog << help->GetName() << "' already added to the list... ";
133 *fLog << "ignored." << endl;
134 delete help;
135 return kFALSE;
136 }
137
138 fList->Add(help);
139 return kTRUE;
140}
141
142// --------------------------------------------------------------------------
143//
144// Call SetupReading for all MReportHelp objects scheduled.
145// Try to open the file and check the file header.
146//
147Int_t MReportFileRead::PreProcess(MParList *pList)
148{
149 fNumLine = 0;
150
151 // Add the MReport instances first to the paramter list
152 // so that SetupReading can find them if needed
153 fList->R__FOR_EACH(MReportHelp, AddToList)(*pList);
154
155 // Setup reading
156 TIter Next(fList);
157 MReportHelp *help=0;
158 while ((help=(MReportHelp*)Next()))
159 if (!help->SetupReading(*pList))
160 return kFALSE;
161
162 //
163 // open the input stream
164 // first of all check if opening the file in the constructor was
165 // successfull
166 //
167 fIn->open(fFileName);
168 if (!(*fIn))
169 {
170 *fLog << err << "Cannot open file " << fFileName << ": ";
171 *fLog << strerror(errno) << endl;
172 return kFALSE;
173 }
174
175 if (TestBit(kHasNoHeader))
176 return kTRUE;
177
178 fNumLine = CheckFileHeader();
179 return fNumLine>0;
180}
181
182// --------------------------------------------------------------------------
183//
184// Read the file line by line as long as a matching MReport* class is found.
185// In this case call its interpreter (Interprete()) and remove the identifier
186// first (XYZ-REPORT)
187//
188Int_t MReportFileRead::Process()
189{
190 TString str;
191
192 MReportHelp *rep=NULL;
193 while (!GetReport(rep))
194 {
195 str.ReadLine(*fIn);
196 if (!*fIn)
197 {
198 *fLog << dbg << "EOF detected." << endl;
199 return kFALSE;
200 }
201
202 fNumLine++;
203
204 const Int_t pos = str.First(' ');
205 if (pos<=0)
206 continue;
207
208 // Check for MReport{str(0,pos)}
209 rep = GetReportHelp(str(0,pos));
210
211 // Remove this part from the string
212 if (GetReport(rep))
213 str.Remove(0, pos);
214 }
215
216 const Int_t rc = rep->Interprete(str, fStart, fStop, fVersion);
217
218 switch (rc)
219 {
220 case kFALSE:
221 *fLog << err << "ERROR - Interpreting '" << rep->GetName() << "' failed (l." << fNumLine << ", V" << fVersion << ")... abort." << endl;
222 break;
223 case kCONTINUE:
224 *fLog << warn << "WARNING - Interpreting '" << rep->GetName() << "' failed (l." << fNumLine << ", V" << fVersion << ")... skipped." << endl;
225 break;
226 case -1: // This is the special case: out of time limit
227 return kCONTINUE;
228 }
229
230 return rc;
231}
232
233// --------------------------------------------------------------------------
234//
235// Close the file and print some execution statistics
236//
237Int_t MReportFileRead::PostProcess()
238{
239 fIn->close();
240
241 if (!GetNumExecutions())
242 return kTRUE;
243
244 *fLog << inf << endl;
245 *fLog << GetDescriptor() << " statistics:" << endl;
246 *fLog << dec << setfill(' ');
247
248 TIter Next(fList);
249 MReportHelp *rep=0;
250
251 while ((rep=(MReportHelp*)Next()))
252 {
253 *fLog << inf;
254 *fLog << " " << setw(7) << rep->GetNumReports() << " (";
255 *fLog << setw(3) << (int)(100.*rep->GetNumReports()/GetNumExecutions());
256 *fLog << "%): " << rep->GetName() << endl;
257
258 if (rep->GetNumSkipped()==0)
259 continue;
260
261 *fLog << warn;
262 *fLog << " " << setw(7) << rep->GetNumSkipped() << " (";
263 *fLog << setw(3) << (int)(100.*rep->GetNumSkipped()/GetNumExecutions());
264 *fLog << "%): " << rep->GetName() << " skipped!" << endl;
265 }
266
267 return kTRUE;
268}
Note: See TracBrowser for help on using the repository browser.