source: trunk/Mars/mcorsika/MCorsikaRead.cc@ 9844

Last change on this file since 9844 was 9616, checked in by rohlfs, 14 years ago
can read also EventIO files
File size: 11.6 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 11/2008 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: Software Development, 2000-2008
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MCorsikaRead
28//
29// Input Containers:
30// -/-
31//
32// Output Containers:
33// MCorsikaRunHeader
34// MCorsikaEvtHeader
35// MPhotonEvent
36//
37//////////////////////////////////////////////////////////////////////////////
38#include "MCorsikaRead.h"
39
40#include <errno.h>
41#include <fstream>
42
43#include <TSystem.h>
44
45#include "MLog.h"
46#include "MLogManip.h"
47
48#include "MParList.h"
49#include "MStatusDisplay.h"
50
51#include "MCorsikaFormat.h"
52#include "MCorsikaRunHeader.h"
53#include "MCorsikaEvtHeader.h"
54
55#include "MPhotonEvent.h"
56
57ClassImp(MCorsikaRead);
58
59using namespace std;
60
61/* ----------- please don't delete and don't care about (Thomas) ------------
62#define kBUFSZ 64 //1024*1024*64
63#include <iomanip.h>
64class bifstream : public istream, public streambuf
65{
66private:
67 char fBuffer[kBUFSZ]; //!
68 FILE *fd;
69
70 int sync()
71 {
72 memset(fBuffer, 0, kBUFSZ);
73 return 0;
74 }
75 int underflow()
76 {
77 int sz=fread(fBuffer, kBUFSZ, 1, fd);
78 //int sz=fread(fBuffer, 1, kBUFSZ, fd);
79 setg(fBuffer, fBuffer, fBuffer+kBUFSZ);
80
81 return sz==1 ? *(unsigned char*)fBuffer : EOF;//EOF;
82 //return sz==kBUFSZ ? *(unsigned char*)fBuffer : EOF;//EOF;
83 }
84public:
85 bifstream(const char *name) : istream(this)
86 {
87 fd = fopen(name, "rb");
88 setbuf(fBuffer, kBUFSZ);
89 }
90};
91*/
92
93// --------------------------------------------------------------------------
94//
95// Default constructor. It tries to open the given file.
96//
97MCorsikaRead::MCorsikaRead(const char *fname, const char *name, const char *title)
98 : fRunHeader(0), fEvtHeader(0), fEvent(0), /*fEvtData(0),*/ fForceMode(kFALSE),
99 fFileNames(0), fNumFile(0), fNumEvents(0), fNumTotalEvents(0),
100 fIn(0), fInFormat(0), fParList(0)
101{
102 fName = name ? name : "MRead";
103 fTitle = title ? title : "Read task to read DAQ binary files";
104
105 fFileNames = new TList;
106 fFileNames->SetOwner();
107
108 if (fname!=NULL)
109 AddFile(fname);
110}
111
112// --------------------------------------------------------------------------
113//
114// Destructor. Delete input stream.
115//
116MCorsikaRead::~MCorsikaRead()
117{
118 delete fFileNames;
119 if (fIn)
120 delete fIn;
121 if (fInFormat)
122 delete fInFormat;
123}
124
125/*
126Byte_t MCorsikaRead::IsFileValid(const char *name)
127{
128 MZlib fin(name);
129 if (!fin)
130 return 0;
131
132 Byte_t c[4];
133 fin.read((char*)c, 4);
134 if (!fin)
135 return 0;
136
137 if (c[0]!=0xc0)
138 return 0;
139
140 if (c[1]==0xc0)
141 return 1;
142
143 if (c[1]==0xc1)
144 return 2;
145
146 return 0;
147}
148*/
149
150// --------------------------------------------------------------------------
151//
152// Add a new file to a list of files to be processed, Returns the number
153// of files added. (We can enhance this with a existance chack and
154// wildcard support)
155//
156Int_t MCorsikaRead::AddFile(const char *fname, Int_t entries)
157{
158 TNamed *name = new TNamed(fname, "");
159 fFileNames->AddLast(name);
160 return 1;
161
162}
163
164Bool_t MCorsikaRead::ReadEvtEnd()
165{
166 if (!fInFormat->SeekEvtEnd())
167 {
168 *fLog << (fForceMode?warn:err) << "Error: RUNE section not found in file." << endl;
169 if (!fForceMode)
170 return kFALSE;
171 }
172
173 if (!fRunHeader->ReadEvtEnd(fInFormat))
174 {
175 *fLog << (fForceMode?warn:err) << "Error: Reading RUNE section failed." << endl;
176 if (!fForceMode)
177 return kFALSE;
178 }
179
180 return kTRUE;
181}
182
183// --------------------------------------------------------------------------
184//
185// This opens the next file in the list and deletes its name from the list.
186//
187Int_t MCorsikaRead::OpenNextFile(Bool_t print)
188{
189
190 //
191 // open the input stream and check if it is really open (file exists?)
192 //
193 if (fIn)
194 delete fIn;
195 fIn = NULL;
196
197 if (fInFormat)
198 delete fInFormat;
199 fInFormat = NULL;
200
201 //
202 // Check for the existance of a next file to read
203 //
204 TObject *file = fFileNames->At(fNumFile);
205 if (!file)
206 return kFALSE;
207
208 //
209 // open the file which is the first one in the chain
210 //
211 const char *name = file->GetName();
212
213 const char *expname = gSystem->ExpandPathName(name);
214 fInFormat = CorsikaFormatFactory(fLog, expname);
215 delete [] expname;
216
217 if (fInFormat == NULL)
218 return kERROR;
219
220 *fLog << inf << "Open file: '" << name << "'" << endl;
221
222 if (fDisplay)
223 {
224 // Show the number of the last event after
225 // which we now open a new file
226 TString txt = GetFileName();
227 txt += " @ ";
228 txt += GetNumExecutions()-1;
229 fDisplay->SetStatusLine2(txt);
230 }
231
232 fNumFile++;
233
234 //
235 // Read RUN HEADER (see specification) from input stream
236 //
237 if (!fRunHeader->ReadEvt(fInFormat))
238 return kERROR;
239// if (!fEvtHeader->ReadRunHeader(*fIn, *fRunHeader))
240// return kERROR;
241
242 fInFormat->StorePos();
243 if (!ReadEvtEnd())
244 return kERROR;
245 fInFormat->ResetPos();
246
247
248 fNumEvents += fRunHeader->GetNumEvents();
249 fRunHeader->SetReadyToSave();
250
251 //
252 // Print Run Header
253 // We print it after the first event was read because
254 // we still miss information which is stored in the event header?!?
255 if (print)
256 fRunHeader->Print();
257
258 if (!fParList)
259 return kTRUE;
260
261 //
262 // Search for MTaskList
263 //
264 MTask *tlist = (MTask*)fParList->FindObject("MTaskList");
265 if (!tlist)
266 {
267 *fLog << err << dbginf << "MTaskList not found... abort." << endl;
268 return kERROR;
269 }
270
271 //
272 // A new file has been opened and new headers have been read.
273 // --> ReInit tasklist
274 //
275 return tlist->ReInit(fParList) ? kTRUE : kERROR;
276}
277
278// --------------------------------------------------------------------------
279//
280// Return file name of current file.
281//
282TString MCorsikaRead::GetFullFileName() const
283{
284 const TObject *file = fFileNames->At(fNumFile-1);
285 return file ? file->GetName() : "";
286}
287
288// --------------------------------------------------------------------------
289//
290// Restart with the first file
291//
292Bool_t MCorsikaRead::Rewind()
293{
294 fNumFile=0;
295 fNumEvents=0;
296 return OpenNextFile()==kTRUE;
297}
298
299Bool_t MCorsikaRead::CalcNumTotalEvents()
300{
301 fNumTotalEvents = 0;
302
303 Bool_t rc = kTRUE;
304
305 while (1)
306 {
307 switch (OpenNextFile(kFALSE))
308 {
309 case kFALSE:
310 break;
311 case kERROR:
312 rc = kFALSE;
313 break;
314 case kTRUE:
315 if (!ReadEvtEnd())
316 {
317 rc = kFALSE;
318 break;
319 }
320
321 fNumTotalEvents += fRunHeader->GetNumEvents();
322 continue;
323 }
324 break;
325 }
326
327 if (fIn)
328 delete fIn;
329 fIn = NULL;
330
331 return rc;
332}
333
334// --------------------------------------------------------------------------
335//
336// The PreProcess of this task checks for the following containers in the
337// list:
338// MCorsikaRunHeader <output> if not found it is created
339// MCorsikaEvtHeader <output> if not found it is created
340// MCorsikaEvtData <output> if not found it is created
341// MCorsikaCrateArray <output> if not found it is created
342// MCorsikaEvtTime <output> if not found it is created (MTime)
343//
344// If all containers are found or created the run header is read from the
345// binary file and printed. If the Magic-Number (file identification)
346// doesn't match we stop the eventloop.
347//
348// Now the EvtHeader and EvtData containers are initialized.
349//
350Int_t MCorsikaRead::PreProcess(MParList *pList)
351{
352 //
353 // open the input stream
354 // first of all check if opening the file in the constructor was
355 // successfull
356 //
357 fParList = 0;
358
359 if (!OpenStream())
360 return kFALSE;
361
362 //
363 // check if all necessary containers exist in the Parameter list.
364 // if not create one and add them to the list
365 //
366 fRunHeader = (MCorsikaRunHeader*)pList->FindCreateObj("MCorsikaRunHeader");
367 if (!fRunHeader)
368 return kFALSE;
369
370 fEvtHeader = (MCorsikaEvtHeader*)pList->FindCreateObj("MCorsikaEvtHeader");
371 if (!fEvtHeader)
372 return kFALSE;
373
374 fEvent = (MPhotonEvent*)pList->FindCreateObj("MPhotonEvent");
375 if (!fEvent)
376 return kFALSE;
377
378 *fLog << inf << "Calculating number of total events..." << flush;
379 if (!CalcNumTotalEvents())
380 return kFALSE;
381 *fLog << inf << " " << fNumTotalEvents << " found." << endl;
382
383 fNumFile=0;
384 fNumEvents=0;
385
386 fParList = pList;
387
388 return kTRUE;
389}
390
391// --------------------------------------------------------------------------
392//
393// Read a single event from the stream
394//
395Bool_t MCorsikaRead::ReadEvent()
396{
397 //
398 // Read in the next EVENT HEADER (see specification),
399 // if there is no next event anymore stop eventloop
400 //
401 Int_t rc = fEvtHeader->ReadEvt(fInFormat); //read event header block
402 if (!rc)
403 return kFALSE;
404
405 rc = fEvent->ReadCorsikaEvt(fInFormat);
406
407 /*
408 // Check if we are allowed to stop reading in the middle of the data
409 if (rc==kFALSE && !fForce)
410 {
411 *fLog << err;
412 *fLog << "ERROR - End of file in the middle of the data stream!" << endl;
413 *fLog << " Set the force-option to force processing." << endl;
414 return kERROR;
415 }
416 */
417
418 return rc==kTRUE ? fEvtHeader->ReadEvtEnd(fInFormat) : rc;
419}
420
421// --------------------------------------------------------------------------
422//
423// The Process reads one event from the binary file:
424// - The event header is read
425// - the run header is read
426// - all crate information is read
427// - the raw data information of one event is read
428//
429Int_t MCorsikaRead::Process()
430{
431 while (1)
432 {
433 if (fInFormat)
434 {
435 // Read a single event from file
436 const Bool_t rc = ReadEvent();
437
438 // kFALSE means: end of file (try next one)
439 if (rc!=kFALSE)
440 return rc;
441
442
443 fInFormat->UnreadLastHeader();
444 if (!fRunHeader->ReadEvtEnd(fInFormat))
445 if (!fForceMode)
446 return kERROR;
447 }
448
449 //
450 // If an event could not be read from file try to open new file
451 //
452 const Int_t rc = OpenNextFile();
453 if (rc!=kTRUE)
454 return rc;
455 }
456 return kTRUE;
457}
458
459// --------------------------------------------------------------------------
460//
461// Close the file. Check whether the number of read events differs from
462// the number the file should containe (MCorsikaRunHeader). Prints a warning
463// if it doesn't match.
464//
465Int_t MCorsikaRead::PostProcess()
466{
467 //
468 // Sanity check for the number of events
469 //
470 if (fNumEvents==GetNumExecutions()-1 || GetNumExecutions()==0)
471 return kTRUE;
472
473 *fLog << warn << dec;
474 *fLog << "Warning - number of read events (" << GetNumExecutions()-1;
475 *fLog << ") doesn't match number in run header(s) (";
476 *fLog << fNumEvents << ")." << endl;
477
478 return kTRUE;
479}
Note: See TracBrowser for help on using the repository browser.