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