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

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