source: branches/Corsika7405Compatibility/mcorsika/MCorsikaRead.cc@ 18227

Last change on this file since 18227 was 18180, checked in by dbaack, 10 years ago
Fixed bug with non cleared "PhotonEvent"
File size: 21.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 Reiner Rohlfs 11/2010 <mailto:Reiner.Rohlfs@unige.ch>
20!
21! Copyright: Software Development, 2000-2008
22!
23!
24\* ======================================================================== */
25
26//////////////////////////////////////////////////////////////////////////////
27//
28// MCorsikaRead
29//
30// Input Containers:
31// -/-
32//
33// Output Containers:
34// MCorsikaRunHeader
35// MCorsikaEvtHeader
36// MPhotonEvent
37//
38//////////////////////////////////////////////////////////////////////////////
39#include "MCorsikaRead.h"
40
41#include <errno.h>
42#include <fstream>
43
44#include <TSystem.h>
45
46#include "MLog.h"
47#include "MLogManip.h"
48
49#include "MParList.h"
50#include "MStatusDisplay.h"
51
52#include "MCorsikaFormat.h"
53#include "MCorsikaRunHeader.h"
54#include "MCorsikaEvtHeader.h"
55
56#include "../msim/MPhotonData.h"
57
58#include "MPhotonEvent.h"
59
60ClassImp(MCorsikaRead);
61
62using namespace std;
63
64
65/* ----------- please don't delete and don't care about (Thomas) ------------
66#define kBUFSZ 64 //1024*1024*64
67#include <iomanip.h>
68class bifstream : public istream, public streambuf
69{
70private:
71 char fBuffer[kBUFSZ]; //!
72 FILE *fd;
73
74 int sync()
75 {
76 memset(fBuffer, 0, kBUFSZ);
77 return 0;
78 }
79 int underflow()
80 {
81 int sz=fread(fBuffer, kBUFSZ, 1, fd);
82 //int sz=fread(fBuffer, 1, kBUFSZ, fd);
83 setg(fBuffer, fBuffer, fBuffer+kBUFSZ);
84
85 return sz==1 ? *(unsigned char*)fBuffer : EOF;//EOF;
86 //return sz==kBUFSZ ? *(unsigned char*)fBuffer : EOF;//EOF;
87 }
88public:
89 bifstream(const char *name) : istream(this)
90 {
91 fd = fopen(name, "rb");
92 setbuf(fBuffer, kBUFSZ);
93 }
94};
95*/
96
97// --------------------------------------------------------------------------
98//
99// Default constructor. It tries to open the given file.
100//
101MCorsikaRead::MCorsikaRead(const char *fname, const char *name, const char *title)
102 : fRunHeader(0), fEvtHeader(0), fEvent(0), fTelescopeIdx(-1), fArrayIdx(-1),
103 fForceMode(kFALSE), fFileNames(0), fNumFile(0), fNumEvents(0),
104 fNumTotalEvents(0), fInFormat(0), fParList(0), fNumTelescopes(1), fReadState(0)
105{
106 fName = name ? name : "MRead";
107 fTitle = title ? title : "Read task to read DAQ binary files";
108
109 fFileNames = new TList;
110 fFileNames->SetOwner();
111
112 if (fname!=NULL)
113 AddFile(fname);
114}
115
116// --------------------------------------------------------------------------
117//
118// Destructor. Delete input stream.
119//
120MCorsikaRead::~MCorsikaRead()
121{
122 delete fFileNames;
123 if (fInFormat)
124 delete fInFormat;
125}
126
127/*
128Byte_t MCorsikaRead::IsFileValid(const char *name)
129{
130 MZlib fin(name);
131 if (!fin)
132 return 0;
133
134 Byte_t c[4];
135 fin.read((char*)c, 4);
136 if (!fin)
137 return 0;
138
139 if (c[0]!=0xc0)
140 return 0;
141
142 if (c[1]==0xc0)
143 return 1;
144
145 if (c[1]==0xc1)
146 return 2;
147
148 return 0;
149}
150*/
151
152// --------------------------------------------------------------------------
153//
154// Add a new file to a list of files to be processed, Returns the number
155// of files added. (We can enhance this with a existance chack and
156// wildcard support)
157//
158Int_t MCorsikaRead::AddFile(const char *fname, Int_t entries)
159{
160 TNamed *name = new TNamed(fname, "");
161 fFileNames->AddLast(name);
162 return 1;
163
164}
165// --------------------------------------------------------------------------
166//
167// This opens the next file in the list and deletes its name from the list.
168//
169Int_t MCorsikaRead::OpenNextFile(Bool_t print)
170{
171
172 //
173 // open the input stream and check if it is really open (file exists?)
174 //
175 if (fInFormat)
176 delete fInFormat;
177 fInFormat = NULL;
178
179 //
180 // Check for the existance of a next file to read
181 //
182 TObject *file = fFileNames->At(fNumFile);
183 if (!file)
184 return kFALSE;
185
186 //
187 // open the file which is the first one in the chain
188 //
189 const char *name = file->GetName();
190
191 const char *expname = gSystem->ExpandPathName(name);
192 fInFormat = MCorsikaFormat::CorsikaFormatFactory(expname);
193 delete [] expname;
194
195 if (fInFormat == NULL)
196 return kERROR;
197
198 *fLog << inf << "Open file: '" << name << "'" << endl;
199
200 if (fDisplay)
201 {
202 // Show the number of the last event after
203 // which we now open a new file
204 TString txt = GetFileName();
205 txt += " @ ";
206 txt += GetNumExecutions()-1;
207 fDisplay->SetStatusLine2(txt);
208 }
209
210 fNumFile++;
211
212 if (!fParList)
213 return kTRUE;
214
215 //
216 // Search for MTaskList
217 //
218 MTask *tlist = (MTask*)fParList->FindObject("MTaskList");
219 if (!tlist)
220 {
221 *fLog << err << dbginf << "MTaskList not found... abort." << endl;
222 return kERROR;
223 }
224
225 //
226 // A new file has been opened and new headers have been read.
227 // --> ReInit tasklist
228 //
229 return tlist->ReInit(fParList) ? kTRUE : kERROR;
230}
231
232// --------------------------------------------------------------------------
233//
234// Return file name of current file.
235//
236TString MCorsikaRead::GetFullFileName() const
237{
238 const TObject *file = fFileNames->At(fNumFile-1);
239 return file ? file->GetName() : "";
240}
241
242// --------------------------------------------------------------------------
243//
244// Restart with the first file
245//
246Bool_t MCorsikaRead::Rewind()
247{
248 fNumFile=0;
249 fNumEvents=0;
250 return OpenNextFile()==kTRUE;
251}
252
253// --------------------------------------------------------------------------
254//
255// Calculates the total number of events from all input files and stores
256// the number in fNumTotalEvents.
257//
258Bool_t MCorsikaRead::CalcNumTotalEvents()
259{
260 fNumTotalEvents = 0;
261
262 Bool_t rc = kTRUE;
263
264 while (1)
265 {
266 switch (OpenNextFile(kFALSE))
267 {
268 case kFALSE:
269 break;
270 case kERROR:
271 rc = kFALSE;
272 break;
273 case kTRUE:
274
275 // read run header(1200), telescope position(1201) and
276 // first event header(1202)
277 Bool_t status = kTRUE;
278 Int_t blockType, blockVersion, blockIdentifier, blockLength;
279 while (status &&
280 fInFormat->NextBlock(fReadState/* == 4*/, blockType, blockVersion,
281 blockIdentifier, blockLength))
282 {
283 if (blockType == 1200)
284 status = fRunHeader->ReadEvt(fInFormat);
285
286 else if(blockType == 1201)
287 status = ReadTelescopePosition();
288
289 else if (blockType == 1202)
290 {
291 Float_t buffer[272];
292 status = fInFormat->Read(buffer, 272 * sizeof(Float_t));
293 status = fRunHeader->ReadEventHeader(buffer);
294 //std::cout << "\x1b[31;1m EVTH Header: \x1b[0m";
295 //fRunHeader->Print(nullptr);
296 break;
297 }
298 else
299 fInFormat->Seek(blockLength);
300 }
301
302 if (status != kTRUE)
303 return status;
304
305 if (!fInFormat->SeekEvtEnd())
306 {
307 *fLog << (fForceMode?warn:err) << "Error: RUNE section not found in file." << endl;
308 if (!fForceMode)
309 return fForceMode ? kTRUE : kFALSE;
310 }
311
312 if (!fRunHeader->ReadEvtEnd(fInFormat, kTRUE))
313 {
314 *fLog << (fForceMode?warn:err) << "Error: Reading RUNE section failed." << endl;
315 if (!fForceMode)
316 return kFALSE;
317 }
318
319 fNumTotalEvents += fRunHeader->GetNumEvents()*fRunHeader->GetNumReuse()*
320 (fTelescopeIdx<0 ? fNumTelescopes : 1);
321 continue;
322 }
323 break;
324 }
325
326 return rc;
327}
328
329// --------------------------------------------------------------------------
330//
331// Reads the the position of all telescopes in one array
332//
333Int_t MCorsikaRead::ReadTelescopePosition()
334{
335 if (!fInFormat->Read(&fNumTelescopes, 4)) return kERROR;
336
337 if (fTelescopeIdx>=fNumTelescopes)
338 {
339 *fLog << err << "ERROR - Requested telescope index " << fTelescopeIdx <<
340 " exceeds number of telescopes " << fNumTelescopes << " in file." << endl;
341 return kERROR;
342 }
343
344 fTelescopeX.Set(fNumTelescopes);
345 fTelescopeY.Set(fNumTelescopes);
346 fTelescopeZ.Set(fNumTelescopes);
347 fTelescopeR.Set(fNumTelescopes);
348
349 if (!fInFormat->Read(fTelescopeX.GetArray(), 4*fNumTelescopes)) return kERROR;
350 if (!fInFormat->Read(fTelescopeY.GetArray(), 4*fNumTelescopes)) return kERROR;
351 if (!fInFormat->Read(fTelescopeZ.GetArray(), 4*fNumTelescopes)) return kERROR;
352 if (!fInFormat->Read(fTelescopeR.GetArray(), 4*fNumTelescopes)) return kERROR;
353
354 *fLog << all;
355 *fLog << "Number of telescopes: " << fNumTelescopes << " (using ";
356 if (fTelescopeIdx>=0)
357 *fLog << "telescope " << fTelescopeIdx;
358 else
359 *fLog << "all telescopes";
360 *fLog << ")" << endl;
361
362 const Int_t lo = fTelescopeIdx<0 ? 0 : fTelescopeIdx;
363 const Int_t up = fTelescopeIdx<0 ? fNumTelescopes : fTelescopeIdx+1;
364
365 for (int i=lo; i<up; i++)
366 {
367 *fLog << all << "Telescope #" << setw(4) << i << " [X/Y/Z (R)]: ";
368 *fLog << setw(7) << fTelescopeX[i] << "/";
369 *fLog << setw(7) << fTelescopeY[i] << "/";
370 *fLog << setw(7) << fTelescopeZ[i] << " (R=" << setw(4) << fTelescopeR[i] << ")" << endl;
371 }
372
373 fNumTelescope = 0;
374
375 return kTRUE;
376}
377
378// --------------------------------------------------------------------------
379//
380// Reads the header of the next block. Depending on the current fReadState
381// and the block-type of the new header the methods decides wether
382// to continue the reading (kCONTINEU), to exit the Process() method (kTRUE)
383// or to try to read a new file (kFALSE).
384// Return codes:
385// - kFALSE : end of file was detected and no new header was read
386// - kTRU: A EventEnd was already found (fReadStatus == 3) and
387// the current header is not the RunEnd
388// - kCONTINUE: all other cases.
389Int_t MCorsikaRead::ReadNextBlockHeader()
390{
391 if (fInFormat->NextBlock(fReadState/* == 4*/, fBlockType, fBlockVersion,
392 fBlockIdentifier, fBlockLength) == kFALSE)
393 // end of file
394 return kFALSE;
395/*
396 gLog << "Next fBlock: type=" << fBlockType << " version=" << fBlockVersion;
397 gLog << " identifier=" << fBlockIdentifier << " length=" << fBlockLength;
398 gLog << " readState= " << fReadState << endl;
399*/
400 if (fReadState == 3 && fBlockType != 1210)
401 // fReadState == 3 means we have read the event end
402 // most of the time we can now save our data. BUT:
403 // bBlockType != 1210 means the next block is not RUNE,
404 // we want to read the RUNE block, before we
405 // save everything (last events and RUNE)
406 return kTRUE;
407
408 if (fBlockType == 1204 && fReadState != 2)
409 // next is a new set of telescope arrays, we store the previous ones
410 // but not if this is the first one (fReadState != 2)
411 return kTRUE;
412
413 return kCONTINUE;
414
415}
416
417// --------------------------------------------------------------------------
418//
419// The PreProcess of this task checks for the following containers in the
420// list:
421// MCorsikaRunHeader <output> if not found it is created
422// MCorsikaEvtHeader <output> if not found it is created
423// MCorsikaEvtData <output> if not found it is created
424// MCorsikaCrateArray <output> if not found it is created
425// MCorsikaEvtTime <output> if not found it is created (MTime)
426//
427// If all containers are found or created the run header is read from the
428// binary file and printed. If the Magic-Number (file identification)
429// doesn't match we stop the eventloop.
430//
431// Now the EvtHeader and EvtData containers are initialized.
432//
433Int_t MCorsikaRead::PreProcess(MParList *pList)
434{
435 //
436 // open the input stream
437 // first of all check if opening the file in the constructor was
438 // successfull
439 //
440 fParList = 0;
441
442 //
443 // check if all necessary containers exist in the Parameter list.
444 // if not create one and add them to the list
445 //
446 fRunHeader = (MCorsikaRunHeader*)pList->FindCreateObj("MCorsikaRunHeader");
447 if (!fRunHeader)
448 return kFALSE;
449
450 fEvtHeader = (MCorsikaEvtHeader*)pList->FindCreateObj("MCorsikaEvtHeader");
451 if (!fEvtHeader)
452 return kFALSE;
453
454 fEvent = (MPhotonEvent*)pList->FindCreateObj("MPhotonEvent");
455 if (!fEvent)
456 return kFALSE;
457
458 *fLog << inf << "Calculating number of total events..." << flush;
459 if (!CalcNumTotalEvents())
460 return kFALSE;
461 *fLog << inf << " " << fNumTotalEvents << " found." << endl;
462
463 fNumFile=0;
464 fNumEvents=0;
465
466 fParList = pList;
467
468 return kTRUE;
469}
470
471// --------------------------------------------------------------------------
472//
473// The Process reads one event from the binary file:
474// - The event header is read
475// - the run header is read (only once per file)
476// - the raw data information of one event is read
477//
478Int_t MCorsikaRead::Process()
479{
480 fEvent->Clear();
481 fEvent->Reset();
482 fEvent->Resize(0);
483
484
485 std::cout << "\n\n MCorsikaRead Process " << std::endl;
486 while (1) // loop for multiple input files
487 {
488
489 if (fInFormat)
490 {
491
492 while (1) // loop per input block
493 {
494
495 if (fReadState == 4)
496 {
497 fTopBlockLength -= fBlockLength + 12;
498 if (fTopBlockLength <= 0)
499 // all data of a top block are read, go back to normal state
500 fReadState = 15; // not used
501 }
502
503
504 Int_t status = kTRUE;
505
506 switch (fBlockType)
507 {
508 case 1200: // the run header
509 status = fRunHeader->ReadEvt(fInFormat);
510 fReadState = 1; // RUNH is read
511 break;
512
513 case 1201: // telescope position
514 // std::cout << "TelescopPos ||||||" << std::endl;
515 status = ReadTelescopePosition();
516 break;
517
518 case 1202: // the event header
519 Float_t buffer[272];
520 static int asd = 0;
521 if (!fInFormat->Read(buffer, 272 * sizeof(Float_t)))
522 return kFALSE;
523
524 if (fReadState == 1) // first event after RUN header
525 {
526 fRunHeader->ReadEventHeader(buffer);
527 //fRunHeader->Print();
528 }
529
530 status = fEvtHeader->ReadEvt(buffer);
531
532
533 if (fArrayIdx >= (Int_t)fEvtHeader->GetTotReuse())
534 {
535 *fLog << err << "ERROR - Requested array index " << fArrayIdx <<
536 " exceeds number of arrays " << fEvtHeader->GetTotReuse() <<
537 " in file." << endl;
538 return kERROR;
539 }
540
541 fReadState = 2;
542 break;
543
544 case 1204: // top level block for one array (only for eventio data)
545 if (fArrayIdx < 0 || fArrayIdx == fBlockIdentifier)
546 {
547 fReadState = 4;
548 fTopBlockLength = fBlockLength;
549 }
550 else
551 // skip this array of telescopes
552 fInFormat->Seek(fBlockLength);
553 std::cout << "Skip Array!" << std::endl;
554 break;
555
556
557 case 1205: // eventio data
558 {
559 Int_t telIdx = fBlockIdentifier % 1000;
560
561 if (fBlockVersion == 0 && (fTelescopeIdx < 0 || fTelescopeIdx == telIdx) )
562 {
563 status = fEvent->ReadEventIoEvt(fInFormat);
564
565 Int_t arrayIdx = fBlockIdentifier / 1000;
566 Float_t xArrOff, yArrOff;
567 fEvtHeader->GetArrayOffset(arrayIdx, xArrOff, yArrOff);
568 fEvtHeader->SetTelescopeOffset(arrayIdx,
569 xArrOff + fTelescopeY[telIdx],
570 yArrOff - fTelescopeX[telIdx] );
571 fEvent->AddXY(xArrOff + fTelescopeY[telIdx],
572 yArrOff - fTelescopeX[telIdx]);
573 fEvent->SimWavelength(fRunHeader->GetWavelengthMin(),
574 fRunHeader->GetWavelengthMax());
575
576
577 bool tmpWeight = false;
578 std::cout << "ReadEventIO Telescope data with " << fEvent->GetNumPhotons() << std::endl;
579 for(int i=0; i<fEvent->GetNumPhotons(); i++)
580 {
581
582 const MPhotonData &ph = (*fEvent)[i];
583 if(ph.GetWeight() != 1)
584 tmpWeight = true;
585 }
586 if(tmpWeight)
587 std::cout << "CorsikaRead Photon weight != 1" << std::endl;
588
589 }
590 else
591 // skip this telescope
592 {
593 fInFormat->Seek(fBlockLength);
594
595 std::cout << "Skip this telescope!" << std::endl;
596 }
597 }
598 break;
599
600 case 1209: // the event end
601 status = fEvtHeader->ReadEvtEnd(fInFormat);
602
603 if (fReadState == 10 || fReadState == 2)
604 {
605 // corsika events
606 fEvtHeader->ResetNumReuse();
607 fEvtHeader->InitXY();
608 fBlockType = 1109; // save corsika events
609 continue;
610 }
611
612 fReadState = 3; // event closed, run still open
613 break;
614
615
616 case 1210: // the run end
617 status = fRunHeader->ReadEvtEnd(fInFormat, kTRUE);
618 fNumEvents += fRunHeader->GetNumEvents();
619 //fRunHeader->SetReadyToSave();
620 fReadState = 5; // back to starting point
621 // this may be the last block in the file.
622 // We exit to write the header into the file
623 fBlockLength = 0;
624 fBlockType = 0; // not available type
625 return kTRUE;
626 break;
627
628
629 case 1105: // event block of raw format
630 static int NmrEventBlock = 0;
631 //std::cout << "EventBlock ||||||" << ++NmrEventBlock << std::endl;
632 if (fReadState == 2 || fReadState == 10)
633 {
634 Int_t oldSize = fRawEvemtBuffer.size();
635 fRawEvemtBuffer.resize(oldSize + fBlockLength / sizeof(Float_t));
636 status = fInFormat->Read(&fRawEvemtBuffer[oldSize], fBlockLength);
637 fReadState = 10;
638 }
639 else
640 fInFormat->Seek(fBlockLength);
641 break;
642
643
644 case 1109: // save corsika events
645 std::cout << "Block1109" << std::endl;
646 fEvtHeader->InitXY();
647 status = fEvent->ReadCorsikaEvt(&fRawEvemtBuffer[0],
648 fRawEvemtBuffer.size() / 7,
649 fEvtHeader->GetNumReuse()+1);
650 fEvtHeader->IncNumReuse();
651
652 if (fEvtHeader->GetNumReuse() == fRunHeader->GetNumReuse())
653 {
654 // this was the last reuse. Set fBlockType to EVTE to save
655 // it the next time.
656 fRawEvemtBuffer.resize(0);
657
658 fReadState = 3;
659 fBlockType = 1209;
660 }
661 else
662 // save this reuse
663 return status;
664
665 break;
666
667 default:
668 // unknown block, we skip it
669 std::cout << "UnknownBlock " << fBlockType << std::endl;
670 fReadState = 15;
671 fInFormat->Seek(fBlockLength);
672
673 }
674
675 if (status != kTRUE)
676 // there was an error while data were read
677 return status;
678
679 Int_t headerStatus = ReadNextBlockHeader();
680
681 if (headerStatus == kFALSE)
682 // end of file
683 break;
684 if (headerStatus == kTRUE)
685 // we shall quit this loop
686 return kTRUE;
687
688 // else continue
689 }
690
691 }
692
693 //
694 // If an event could not be read from file try to open new file
695 //
696 const Int_t rc = OpenNextFile();
697 if (rc!=kTRUE)
698 return rc;
699
700 if (ReadNextBlockHeader() == kFALSE)
701 return kFALSE;
702 }
703 return kTRUE;
704}
705
706
707// --------------------------------------------------------------------------
708//
709// Close the file. Check whether the number of read events differs from
710// the number the file should containe (MCorsikaRunHeader). Prints a warning
711// if it doesn't match.
712//
713Int_t MCorsikaRead::PostProcess()
714{
715
716 const UInt_t n = fNumEvents*fRunHeader->GetNumReuse()*(fTelescopeIdx<0?fNumTelescopes:1);
717
718 //
719 // Sanity check for the number of events
720 //
721 if (n==GetNumExecutions()-1 || GetNumExecutions()==0)
722 return kTRUE;
723
724 *fLog << warn << dec;
725 *fLog << "Warning - number of read events (" << GetNumExecutions()-1;
726 *fLog << ") doesn't match number in run header(s) (";
727 *fLog << fRunHeader->GetNumReuse() << "*" << fNumEvents << "=" << n << ")." << endl;
728
729
730 return kTRUE;
731}
732
Note: See TracBrowser for help on using the repository browser.