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

Last change on this file since 9565 was 9314, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 11.7 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 // Show the number of the last event after
221 // which we now open a new file
222 TString txt = GetFileName();
223 txt += " @ ";
224 txt += GetNumExecutions()-1;
225 fDisplay->SetStatusLine2(txt);
226 }
227 }
228
229 delete [] expname;
230
231 if (noexist)
232 return kERROR;
233
234 fNumFile++;
235
236 //
237 // Read RUN HEADER (see specification) from input stream
238 //
239 if (!fRunHeader->ReadEvt(*fIn))
240 return kERROR;
241// if (!fEvtHeader->ReadRunHeader(*fIn, *fRunHeader))
242// return kERROR;
243
244 const streampos pos = fIn->tellg();
245 if (!ReadEvtEnd())
246 return kERROR;
247 fIn->seekg(pos, ios::beg);
248
249
250 fNumEvents += fRunHeader->GetNumEvents();
251 fRunHeader->SetReadyToSave();
252
253 //
254 // Print Run Header
255 // We print it after the first event was read because
256 // we still miss information which is stored in the event header?!?
257 if (print)
258 fRunHeader->Print();
259
260 if (!fParList)
261 return kTRUE;
262
263 //
264 // Search for MTaskList
265 //
266 MTask *tlist = (MTask*)fParList->FindObject("MTaskList");
267 if (!tlist)
268 {
269 *fLog << err << dbginf << "MTaskList not found... abort." << endl;
270 return kERROR;
271 }
272
273 //
274 // A new file has been opened and new headers have been read.
275 // --> ReInit tasklist
276 //
277 return tlist->ReInit(fParList) ? kTRUE : kERROR;
278}
279
280// --------------------------------------------------------------------------
281//
282// Return file name of current file.
283//
284TString MCorsikaRead::GetFullFileName() const
285{
286 const TObject *file = fFileNames->At(fNumFile-1);
287 return file ? file->GetName() : "";
288}
289
290// --------------------------------------------------------------------------
291//
292// Restart with the first file
293//
294Bool_t MCorsikaRead::Rewind()
295{
296 fNumFile=0;
297 fNumEvents=0;
298 return OpenNextFile()==kTRUE;
299}
300
301Bool_t MCorsikaRead::CalcNumTotalEvents()
302{
303 fNumTotalEvents = 0;
304
305 Bool_t rc = kTRUE;
306
307 while (1)
308 {
309 switch (OpenNextFile(kFALSE))
310 {
311 case kFALSE:
312 break;
313 case kERROR:
314 rc = kFALSE;
315 break;
316 case kTRUE:
317 if (!ReadEvtEnd())
318 {
319 rc = kFALSE;
320 break;
321 }
322
323 fNumTotalEvents += fRunHeader->GetNumEvents();
324 continue;
325 }
326 break;
327 }
328
329 if (fIn)
330 delete fIn;
331 fIn = NULL;
332
333 return rc;
334}
335
336// --------------------------------------------------------------------------
337//
338// The PreProcess of this task checks for the following containers in the
339// list:
340// MCorsikaRunHeader <output> if not found it is created
341// MCorsikaEvtHeader <output> if not found it is created
342// MCorsikaEvtData <output> if not found it is created
343// MCorsikaCrateArray <output> if not found it is created
344// MCorsikaEvtTime <output> if not found it is created (MTime)
345//
346// If all containers are found or created the run header is read from the
347// binary file and printed. If the Magic-Number (file identification)
348// doesn't match we stop the eventloop.
349//
350// Now the EvtHeader and EvtData containers are initialized.
351//
352Int_t MCorsikaRead::PreProcess(MParList *pList)
353{
354 //
355 // open the input stream
356 // first of all check if opening the file in the constructor was
357 // successfull
358 //
359 fParList = 0;
360
361 if (!OpenStream())
362 return kFALSE;
363
364 //
365 // check if all necessary containers exist in the Parameter list.
366 // if not create one and add them to the list
367 //
368 fRunHeader = (MCorsikaRunHeader*)pList->FindCreateObj("MCorsikaRunHeader");
369 if (!fRunHeader)
370 return kFALSE;
371
372 fEvtHeader = (MCorsikaEvtHeader*)pList->FindCreateObj("MCorsikaEvtHeader");
373 if (!fEvtHeader)
374 return kFALSE;
375
376 fEvent = (MPhotonEvent*)pList->FindCreateObj("MPhotonEvent");
377 if (!fEvent)
378 return kFALSE;
379
380 *fLog << inf << "Calculating number of total events..." << flush;
381 if (!CalcNumTotalEvents())
382 return kFALSE;
383 *fLog << inf << " " << fNumTotalEvents << " found." << endl;
384
385 fNumFile=0;
386 fNumEvents=0;
387
388 fParList = pList;
389
390 return kTRUE;
391}
392
393// --------------------------------------------------------------------------
394//
395// Read a single event from the stream
396//
397Bool_t MCorsikaRead::ReadEvent(istream &fin)
398{
399 //
400 // Read in the next EVENT HEADER (see specification),
401 // if there is no next event anymore stop eventloop
402 //
403 Int_t rc = fEvtHeader->ReadEvt(fin); //read event header block
404 if (!rc)
405 return kFALSE;
406
407 rc = fEvent->ReadCorsikaEvt(fin);
408
409 /*
410 // Check if we are allowed to stop reading in the middle of the data
411 if (rc==kFALSE && !fForce)
412 {
413 *fLog << err;
414 *fLog << "ERROR - End of file in the middle of the data stream!" << endl;
415 *fLog << " Set the force-option to force processing." << endl;
416 return kERROR;
417 }
418 */
419
420 return rc==kTRUE ? fEvtHeader->ReadEvtEnd(fin) : rc;
421}
422
423// --------------------------------------------------------------------------
424//
425// The Process reads one event from the binary file:
426// - The event header is read
427// - the run header is read
428// - all crate information is read
429// - the raw data information of one event is read
430//
431Int_t MCorsikaRead::Process()
432{
433 while (1)
434 {
435 if (fIn)
436 {
437 // Read a single event from file
438 const Bool_t rc = ReadEvent(*fIn);
439
440 // kFALSE means: end of file (try next one)
441 if (rc!=kFALSE)
442 return rc;
443
444 if (!fRunHeader->ReadEvtEnd(*fIn))
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.