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

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