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

Last change on this file since 8744 was 7804, checked in by tbretz, 18 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 fList = new THashTable(1,1);
71 fList->SetOwner();
72}
73
74// --------------------------------------------------------------------------
75//
76// Destructor. Delete input stream and hash table.
77//
78MReportFileRead::~MReportFileRead()
79{
80 delete fIn;
81 delete fList;
82}
83
84// --------------------------------------------------------------------------
85//
86// Wrapper. Returns the MReportHelp with the given identifier from the
87// hash table.
88//
89MReportHelp *MReportFileRead::GetReportHelp(const TString &str) const
90{
91 return (MReportHelp*)fList->FindObject(str);
92}
93
94// --------------------------------------------------------------------------
95//
96// Wrapper. Returns the MReport stored in the given MReportHelp
97//
98MReport *MReportFileRead::GetReport(MReportHelp *help) const
99{
100 return help ? help->GetReport() : 0;
101}
102
103// --------------------------------------------------------------------------
104//
105// Wrapper. Returns the MReport stored in the MReportHelp given by its
106// identifier.
107//
108MReport *MReportFileRead::GetReport(const TString &str) const
109{
110 return GetReport(GetReportHelp(str));
111}
112
113// --------------------------------------------------------------------------
114//
115// Add a new MReport* to the list (eg 'Drive' will add MReportDrive)
116// For convinience the object is created as a MReportHelp object.
117//
118Bool_t MReportFileRead::AddToList(const char *name) const
119{
120 MReportHelp *help = new MReportHelp(name, fLog);
121
122 if (!help->GetReport())
123 return kFALSE;
124
125 if (GetReport(help->GetName()))
126 {
127 *fLog << warn << "WARNING - Report with Identifier '";
128 *fLog << help->GetName() << "' already added to the list... ";
129 *fLog << "ignored." << endl;
130 delete help;
131 return kFALSE;
132 }
133
134 fList->Add(help);
135 return kTRUE;
136}
137
138// --------------------------------------------------------------------------
139//
140// Call SetupReading for all MReportHelp objects scheduled.
141// Try to open the file and check the file header.
142//
143Int_t MReportFileRead::PreProcess(MParList *pList)
144{
145 //MTime *time = (MTime*)pList->FindCreateObj("MTime");
146 //if (!time)
147 // return kFALSE;
148 fNumLine = 0;
149
150 TIter Next(fList);
151 MReportHelp *help=0;
152 while ((help=(MReportHelp*)Next()))
153 if (!help->SetupReading(*pList))
154 return kFALSE;
155
156 fList->R__FOR_EACH(MReportHelp, AddToList)(*pList);
157
158 //
159 // open the input stream
160 // first of all check if opening the file in the constructor was
161 // successfull
162 //
163 fIn->open(fFileName);
164 if (!(*fIn))
165 {
166 *fLog << err << "Cannot open file " << fFileName << ": ";
167 *fLog << strerror(errno) << endl;
168 return kFALSE;
169 }
170
171 if (TestBit(kHasNoHeader))
172 return kTRUE;
173
174 const Int_t n = CheckFileHeader();
175 fNumLine += n;
176 return n==2;
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.