| 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> | 
|---|
| 34 | #include <fstream> | 
|---|
| 35 |  | 
|---|
| 36 | #include "MLogManip.h" | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 | using namespace std; | 
|---|
| 40 |  | 
|---|
| 41 |  | 
|---|
| 42 | const unsigned int MCorsikaFormat::kSyncMarker = 0xd41f8a37; | 
|---|
| 43 |  | 
|---|
| 44 | // -------------------------------------------------------------------------- | 
|---|
| 45 | // | 
|---|
| 46 | MCorsikaFormat *MCorsikaFormat::CorsikaFormatFactory(const char * fileName) | 
|---|
| 47 | { | 
|---|
| 48 | ifstream * fileIn = new ifstream(fileName); | 
|---|
| 49 |  | 
|---|
| 50 | const Bool_t noexist = !(*fileIn); | 
|---|
| 51 | if (noexist) | 
|---|
| 52 | { | 
|---|
| 53 | gLog << err << "Cannot open file " << fileName << ": "; | 
|---|
| 54 | gLog << (errno!=0?strerror(errno):"Insufficient memory for decompression") << endl; | 
|---|
| 55 | delete fileIn; | 
|---|
| 56 | return NULL; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | char *buffer = new char[5]; | 
|---|
| 60 | memset(buffer, 0, 5); | 
|---|
| 61 | fileIn->read(buffer, 4); | 
|---|
| 62 | fileIn->seekg(-4, ios::cur); | 
|---|
| 63 |  | 
|---|
| 64 | if (strcmp(buffer, "RUNH") == 0) | 
|---|
| 65 | { | 
|---|
| 66 | delete [] buffer; | 
|---|
| 67 | return new MCorsikaFormatRaw(fileIn); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | if (*reinterpret_cast<unsigned int*>(buffer) == kSyncMarker) | 
|---|
| 71 | { | 
|---|
| 72 | delete [] buffer; | 
|---|
| 73 | return new MCorsikaFormatEventIO(fileIn); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | gLog << err << "File " << fileName << | 
|---|
| 77 | " is neither a CORSIKA raw nor EventIO file" << endl; | 
|---|
| 78 | delete fileIn; | 
|---|
| 79 | delete [] buffer; | 
|---|
| 80 |  | 
|---|
| 81 | return NULL; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | Bool_t MCorsikaFormat::Read(void *ptr, Int_t i) const | 
|---|
| 85 | { | 
|---|
| 86 | fIn->read((char*)ptr, i); | 
|---|
| 87 | return !fIn->fail(); | 
|---|
| 88 |  | 
|---|
| 89 | } | 
|---|
| 90 | // -------------------------------------------------------------------------- | 
|---|
| 91 | // | 
|---|
| 92 | Bool_t MCorsikaFormat::Eof() const | 
|---|
| 93 | { | 
|---|
| 94 | return fIn->eof(); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | // -------------------------------------------------------------------------- | 
|---|
| 98 | // | 
|---|
| 99 | MCorsikaFormat::~MCorsikaFormat() | 
|---|
| 100 | { | 
|---|
| 101 | delete fIn; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 |  | 
|---|
| 105 | // -------------------------------------------------------------------------- | 
|---|
| 106 | // | 
|---|
| 107 | // After a call to this function, the file pointer is located after the | 
|---|
| 108 | // header of the block. As the event block has no header it is located | 
|---|
| 109 | // at the beginning of this block, which is as the beginning of the data | 
|---|
| 110 | Bool_t MCorsikaFormatRaw::NextBlock(Bool_t  subBlock, | 
|---|
| 111 | Int_t & blockType, | 
|---|
| 112 | Int_t & blockVersion, | 
|---|
| 113 | Int_t & blockIdentifier, | 
|---|
| 114 | Int_t & blockLength) const | 
|---|
| 115 | { | 
|---|
| 116 | int blockHeader; | 
|---|
| 117 | fIn->read((char*)&blockHeader, 4); | 
|---|
| 118 | if (fIn->eof()) | 
|---|
| 119 | return kFALSE; | 
|---|
| 120 |  | 
|---|
| 121 | blockVersion = 0; | 
|---|
| 122 | blockIdentifier = 0; | 
|---|
| 123 | blockLength     = 272 * 4; | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | switch(blockHeader) | 
|---|
| 127 | { | 
|---|
| 128 | case 1213093202 : // RUNH | 
|---|
| 129 | blockType = 1200; | 
|---|
| 130 | break; | 
|---|
| 131 | case 1162761554 : // RUNE | 
|---|
| 132 | blockType = 1210; | 
|---|
| 133 | break; | 
|---|
| 134 | case 1213486661 : // EVTH | 
|---|
| 135 | blockType = 1202; | 
|---|
| 136 | break; | 
|---|
| 137 | case 1163155013 : // EVTE | 
|---|
| 138 | blockType = 1209; | 
|---|
| 139 | break; | 
|---|
| 140 | default:          // the events, they don't have a specific header | 
|---|
| 141 | blockType = 1105; | 
|---|
| 142 | fIn->seekg(-4, ios::cur); | 
|---|
| 143 | blockLength += 4; | 
|---|
| 144 | } | 
|---|
| 145 | return kTRUE; | 
|---|
| 146 | } | 
|---|
| 147 | // -------------------------------------------------------------------------- | 
|---|
| 148 | // | 
|---|
| 149 | Bool_t MCorsikaFormatRaw::SeekEvtEnd() | 
|---|
| 150 | { | 
|---|
| 151 | // Search subblockwise backward (Block: 5733*4 = 21*273*4) | 
|---|
| 152 | for (int i=1; i<22; i++) | 
|---|
| 153 | { | 
|---|
| 154 | fIn->seekg(-i*273*4, ios::end); | 
|---|
| 155 |  | 
|---|
| 156 | char runh[5]="\0\0\0\0"; | 
|---|
| 157 | fIn->read(runh, 4); | 
|---|
| 158 |  | 
|---|
| 159 | if (!strcmp(runh, "RUNE")) | 
|---|
| 160 | { | 
|---|
| 161 | //            fIn->seekg(-4, ios::cur); | 
|---|
| 162 | return kTRUE; | 
|---|
| 163 | } | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | return kTRUE; | 
|---|
| 167 | } | 
|---|
| 168 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 169 | /////////////////////////////////////////////////////////////////////////////// | 
|---|
| 170 |  | 
|---|
| 171 | Bool_t MCorsikaFormatEventIO::NextBlock(Bool_t  subBlock, | 
|---|
| 172 | Int_t & blockType, | 
|---|
| 173 | Int_t & blockVersion, | 
|---|
| 174 | Int_t & blockIdentifier, | 
|---|
| 175 | Int_t & blockLength) const | 
|---|
| 176 | { | 
|---|
| 177 | // we read - synchronisation markerif not subBlock | 
|---|
| 178 | //         - type / version field | 
|---|
| 179 | //         - identification field | 
|---|
| 180 | //         - length | 
|---|
| 181 | //         - unknown field | 
|---|
| 182 | //         - id (first 4 bytes of data field) | 
|---|
| 183 |  | 
|---|
| 184 | if (subBlock) | 
|---|
| 185 | { | 
|---|
| 186 | int blockHeader[3]; | 
|---|
| 187 | fIn->read((char*)blockHeader, 3 * sizeof(int)); | 
|---|
| 188 |  | 
|---|
| 189 | if (fIn->eof()) | 
|---|
| 190 | return kFALSE; | 
|---|
| 191 |  | 
|---|
| 192 |  | 
|---|
| 193 | blockType       = blockHeader[0] & 0xFFFF; | 
|---|
| 194 | blockVersion    = (blockHeader[0] & 0xFFF00000) >> 20; | 
|---|
| 195 | blockIdentifier = blockHeader[1]; | 
|---|
| 196 | blockLength     = blockHeader[2] & 0x3FFFFFFF; | 
|---|
| 197 | } | 
|---|
| 198 | else | 
|---|
| 199 | { | 
|---|
| 200 | int blockHeader[4]; | 
|---|
| 201 | fIn->read((char*)blockHeader, 4 * sizeof(int)); | 
|---|
| 202 |  | 
|---|
| 203 | if (fIn->eof()) | 
|---|
| 204 | return kFALSE; | 
|---|
| 205 |  | 
|---|
| 206 |  | 
|---|
| 207 | blockType       = blockHeader[1] & 0xFFFF; | 
|---|
| 208 | blockVersion    = (blockHeader[1] & 0xFFF00000) >> 20; | 
|---|
| 209 | blockIdentifier = blockHeader[2]; | 
|---|
| 210 | blockLength     = blockHeader[3] & 0x3FFFFFFF; | 
|---|
| 211 |  | 
|---|
| 212 | if (blockType == 1200  || blockType == 1210 || | 
|---|
| 213 | blockType == 1202  || blockType == 1209    ) | 
|---|
| 214 | { | 
|---|
| 215 | // read the "old" corsika header plus additional 4 unknown byte | 
|---|
| 216 | char tmp[8]; | 
|---|
| 217 | fIn->read(tmp, 8); | 
|---|
| 218 | blockLength -= 8; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | } | 
|---|
| 222 | return kTRUE; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | // -------------------------------------------------------------------------- | 
|---|
| 226 | // | 
|---|
| 227 | Bool_t MCorsikaFormatEventIO::SeekEvtEnd() | 
|---|
| 228 | { | 
|---|
| 229 |  | 
|---|
| 230 | // the RUNE block it at the very end of the file. | 
|---|
| 231 | fIn->seekg(-32, ios::end); | 
|---|
| 232 |  | 
|---|
| 233 | unsigned int blockHeader[4]; | 
|---|
| 234 | fIn->read((char*)blockHeader, 4 * sizeof(int)); | 
|---|
| 235 |  | 
|---|
| 236 | if ( blockHeader[0]               == kSyncMarker && | 
|---|
| 237 | (blockHeader[1] & 0xffff)     == 1210        && | 
|---|
| 238 | (blockHeader[3] & 0x3fffffff) == 16) | 
|---|
| 239 | { | 
|---|
| 240 | // this seams to be a RUNE (1210)  block | 
|---|
| 241 | fIn->seekg( 8, ios::cur); | 
|---|
| 242 | return kTRUE; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | return kFALSE; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|