source: trunk/Mars/mfileio/MReadRflFile.cc@ 17667

Last change on this file since 17667 was 9032, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 10.2 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-2008
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 "structures_rfl.h"
35
36#include "MLog.h"
37#include "MLogManip.h"
38
39#include "MZlib.h"
40
41#include "MParList.h"
42
43#include "MRflEvtData.h"
44#include "MRflEvtHeader.h"
45#include "MRflRunHeader.h"
46#include "MRflSinglePhoton.h"
47
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 }
78 else
79 {
80 RflEventHeader revth;
81 fIn->read((char*)&revth, sizeof(RflEventHeader));
82 fEvtHeader->SetEvtNumber((int)revth.EvtNumber);
83 fEvtHeader->SetEnergy((int)revth.Etotal);
84 fEvtHeader->SetMomentum(revth.p[0], revth.p[1], revth.p[2]);
85 fEvtHeader->SetCorePosition(revth.CorePos[0][0], revth.CorePos[1][0]);
86
87 fEvtHeader->SetHeightFirstInt((Float_t)revth.zFirstInt);
88
89 fEvtHeader->SetPhi((Float_t)revth.telescopePhi);
90 fEvtHeader->SetTheta((Float_t)revth.telescopeTheta);
91
92 fEvtHeader->SetNmax((Float_t)revth.longi_Nmax);
93 fEvtHeader->SetT0((Float_t)revth.longi_t0);
94 fEvtHeader->SetTmax((Float_t)revth.longi_tmax);
95 fEvtHeader->SetChi2((Float_t)revth.longi_chi2);
96
97 fEvtHeader->SetEFraction((Float_t)revth.elec_cph_fraction);
98 fEvtHeader->SetMFraction((Float_t)revth.muon_cph_fraction);
99 fEvtHeader->SetOFraction((Float_t)revth.other_cph_fraction);
100 }
101
102 return (bool)*fIn;
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 if (FlagIsA(flag, FLAG_END_OF_FILE))
114 return kEndOfFile;
115 if (FlagIsA(flag, FLAG_END_OF_RUN))
116 return kEndOfRun;
117 if (FlagIsA(flag, FLAG_START_OF_RUN))
118 return kStartOfRun;
119 if (FlagIsA(flag, FLAG_END_OF_EVENT))
120 return kEndOfEvtData;
121 if (FlagIsA(flag, FLAG_START_OF_EVENT))
122 return kStartOfEvtData;
123
124 return kUndefined;
125}
126
127Bool_t MReadRflFile::ReadEvtData()
128{
129 Bool_t rc = kFALSE;
130 while (1)
131 {
132 cphoton data; // FIRST READ "START OF EVENT"
133 fIn->read((char*)&data, SIZE_OF_FLAGS);
134 if (!*fIn)
135 break;
136
137 if (FlagIsA((char*)&data, FLAG_END_OF_EVENT))
138 {
139 rc = kTRUE;
140 break;
141 }
142
143 fIn->read((char*)&data+SIZE_OF_FLAGS, sizeof(cphoton)-SIZE_OF_FLAGS);
144 if (!*fIn)
145 break;
146
147 MRflSinglePhoton &ph = fEvtData->GetNewPhoton();
148 ph.SetXY(data.x*10, data.y*10);
149 ph.SetCosUV(data.u, data.v);
150 ph.SetTime(data.t);
151 ph.SetHeight(data.h);
152 ph.SetInclinationAngle(data.phi);
153 }
154
155 fEvtData->FixSize();
156 return rc;
157}
158
159Int_t MReadRflFile::EvalFlag()
160{
161 const Int_t flag = ReadFlag();
162
163 switch (flag)
164 {
165 case kEndOfFile:
166 fCurrentVersion = ReadVersion();
167 if (fCurrentVersion<0)
168 {
169 *fLog << inf << "Found end of file...Everything OK." << endl;
170 break;
171 }
172
173 *fLog << warn << "Found flag of end of file, but file goes on..." << endl;
174 if (ReadFlag()<0)
175 return kError;
176 /* FALLTHROU */
177 case kStartOfRun:
178 if (fCurrentVersion>0.5)
179 {
180 RflRunHeader rrunh;
181 fIn->read((char*)&rrunh, sizeof(RflRunHeader));
182 if (*fIn)
183 {
184 *fLog << inf << "FIXME: Call ReInit" << endl;
185
186 // FIXME: fRunHeader->Read(fIn);
187 // fRunHeader->Print();
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 *fLog << " Energy Range: " << rrunh.ELowLim << "GeV to " << rrunh.EUppLim << "GeV" << endl;
195 *fLog << " Energy Slope: " << rrunh.SlopeSpec << endl;
196 *fLog << " Wobble Mode: " << rrunh.wobble_mode << endl;
197 *fLog << " Atm. Model: " << rrunh.atmospheric_model << endl;
198 // *fLog << " Theta Range: " << rrunh.ThetaMin << "deg to " << rrunh.ThetaMax << "deg" << endl;
199 // *fLog << " Phi Range: " << rrunh.PhiMin << "deg to " << rrunh.PhiMax << "deg" << endl;
200 // *fLog << " View Cone Rad: " << rrunh.ViewconeRadius << "deg" << endl;
201 break;
202 }
203
204 *fLog << err << "Error! found end of file... But no EOF flag. Exiting." << endl;
205 return kError;
206 }/*
207 else
208 {
209 *fLog << err << "ERROR - Reflector versions <= 0.5 are not supported!" << endl;
210 return kError;
211 }*/
212 return kUndefined;
213
214 case kStartOfEvtData:
215 case kEndOfRun:
216 break;
217
218 case kError:
219 *fLog << err << "ERROR - Flag 'error'" << endl;
220 return kError;
221
222 case kUndefined:
223 *fLog << err << "ERROR - Flag 'undefined'" << endl;
224 return kError;
225
226 default:
227 *fLog << err << "ERROR - Unhandled flag" << endl;
228 return kError;
229
230 }
231 return flag;
232}
233
234Int_t MReadRflFile::ReadEvent()
235{
236 for (;;)
237 {
238 switch (EvalFlag())
239 {
240 case kError:
241 return kFALSE;
242
243 case kEndOfFile:
244 Close();
245 return kCONTINUE; // FIXME: CHECK
246// if (!OpenNextFile())
247// return kFALSE;
248 /* FALLTHROU */
249 case kStartOfRun:
250 case kEndOfRun:
251 continue;
252
253 case kStartOfEvtData:
254 break;
255 }
256 break;
257 }
258
259 if (!ReadEvtHeader())
260 return kFALSE;
261
262 return ReadEvtData();
263}
264
265/*
266Int_t MReadRflFile::Process()
267{
268 for (;;)
269 {
270 switch (EvalFlag())
271 {
272 case kError:
273 return kFALSE;
274
275 case kEndOfFile:
276 if (!OpenNextFile())
277 return kFALSE;
278 // FALLTHROU
279 case kStartOfRun:
280 case kEndOfRun:
281 continue;
282
283 case kStartOfEvtData:
284 break;
285 }
286 break;
287 }
288
289 if (!ReadEvtHeader())
290 return kFALSE;
291
292 return ReadEvtData();
293}
294*/
295
296Int_t MReadRflFile::PreProcess(MParList *plist)
297{
298 fEvtData=(MRflEvtData*)plist->FindCreateObj("MRflEvtData");
299 if (!fEvtData)
300 return kFALSE;
301
302 fEvtHeader=(MRflEvtHeader*)plist->FindCreateObj("MRflEvtHeader");
303 if (!fEvtHeader)
304 return kFALSE;
305
306 fRunHeader=(MRflRunHeader*)plist->FindCreateObj("MRflRunHeader");
307 if (!fRunHeader)
308 return kFALSE;
309
310 return MReadFiles::PreProcess(plist);
311}
312
313Bool_t MReadRflFile::ReadHeader()
314{
315 fCurrentVersion = ReadVersion();
316 if (fCurrentVersion<0)
317 {
318 *fLog << err << "ERROR reading signature." << endl;
319 return kFALSE;
320 }
321 *fLog << inf << "Version " << fCurrentVersion << endl << endl;
322
323 return kTRUE;
324}
325
326
327/****************************************************/
328
329float MReadRflFile::ReadVersion()
330{
331 char sign[20];
332 fIn->read(sign, SIZE_OF_SIGNATURE);
333 if (!*fIn)
334 return -1;
335
336 if (strncmp(sign, PROGNAME, strlen(PROGNAME)) != 0)
337 {
338 /* For the ascii tail of the file! : */
339 if (strncmp(sign, "\n############", SIZE_OF_SIGNATURE))
340 cout << "ERROR: Signature of .rfl file is not correct: " << sign << endl;
341
342 return -1;
343 }
344
345 float version;
346 sscanf(sign, "%*s %f", &version);
347
348 //If the version is < 0.6 the signature had one more byte
349 if (version < 0.6)
350 *fIn >> sign[0];
351
352 return version;
353}
354
355// --------------------------------------------------------------------------
356//
357// Default constructor. Creates an array which stores the file names of
358// the files which should be read. If a filename is given it is added
359// to the list.
360//
361MReadRflFile::MReadRflFile(const char *fname, const char *name,
362 const char *title) : MReadFiles(fname, name, title)
363{
364 fName = name ? name : "MRead";
365 fTitle = title ? title : "Reads a Reflector output file";
366}
367
368Bool_t MReadRflFile::SearchFor(Int_t runno, Int_t eventno)
369{
370 if (!fEvtHeader)
371 return kFALSE;
372
373 Rewind();
374
375 while (1)
376 {
377 fEvtData->Reset();
378 if (!Process())
379 return kFALSE;
380
381 if (fEvtHeader->GetEvtNumber()==eventno &&
382 fRunHeader->GetRunNumber()==runno)
383 return kTRUE;
384 }
385
386 Close();
387}
Note: See TracBrowser for help on using the repository browser.