Changeset 9938


Ignore:
Timestamp:
Sep 22, 2010, 4:15:22 PM (10 years ago)
Author:
tbretz
Message:
Some cleanup in MCorsikaFormat.
Location:
trunk/Mars
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/Changelog

    r9937 r9938  
    7575     - added functions to calculate mean x and mean y
    7676     - propagate re-usage counter through ReadEvent
     77
     78   * mcorsika/MCorsikaFormat.[h,cc]:
     79     - a few clean-ups to the code
     80     - added standard file header
     81     - a few minor simplifications
     82     - replaced local logging stream with gLog
     83     - added some empty comments
    7784
    7885
  • trunk/Mars/mcorsika/MCorsikaFormat.cc

    r9893 r9938  
     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): Reiner Rohlfs 2010
     19!   Author(s): Thomas Bretz  2010 <mailto:thomas.bretz@epfl.ch>
     20!
     21!   Copyright: Software Development, 2000-2010
     22!
     23!
     24\* ======================================================================== */
     25
     26//////////////////////////////////////////////////////////////////////////////
     27//
     28// MCorsikaFormat
     29//
     30//////////////////////////////////////////////////////////////////////////////
     31#include "MCorsikaFormat.h"
     32
     33#include <errno.h>
    134#include <fstream>
    2 #include <errno.h>
    3 
    435
    536#include "MLogManip.h"
    637
    7 #include "MCorsikaFormat.h"
    8 
    9 
    1038using namespace std;
    1139
    12 MCorsikaFormat * CorsikaFormatFactory(MLog * log, const char * fileName)
     40const unsigned int MCorsikaFormat::kSyncMarker = 0xd41f8a37;
     41
     42// --------------------------------------------------------------------------
     43//
     44MCorsikaFormat *MCorsikaFormat::CorsikaFormatFactory(const char * fileName)
    1345{
    1446    ifstream * fileIn = new ifstream(fileName);
     
    1749    if (noexist)
    1850    {
    19         *log << err << "Cannot open file " << fileName << ": ";
    20         *log << (errno!=0?strerror(errno):"Insufficient memory for decompression") << endl;
     51        gLog << err << "Cannot open file " << fileName << ": ";
     52        gLog << (errno!=0?strerror(errno):"Insufficient memory for decompression") << endl;
    2153        delete fileIn;
    22         fileIn = NULL;
    2354        return NULL;
    2455    }
    2556
    26 
    27     if (!fileIn->is_open())
    28         {
    29         *log << err << "Failed to open file " << fileName << endl;
    30         delete fileIn;
    31         fileIn = NULL;
    32         return NULL;
    33         }
    34    
    35     char buffer[4];
     57    char buffer[5]="\0\0\0\0";
    3658    fileIn->read(buffer, 4);
    3759    fileIn->seekg(-4, ios::cur);
    3860
    39     int * syncmarker = reinterpret_cast<int*>(buffer);
    40     if (memcmp(buffer, "RUNH", 4) == 0)
    41         return new MCorsikaFormatRaw(log, fileIn);
    42          
    43     else if (*syncmarker == -736130505)
    44         return new MCorsikaFormatEventIO(log, fileIn);
    45 
    46     *log << err << "File " << fileName <<
    47             " is neither a corsica file, nor an eventio file" << endl;
     61    if (strcmp(buffer, "RUNH") == 0)
     62        return new MCorsikaFormatRaw(fileIn);
     63
     64    if (*reinterpret_cast<unsigned int*>(buffer) == kSyncMarker)
     65        return new MCorsikaFormatEventIO(fileIn);
     66
     67    gLog << err << "File " << fileName <<
     68            " is neither a CORSIKA raw nor EventIO file" << endl;
    4869    delete fileIn;
    49     fileIn = NULL;
    5070
    5171    return NULL;
    5272}
    5373
    54 ///////////////////////////////////////////////////////////////////////////////
    55 ///////////////////////////////////////////////////////////////////////////////
    56 
     74// --------------------------------------------------------------------------
     75//
     76Bool_t MCorsikaFormat::Eof() const
     77{
     78    return fIn->eof();
     79}
     80
     81// --------------------------------------------------------------------------
     82//
     83streampos MCorsikaFormat::GetCurrPos() const
     84{
     85    return fIn->tellg();
     86}
     87
     88// --------------------------------------------------------------------------
     89//
    5790Bool_t MCorsikaFormat::ReadData(Int_t numValues, Float_t * buffer,
    5891                                Int_t minSeekValues)
     
    70103}
    71104
    72 void MCorsikaFormat::UnreadLastData()
     105// --------------------------------------------------------------------------
     106//
     107void MCorsikaFormat::UnreadLastData() const
    73108{
    74109    fIn->seekg(fPrevPos, ios::beg);
    75110}
    76111
     112// --------------------------------------------------------------------------
     113//
    77114void MCorsikaFormat::StorePos()
    78115{
    79116    fPos = fIn->tellg();
    80 //*fLog << all << "storePos: " << fPos << endl;
    81 }
    82 
    83 void MCorsikaFormat::ResetPos()
     117}
     118
     119// --------------------------------------------------------------------------
     120//
     121void MCorsikaFormat::ResetPos() const
    84122{
    85123    fIn->seekg(fPos, ios::beg);
    86124}
    87125
    88 
    89 ///////////////////////////////////////////////////////////////////////////////
    90 ///////////////////////////////////////////////////////////////////////////////
    91 
    92 
     126// --------------------------------------------------------------------------
     127//
    93128MCorsikaFormat::~MCorsikaFormat()
    94129{
    95130    delete fIn;
    96131}
    97 
    98132
    99133// --------------------------------------------------------------------------
     
    107141// of the id.
    108142//
    109 Bool_t MCorsikaFormatRaw::SeekNextBlock(const char * id, unsigned short type)
    110 {
    111     char blockHeader[5];
     143Bool_t MCorsikaFormatRaw::SeekNextBlock(const char * id, unsigned short type) const
     144{
     145    char blockHeader[5]="\0\0\0\0";
    112146    fIn->read(blockHeader, 4);
    113147
    114     if (memcmp(blockHeader, id, 4))
    115     {
    116         blockHeader[4] = 0;
    117         if (strcmp(id, "EVTH") != 0 || strcmp(blockHeader, "RUNE") != 0 )
    118             {
    119             // at the end of a file we are looking for the next Event header,
    120             // but find the end of a run. This is expected, therefor no error
    121             // message.
    122             *fLog << err << "ERROR - Wrong identifier: " << id << " expected."
    123                   << " But read " << blockHeader << " from file." << endl;
    124             }
    125         return kFALSE;
    126     }
    127 
    128     return kTRUE;
    129 }
    130 
    131 // --------------------------------------------------------------------------
    132 //
    133 void MCorsikaFormatRaw::UnreadLastHeader()
     148    if (strcmp(blockHeader, id)==0)
     149        return kTRUE;
     150
     151    // at the end of a file we are looking for the next Event header,
     152    // but find the end of a run. This is expected, therefor no error
     153    // message.
     154    if (strcmp(id, "EVTH")==0 && strcmp(blockHeader, "RUNE")==0)
     155        return kTRUE;
     156
     157    gLog << err << "ERROR - Wrong identifier: " << id << " expected.";
     158    gLog << " But read " << blockHeader << " from file." << endl;
     159
     160    return kFALSE;
     161}
     162
     163// --------------------------------------------------------------------------
     164//
     165void MCorsikaFormatRaw::UnreadLastHeader() const
    134166{
    135167    fIn->seekg(-4, ios::cur);
    136168}
    137169
     170// --------------------------------------------------------------------------
     171//
    138172Bool_t MCorsikaFormatRaw::SeekEvtEnd()
    139173{
     
    144178        fIn->seekg(-i*273*4, ios::end);
    145179
    146         char runh[4];
     180        char runh[5]="\0\0\0\0";
    147181        fIn->read(runh, 4);
    148182
    149         if (!memcmp(runh, "RUNE", 4))
     183        if (!strcmp(runh, "RUNE"))
    150184        {
    151185            fIn->seekg(-4, ios::cur);
     
    156190    return kTRUE;
    157191}
    158 
    159192
    160193// --------------------------------------------------------------------------
     
    163196// block is found.                                                           
    164197// If a read error occurred, the readError is set to kTRUE and the function 
    165 // returns kFALSE;                                                           
    166 Bool_t MCorsikaFormatRaw::GetNextEvent(Float_t ** buffer, Bool_t & readError)
     198// returns kFALSE;
     199//
     200Bool_t MCorsikaFormatRaw::GetNextEvent(Float_t ** buffer, Bool_t & readError)
    167201{
    168202    static Float_t data[273];
     
    194228
    195229}
    196 
    197230
    198231///////////////////////////////////////////////////////////////////////////////
     
    211244// of the id.
    212245//
    213 Bool_t MCorsikaFormatEventIO::SeekNextBlock(const char * id, unsigned short type)
     246Bool_t MCorsikaFormatEventIO::SeekNextBlock(const char * id, unsigned short type) const
    214247{
    215248    int blockHeader[7];
     
    227260        if (fIn->eof())
    228261            {
    229             *fLog << err << "ERROR - Missing identifier: " << id  <<
     262            gLog << err << "ERROR - Missing identifier: " << id  <<
    230263                   " type: " << type << endl;
    231264            return kFALSE;
     
    233266
    234267        unsigned short fileType = blockHeader[1] & 0xFFFF;
    235         if (type == fileType                       )
     268        if (type == fileType)
    236269            break;
    237270
     
    243276
    244277        // a unknown block, we jump to the next one
    245 //*fLog << "unknown: " <<  id << " type: " << fileType << " sub-blocks: " <<  (blockHeader[3]>>29);
    246 //*fLog <<  " length: " << (blockHeader[3] & 0x3fffffff) <<   "  pos: " << fIn->tellg() << endl;
    247         int length = blockHeader[3] & 0x3fffffff;
     278//gLog << "unknown: " <<  id << " type: " << fileType << " sub-blocks: " <<  (blockHeader[3]>>29);
     279//gLog <<  " length: " << (blockHeader[3] & 0x3fffffff) <<   "  pos: " << fIn->tellg() << endl;
     280        const int length = blockHeader[3] & 0x3fffffff;
    248281       
    249282        fIn->seekg(length - 2 * (Int_t)(sizeof(int)), ios::cur);   
     
    256289// --------------------------------------------------------------------------
    257290//
    258 void MCorsikaFormatEventIO::UnreadLastHeader()
     291void MCorsikaFormatEventIO::UnreadLastHeader() const
    259292{
    260293    fIn->seekg( -6 * (Int_t)(sizeof(int)), ios::cur);
     
    266299{
    267300    if (fRunePos != streampos(0))
    268         {
     301    {
    269302        fIn->seekg(fRunePos, ios::beg);
    270303        return kTRUE;
    271         }
    272     else
    273         {
    274         // it is the first time we are looking for the RUNE block
    275 
    276         // is the RUNE block at the very end of the file?
    277         std::streampos currentPos = fIn->tellg();
    278 
    279         fIn->seekg(-32, ios::end);
    280         unsigned int blockHeader[4];
    281         fIn->read((char*)blockHeader, 4 * sizeof(int));
    282         if ( blockHeader[0]               == 0xd41f8a37 &&
    283             (blockHeader[1] & 0xffff)     == 1210       &&
    284             (blockHeader[3] & 0x3fffffff) == 16)
    285             {
    286             // this seams to be a RUNE (1210)  block
    287             fIn->seekg( -4 * (Int_t)(sizeof(int)), ios::cur);
    288             fRunePos = fIn->tellg();
    289             return kTRUE;
    290             }
    291 
    292         // we do not find a RUNE block at the end of the file
    293         // we have to search in the file
    294         fIn->seekg(currentPos, ios::beg);
    295         if (SeekNextBlock("RUNE", 1210))
    296             {
    297             UnreadLastHeader();
    298             fRunePos = fIn->tellg();
    299             return kTRUE;
    300             }
    301         }
    302     return kFALSE;
     304    }
     305
     306    // it is the first time we are looking for the RUNE block
     307
     308    // is the RUNE block at the very end of the file?
     309    std::streampos currentPos = fIn->tellg();
     310
     311    fIn->seekg(-32, ios::end);
     312
     313    unsigned int blockHeader[4];
     314    fIn->read((char*)blockHeader, 4 * sizeof(int));
     315
     316    if ( blockHeader[0]               == kSyncMarker &&
     317        (blockHeader[1] & 0xffff)     == 1210        &&
     318        (blockHeader[3] & 0x3fffffff) == 16)
     319    {
     320        // this seams to be a RUNE (1210)  block
     321        fIn->seekg( -4 * (Int_t)(sizeof(int)), ios::cur);
     322        fRunePos = fIn->tellg();
     323        return kTRUE;
     324    }
     325
     326    // we do not find a RUNE block at the end of the file
     327    // we have to search in the file
     328    fIn->seekg(currentPos, ios::beg);
     329    if (!SeekNextBlock("RUNE", 1210))
     330        return kFALSE;
     331
     332    UnreadLastHeader();
     333    fRunePos = fIn->tellg();
     334    return kTRUE;
    303335}
    304336
     
    309341// If a read error occurred, the readError is set to kTRUE and the function 
    310342// returns kFALSE;                                                           
    311 Bool_t MCorsikaFormatEventIO::GetNextEvent(Float_t ** buffer,
     343//
     344Bool_t MCorsikaFormatEventIO::GetNextEvent(Float_t ** buffer,
    312345                                           Bool_t & readError)
    313346{
     
    354387    return kTRUE;
    355388}
     389
    356390// --------------------------------------------------------------------------
    357391//                                                                           
    358392// Looks for the next Block with type 1204 and return kTRUE.                 
    359393// The function also stops moving forward in the file, if it finds a         
    360 // EventEnd block (1209). In this case kFALSE is returned                   
     394// EventEnd block (1209). In this case kFALSE is returned
     395//
    361396Bool_t MCorsikaFormatEventIO::NextTopLevelBlock(Int_t & length,
    362                                                 Bool_t & readError)
     397                                                Bool_t & readError) const
    363398{
    364399    Int_t blockHeader[4];
     
    374409        if (fIn->eof())
    375410            {
    376             *fLog << err << "ERROR - Missing identifier: 1204 or 1209" << endl;
     411            gLog << err << "ERROR - Missing identifier: 1204 or 1209" << endl;
    377412            readError = kTRUE;
    378413            return kFALSE;
     
    380415
    381416        length = blockHeader[3] & 0x3fffffff;
    382         unsigned short fileType = blockHeader[1] & 0xFFFF;
     417        const unsigned short fileType = blockHeader[1] & 0xFFFF;
    383418        if (fileType == 1204)
    384419            return kTRUE;
     
    400435}
    401436
     437// --------------------------------------------------------------------------
     438//
    402439Bool_t MCorsikaFormatEventIO::NextEventBlock(Int_t & length,
    403                                              Bool_t & readError)
     440                                             Bool_t & readError) const
    404441{
    405442    Int_t blockHeader[3];
     
    413450    if (fIn->eof())
    414451        {
    415         *fLog << err << "ERROR - Missing identifier: 1205" << endl;
     452        gLog << err << "ERROR - Missing identifier: 1205" << endl;
    416453        readError = kTRUE;
    417454        return kFALSE;
    418455        }
    419456   
    420     unsigned short fileType = blockHeader[0] & 0xFFFF;
     457    const unsigned short fileType = blockHeader[0] & 0xFFFF;
    421458    if (fileType != 1205)
    422459        {
    423         *fLog << err << "ERROR - Unexpected type: " << fileType << "expected 1205" << endl;
     460        gLog << err << "ERROR - Unexpected type: " << fileType << "expected 1205" << endl;
    424461        readError = kTRUE;
    425462        return kFALSE;
    426463        }
    427464
    428     unsigned short version = (blockHeader[0] >> 20) & 0x0FFF;
     465    const unsigned short version = (blockHeader[0] >> 20) & 0x0FFF;
    429466    if (version != 0)
    430467        {
    431         *fLog << err << "ERROR - Unexpected version: " << version << "expected: 0" << endl;
     468        gLog << err << "ERROR - Unexpected version: " << version << "expected: 0" << endl;
    432469        readError = kTRUE;
    433470        return kFALSE;
     
    439476
    440477}
    441 
    442 
  • trunk/Mars/mcorsika/MCorsikaFormat.h

    r9893 r9938  
    77#endif
    88
    9 
    109#include <iosfwd>
    11 
    12 #include "MLog.h"
    13 
    1410
    1511class MCorsikaFormat
    1612{
    1713protected:
    18    MLog           * fLog;
    19    std::istream   * fIn;
     14   std::istream  *fIn;
    2015
    2116   std::streampos fPrevPos; // file position before previous read
     
    2318
    2419public:
    25    MCorsikaFormat(MLog * log, std::istream * in)
    26         : fIn(in) {fLog = log; }
     20    static const unsigned int kSyncMarker;
     21
     22   MCorsikaFormat(std::istream * in)
     23        : fIn(in) { }
    2724   virtual ~MCorsikaFormat();
    2825
    29    virtual Bool_t SeekNextBlock(const char * id, unsigned short type) = 0;
    30    virtual void   UnreadLastHeader() = 0;
     26   virtual Bool_t SeekNextBlock(const char * id, unsigned short type) const = 0;
     27   virtual void   UnreadLastHeader() const = 0;
    3128
    3229   virtual Bool_t ReadData(Int_t numValues, Float_t * buffer,
    3330                                Int_t minSeekValues = 272);
    34    virtual void   UnreadLastData();
     31   virtual void   UnreadLastData() const;
    3532
    3633   virtual Bool_t SeekEvtEnd() = 0;
    3734   virtual void   StorePos();
    38    virtual void   ResetPos();
     35   virtual void   ResetPos() const;
    3936
    4037   virtual Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError) = 0;
    41    virtual Bool_t IsEventioFormat() = 0;
     38   virtual Bool_t IsEventioFormat() const = 0;
    4239
    43         virtual Bool_t Eof()   {return fIn->eof();}
     40   virtual Bool_t Eof() const;
    4441
     42   std::streampos GetCurrPos() const;
    4543
    46         std::streampos GetCurrPos()  {return fIn->tellg();}
     44   static MCorsikaFormat *CorsikaFormatFactory(const char *fileName);
    4745};
    4846
     
    5351
    5452public:
    55    MCorsikaFormatRaw(MLog * log, std::istream * in)
    56         : MCorsikaFormat(log, in) {}
     53   MCorsikaFormatRaw(std::istream * in)
     54        : MCorsikaFormat(in) {}
    5755
    58    Bool_t SeekNextBlock(const char * id, unsigned short type);
    59    void   UnreadLastHeader();
     56   Bool_t SeekNextBlock(const char * id, unsigned short type) const;
     57   void   UnreadLastHeader() const;
    6058
    6159   Bool_t SeekEvtEnd();
    6260
    6361   Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError);
    64    Bool_t IsEventioFormat()   {return kFALSE;}
     62   Bool_t IsEventioFormat() const {return kFALSE;}
    6563};
    6664
     
    7270
    7371public:
    74    MCorsikaFormatEventIO(MLog * log, std::istream * in)
    75         : MCorsikaFormat(log, in) {fRunePos = std::streampos(0);}
     72   MCorsikaFormatEventIO(std::istream * in)
     73        : MCorsikaFormat(in) {fRunePos = std::streampos(0);}
    7674
    77    Bool_t SeekNextBlock(const char * id, unsigned short type);
    78    void   UnreadLastHeader();
     75   Bool_t SeekNextBlock(const char * id, unsigned short type) const;
     76   void   UnreadLastHeader() const;
    7977
    8078   Bool_t SeekEvtEnd();
    8179
    8280   Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError);
    83    Bool_t IsEventioFormat()   {return kTRUE;}
     81   Bool_t IsEventioFormat() const {return kTRUE;}
    8482
    8583private:
    86         Bool_t NextTopLevelBlock(Int_t & length, Bool_t & readError);
    87         Bool_t NextEventBlock(Int_t & length, Bool_t & readError);
     84   Bool_t NextTopLevelBlock(Int_t & length, Bool_t & readError) const;
     85   Bool_t NextEventBlock(Int_t & length, Bool_t & readError) const;
    8886
    8987};
    9088
    91 
    92 MCorsikaFormat * CorsikaFormatFactory(MLog * log, const char * fileName);
    93 
    94 
    9589#endif
    9690
Note: See TracChangeset for help on using the changeset viewer.