Changeset 9938 for trunk/Mars
- Timestamp:
- 09/22/10 16:15:22 (14 years ago)
- Location:
- trunk/Mars
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/Changelog
r9937 r9938 75 75 - added functions to calculate mean x and mean y 76 76 - 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 77 84 78 85 -
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> 1 34 #include <fstream> 2 #include <errno.h>3 4 35 5 36 #include "MLogManip.h" 6 37 7 #include "MCorsikaFormat.h"8 9 10 38 using namespace std; 11 39 12 MCorsikaFormat * CorsikaFormatFactory(MLog * log, const char * fileName) 40 const unsigned int MCorsikaFormat::kSyncMarker = 0xd41f8a37; 41 42 // -------------------------------------------------------------------------- 43 // 44 MCorsikaFormat *MCorsikaFormat::CorsikaFormatFactory(const char * fileName) 13 45 { 14 46 ifstream * fileIn = new ifstream(fileName); … … 17 49 if (noexist) 18 50 { 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; 21 53 delete fileIn; 22 fileIn = NULL;23 54 return NULL; 24 55 } 25 56 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"; 36 58 fileIn->read(buffer, 4); 37 59 fileIn->seekg(-4, ios::cur); 38 60 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; 48 69 delete fileIn; 49 fileIn = NULL;50 70 51 71 return NULL; 52 72 } 53 73 54 /////////////////////////////////////////////////////////////////////////////// 55 /////////////////////////////////////////////////////////////////////////////// 56 74 // -------------------------------------------------------------------------- 75 // 76 Bool_t MCorsikaFormat::Eof() const 77 { 78 return fIn->eof(); 79 } 80 81 // -------------------------------------------------------------------------- 82 // 83 streampos MCorsikaFormat::GetCurrPos() const 84 { 85 return fIn->tellg(); 86 } 87 88 // -------------------------------------------------------------------------- 89 // 57 90 Bool_t MCorsikaFormat::ReadData(Int_t numValues, Float_t * buffer, 58 91 Int_t minSeekValues) … … 70 103 } 71 104 72 void MCorsikaFormat::UnreadLastData() 105 // -------------------------------------------------------------------------- 106 // 107 void MCorsikaFormat::UnreadLastData() const 73 108 { 74 109 fIn->seekg(fPrevPos, ios::beg); 75 110 } 76 111 112 // -------------------------------------------------------------------------- 113 // 77 114 void MCorsikaFormat::StorePos() 78 115 { 79 116 fPos = fIn->tellg(); 80 //*fLog << all << "storePos: " << fPos << endl; 81 } 82 83 void MCorsikaFormat::ResetPos() 117 } 118 119 // -------------------------------------------------------------------------- 120 // 121 void MCorsikaFormat::ResetPos() const 84 122 { 85 123 fIn->seekg(fPos, ios::beg); 86 124 } 87 125 88 89 /////////////////////////////////////////////////////////////////////////////// 90 /////////////////////////////////////////////////////////////////////////////// 91 92 126 // -------------------------------------------------------------------------- 127 // 93 128 MCorsikaFormat::~MCorsikaFormat() 94 129 { 95 130 delete fIn; 96 131 } 97 98 132 99 133 // -------------------------------------------------------------------------- … … 107 141 // of the id. 108 142 // 109 Bool_t MCorsikaFormatRaw::SeekNextBlock(const char * id, unsigned short type) 110 { 111 char blockHeader[5] ;143 Bool_t MCorsikaFormatRaw::SeekNextBlock(const char * id, unsigned short type) const 144 { 145 char blockHeader[5]="\0\0\0\0"; 112 146 fIn->read(blockHeader, 4); 113 147 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 // 165 void MCorsikaFormatRaw::UnreadLastHeader() const 134 166 { 135 167 fIn->seekg(-4, ios::cur); 136 168 } 137 169 170 // -------------------------------------------------------------------------- 171 // 138 172 Bool_t MCorsikaFormatRaw::SeekEvtEnd() 139 173 { … … 144 178 fIn->seekg(-i*273*4, ios::end); 145 179 146 char runh[ 4];180 char runh[5]="\0\0\0\0"; 147 181 fIn->read(runh, 4); 148 182 149 if (! memcmp(runh, "RUNE", 4))183 if (!strcmp(runh, "RUNE")) 150 184 { 151 185 fIn->seekg(-4, ios::cur); … … 156 190 return kTRUE; 157 191 } 158 159 192 160 193 // -------------------------------------------------------------------------- … … 163 196 // block is found. 164 197 // 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 // 200 Bool_t MCorsikaFormatRaw::GetNextEvent(Float_t ** buffer, Bool_t & readError) 167 201 { 168 202 static Float_t data[273]; … … 194 228 195 229 } 196 197 230 198 231 /////////////////////////////////////////////////////////////////////////////// … … 211 244 // of the id. 212 245 // 213 Bool_t MCorsikaFormatEventIO::SeekNextBlock(const char * id, unsigned short type) 246 Bool_t MCorsikaFormatEventIO::SeekNextBlock(const char * id, unsigned short type) const 214 247 { 215 248 int blockHeader[7]; … … 227 260 if (fIn->eof()) 228 261 { 229 *fLog << err << "ERROR - Missing identifier: " << id <<262 gLog << err << "ERROR - Missing identifier: " << id << 230 263 " type: " << type << endl; 231 264 return kFALSE; … … 233 266 234 267 unsigned short fileType = blockHeader[1] & 0xFFFF; 235 if (type == fileType 268 if (type == fileType) 236 269 break; 237 270 … … 243 276 244 277 // 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; 248 281 249 282 fIn->seekg(length - 2 * (Int_t)(sizeof(int)), ios::cur); … … 256 289 // -------------------------------------------------------------------------- 257 290 // 258 void MCorsikaFormatEventIO::UnreadLastHeader() 291 void MCorsikaFormatEventIO::UnreadLastHeader() const 259 292 { 260 293 fIn->seekg( -6 * (Int_t)(sizeof(int)), ios::cur); … … 266 299 { 267 300 if (fRunePos != streampos(0)) 268 301 { 269 302 fIn->seekg(fRunePos, ios::beg); 270 303 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; 303 335 } 304 336 … … 309 341 // If a read error occurred, the readError is set to kTRUE and the function 310 342 // returns kFALSE; 311 Bool_t MCorsikaFormatEventIO::GetNextEvent(Float_t ** buffer, 343 // 344 Bool_t MCorsikaFormatEventIO::GetNextEvent(Float_t ** buffer, 312 345 Bool_t & readError) 313 346 { … … 354 387 return kTRUE; 355 388 } 389 356 390 // -------------------------------------------------------------------------- 357 391 // 358 392 // Looks for the next Block with type 1204 and return kTRUE. 359 393 // 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 // 361 396 Bool_t MCorsikaFormatEventIO::NextTopLevelBlock(Int_t & length, 362 Bool_t & readError) 397 Bool_t & readError) const 363 398 { 364 399 Int_t blockHeader[4]; … … 374 409 if (fIn->eof()) 375 410 { 376 *fLog << err << "ERROR - Missing identifier: 1204 or 1209" << endl;411 gLog << err << "ERROR - Missing identifier: 1204 or 1209" << endl; 377 412 readError = kTRUE; 378 413 return kFALSE; … … 380 415 381 416 length = blockHeader[3] & 0x3fffffff; 382 unsigned short fileType = blockHeader[1] & 0xFFFF;417 const unsigned short fileType = blockHeader[1] & 0xFFFF; 383 418 if (fileType == 1204) 384 419 return kTRUE; … … 400 435 } 401 436 437 // -------------------------------------------------------------------------- 438 // 402 439 Bool_t MCorsikaFormatEventIO::NextEventBlock(Int_t & length, 403 Bool_t & readError) 440 Bool_t & readError) const 404 441 { 405 442 Int_t blockHeader[3]; … … 413 450 if (fIn->eof()) 414 451 { 415 *fLog << err << "ERROR - Missing identifier: 1205" << endl;452 gLog << err << "ERROR - Missing identifier: 1205" << endl; 416 453 readError = kTRUE; 417 454 return kFALSE; 418 455 } 419 456 420 unsigned short fileType = blockHeader[0] & 0xFFFF;457 const unsigned short fileType = blockHeader[0] & 0xFFFF; 421 458 if (fileType != 1205) 422 459 { 423 *fLog << err << "ERROR - Unexpected type: " << fileType << "expected 1205" << endl;460 gLog << err << "ERROR - Unexpected type: " << fileType << "expected 1205" << endl; 424 461 readError = kTRUE; 425 462 return kFALSE; 426 463 } 427 464 428 unsigned short version = (blockHeader[0] >> 20) & 0x0FFF;465 const unsigned short version = (blockHeader[0] >> 20) & 0x0FFF; 429 466 if (version != 0) 430 467 { 431 *fLog << err << "ERROR - Unexpected version: " << version << "expected: 0" << endl;468 gLog << err << "ERROR - Unexpected version: " << version << "expected: 0" << endl; 432 469 readError = kTRUE; 433 470 return kFALSE; … … 439 476 440 477 } 441 442 -
trunk/Mars/mcorsika/MCorsikaFormat.h
r9893 r9938 7 7 #endif 8 8 9 10 9 #include <iosfwd> 11 12 #include "MLog.h"13 14 10 15 11 class MCorsikaFormat 16 12 { 17 13 protected: 18 MLog * fLog; 19 std::istream * fIn; 14 std::istream *fIn; 20 15 21 16 std::streampos fPrevPos; // file position before previous read … … 23 18 24 19 public: 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) { } 27 24 virtual ~MCorsikaFormat(); 28 25 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; 31 28 32 29 virtual Bool_t ReadData(Int_t numValues, Float_t * buffer, 33 30 Int_t minSeekValues = 272); 34 virtual void UnreadLastData() ;31 virtual void UnreadLastData() const; 35 32 36 33 virtual Bool_t SeekEvtEnd() = 0; 37 34 virtual void StorePos(); 38 virtual void ResetPos() ;35 virtual void ResetPos() const; 39 36 40 37 virtual Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError) = 0; 41 virtual Bool_t IsEventioFormat() = 0;38 virtual Bool_t IsEventioFormat() const = 0; 42 39 43 virtual Bool_t Eof() {return fIn->eof();} 40 virtual Bool_t Eof() const; 44 41 42 std::streampos GetCurrPos() const; 45 43 46 std::streampos GetCurrPos() {return fIn->tellg();} 44 static MCorsikaFormat *CorsikaFormatFactory(const char *fileName); 47 45 }; 48 46 … … 53 51 54 52 public: 55 MCorsikaFormatRaw( MLog * log,std::istream * in)56 : MCorsikaFormat( log, in) {}53 MCorsikaFormatRaw(std::istream * in) 54 : MCorsikaFormat(in) {} 57 55 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; 60 58 61 59 Bool_t SeekEvtEnd(); 62 60 63 61 Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError); 64 Bool_t IsEventioFormat() 62 Bool_t IsEventioFormat() const {return kFALSE;} 65 63 }; 66 64 … … 72 70 73 71 public: 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);} 76 74 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; 79 77 80 78 Bool_t SeekEvtEnd(); 81 79 82 80 Bool_t GetNextEvent(Float_t ** buffer, Bool_t & readError); 83 Bool_t IsEventioFormat() 81 Bool_t IsEventioFormat() const {return kTRUE;} 84 82 85 83 private: 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; 88 86 89 87 }; 90 88 91 92 MCorsikaFormat * CorsikaFormatFactory(MLog * log, const char * fileName);93 94 95 89 #endif 96 90
Note:
See TracChangeset
for help on using the changeset viewer.