source: branches/Corsika7405Compatibility/mcorsika/MCorsikaFormat.cc@ 19851

Last change on this file since 19851 was 18179, checked in by dbaack, 9 years ago
File size: 7.7 KB
Line 
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
39using namespace std;
40
41
42const unsigned int MCorsikaFormat::kSyncMarker = 0xd41f8a37;
43
44// --------------------------------------------------------------------------
45//
46MCorsikaFormat *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[9];
60 memset(buffer, 0, 9);
61 fileIn->read(buffer, 8);
62 fileIn->seekg(-8, ios::cur);
63
64 if (strcmp(buffer, "RUNH") == 0)
65 {
66 delete [] buffer;
67 return new MCorsikaFormatRaw(fileIn);
68 }
69 else if(strcmp(&buffer[4], "RUNH") == 0)
70 {
71 fileIn->seekg(4, ios::cur);
72 delete [] buffer;
73 return new MCorsikaFormatRaw(fileIn, true);
74 }
75 if (*reinterpret_cast<unsigned int*>(buffer) == kSyncMarker)
76 {
77 //std::cout << "EventIO!!" << std::endl;
78 delete [] buffer;
79 return new MCorsikaFormatEventIO(fileIn);
80 }
81
82 gLog << err << "File " << fileName <<
83 " is neither a CORSIKA raw nor EventIO file" << endl;
84 delete fileIn;
85 delete [] buffer;
86
87 return NULL;
88}
89
90Bool_t MCorsikaFormat::Read(void *ptr, Int_t i) const
91{
92 //std::cout << "RawRead of " << i << " Bytes" << std::endl;
93 fIn->read((char*)ptr, i);
94 return !fIn->fail();
95
96}
97// --------------------------------------------------------------------------
98//
99Bool_t MCorsikaFormat::Eof() const
100{
101 return fIn->eof();
102}
103
104// --------------------------------------------------------------------------
105//
106MCorsikaFormat::~MCorsikaFormat()
107{
108 delete fIn;
109}
110
111
112// --------------------------------------------------------------------------
113//
114// After a call to this function, the file pointer is located after the
115// header of the block. As the event block has no header it is located
116// at the beginning of this block, which is as the beginning of the data
117Bool_t MCorsikaFormatRaw::NextBlock(Int_t readState,
118 Int_t & blockType,
119 Int_t & blockVersion,
120 Int_t & blockIdentifier,
121 Int_t & blockLength) const
122{
123 //std::cout << "||||||| NextBlock " << std::endl;
124 int blockHeader;
125 fIn->read((char*)&blockHeader, 4);
126 if (fIn->eof())
127 return kFALSE;
128
129
130 blockVersion = 0;
131 blockIdentifier = 0;
132 blockLength = 272 * 4;
133
134 switch(blockHeader)
135 {
136 case 1213093202 : // RUNH
137 blockType = 1200;
138 break;
139 case 1162761554 : // RUNE
140 blockType = 1210;
141 break;
142 case 1213486661 : // EVTH
143 if (readState != 10)
144 { blockType = 1202; }
145 else
146 {
147 blockType = 1105;
148 fIn->seekg(-4, ios::cur);
149 blockLength += 4;
150 }
151 break;
152 case 1163155013 : // EVTE
153 blockType = 1209;
154 break;
155 default: // the events, they don't have a specific header
156 blockType = 1105;
157 fIn->seekg(-4, ios::cur);
158 blockLength += 4;
159 }
160 return kTRUE;
161}
162// --------------------------------------------------------------------------
163//
164Bool_t MCorsikaFormatRaw::SeekEvtEnd()
165{
166 // Search subblockwise backward (Block: 5733*4 = 21*273*4)
167 for (int i=1; i<22; i++)
168 {
169 if(fFortranRaw)
170 fIn->seekg(-i*273*4 - 4, ios::end);
171 else
172 fIn->seekg(-i*273*4, ios::end);
173
174 char runh[5]="\0\0\0\0";
175 fIn->read(runh, 4);
176
177 if (!strcmp(runh, "RUNE"))
178 {
179// fIn->seekg(-4, ios::cur);
180 return kTRUE;
181 }
182 }
183
184 return kTRUE;
185}
186///////////////////////////////////////////////////////////////////////////////
187///////////////////////////////////////////////////////////////////////////////
188
189Bool_t MCorsikaFormatEventIO::NextBlock(Int_t readState,
190 Int_t & blockType,
191 Int_t & blockVersion,
192 Int_t & blockIdentifier,
193 Int_t & blockLength) const
194{
195// we read - synchronisation markerif not subBlock
196// - type / version field
197// - identification field
198// - length
199// - unknown field
200// - id (first 4 bytes of data field)
201
202 if (readState == 4)
203// if (subBlock)
204 {
205 // this is a sub-block
206 int blockHeader[3];
207 fIn->read((char*)blockHeader, 3 * sizeof(int));
208
209 if (fIn->eof())
210 return kFALSE;
211
212
213 blockType = blockHeader[0] & 0xFFFF;
214 blockVersion = (blockHeader[0] & 0xFFF00000) >> 20;
215 blockIdentifier = blockHeader[1];
216 blockLength = blockHeader[2] & 0x3FFFFFFF;
217
218 std::cout << "ReadState " << readState << " | Type:"
219 << blockType << " Version:" << blockVersion << " Length:" << blockLength << " Id:" << blockIdentifier << std::endl;
220
221 }
222 else
223 {
224 int blockHeader[4];
225 fIn->read((char*)blockHeader, 4 * sizeof(int));
226
227 if (fIn->eof())
228 return kFALSE;
229
230
231 blockType = blockHeader[1] & 0xFFFF;
232 blockVersion = (blockHeader[1] & 0xFFF00000) >> 20;
233 blockIdentifier = blockHeader[2];
234 blockLength = blockHeader[3] & 0x3FFFFFFF;
235
236 std::cout << "ReadState " << readState << " | Type:"
237 << blockType << " Version:" << blockVersion << " Length:" << blockLength << " Id:" << blockIdentifier << std::endl;
238
239
240 if (blockType == 1200 || blockType == 1210 ||
241 blockType == 1202 || blockType == 1209 )
242 {
243 // read the "old" corsika header plus additional 4 unknown byte
244 char tmp[8];
245 fIn->read(tmp, 8);
246 blockLength -= 8;
247 }
248
249 }
250 return kTRUE;
251}
252
253// --------------------------------------------------------------------------
254//
255Bool_t MCorsikaFormatEventIO::SeekEvtEnd()
256{
257
258 // the RUNE block it at the very end of the file.
259 fIn->seekg(-32, ios::end);
260
261 unsigned int blockHeader[4];
262 fIn->read((char*)blockHeader, 4 * sizeof(int));
263
264 if ( blockHeader[0] == kSyncMarker &&
265 (blockHeader[1] & 0xffff) == 1210 &&
266 (blockHeader[3] & 0x3fffffff) == 16)
267 {
268 // this seams to be a RUNE (1210) block
269 fIn->seekg( 8, ios::cur);
270 return kTRUE;
271 }
272
273 return kFALSE;
274}
275
Note: See TracBrowser for help on using the repository browser.