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

Last change on this file since 6902 was 4898, checked in by tbretz, 20 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 fFileName = file->GetName();
303 const TString expname = fFileName;
304 gSystem->ExpandPathName(expname);
305
306 //
307 // Remove this file from the list of pending files
308 //
309 //fFileNames->Remove(file);
310
311 *fLog << inf << "Open file: '" << fFileName << "'" << endl;
312
313 fIn = new ifstream(expname);
314 if (!*fIn)
315 {
316 *fLog << err << "Cannot open file " << expname << ": ";
317 *fLog << strerror(errno) << endl;
318 return kFALSE;
319 }
320
321 *fLog << inf;
322 fLog->Separator(fFileName);
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.