Changeset 9943 for trunk


Ignore:
Timestamp:
09/28/10 10:11:31 (14 years ago)
Author:
tbretz
Message:
Implemented a working version of eventio which reads all telescope's data and all array's data.
Location:
trunk/Mars/mcorsika
Files:
5 edited

Legend:

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

    r9941 r9943  
    7171
    7272    void InitXY() { fX=fTempY[fNumReuse]; fY=-fTempX[fNumReuse]; }
     73    void AddXY(Float_t x, Float_t y) { fX+=x; fY+=y; }
    7374
    7475    Int_t ReadEvt(MCorsikaFormat *informat);    // read in event header block
  • trunk/Mars/mcorsika/MCorsikaFormat.cc

    r9941 r9943  
    7272}
    7373
     74void MCorsikaFormat::Read(void *ptr, Int_t i) const
     75{
     76    fIn->read((char*)ptr, i);
     77}
     78
    7479// --------------------------------------------------------------------------
    7580//
     
    122127{
    123128    fIn->seekg(fPos, ios::beg);
     129}
     130
     131void MCorsikaFormat::Rewind() const
     132{
     133    fIn->seekg(0, ios::beg);
    124134}
    125135
     
    198208// returns kFALSE;
    199209//
    200 Int_t MCorsikaFormatRaw::GetNextEvent(Float_t **buffer, UInt_t)
     210Int_t MCorsikaFormatRaw::GetNextEvent(Float_t **buffer, Int_t)
    201211{
    202212    static Float_t data[273];
     
    242252Bool_t MCorsikaFormatEventIO::SeekNextBlock(const char * id, unsigned short type) const
    243253{
    244     int blockHeader[7];
    245 
     254    cout << "Seek " << type << endl;
    246255    while (1)
    247256    {
     
    252261        //         - unknown field
    253262        //         - id (first 4 bytes of data field)
    254         fIn->read((char*)blockHeader, 6 * sizeof(int));
     263        int blockHeader[4];
     264        fIn->read((char*)blockHeader, 4 * sizeof(int));
    255265
    256266        if (fIn->eof())
    257267        {
    258             gLog << err << "MCorsikaFormatEventIO::SeekNextBlock: ERROR - unexpected end-of-file." << endl;
     268            gLog << err << "MCorsikaFormatEventIO::SeekNextBlock: ERROR - Unexpected end-of-file seeking " << id << " (" << type << ")." << endl;
    259269            return kFALSE;
    260270        }
    261271
    262272        const unsigned short objType = blockHeader[1] & 0xFFFF;
     273        cout << "objType=" << objType << endl;
    263274        if (type == objType)
    264             break;
     275        {
     276            if (!id)
     277                break;
     278
     279            char c[9];
     280            fIn->read(c, 8);
     281
     282            if (memcmp(c+4, id, 4)==0)
     283                break;
     284
     285            c[8] = 0;
     286            gLog << err << "MCorsikaFormatEventIO::SeekNextBlock: ERROR - '" << c+4 << "' doesn't match expected one '" << id << "' in object " << type << endl;
     287            return kFALSE;
     288        }
    265289
    266290        // we are looking for a event header, but found a runEnd.
     
    272296        // a unknown block, we jump to the next one
    273297        const int length = blockHeader[3] & 0x3fffffff;
    274 
    275         fIn->seekg(length - 2 * (Int_t)(sizeof(int)), ios::cur);
     298        fIn->seekg(length, ios::cur);
    276299    }
    277300
     
    336359// returns kFALSE;                                                           
    337360//
    338 Int_t MCorsikaFormatEventIO::GetNextEvent(Float_t **buffer, UInt_t telescope)
     361Int_t MCorsikaFormatEventIO::GetNextEvent(Float_t **buffer, Int_t telescope)
    339362{
    340363    static Float_t *data = NULL;
    341     static Int_t    lengthEvent      = -1;
    342     static Int_t    lengthPhotonData = 0;
    343 
    344     while (lengthPhotonData == 0)
     364
     365    static int lengthPhotonData = 0;
     366    static int lengthEvent      = 0;
     367
     368    if (lengthPhotonData>0)
     369    {
     370        cout << "Return Bunch l2=" << lengthPhotonData << endl;
     371
     372        lengthPhotonData -= 8 * sizeof(Float_t);
     373        *buffer += 8; // Return the pointer to the start of the current photon data
     374        return kTRUE;
     375    }
     376
     377    if (data)
    345378    {
    346379        delete [] data;
    347380        data = NULL;
    348 
    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)
     381        cout << "Return end-of-tel LE=" << lengthEvent << endl;
     382        return kFALSE;
     383    }
     384
     385    if (lengthEvent==0)
     386    {
     387        cout << "Searching 1204... " << flush;
     388        // The length of the block is stored and we use it to determine
     389        // when the next top level block is expected
     390        const Int_t rc = NextEventObject(lengthEvent);
     391        if (rc==kERROR)
     392            return kERROR;
     393        if (!rc)
    352394        {
    353             lengthEvent=-1;
    354             return kFALSE; // Signal to start with a new event (process task list first)
     395            cout << "skip EVTE." << endl;
     396            return kFALSE;
    355397        }
    356398
    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         {
    392             delete [] data;
    393             data = NULL;
    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.
     399        cout << "found new array." << endl;
     400    }
     401
     402    cout << "Searching 1205..." << flush;
     403
     404    // Look for the next event photon bunch (object type 1205)
     405    const Int_t tel = NextPhotonObject(lengthPhotonData);
     406    if (tel<0)
     407        return kERROR;
     408
     409    lengthEvent -= lengthPhotonData+12; // Length of data + Length of header
     410
     411    lengthPhotonData -= 12;     // Length of data before the photon bunches
     412    fIn->seekg(12, ios::cur);   // Skip this data
     413
     414    cout << " found (tel=" << tel << ",LEN=" << lengthEvent << ",len=" << lengthPhotonData << ")... " << flush;
     415
     416    if (lengthPhotonData==0)
     417    {
     418        cout << "empty!" << endl;
     419        return kFALSE;
     420    }
     421
     422    const UInt_t cnt = lengthPhotonData / sizeof(Float_t);
     423    // Read next object (photon bunch)
     424    data = new Float_t[cnt];
     425    if (!ReadData(cnt, data, 0))
     426    {
     427        delete [] data;
     428        data = NULL;
     429        return kERROR;
     430    }
     431
     432    cout << "return!" << endl;
     433
    405434    lengthPhotonData -= 8 * sizeof(Float_t);
    406 
    407     // Return the pointer to the start of the current photon data
    408     *buffer = data + 3;
     435    *buffer = data; // Return the pointer to the start of the current photon data
    409436
    410437    return kTRUE;
     438}
    411439}
    412440
     
    425453        //         - identification field
    426454        //         - length
    427 
    428455        UInt_t blockHeader[4];
    429456        fIn->read((char*)blockHeader, 4 * sizeof(Int_t));
     
    440467        const unsigned short objType = blockHeader[1] & 0xFFFF;
    441468
     469        cout << "objType=" << objType << endl;
     470
    442471        // Decode length of block
    443472        length = blockHeader[3] & 0x3fffffff;
  • trunk/Mars/mcorsika/MCorsikaFormat.h

    r9941 r9943  
    3737   virtual void   StorePos();
    3838   virtual void   ResetPos() const;
     39   virtual void   Rewind() const;
    3940
    40    virtual Int_t  GetNextEvent(Float_t **buffer, UInt_t tel=0) = 0;
     41   virtual Int_t  GetNextEvent(Float_t **buffer, Int_t tel=0) = 0;
    4142   virtual Bool_t IsEventioFormat() const = 0;
    4243
     
    4445
    4546   std::streampos GetCurrPos() const;
     47
     48   void Read(void *ptr, Int_t i=4) const;
    4649
    4750   static MCorsikaFormat *CorsikaFormatFactory(const char *fileName);
     
    6265   Bool_t SeekEvtEnd();
    6366
    64    Int_t  GetNextEvent(Float_t **buffer, UInt_t);
     67   Int_t  GetNextEvent(Float_t **buffer, Int_t);
    6568   Bool_t IsEventioFormat() const {return kFALSE;}
    6669};
     
    8184    Bool_t SeekEvtEnd();
    8285
    83     Int_t  GetNextEvent(Float_t **buffer, UInt_t tel);
     86    Int_t  GetNextEvent(Float_t **buffer, Int_t tel);
    8487    Bool_t IsEventioFormat() const { return kTRUE; }
    8588
  • trunk/Mars/mcorsika/MCorsikaRead.cc

    r9941 r9943  
    9696//
    9797MCorsikaRead::MCorsikaRead(const char *fname, const char *name, const char *title)
    98     : fRunHeader(0), fEvtHeader(0), fEvent(0), fTelescopeIdx(0), fForceMode(kFALSE),
     98    : fRunHeader(0), fEvtHeader(0), fEvent(0), fTelescopeIdx(-1), fForceMode(kFALSE),
    9999    fFileNames(0), fNumFile(0), fNumEvents(0), fNumTotalEvents(0),
    100     fIn(0),  fInFormat(0), fParList(0)
     100    /*fIn(0),*/  fInFormat(0), fParList(0), fNumTelescopes(1)
    101101{
    102102    fName  = name  ? name  : "MRead";
     
    117117{
    118118    delete fFileNames;
    119     if (fIn)
    120         delete fIn;
     119//    if (fIn)
     120//        delete fIn;
    121121    if (fInFormat)
    122122        delete fInFormat;
     
    191191    // open the input stream and check if it is really open (file exists?)
    192192    //
    193     if (fIn)
     193/*    if (fIn)
    194194        delete fIn;
    195195    fIn = NULL;
    196 
     196*/
    197197    if (fInFormat)
    198198       delete fInFormat;
     
    239239//    if (!fEvtHeader->ReadRunHeader(*fIn, *fRunHeader))
    240240//        return kERROR;
     241
     242    fNumTelescopes = 1;
     243    if (fInFormat->IsEventioFormat())
     244    {
     245        fInFormat->StorePos();
     246        fInFormat->Rewind();
     247
     248        if (!fInFormat->SeekNextBlock(0, 1201))
     249        {
     250            *fLog << err << "ERROR - Object 1201 not found in file." << endl;
     251            return kERROR;
     252        }
     253
     254        fInFormat->Read(&fNumTelescopes);
     255
     256        if (fTelescopeIdx>=fNumTelescopes)
     257        {
     258            *fLog << err << "ERROR - Requested telescope index " << fTelescopeIdx << " exceeds number of telescopes " << fNumTelescopes << " in file." << endl;
     259            return kERROR;
     260        }
     261
     262        fTelescopeX.Set(fNumTelescopes);
     263        fTelescopeY.Set(fNumTelescopes);
     264        fTelescopeZ.Set(fNumTelescopes);
     265        fTelescopeR.Set(fNumTelescopes);
     266
     267        fInFormat->Read(fTelescopeX.GetArray(), 4*fNumTelescopes);
     268        fInFormat->Read(fTelescopeY.GetArray(), 4*fNumTelescopes);
     269        fInFormat->Read(fTelescopeZ.GetArray(), 4*fNumTelescopes);
     270        fInFormat->Read(fTelescopeR.GetArray(), 4*fNumTelescopes);
     271
     272        *fLog << all;
     273        *fLog << "Number of telescopes: " << fNumTelescopes << " (using ";
     274        if (fTelescopeIdx>=0)
     275            *fLog << "telescope " << fTelescopeIdx;
     276        else
     277            *fLog << "all telescopes";
     278        *fLog << ")" << endl;
     279
     280        const Int_t lo = fTelescopeIdx<0 ? 0              : fTelescopeIdx;
     281        const Int_t up = fTelescopeIdx<0 ? fNumTelescopes : fTelescopeIdx+1;
     282
     283        for (int i=lo; i<up; i++)
     284        {
     285            *fLog << inf << "Telescope #" << setw(4) << i << " [X/Y/Z (R)]: ";
     286            *fLog << setw(7) << fTelescopeX[i] << "/";
     287            *fLog << setw(7) << fTelescopeY[i] << "/";
     288            *fLog << setw(7) << fTelescopeZ[i] << "  (R=" << setw(4) << fTelescopeR[i] << ")" << endl;
     289        }
     290
     291        fNumTelescope = 0;
     292
     293        fInFormat->ResetPos();
     294    }
     295
    241296
    242297    fInFormat->StorePos();
     
    319374            }
    320375
    321             fNumTotalEvents += fRunHeader->GetNumEvents()*fRunHeader->GetNumReuse();
     376            fNumTotalEvents += fRunHeader->GetNumEvents()*fRunHeader->GetNumReuse()*
     377                (fTelescopeIdx<0 ? fNumTelescopes : 1);
    322378            continue;
    323379        }
    324380        break;
    325381    }
    326 
     382/*
    327383    if (fIn)
    328384        delete fIn;
    329385    fIn = NULL;
    330 
     386*/
    331387    return rc;
    332388}
     
    356412    //
    357413    fParList = 0;
    358 
     414/*
    359415    if (!OpenStream())
    360416        return kFALSE;
    361 
     417*/
    362418    //
    363419    //  check if all necessary containers exist in the Parameter list.
     
    395451Int_t MCorsikaRead::ReadEvent()
    396452{
    397     // Store the position to re-read a single event several times
    398     //  FIXME: Does this work with EventIO, too?
    399     if (fEvtHeader->GetNumReuse()>=fRunHeader->GetNumReuse()-1)
    400     {
    401         fInFormat->StorePos();
    402         fEvtHeader->ResetNumReuse();
    403 
    404         if (fInFormat->IsEventioFormat())
     453    if (fInFormat->IsEventioFormat())
     454    {
     455        if (fNumTelescope==0)
    405456        {
    406             // Read the event header
    407457            const Int_t rc1 = fEvtHeader->ReadEvt(fInFormat);
    408458            if (rc1!=kTRUE)
    409459                return rc1;
     460
     461            if (fEvtHeader->GetNumReuse()==fRunHeader->GetNumReuse()-1)
     462                fEvtHeader->ResetNumReuse();
     463            fEvtHeader->IncNumReuse();
     464
     465            cout << "===== Array " << fEvtHeader->GetNumReuse() << " =====" << endl;
    410466        }
    411467
     468        const Int_t rc2 = fEvent->ReadCorsikaEvt(fInFormat, fTelescopeIdx);
     469        if (rc2!=kTRUE)
     470            return rc2;
     471
     472        fEvtHeader->InitXY();
     473
     474        const Double_t x = fTelescopeX[fNumTelescope];
     475        const Double_t y = fTelescopeY[fNumTelescope];
     476
     477        fEvtHeader->AddXY(y, -x);
     478        fEvent->AddXY(fEvtHeader->GetX(), fEvtHeader->GetY());
     479        fEvent->SimWavelength(fRunHeader->GetWavelengthMin(), fRunHeader->GetWavelengthMax());
     480
     481        fNumTelescope++;
     482        fNumTelescope%=fNumTelescopes;
     483
     484        return kTRUE;
    412485    }
    413486    else
    414487    {
    415         if (!fInFormat->IsEventioFormat())
     488        // Store the position to re-read a single event several times
     489        if (fEvtHeader->GetNumReuse()<fRunHeader->GetNumReuse()-1)
    416490            fInFormat->ResetPos();
    417     }
    418 
    419     if (!fInFormat->IsEventioFormat())
    420     {
     491        else
     492        {
     493            fInFormat->StorePos();
     494            fEvtHeader->ResetNumReuse();
     495        }
     496
    421497        // Read the event header
    422498        const Int_t rc1 = fEvtHeader->ReadEvt(fInFormat);
     
    424500            return rc1;
    425501
    426     }
    427 
    428     fEvtHeader->IncNumReuse();
    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;
    436 
    437     // Read the photons corresponding to the i-th core location
    438     //  EventIO: Number of telescope
    439     //  Raw:     Number of re-use
    440     const Int_t rc2 = fEvent->ReadCorsikaEvt(fInFormat, id);
    441     if (rc2!=kTRUE)
    442         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 
    453 
    454     // read event end
    455     return fEvtHeader->ReadEvtEnd(fInFormat);
     502        fEvtHeader->IncNumReuse();
     503        fEvtHeader->InitXY();
     504
     505        // In the case of corsika raw data (MMCS only) we have too loop over one event
     506        // several times to read all impact parameters (reusage of events)
     507        // In case of the eventio format we have to decide, the data of which telescope
     508        // we want to extract
     509
     510        // Read the photons corresponding to the i-th core location
     511        //  EventIO: Number of telescope
     512        //  Raw:     Number of re-use
     513        const Int_t rc2 = fEvent->ReadCorsikaEvt(fInFormat, fEvtHeader->GetNumReuse()+1);
     514        if (rc2!=kTRUE)
     515            return rc2;
     516
     517        // read event end
     518        return fEvtHeader->ReadEvtEnd(fInFormat);
     519    }
    456520}
    457521
     
    502566Int_t MCorsikaRead::PostProcess()
    503567{
    504     const UInt_t n = fNumEvents*fRunHeader->GetNumReuse();
     568    const UInt_t n = fNumEvents*fRunHeader->GetNumReuse()*(fTelescopeIdx<0?fNumTelescopes:1);
    505569
    506570    //
     
    517581    return kTRUE;
    518582}
     583
     584// --------------------------------------------------------------------------
     585//
     586//    Telescope: 1
     587//
     588// In the case of eventio-format select which telescope to extract from the
     589// file. -1 will extract all telescopes
     590//
     591Int_t MCorsikaRead::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
     592{
     593    Bool_t rc = kFALSE;
     594    if (IsEnvDefined(env, prefix, "Telescope", print))
     595    {
     596        rc = kTRUE;
     597        fTelescopeIdx = GetEnvValue(env, prefix, "Telescope", fTelescopeIdx);
     598    }
     599
     600    return rc;
     601}
  • trunk/Mars/mcorsika/MCorsikaRead.h

    r9941 r9943  
    44#ifndef MARS_MRead
    55#include "MRead.h"
     6#endif
     7
     8#ifndef ROOT_TArrayF
     9#include <TArrayF.h>
    610#endif
    711
     
    2226    MPhotonEvent      *fEvent;      //! event information
    2327
    24     UInt_t          fTelescopeIdx;  // Index of telescope to be extracted
     28    Int_t           fTelescopeIdx;  // Index of telescope to be extracted
    2529    Bool_t          fForceMode;     // Force mode skipping defect RUNE
    2630
     
    3034    UInt_t    fNumTotalEvents; //! total number of events in all files
    3135
    32     ifstream       *fIn;       //! input stream (file to read from)
     36//    ifstream       *fIn;       //! input stream (file to read from)
    3337    MCorsikaFormat *fInFormat; //! access to input corsika data
    3438
    3539    MParList *fParList;        //! tasklist to call ReInit from
    3640
     41    Int_t  fNumTelescopes;     //! Number of telescopes in array
     42    Int_t  fNumTelescope;      //! Number of telescope currently being read
     43    TArrayF fTelescopeX;       //! x pos (measured towards north, unit: cm)
     44    TArrayF fTelescopeY;       //! y pos (measured towards west,  unit: cm)
     45    TArrayF fTelescopeZ;       //! z pos (from detection level,   unit: cm)
     46    TArrayF fTelescopeR;       //! Radii of spheres around tel. (unit: cm)
     47
    3748    //UInt_t    fInterleave;
    3849    //Bool_t    fForce;
    3950
    40     virtual Bool_t OpenStream() { return kTRUE; }
     51//    virtual Bool_t OpenStream() { return kTRUE; }
    4152
    4253    Bool_t ReadEvtEnd();
     
    4556    Int_t  ReadEvent();
    4657
     58    // MTask
    4759    Int_t PreProcess(MParList *pList);
    4860    Int_t Process();
    4961    Int_t PostProcess();
     62
     63    // MParContainer
     64    Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print);
    5065
    5166public:
     
    5469
    5570    void SetForceMode(Bool_t b=kTRUE) { fForceMode=b; }
    56     void SetTelescopeIdx(UInt_t idx=0) { fTelescopeIdx = idx; }
     71    void SetTelescopeIdx(Int_t idx=-1) { fTelescopeIdx = idx; }
    5772
    5873    TString GetFullFileName() const;
Note: See TracChangeset for help on using the changeset viewer.