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

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