Changeset 9941 for trunk/Mars/mcorsika


Ignore:
Timestamp:
09/24/10 15:35:55 (14 years ago)
Author:
tbretz
Message:
Implemented reading of a single telescope from an eventio file.
Location:
trunk/Mars/mcorsika
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mcorsika/MCorsikaEvtHeader.cc

    r9937 r9941  
    160160    // f[96] // Number of reuse of event [max=20]
    161161
     162    memcpy(fTempX, f +97, 20*sizeof(Float_t));
     163    memcpy(fTempY, f+117, 20*sizeof(Float_t));
     164
    162165    // FIXME: Check fNumReuse<20
    163     fX =  f[117 + fNumReuse];
    164     fY = -f[ 97 + fNumReuse];
     166//    fX =  f[117 + fNumReuse];
     167//    fY = -f[ 97 + fNumReuse];
    165168
    166169    fWeightedNumPhotons = 0;
  • trunk/Mars/mcorsika/MCorsikaEvtHeader.h

    r9937 r9941  
    4040    Float_t  fWeightedNumPhotons;     // weighted number of photons arriving at observation level
    4141
     42    Float_t fTempX[20];               //! Temporary storage for impact parameter
     43    Float_t fTempY[20];               //! Temporary storage for impact parameter
     44
    4245public:
    4346    MCorsikaEvtHeader(const char *name=NULL, const char *title=NULL);
     
    6770    void ResetNumReuse() { fNumReuse=(UInt_t)-1; }
    6871
     72    void InitXY() { fX=fTempY[fNumReuse]; fY=-fTempX[fNumReuse]; }
     73
    6974    Int_t ReadEvt(MCorsikaFormat *informat);    // read in event header block
    7075    Int_t ReadEvtEnd(MCorsikaFormat *informat); // read in event end block
  • trunk/Mars/mcorsika/MCorsikaFormat.cc

    r9938 r9941  
    198198// returns kFALSE;
    199199//
    200 Bool_t MCorsikaFormatRaw::GetNextEvent(Float_t ** buffer, Bool_t & readError)
     200Int_t MCorsikaFormatRaw::GetNextEvent(Float_t **buffer, UInt_t)
    201201{
    202202    static Float_t data[273];
     
    204204
    205205    if (next == data + 273)
    206         {
     206    {
    207207        // read next block of events
    208208        if (!ReadData(273, data))
    209             {
    210             readError = kTRUE;
    211             return kFALSE;
    212             }
     209            return kERROR;
    213210
    214211        if (!memcmp(data, "EVTE", 4))
    215             {
     212        {
    216213            // we found the end of list of events
    217214            UnreadLastData();
    218215            return kFALSE;
    219             }
     216        }
    220217
    221218        next = data;
    222         }
     219    }
    223220
    224221    *buffer = next;
     
    226223
    227224    return kTRUE;
    228 
    229225}
    230226
     
    259255
    260256        if (fIn->eof())
    261             {
    262             gLog << err << "ERROR - Missing identifier: " << id  <<
    263                    " type: " << type << endl;
     257        {
     258            gLog << err << "MCorsikaFormatEventIO::SeekNextBlock: ERROR - unexpected end-of-file." << endl;
    264259            return kFALSE;
    265             }
    266 
    267         unsigned short fileType = blockHeader[1] & 0xFFFF;
    268         if (type == fileType)
     260        }
     261
     262        const unsigned short objType = blockHeader[1] & 0xFFFF;
     263        if (type == objType)
    269264            break;
    270265
    271          if (type == 1202 && fileType == 1210)
    272             // we are looking for a event header, but found a runEnd.
    273             // This will happen at the end of a run. We stop here looking for
    274             // the next event header
     266        // we are looking for a event header, but found a runEnd.
     267        // This will happen at the end of a run. We stop here looking for
     268        // the next event header
     269        if (type == 1202 && objType == 1210)
    275270            return kFALSE;
    276271
    277272        // a unknown block, we jump to the next one
    278 //gLog << "unknown: " <<  id << " type: " << fileType << " sub-blocks: " <<  (blockHeader[3]>>29);
    279 //gLog <<  " length: " << (blockHeader[3] & 0x3fffffff) <<   "  pos: " << fIn->tellg() << endl;
    280273        const int length = blockHeader[3] & 0x3fffffff;
    281        
    282         fIn->seekg(length - 2 * (Int_t)(sizeof(int)), ios::cur);   
     274
     275        fIn->seekg(length - 2 * (Int_t)(sizeof(int)), ios::cur);
    283276    }
    284277
     
    307300
    308301    // is the RUNE block at the very end of the file?
    309     std::streampos currentPos = fIn->tellg();
     302    const streampos currentPos = fIn->tellg();
    310303
    311304    fIn->seekg(-32, ios::end);
     
    332325    UnreadLastHeader();
    333326    fRunePos = fIn->tellg();
     327
    334328    return kTRUE;
    335329}
     
    342336// returns kFALSE;                                                           
    343337//
    344 Bool_t MCorsikaFormatEventIO::GetNextEvent(Float_t ** buffer,
    345                                            Bool_t & readError)
    346 {
    347 
    348     static Float_t * data = NULL;
    349     static Float_t * next;
    350     static Int_t   topLevelLength = 0;
    351     static Int_t   eventLength = 0;
    352 
    353 
    354     while (eventLength == 0)
    355         {
     338Int_t MCorsikaFormatEventIO::GetNextEvent(Float_t **buffer, UInt_t telescope)
     339{
     340    static Float_t *data = NULL;
     341    static Int_t    lengthEvent      = -1;
     342    static Int_t    lengthPhotonData = 0;
     343
     344    while (lengthPhotonData == 0)
     345    {
    356346        delete [] data;
    357347        data = NULL;
    358348
    359         if (topLevelLength == 0)
    360             {
    361             if (!NextTopLevelBlock(topLevelLength, readError))
    362                 return kFALSE;
    363             }
    364 
    365         if (!NextEventBlock(eventLength, readError))
    366             return kFALSE;
    367         topLevelLength -= eventLength + 3 * sizeof(int);
    368 
    369         // read next block of events
    370         data = new Float_t [eventLength / sizeof(Float_t)];
    371         if (!ReadData(eventLength / sizeof(Float_t), data, 0))
    372             {
     349        // If we are at the end of the event signal this and
     350        // start next time with a new event
     351        if (lengthEvent==0)
     352        {
     353            lengthEvent=-1;
     354            return kFALSE; // Signal to start with a new event (process task list first)
     355        }
     356
     357        // Look for the next top level block (an "event", object type 1204)
     358        // A top level block contains the data of a full array simulation
     359        // (a single re-usage of the shower)
     360        if (lengthEvent<0)
     361        {
     362            // The length of the block is stored and we use it to determine
     363            // when the next top level block is expected
     364            const Int_t rc = NextEventObject(lengthEvent);
     365            if (rc!=kTRUE)
     366                return rc;
     367        }
     368
     369        // Look for the next event photon bunch (object type 1205)
     370        const Int_t tel = NextPhotonObject(lengthPhotonData);
     371        if (tel<0)
     372            return kERROR;
     373
     374        // Decrease the distance to the next "event" by the current photon bunches
     375        lengthEvent -= lengthPhotonData + 3 * sizeof(int);
     376
     377        // If the telescope is not the one we want to read skip the photon data
     378        if (UInt_t(tel)!=telescope)
     379        {
     380            fPrevPos = fIn->tellg();
     381            fIn->seekg(lengthPhotonData, ios::cur);
     382            lengthPhotonData=0;
     383            continue;
     384        }
     385
     386        const UInt_t cnt = lengthPhotonData / sizeof(Float_t);
     387
     388        // Read next object (photon bunch)
     389        data = new Float_t[cnt];
     390        if (!ReadData(cnt, data, 0))
     391        {
    373392            delete [] data;
    374393            data = NULL;
    375             readError = kTRUE;
    376             return kFALSE;
    377             }
    378         next = data + 3;       
    379         eventLength -= 3 * sizeof(Float_t);
    380         }
    381 
    382     eventLength -= 8 * sizeof(Float_t);
    383     *buffer = next;
    384     next += 8;
    385 
     394            return kERROR;
     395        }
     396
     397        // The object containing the photon bunches starts with:
     398        // Array[short], Telescope[Short], NumPhotons[Real], NumBunches[Long]
     399        // eventLength contains now the number of bytes with photon data not yet evaluated
     400        lengthPhotonData -= 3 * sizeof(Float_t);
     401    }
     402
     403    // The photon data itself is 8 floats long. When we return we expect this to be
     404    // evaluated already when the function is called the next time.
     405    lengthPhotonData -= 8 * sizeof(Float_t);
     406
     407    // Return the pointer to the start of the current photon data
     408    *buffer = data + 3;
    386409
    387410    return kTRUE;
     
    394417// EventEnd block (1209). In this case kFALSE is returned
    395418//
    396 Bool_t MCorsikaFormatEventIO::NextTopLevelBlock(Int_t & length,
    397                                                 Bool_t & readError) const
    398 {
    399     Int_t blockHeader[4];
    400 
     419Int_t MCorsikaFormatEventIO::NextEventObject(Int_t &length) const
     420{
    401421    while (1)
    402422    {
     
    405425        //         - identification field
    406426        //         - length
     427
     428        UInt_t blockHeader[4];
    407429        fIn->read((char*)blockHeader, 4 * sizeof(Int_t));
    408430
    409431        if (fIn->eof())
    410             {
    411             gLog << err << "ERROR - Missing identifier: 1204 or 1209" << endl;
    412             readError = kTRUE;
    413             return kFALSE;
    414             }
    415 
     432        {
     433            gLog << err << "MCorsikaFormatEventIO::NextEventObject: ERROR - Unexpected end-of-file." << endl;
     434            return kERROR;
     435        }
     436
     437        // For speed reasons we skip the check of the synchronization marker
     438
     439        // Decode the object type
     440        const unsigned short objType = blockHeader[1] & 0xFFFF;
     441
     442        // Decode length of block
    416443        length = blockHeader[3] & 0x3fffffff;
    417         const unsigned short fileType = blockHeader[1] & 0xFFFF;
    418         if (fileType == 1204)
     444
     445        // Check if we found the next array (reuse / impact parameter)
     446        // blockHeader[2] == array number (reuse)
     447        if (objType==1204)
    419448            return kTRUE;
    420449
    421         if (fileType == 1209)
    422             {
    423             // we found an eventEnd block, reset file pointer
    424             fIn->seekg( -4 * (Int_t)(sizeof(Int_t)), ios::cur);
    425             length = 0;
    426             return kFALSE;
    427             }
    428 
     450        // we found an eventEnd block, reset file pointer
     451        if (objType==1209)
     452            break;
    429453
    430454        // a unknown block, we jump to the next one
    431         fIn->seekg(length, ios::cur);   
    432     }
    433 
    434     return kTRUE;
    435 }
    436 
    437 // --------------------------------------------------------------------------
    438 //
    439 Bool_t MCorsikaFormatEventIO::NextEventBlock(Int_t & length,
    440                                              Bool_t & readError) const
    441 {
    442     Int_t blockHeader[3];
     455        fIn->seekg(length, ios::cur);
     456    }
     457
     458    fIn->seekg( -4 * (Int_t)(sizeof(Int_t)), ios::cur);
     459    length = 0;
     460
     461    return kFALSE;
     462}
     463
     464// --------------------------------------------------------------------------
     465//
     466Int_t MCorsikaFormatEventIO::NextPhotonObject(Int_t &length) const
     467{
     468    UInt_t blockHeader[3];
    443469
    444470    // we read - synchronisation marker
     
    446472    //         - identification field
    447473    //         - length
    448     fIn->read((char*)blockHeader, 3 * sizeof(Int_t));
     474    fIn->read((char*)blockHeader, 3 * sizeof(UInt_t));
    449475
    450476    if (fIn->eof())
    451         {
    452         gLog << err << "ERROR - Missing identifier: 1205" << endl;
    453         readError = kTRUE;
    454         return kFALSE;
    455         }
    456    
    457     const unsigned short fileType = blockHeader[0] & 0xFFFF;
    458     if (fileType != 1205)
    459         {
    460         gLog << err << "ERROR - Unexpected type: " << fileType << "expected 1205" << endl;
    461         readError = kTRUE;
    462         return kFALSE;
    463         }
     477    {
     478        gLog << err << "MCorsikaFormatEventIO::NextPhotonObject: ERROR - Unexpected end-of-file.." << endl;
     479        return -1;
     480    }
     481
     482    const unsigned short objType = blockHeader[0] & 0xFFFF;
     483    if (objType != 1205)
     484    {
     485        gLog << err << "MCorsikaFormatEventIO::NextPhotonObject: ERROR - Unexpected EventIO object type " << objType << " (expected 1205)." << endl;
     486        return -1;
     487    }
    464488
    465489    const unsigned short version = (blockHeader[0] >> 20) & 0x0FFF;
    466490    if (version != 0)
    467         {
    468         gLog << err << "ERROR - Unexpected version: " << version << "expected: 0" << endl;
    469         readError = kTRUE;
    470         return kFALSE;
    471         }
     491    {
     492        gLog << err << "MCorsikaFormatEventIO::NextPhotonObject: ERROR - Unexpected EventIO object version " << version << " (expected 0 for object type 1205)." << endl;
     493        return -1;
     494    }
    472495
    473496    length = blockHeader[2] & 0x3fffffff;
    474497
    475     return kTRUE;
    476 
    477 }
     498    // blockHeader[1] == 1000 x array number (reuse) + telescope number
     499    return blockHeader[1] % 1000;
     500}
  • trunk/Mars/mcorsika/MCorsikaFormat.h

    r9938 r9941  
    22#define MARS_MDataFormat
    33
     4#ifndef MARS_MAGIC
     5#include "MAGIC.h"
     6#endif
    47
    58#ifndef ROOT_Rtypes
     
    3538   virtual void   ResetPos() const;
    3639
    37    virtual Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError) = 0;
     40   virtual Int_t  GetNextEvent(Float_t **buffer, UInt_t tel=0) = 0;
    3841   virtual Bool_t IsEventioFormat() const = 0;
    3942
     
    5962   Bool_t SeekEvtEnd();
    6063
    61    Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError);
     64   Int_t  GetNextEvent(Float_t **buffer, UInt_t);
    6265   Bool_t IsEventioFormat() const {return kFALSE;}
    6366};
     
    6770{
    6871private:
    69         std::streampos fRunePos; // file position of the RUNE block
     72    std::streampos fRunePos; // file position of the RUNE block
    7073
    7174public:
    72    MCorsikaFormatEventIO(std::istream * in)
     75    MCorsikaFormatEventIO(std::istream *in)
    7376        : MCorsikaFormat(in) {fRunePos = std::streampos(0);}
    7477
    75    Bool_t SeekNextBlock(const char * id, unsigned short type) const;
    76    void   UnreadLastHeader() const;
     78    Bool_t SeekNextBlock(const char *id, unsigned short type) const;
     79    void   UnreadLastHeader() const;
    7780
    78    Bool_t SeekEvtEnd();
     81    Bool_t SeekEvtEnd();
    7982
    80    Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError);
    81    Bool_t IsEventioFormat() const {return kTRUE;}
     83    Int_t  GetNextEvent(Float_t **buffer, UInt_t tel);
     84    Bool_t IsEventioFormat() const { return kTRUE; }
    8285
    8386private:
    84    Bool_t NextTopLevelBlock(Int_t & length, Bool_t & readError) const;
    85    Bool_t NextEventBlock(Int_t & length, Bool_t & readError) const;
    86 
     87    Int_t  NextEventObject(Int_t &length) const;
     88    Int_t  NextPhotonObject(Int_t &length) const;
    8789};
    8890
  • trunk/Mars/mcorsika/MCorsikaRead.cc

    r9937 r9941  
    9696//
    9797MCorsikaRead::MCorsikaRead(const char *fname, const char *name, const char *title)
    98     : fRunHeader(0), fEvtHeader(0), fEvent(0), /*fEvtData(0),*/ fForceMode(kFALSE),
     98    : fRunHeader(0), fEvtHeader(0), fEvent(0), fTelescopeIdx(0), fForceMode(kFALSE),
    9999    fFileNames(0), fNumFile(0), fNumEvents(0), fNumTotalEvents(0),
    100100    fIn(0),  fInFormat(0), fParList(0)
     
    212212
    213213    const char *expname = gSystem->ExpandPathName(name);
    214     fInFormat = CorsikaFormatFactory(fLog, expname);
     214    fInFormat = MCorsikaFormat::CorsikaFormatFactory(expname);
    215215    delete [] expname;
    216216
     
    401401        fInFormat->StorePos();
    402402        fEvtHeader->ResetNumReuse();
     403
     404        if (fInFormat->IsEventioFormat())
     405        {
     406            // Read the event header
     407            const Int_t rc1 = fEvtHeader->ReadEvt(fInFormat);
     408            if (rc1!=kTRUE)
     409                return rc1;
     410        }
     411
    403412    }
    404413    else
    405         fInFormat->ResetPos();
     414    {
     415        if (!fInFormat->IsEventioFormat())
     416            fInFormat->ResetPos();
     417    }
     418
     419    if (!fInFormat->IsEventioFormat())
     420    {
     421        // Read the event header
     422        const Int_t rc1 = fEvtHeader->ReadEvt(fInFormat);
     423        if (rc1!=kTRUE)
     424            return rc1;
     425
     426    }
    406427
    407428    fEvtHeader->IncNumReuse();
    408 
    409     // Read the event header
    410     const Int_t rc1 = fEvtHeader->ReadEvt(fInFormat);
    411     if (rc1!=kTRUE)
    412         return rc1;
    413 
    414     // Check if reusage number should be reset and increase it
    415     //  Note, that the trick here is that after reset it is set to -1
     429    fEvtHeader->InitXY();
     430
     431    // In the case of corsika raw data (MMCS only) we have too loop over one event
     432    // several times to read all impact parameters (reusage of events)
     433    // In case of the eventio format we have to decide, the data of which telescope
     434    // we want to extract
     435    const Int_t id = fInFormat->IsEventioFormat() ? fTelescopeIdx : fEvtHeader->GetNumReuse()+1;
    416436
    417437    // Read the photons corresponding to the i-th core location
    418     const Int_t rc2 = fEvent->ReadCorsikaEvt(fInFormat, fEvtHeader->GetNumReuse()+1);
     438    //  EventIO: Number of telescope
     439    //  Raw:     Number of re-use
     440    const Int_t rc2 = fEvent->ReadCorsikaEvt(fInFormat, id);
    419441    if (rc2!=kTRUE)
    420442        return rc2;
     443
     444    //----
     445    if (fInFormat->IsEventioFormat())
     446    {
     447        fEvent->AddXY(fEvtHeader->GetX(), fEvtHeader->GetY());
     448        fEvent->SimWavelength(fRunHeader->GetWavelengthMin(), fRunHeader->GetWavelengthMax());
     449        return kTRUE;
     450    }
     451    //----
     452
    421453
    422454    // read event end
  • trunk/Mars/mcorsika/MCorsikaRead.h

    r9937 r9941  
    2222    MPhotonEvent      *fEvent;      //! event information
    2323
     24    UInt_t          fTelescopeIdx;  // Index of telescope to be extracted
    2425    Bool_t          fForceMode;     // Force mode skipping defect RUNE
    2526
     
    5253    ~MCorsikaRead();
    5354
    54     //static Byte_t IsFileValid(const char *name);
    55 
    56     //void SetInterleave(UInt_t i) { fInterleave = i; }
    5755    void SetForceMode(Bool_t b=kTRUE) { fForceMode=b; }
     56    void SetTelescopeIdx(UInt_t idx=0) { fTelescopeIdx = idx; }
    5857
    5958    TString GetFullFileName() const;
Note: See TracChangeset for help on using the changeset viewer.