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

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