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

Last change on this file since 9492 was 8963, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 7.3 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 (eg 'Drive' will add 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 return kFALSE;
125
126 if (GetReport(help->GetName()))
127 {
128 *fLog << warn << "WARNING - Report with Identifier '";
129 *fLog << help->GetName() << "' already added to the list... ";
130 *fLog << "ignored." << endl;
131 delete help;
132 return kFALSE;
133 }
134
135 fList->Add(help);
136 return kTRUE;
137}
138
139// --------------------------------------------------------------------------
140//
141// Call SetupReading for all MReportHelp objects scheduled.
142// Try to open the file and check the file header.
143//
144Int_t MReportFileRead::PreProcess(MParList *pList)
145{
146 fNumLine = 0;
147
148 // Add the MReport instances first to the paramter list
149 // so that SetupReading can find them if needed
150 fList->R__FOR_EACH(MReportHelp, AddToList)(*pList);
151
152 // Setup reading
153 TIter Next(fList);
154 MReportHelp *help=0;
155 while ((help=(MReportHelp*)Next()))
156 if (!help->SetupReading(*pList))
157 return kFALSE;
158
159 //
160 // open the input stream
161 // first of all check if opening the file in the constructor was
162 // successfull
163 //
164 fIn->open(fFileName);
165 if (!(*fIn))
166 {
167 *fLog << err << "Cannot open file " << fFileName << ": ";
168 *fLog << strerror(errno) << endl;
169 return kFALSE;
170 }
171
172 if (TestBit(kHasNoHeader))
173 return kTRUE;
174
175 fNumLine = CheckFileHeader();
176 return fNumLine>0;
177}
178
179// --------------------------------------------------------------------------
180//
181// Read the file line by line as long as a matching MReport* class is found.
182// In this case call its interpreter (Interprete()) and remove the identifier
183// first (XYZ-REPORT)
184//
185Int_t MReportFileRead::Process()
186{
187 TString str;
188
189 MReportHelp *rep=NULL;
190 while (!GetReport(rep))
191 {
192 str.ReadLine(*fIn);
193 if (!*fIn)
194 {
195 *fLog << dbg << "EOF detected." << endl;
196 return kFALSE;
197 }
198
199 fNumLine++;
200
201 const Int_t pos = str.First(' ');
202 if (pos<=0)
203 continue;
204
205 rep = GetReportHelp(str(0,pos));
206 if (GetReport(rep))
207 str.Remove(0, pos);
208 }
209
210 const Int_t rc = rep->Interprete(str, fStart, fStop, fVersion);
211
212 switch (rc)
213 {
214 case kFALSE:
215 *fLog << err << "ERROR - Interpreting '" << rep->GetName() << "' failed (l." << fNumLine << ", V" << fVersion << ")... abort." << endl;
216 break;
217 case kCONTINUE:
218 *fLog << warn << "WARNING - Interpreting '" << rep->GetName() << "' failed (l." << fNumLine << ", V" << fVersion << ")... skipped." << endl;
219 break;
220 case -1: // This is the special case: out of time limit
221 return kCONTINUE;
222 }
223
224 return rc;
225}
226
227// --------------------------------------------------------------------------
228//
229// Close the file and print some execution statistics
230//
231Int_t MReportFileRead::PostProcess()
232{
233 fIn->close();
234
235 if (!GetNumExecutions())
236 return kTRUE;
237
238 *fLog << inf << endl;
239 *fLog << GetDescriptor() << " statistics:" << endl;
240 *fLog << dec << setfill(' ');
241
242 TIter Next(fList);
243 MReportHelp *rep=0;
244
245 while ((rep=(MReportHelp*)Next()))
246 {
247 *fLog << inf;
248 *fLog << " " << setw(7) << rep->GetNumReports() << " (";
249 *fLog << setw(3) << (int)(100.*rep->GetNumReports()/GetNumExecutions());
250 *fLog << "%): " << rep->GetName() << endl;
251
252 if (rep->GetNumSkipped()==0)
253 continue;
254
255 *fLog << warn;
256 *fLog << " " << setw(7) << rep->GetNumSkipped() << " (";
257 *fLog << setw(3) << (int)(100.*rep->GetNumSkipped()/GetNumExecutions());
258 *fLog << "%): " << rep->GetName() << " skipped!" << endl;
259 }
260
261 return kTRUE;
262}
Note: See TracBrowser for help on using the repository browser.