source: trunk/MagicSoft/Mars/mfileio/MReadRflFile.cc@ 2134

Last change on this file since 2134 was 2134, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 9.5 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, 5/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MReadRflFile
28//
29// Reads a output file of the reflector program
30//
31/////////////////////////////////////////////////////////////////////////////
32#include "MReadRflFile.h"
33
34#include <fstream.h>
35
36#include <TSystem.h>
37
38#include "structures_rfl.h"
39
40#include "MParList.h"
41#include "MRflEvtData.h"
42
43#include "MLog.h"
44#include "MLogManip.h"
45
46ClassImp(MReadRflFile);
47
48// ------------------------------------------------
49const char PROGNAME[] = "reflector";
50#define SIZE_OF_FLAGS 13
51#define SIZE_OF_SIGNATURE 13
52#define FLAG_START_OF_RUN "\nSTART---RUN\n"
53#define FLAG_START_OF_EVENT "\nSTART-EVENT\n"
54#define FLAG_END_OF_EVENT "\nEND---EVENT\n"
55#define FLAG_END_OF_RUN "\nEND-----RUN\n"
56#define FLAG_END_OF_FILE "\nEND----FILE\n"
57#define FLAG_END_OF_STDIN "\nEND---STDIN\n"
58// ------------------------------------------------
59
60Bool_t MReadRflFile::FlagIsA(const char *s1, const char *flag)
61{
62 return strncmp(s1, flag, SIZE_OF_FLAGS)==0;
63}
64
65Bool_t MReadRflFile::ReadEvtHeader()
66{
67 if (fCurrentVersion <= 0.5)
68 {
69 RflEventHeader_old revth;
70 fIn->read(&revth, sizeof(RflEventHeader_old));
71 return (bool)*fIn;
72 }
73 else
74 {
75 RflEventHeader revth_new;
76 fIn->read(&revth_new, sizeof(RflEventHeader));
77 return (bool)*fIn;
78 }
79}
80
81enum {
82 kError,
83 kEndOfFile,
84 kStartOfRun,
85 kEndOfRun,
86 kStartOfEvt,
87 kEndOfEvt,
88 kUndefined
89};
90
91
92int MReadRflFile::ReadFlag()
93{
94 char flag[SIZE_OF_FLAGS];
95 fIn->read(flag, SIZE_OF_FLAGS);
96
97 if (!fIn)
98 return kError;
99
100 //*fLog << "<" << TString(&flag[1], 11) << ">" <<endl;
101
102 if (FlagIsA(flag, FLAG_END_OF_FILE))
103 return kEndOfFile;
104 if (FlagIsA(flag, FLAG_END_OF_RUN))
105 return kEndOfRun;
106 if (FlagIsA(flag, FLAG_START_OF_RUN))
107 return kStartOfRun;
108 if (FlagIsA(flag, FLAG_END_OF_EVENT))
109 return kEndOfEvt;
110 if (FlagIsA(flag, FLAG_START_OF_EVENT))
111 return kStartOfEvt;
112
113 return kUndefined;
114}
115
116Bool_t MReadRflFile::ReadEvtData()
117{
118 Bool_t rc = kFALSE;
119 while (1)
120 {
121 cphoton data; // FIRST READ "START OF EVENT"
122 fIn->read((char*)&data, SIZE_OF_FLAGS);
123 if (!*fIn)
124 break;
125
126 if (FlagIsA((char*)&data, FLAG_END_OF_EVENT))
127 {
128 rc = kTRUE;
129 break;
130 }
131
132 fIn->read((char*)&data+SIZE_OF_FLAGS, sizeof(cphoton)-SIZE_OF_FLAGS);
133 if (!*fIn)
134 break;
135
136 MRflSinglePhoton &ph = fEvt->GetNewPhoton();
137 ph.SetXY(data.x*10, data.y*10);
138 ph.SetCosUV(data.u, data.v);
139 ph.SetTime(data.t);
140 ph.SetHeight(data.h);
141 ph.SetInclinationAngle(data.phi);
142 }
143
144 fEvt->FixSize();
145 return rc;
146}
147
148Int_t MReadRflFile::EvalFlag()
149{
150 const Int_t flag = ReadFlag();
151
152 switch (flag)
153 {
154 case kEndOfFile:
155 fCurrentVersion = ReadVersion();
156 if (fCurrentVersion<0)
157 {
158 *fLog << inf << "Found end of file...Everything OK." << endl;
159 break;
160 }
161
162 *fLog << warn << "Found flag of end of file, but file goes on..." << endl;
163 if (ReadFlag()<0)
164 return kError;
165 /* FALLTHROU */
166 case kStartOfRun:
167 if (fCurrentVersion>0.5)
168 {
169 RflRunHeader rrunh;
170 fIn->read(&rrunh, sizeof(RflRunHeader));
171 if (*fIn)
172 {
173 *fLog << inf << "FIXME: Call ReInit" << endl;
174 break;
175 }
176
177 *fLog << err << "Error! found end of file... But no EOF flag. Exiting." << endl;
178 return kError;
179 }
180 return kUndefined;
181
182 case kStartOfEvt:
183 case kEndOfRun:
184 break;
185
186 case kError:
187 *fLog << err << "ERROR - Flag 'error'" << endl;
188 return kError;
189
190 case kUndefined:
191 *fLog << err << "ERROR - Flag 'undefined'" << endl;
192 return kError;
193
194 default:
195 *fLog << err << "ERROR - Unhandled flag" << endl;
196 return kError;
197
198 }
199 return flag;
200}
201
202Bool_t MReadRflFile::Process()
203{
204 for (;;)
205 {
206 switch (EvalFlag())
207 {
208 case kError:
209 return kFALSE;
210
211 case kEndOfFile:
212 if (!OpenNextFile())
213 return kFALSE;
214 /* FALLTHROU */
215 case kStartOfRun:
216 case kEndOfRun:
217 continue;
218
219 case kStartOfEvt:
220 break;
221 }
222 break;
223 }
224
225 if (!ReadEvtHeader())
226 return kFALSE;
227
228 return ReadEvtData();
229}
230
231Bool_t MReadRflFile::PreProcess(MParList *plist)
232{
233 fEvt=(MRflEvtData*)plist->FindCreateObj("MRflEvtData");
234 if (!fEvt)
235 return kFALSE;
236
237 Rewind();
238
239 return OpenNextFile();
240}
241
242// --------------------------------------------------------------------------
243//
244// This opens the next file in the list and deletes its name from the list.
245//
246Bool_t MReadRflFile::OpenNextFile()
247{
248 //
249 // open the input stream and check if it is really open (file exists?)
250 //
251 if (fIn)
252 delete fIn;
253 fIn = NULL;
254
255 //
256 // Check for the existence of a next file to read
257 //
258 if (fNumFile >= (UInt_t)fFileNames->GetSize())
259 {
260 *fLog << inf << GetDescriptor() << ": No unread files anymore..." << endl;
261 return kFALSE;
262 }
263
264 TNamed *file = (TNamed*)fFileNames->At(fNumFile);
265
266 //TNamed *file = (TNamed*)fFileNames->GetFirst();
267 //if (!file)
268 // return kFALSE;
269
270 //
271 // open the file which is the first one in the chain
272 //
273 const TString name = file->GetName();
274
275 const char *expname = gSystem->ExpandPathName(name);
276 const TString fname(expname);
277 delete [] expname;
278
279 //
280 // Remove this file from the list of pending files
281 //
282 //fFileNames->Remove(file);
283
284 *fLog << inf << "Open file: '" << name << "'" << endl;
285
286 fIn = new ifstream(name);
287 if (!*fIn)
288 {
289 cout << "Error openeng file " << name << "." << endl;
290 return kFALSE;
291 }
292
293 *fLog << inf;
294 fLog->Separator(name);
295
296 fCurrentVersion = ReadVersion();
297 if (fCurrentVersion<0)
298 {
299 cout << "ERROR reading signature." << endl;
300 return kFALSE;
301 }
302 cout << "Version " << fCurrentVersion << endl << endl;
303
304 fNumFile++;
305 return kTRUE;
306}
307
308/****************************************************/
309
310float MReadRflFile::ReadVersion()
311{
312 char sign[20];
313 fIn->read(sign, SIZE_OF_SIGNATURE);
314 if (!*fIn)
315 return -1;
316
317 if (strncmp(sign, PROGNAME, strlen(PROGNAME)) != 0)
318 {
319 /* For the ascii tail of the file! : */
320 if (strncmp(sign, "\n############", SIZE_OF_SIGNATURE))
321 cout << "ERROR: Signature of .rfl file is not correct: " << sign << endl;
322
323 return -1;
324 }
325
326 float version;
327 sscanf(sign, "%*s %f", &version);
328
329 //If the version is < 0.6 the signature had one more byte
330 if (version < 0.6)
331 *fIn >> sign[0];
332
333 return version;
334}
335
336
337Bool_t MReadRflFile::PostProcess()
338{
339 return kTRUE;
340}
341
342// --------------------------------------------------------------------------
343//
344// Default constructor. Creates an array which stores the file names of
345// the files which should be read. If a filename is given it is added
346// to the list.
347//
348MReadRflFile::MReadRflFile(const char *fname, const char *name,
349 const char *title) : fIn(NULL), fEntries(0)
350{
351 fName = name ? name : "MRead";
352 fTitle = title ? title : "Reads a Reflector output file";
353
354 //
355 // remember file name for opening the file in the preprocessor
356 //
357 fFileNames = new TList;
358 fFileNames->SetOwner();
359
360 if (fname)
361 AddFile(fname);
362}
363
364// --------------------------------------------------------------------------
365//
366// Delete the filename list and the input stream if one exists.
367//
368MReadRflFile::~MReadRflFile()
369{
370 delete fFileNames;
371 if (fIn)
372 delete fIn;
373}
374
375// --------------------------------------------------------------------------
376//
377// Add this file as the last entry in the chain
378//
379void MReadRflFile::AddFile(const char *txt)
380{
381 const char *name = gSystem->ExpandPathName(txt);
382
383 TString fname(name);
384 delete [] name;
385/*
386 if (!CheckHeader(fname))
387 {
388 *fLog << warn << "WARNING - Problem reading header... ignored." << endl;
389 return;
390 }
391
392 const Int_t n = GetNumEvents(fname);
393 if (n==0)
394 {
395 *fLog << warn << "WARNING - File contains no data... ignored." << endl;
396 return;
397 }
398
399 fEntries += n;
400
401 *fLog << inf << "File " << txt << " contains " << n << " events (Total=" << fEntries << ")" << endl;
402*/
403 fFileNames->AddLast(new TNamed(txt, ""));
404}
405
Note: See TracBrowser for help on using the repository browser.