Index: /trunk/Mars/Changelog
===================================================================
--- /trunk/Mars/Changelog	(revision 9937)
+++ /trunk/Mars/Changelog	(revision 9938)
@@ -75,4 +75,11 @@
      - added functions to calculate mean x and mean y
      - propagate re-usage counter through ReadEvent
+
+   * mcorsika/MCorsikaFormat.[h,cc]:
+     - a few clean-ups to the code
+     - added standard file header
+     - a few minor simplifications
+     - replaced local logging stream with gLog
+     - added some empty comments
 
 
Index: /trunk/Mars/mcorsika/MCorsikaFormat.cc
===================================================================
--- /trunk/Mars/mcorsika/MCorsikaFormat.cc	(revision 9937)
+++ /trunk/Mars/mcorsika/MCorsikaFormat.cc	(revision 9938)
@@ -1,14 +1,46 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+! * Software. It is distributed to you in the hope that it can be a useful
+! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+! * It is distributed WITHOUT ANY WARRANTY.
+! *
+! * Permission to use, copy, modify and distribute this software and its
+! * documentation for any purpose is hereby granted without fee,
+! * provided that the above copyright notice appear in all copies and
+! * that both that copyright notice and this permission notice appear
+! * in supporting documentation. It is provided "as is" without express
+! * or implied warranty.
+! *
+!
+!
+!   Author(s): Reiner Rohlfs 2010
+!   Author(s): Thomas Bretz  2010 <mailto:thomas.bretz@epfl.ch>
+!
+!   Copyright: Software Development, 2000-2010
+!
+!
+\* ======================================================================== */
+
+//////////////////////////////////////////////////////////////////////////////
+//
+// MCorsikaFormat
+//
+//////////////////////////////////////////////////////////////////////////////
+#include "MCorsikaFormat.h"
+
+#include <errno.h>
 #include <fstream>
-#include <errno.h>
-
 
 #include "MLogManip.h"
 
-#include "MCorsikaFormat.h"
-
-
 using namespace std;
 
-MCorsikaFormat * CorsikaFormatFactory(MLog * log, const char * fileName)
+const unsigned int MCorsikaFormat::kSyncMarker = 0xd41f8a37;
+
+// --------------------------------------------------------------------------
+//
+MCorsikaFormat *MCorsikaFormat::CorsikaFormatFactory(const char * fileName)
 {
     ifstream * fileIn = new ifstream(fileName);
@@ -17,42 +49,43 @@
     if (noexist)
     {
-        *log << err << "Cannot open file " << fileName << ": ";
-        *log << (errno!=0?strerror(errno):"Insufficient memory for decompression") << endl;
+        gLog << err << "Cannot open file " << fileName << ": ";
+        gLog << (errno!=0?strerror(errno):"Insufficient memory for decompression") << endl;
         delete fileIn;
-        fileIn = NULL;
         return NULL;
     }
 
-
-    if (!fileIn->is_open())
-        {
-        *log << err << "Failed to open file " << fileName << endl;
-        delete fileIn;
-        fileIn = NULL;
-        return NULL;
-        }
-    
-    char buffer[4];
+    char buffer[5]="\0\0\0\0";
     fileIn->read(buffer, 4);
     fileIn->seekg(-4, ios::cur);
 
-    int * syncmarker = reinterpret_cast<int*>(buffer);
-    if (memcmp(buffer, "RUNH", 4) == 0)
-        return new MCorsikaFormatRaw(log, fileIn);
-         
-    else if (*syncmarker == -736130505)
-        return new MCorsikaFormatEventIO(log, fileIn);
-
-    *log << err << "File " << fileName << 
-            " is neither a corsica file, nor an eventio file" << endl;
+    if (strcmp(buffer, "RUNH") == 0)
+        return new MCorsikaFormatRaw(fileIn);
+
+    if (*reinterpret_cast<unsigned int*>(buffer) == kSyncMarker)
+        return new MCorsikaFormatEventIO(fileIn);
+
+    gLog << err << "File " << fileName <<
+            " is neither a CORSIKA raw nor EventIO file" << endl;
     delete fileIn;
-    fileIn = NULL;
 
     return NULL;
 }
 
-///////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////
-
+// --------------------------------------------------------------------------
+//
+Bool_t MCorsikaFormat::Eof() const
+{
+    return fIn->eof();
+}
+
+// --------------------------------------------------------------------------
+//
+streampos MCorsikaFormat::GetCurrPos() const
+{
+    return fIn->tellg();
+}
+
+// --------------------------------------------------------------------------
+//
 Bool_t MCorsikaFormat::ReadData(Int_t numValues, Float_t * buffer, 
                                 Int_t minSeekValues)
@@ -70,30 +103,31 @@
 }
 
-void MCorsikaFormat::UnreadLastData()
+// --------------------------------------------------------------------------
+//
+void MCorsikaFormat::UnreadLastData() const
 {
     fIn->seekg(fPrevPos, ios::beg);
 }
 
+// --------------------------------------------------------------------------
+//
 void MCorsikaFormat::StorePos()
 {
     fPos = fIn->tellg();
-//*fLog << all << "storePos: " << fPos << endl;
-}
-
-void MCorsikaFormat::ResetPos()
+}
+
+// --------------------------------------------------------------------------
+//
+void MCorsikaFormat::ResetPos() const
 {
     fIn->seekg(fPos, ios::beg);
 }
 
-
-///////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////
-
-
+// --------------------------------------------------------------------------
+//
 MCorsikaFormat::~MCorsikaFormat() 
 {
     delete fIn;
 }
-
 
 // --------------------------------------------------------------------------
@@ -107,33 +141,33 @@
 // of the id.
 //
-Bool_t MCorsikaFormatRaw::SeekNextBlock(const char * id, unsigned short type)
-{
-    char blockHeader[5];
+Bool_t MCorsikaFormatRaw::SeekNextBlock(const char * id, unsigned short type) const
+{
+    char blockHeader[5]="\0\0\0\0";
     fIn->read(blockHeader, 4);
 
-    if (memcmp(blockHeader, id, 4))
-    {
-        blockHeader[4] = 0;
-        if (strcmp(id, "EVTH") != 0 || strcmp(blockHeader, "RUNE") != 0 )
-            {
-            // at the end of a file we are looking for the next Event header,
-            // but find the end of a run. This is expected, therefor no error
-            // message.
-            *fLog << err << "ERROR - Wrong identifier: " << id << " expected." 
-                  << " But read " << blockHeader << " from file." << endl;
-            }
-        return kFALSE;
-    }
-
-    return kTRUE;
-}
-
-// --------------------------------------------------------------------------
-//
-void MCorsikaFormatRaw::UnreadLastHeader() 
+    if (strcmp(blockHeader, id)==0)
+        return kTRUE;
+
+    // at the end of a file we are looking for the next Event header,
+    // but find the end of a run. This is expected, therefor no error
+    // message.
+    if (strcmp(id, "EVTH")==0 && strcmp(blockHeader, "RUNE")==0)
+        return kTRUE;
+
+    gLog << err << "ERROR - Wrong identifier: " << id << " expected.";
+    gLog << " But read " << blockHeader << " from file." << endl;
+
+    return kFALSE;
+}
+
+// --------------------------------------------------------------------------
+//
+void MCorsikaFormatRaw::UnreadLastHeader() const
 {
     fIn->seekg(-4, ios::cur);
 }
 
+// --------------------------------------------------------------------------
+//
 Bool_t MCorsikaFormatRaw::SeekEvtEnd()
 {
@@ -144,8 +178,8 @@
         fIn->seekg(-i*273*4, ios::end);
 
-        char runh[4];
+        char runh[5]="\0\0\0\0";
         fIn->read(runh, 4);
 
-        if (!memcmp(runh, "RUNE", 4))
+        if (!strcmp(runh, "RUNE"))
         {
             fIn->seekg(-4, ios::cur);
@@ -156,5 +190,4 @@
     return kTRUE;
 }
-
 
 // --------------------------------------------------------------------------
@@ -163,6 +196,7 @@
 // block is found.                                                           
 // If a read error occurred, the readError is set to kTRUE and the function  
-// returns kFALSE;                                                           
-Bool_t MCorsikaFormatRaw::GetNextEvent(Float_t ** buffer, Bool_t & readError) 
+// returns kFALSE;
+//
+Bool_t MCorsikaFormatRaw::GetNextEvent(Float_t ** buffer, Bool_t & readError)
 {
     static Float_t data[273];
@@ -194,5 +228,4 @@
 
 }
-
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -211,5 +244,5 @@
 // of the id.
 //
-Bool_t MCorsikaFormatEventIO::SeekNextBlock(const char * id, unsigned short type)
+Bool_t MCorsikaFormatEventIO::SeekNextBlock(const char * id, unsigned short type) const
 {
     int blockHeader[7];
@@ -227,5 +260,5 @@
         if (fIn->eof())
             {
-            *fLog << err << "ERROR - Missing identifier: " << id  <<
+            gLog << err << "ERROR - Missing identifier: " << id  <<
                    " type: " << type << endl;
             return kFALSE;
@@ -233,5 +266,5 @@
 
         unsigned short fileType = blockHeader[1] & 0xFFFF;
-        if (type == fileType                       )
+        if (type == fileType)
             break;
 
@@ -243,7 +276,7 @@
 
         // a unknown block, we jump to the next one
-//*fLog << "unknown: " <<  id << " type: " << fileType << " sub-blocks: " <<  (blockHeader[3]>>29);
-//*fLog <<  " length: " << (blockHeader[3] & 0x3fffffff) <<   "  pos: " << fIn->tellg() << endl;
-        int length = blockHeader[3] & 0x3fffffff;
+//gLog << "unknown: " <<  id << " type: " << fileType << " sub-blocks: " <<  (blockHeader[3]>>29);
+//gLog <<  " length: " << (blockHeader[3] & 0x3fffffff) <<   "  pos: " << fIn->tellg() << endl;
+        const int length = blockHeader[3] & 0x3fffffff;
         
         fIn->seekg(length - 2 * (Int_t)(sizeof(int)), ios::cur);    
@@ -256,5 +289,5 @@
 // --------------------------------------------------------------------------
 //
-void MCorsikaFormatEventIO::UnreadLastHeader() 
+void MCorsikaFormatEventIO::UnreadLastHeader() const
 {
     fIn->seekg( -6 * (Int_t)(sizeof(int)), ios::cur);
@@ -266,39 +299,38 @@
 {
     if (fRunePos != streampos(0))
-        {
+    {
         fIn->seekg(fRunePos, ios::beg);
         return kTRUE;
-        }
-    else
-        {
-        // it is the first time we are looking for the RUNE block
-
-        // is the RUNE block at the very end of the file?
-        std::streampos currentPos = fIn->tellg();
-
-        fIn->seekg(-32, ios::end);
-        unsigned int blockHeader[4];
-        fIn->read((char*)blockHeader, 4 * sizeof(int));
-        if ( blockHeader[0]               == 0xd41f8a37 &&
-            (blockHeader[1] & 0xffff)     == 1210       &&
-            (blockHeader[3] & 0x3fffffff) == 16)
-            {
-            // this seams to be a RUNE (1210)  block
-            fIn->seekg( -4 * (Int_t)(sizeof(int)), ios::cur);
-            fRunePos = fIn->tellg();
-            return kTRUE;
-            }
-
-        // we do not find a RUNE block at the end of the file
-        // we have to search in the file
-        fIn->seekg(currentPos, ios::beg);
-        if (SeekNextBlock("RUNE", 1210))
-            {
-            UnreadLastHeader();
-            fRunePos = fIn->tellg();
-            return kTRUE;
-            }
-        }
-    return kFALSE;
+    }
+
+    // it is the first time we are looking for the RUNE block
+
+    // is the RUNE block at the very end of the file?
+    std::streampos currentPos = fIn->tellg();
+
+    fIn->seekg(-32, ios::end);
+
+    unsigned int blockHeader[4];
+    fIn->read((char*)blockHeader, 4 * sizeof(int));
+
+    if ( blockHeader[0]               == kSyncMarker &&
+        (blockHeader[1] & 0xffff)     == 1210        &&
+        (blockHeader[3] & 0x3fffffff) == 16)
+    {
+        // this seams to be a RUNE (1210)  block
+        fIn->seekg( -4 * (Int_t)(sizeof(int)), ios::cur);
+        fRunePos = fIn->tellg();
+        return kTRUE;
+    }
+
+    // we do not find a RUNE block at the end of the file
+    // we have to search in the file
+    fIn->seekg(currentPos, ios::beg);
+    if (!SeekNextBlock("RUNE", 1210))
+        return kFALSE;
+
+    UnreadLastHeader();
+    fRunePos = fIn->tellg();
+    return kTRUE;
 }
 
@@ -309,5 +341,6 @@
 // If a read error occurred, the readError is set to kTRUE and the function  
 // returns kFALSE;                                                           
-Bool_t MCorsikaFormatEventIO::GetNextEvent(Float_t ** buffer, 
+//
+Bool_t MCorsikaFormatEventIO::GetNextEvent(Float_t ** buffer,
                                            Bool_t & readError) 
 {
@@ -354,11 +387,13 @@
     return kTRUE;
 }
+
 // --------------------------------------------------------------------------
 //                                                                           
 // Looks for the next Block with type 1204 and return kTRUE.                 
 // The function also stops moving forward in the file, if it finds a         
-// EventEnd block (1209). In this case kFALSE is returned                    
+// EventEnd block (1209). In this case kFALSE is returned
+//
 Bool_t MCorsikaFormatEventIO::NextTopLevelBlock(Int_t & length, 
-                                                Bool_t & readError)
+                                                Bool_t & readError) const
 {
     Int_t blockHeader[4];
@@ -374,5 +409,5 @@
         if (fIn->eof())
             {
-            *fLog << err << "ERROR - Missing identifier: 1204 or 1209" << endl;
+            gLog << err << "ERROR - Missing identifier: 1204 or 1209" << endl;
             readError = kTRUE;
             return kFALSE;
@@ -380,5 +415,5 @@
 
         length = blockHeader[3] & 0x3fffffff;
-        unsigned short fileType = blockHeader[1] & 0xFFFF;
+        const unsigned short fileType = blockHeader[1] & 0xFFFF;
         if (fileType == 1204)
             return kTRUE;
@@ -400,6 +435,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
 Bool_t MCorsikaFormatEventIO::NextEventBlock(Int_t & length, 
-                                             Bool_t & readError)
+                                             Bool_t & readError) const
 {
     Int_t blockHeader[3];
@@ -413,21 +450,21 @@
     if (fIn->eof())
         {
-        *fLog << err << "ERROR - Missing identifier: 1205" << endl;
+        gLog << err << "ERROR - Missing identifier: 1205" << endl;
         readError = kTRUE;
         return kFALSE;
         }
     
-    unsigned short fileType = blockHeader[0] & 0xFFFF;
+    const unsigned short fileType = blockHeader[0] & 0xFFFF;
     if (fileType != 1205)
         {
-        *fLog << err << "ERROR - Unexpected type: " << fileType << "expected 1205" << endl;
+        gLog << err << "ERROR - Unexpected type: " << fileType << "expected 1205" << endl;
         readError = kTRUE;
         return kFALSE;
         }
 
-    unsigned short version = (blockHeader[0] >> 20) & 0x0FFF;
+    const unsigned short version = (blockHeader[0] >> 20) & 0x0FFF;
     if (version != 0)
         {
-        *fLog << err << "ERROR - Unexpected version: " << version << "expected: 0" << endl;
+        gLog << err << "ERROR - Unexpected version: " << version << "expected: 0" << endl;
         readError = kTRUE;
         return kFALSE;
@@ -439,4 +476,2 @@
 
 }
-
-
Index: /trunk/Mars/mcorsika/MCorsikaFormat.h
===================================================================
--- /trunk/Mars/mcorsika/MCorsikaFormat.h	(revision 9937)
+++ /trunk/Mars/mcorsika/MCorsikaFormat.h	(revision 9938)
@@ -7,15 +7,10 @@
 #endif
 
-
 #include <iosfwd>
-
-#include "MLog.h"
-
 
 class MCorsikaFormat
 {
 protected:
-   MLog           * fLog;
-   std::istream   * fIn;
+   std::istream  *fIn;
 
    std::streampos fPrevPos; // file position before previous read
@@ -23,26 +18,29 @@
 
 public:
-   MCorsikaFormat(MLog * log, std::istream * in)
-        : fIn(in) {fLog = log; } 
+    static const unsigned int kSyncMarker;
+
+   MCorsikaFormat(std::istream * in)
+        : fIn(in) { }
    virtual ~MCorsikaFormat();
 
-   virtual Bool_t SeekNextBlock(const char * id, unsigned short type) = 0;
-   virtual void   UnreadLastHeader() = 0;
+   virtual Bool_t SeekNextBlock(const char * id, unsigned short type) const = 0;
+   virtual void   UnreadLastHeader() const = 0;
 
    virtual Bool_t ReadData(Int_t numValues, Float_t * buffer, 
 	                        Int_t minSeekValues = 272);
-   virtual void   UnreadLastData();
+   virtual void   UnreadLastData() const;
 
    virtual Bool_t SeekEvtEnd() = 0;
    virtual void   StorePos();
-   virtual void   ResetPos();
+   virtual void   ResetPos() const;
 
    virtual Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError) = 0;
-   virtual Bool_t IsEventioFormat() = 0;
+   virtual Bool_t IsEventioFormat() const = 0;
 
-	virtual Bool_t Eof()   {return fIn->eof();}
+   virtual Bool_t Eof() const;
 
+   std::streampos GetCurrPos() const;
 
-	std::streampos GetCurrPos()  {return fIn->tellg();}
+   static MCorsikaFormat *CorsikaFormatFactory(const char *fileName);
 };
 
@@ -53,14 +51,14 @@
 
 public:
-   MCorsikaFormatRaw(MLog * log, std::istream * in)
-        : MCorsikaFormat(log, in) {} 
+   MCorsikaFormatRaw(std::istream * in)
+        : MCorsikaFormat(in) {}
 
-   Bool_t SeekNextBlock(const char * id, unsigned short type);
-   void   UnreadLastHeader();
+   Bool_t SeekNextBlock(const char * id, unsigned short type) const;
+   void   UnreadLastHeader() const;
 
    Bool_t SeekEvtEnd();
 
    Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError);
-   Bool_t IsEventioFormat()   {return kFALSE;}
+   Bool_t IsEventioFormat() const {return kFALSE;}
 };
 
@@ -72,25 +70,21 @@
 
 public:
-   MCorsikaFormatEventIO(MLog * log, std::istream * in)
-        : MCorsikaFormat(log, in) {fRunePos = std::streampos(0);} 
+   MCorsikaFormatEventIO(std::istream * in)
+        : MCorsikaFormat(in) {fRunePos = std::streampos(0);}
 
-   Bool_t SeekNextBlock(const char * id, unsigned short type);
-   void   UnreadLastHeader();
+   Bool_t SeekNextBlock(const char * id, unsigned short type) const;
+   void   UnreadLastHeader() const;
 
    Bool_t SeekEvtEnd();
 
    Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError);
-   Bool_t IsEventioFormat()   {return kTRUE;}
+   Bool_t IsEventioFormat() const {return kTRUE;}
 
 private:
-	Bool_t NextTopLevelBlock(Int_t & length, Bool_t & readError);
-	Bool_t NextEventBlock(Int_t & length, Bool_t & readError);
+   Bool_t NextTopLevelBlock(Int_t & length, Bool_t & readError) const;
+   Bool_t NextEventBlock(Int_t & length, Bool_t & readError) const;
 
 };
 
-
-MCorsikaFormat * CorsikaFormatFactory(MLog * log, const char * fileName);
-
-
 #endif
 
