source: trunk/MagicSoft/Mars/mcorsika/MCorsikaRead.cc@ 9262

Last change on this file since 9262 was 9212, checked in by tbretz, 16 years ago
*** empty log message ***
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 "MCorsikaRunHeader.h"
52#include "MCorsikaEvtHeader.h"
53
54#include "MPhotonEvent.h"
55
56ClassImp(MCorsikaRead);
57
58using namespace std;
59
60/* ----------- please don't delete and don't care about (Thomas) ------------
61#define kBUFSZ 64 //1024*1024*64
62#include <iomanip.h>
63class bifstream : public istream, public streambuf
64{
65private:
66 char fBuffer[kBUFSZ]; //!
67 FILE *fd;
68
69 int sync()
70 {
71 memset(fBuffer, 0, kBUFSZ);
72 return 0;
73 }
74 int underflow()
75 {
76 int sz=fread(fBuffer, kBUFSZ, 1, fd);
77 //int sz=fread(fBuffer, 1, kBUFSZ, fd);
78 setg(fBuffer, fBuffer, fBuffer+kBUFSZ);
79
80 return sz==1 ? *(unsigned char*)fBuffer : EOF;//EOF;
81 //return sz==kBUFSZ ? *(unsigned char*)fBuffer : EOF;//EOF;
82 }
83public:
84 bifstream(const char *name) : istream(this)
85 {
86 fd = fopen(name, "rb");
87 setbuf(fBuffer, kBUFSZ);
88 }
89};
90*/
91
92// --------------------------------------------------------------------------
93//
94// Default constructor. It tries to open the given file.
95//
96MCorsikaRead::MCorsikaRead(const char *fname, const char *name, const char *title)
97 : fRunHeader(0), fEvtHeader(0), fEvent(0), /*fEvtData(0),*/ fForceMode(kFALSE),
98 fFileNames(0), fNumFile(0), fNumEvents(0), fNumTotalEvents(0),
99 fIn(0), fParList(0)
100{
101 fName = name ? name : "MRead";
102 fTitle = title ? title : "Read task to read DAQ binary files";
103
104 fFileNames = new TList;
105 fFileNames->SetOwner();
106
107 if (fname!=NULL)
108 AddFile(fname);
109}
110
111// --------------------------------------------------------------------------
112//
113// Destructor. Delete input stream.
114//
115MCorsikaRead::~MCorsikaRead()
116{
117 delete fFileNames;
118 if (fIn)
119 delete fIn;
120}
121
122/*
123Byte_t MCorsikaRead::IsFileValid(const char *name)
124{
125 MZlib fin(name);
126 if (!fin)
127 return 0;
128
129 Byte_t c[4];
130 fin.read((char*)c, 4);
131 if (!fin)
132 return 0;
133
134 if (c[0]!=0xc0)
135 return 0;
136
137 if (c[1]==0xc0)
138 return 1;
139
140 if (c[1]==0xc1)
141 return 2;
142
143 return 0;
144}
145*/
146
147// --------------------------------------------------------------------------
148//
149// Add a new file to a list of files to be processed, Returns the number
150// of files added. (We can enhance this with a existance chack and
151// wildcard support)
152//
153Int_t MCorsikaRead::AddFile(const char *fname, Int_t entries)
154{
155 TNamed *name = new TNamed(fname, "");
156 fFileNames->AddLast(name);
157 return 1;
158
159}
160
161Bool_t MCorsikaRead::ReadEvtEnd()
162{
163 if (!fRunHeader->SeekEvtEnd(*fIn))
164 {
165 *fLog << (fForceMode?warn:err) << "Error: RUNE section not found in file." << endl;
166 if (!fForceMode)
167 return kFALSE;
168 }
169
170 if (!fRunHeader->ReadEvtEnd(*fIn))
171 {
172 *fLog << (fForceMode?warn:err) << "Error: Reading RUNE section failed." << endl;
173 if (!fForceMode)
174 return kFALSE;
175 }
176
177 return kTRUE;
178}
179
180// --------------------------------------------------------------------------
181//
182// This opens the next file in the list and deletes its name from the list.
183//
184Int_t MCorsikaRead::OpenNextFile(Bool_t print)
185{
186 //
187 // open the input stream and check if it is really open (file exists?)
188 //
189 if (fIn)
190 delete fIn;
191 fIn = NULL;
192
193 //
194 // Check for the existance of a next file to read
195 //
196 TObject *file = fFileNames->At(fNumFile);
197 if (!file)
198 return kFALSE;
199
200 //
201 // open the file which is the first one in the chain
202 //
203 const char *name = file->GetName();
204
205 const char *expname = gSystem->ExpandPathName(name);
206 fIn = new ifstream(expname);
207
208 const Bool_t noexist = !(*fIn);
209 if (noexist)
210 {
211 *fLog << err << "Cannot open file " << expname << ": ";
212 *fLog << (errno!=0?strerror(errno):"Insufficient memory for decompression") << endl;
213 }
214 else
215 {
216 *fLog << inf << "Open file: '" << name << "'" << endl;
217
218 if (fDisplay)
219 {
220 TString txt = GetFileName();
221 txt += " @ ";
222 txt += GetNumExecutions()-1;
223 fDisplay->SetStatusLine2(txt);
224 }
225 }
226
227 delete [] expname;
228
229 if (noexist)
230 return kERROR;
231
232 fNumFile++;
233
234 //
235 // Read RUN HEADER (see specification) from input stream
236 //
237 if (!fRunHeader->ReadEvt(*fIn))
238 return kERROR;
239// if (!fEvtHeader->ReadRunHeader(*fIn, *fRunHeader))
240// return kERROR;
241
242 const streampos pos = fIn->tellg();
243 if (!ReadEvtEnd())
244 return kERROR;
245 fIn->seekg(pos, ios::beg);
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(istream &fin)
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(fin); //read event header block
402 if (!rc)
403 return kFALSE;
404
405 rc = fEvent->ReadCorsikaEvt(fin);
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(fin) : 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 (fIn)
434 {
435 // Read a single event from file
436 const Bool_t rc = ReadEvent(*fIn);
437
438 // kFALSE means: end of file (try next one)
439 if (rc!=kFALSE)
440 return rc;
441
442 if (!fRunHeader->ReadEvtEnd(*fIn))
443 if (!fForceMode)
444 return kERROR;
445 }
446
447 //
448 // If an event could not be read from file try to open new file
449 //
450 const Int_t rc = OpenNextFile();
451 if (rc!=kTRUE)
452 return rc;
453 }
454 return kTRUE;
455}
456
457// --------------------------------------------------------------------------
458//
459// Close the file. Check whether the number of read events differs from
460// the number the file should containe (MCorsikaRunHeader). Prints a warning
461// if it doesn't match.
462//
463Int_t MCorsikaRead::PostProcess()
464{
465 //
466 // Sanity check for the number of events
467 //
468 if (fNumEvents==GetNumExecutions()-1 || GetNumExecutions()==0)
469 return kTRUE;
470
471 *fLog << warn << dec;
472 *fLog << "Warning - number of read events (" << GetNumExecutions()-1;
473 *fLog << ") doesn't match number in run header(s) (";
474 *fLog << fNumEvents << ")." << endl;
475
476 return kTRUE;
477}
Note: See TracBrowser for help on using the repository browser.