source: trunk/Mars/mcorsika/MCorsikaFormat.cc@ 19336

Last change on this file since 19336 was 19333, checked in by tbretz, 6 years ago
Fixed two typos.
File size: 7.5 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
42// --------------------------------------------------------------------------
43//
44MCorsikaFormat *MCorsikaFormat::CorsikaFormatFactory(const char * fileName)
45{
46 ifstream * fileIn = new ifstream(fileName);
47
48 const Bool_t noexist = !(*fileIn);
49 if (noexist)
50 {
51 gLog << err << "Cannot open file " << fileName << ": ";
52 gLog << (errno!=0?strerror(errno):"Insufficient memory for decompression") << endl;
53 delete fileIn;
54 return NULL;
55 }
56
57 uint32_t magic = 0;
58 fileIn->read((char*)&magic, 4);
59
60 uint32_t blocklength = 0;
61
62 // This seems to be a new corsika binary identifier
63 if (magic==kBlockLengthRaw || magic==kBlockLengthThin)
64 {
65 blocklength = magic;
66 fileIn->read((char*)&magic, 4);
67 }
68
69 // Jump back that the "RUNH" is can be read again
70 fileIn->seekg(-4, ios::cur);
71
72 if (magic==kRUNH)
73 {
74 gLog << inf2 << "Corsika RAW" << (blocklength==kBlockLengthThin?"(+THIN)":"") << " format detected." << endl;
75 return new MCorsikaFormatRaw(fileIn, blocklength);
76 }
77
78 if (magic==kSyncMarker)
79 {
80 gLog << inf2 << "Corsika EventIO format detected." << endl;
81 return new MCorsikaFormatEventIO(fileIn);
82 }
83
84 gLog << err << "File " << fileName << Form(" (%08x) ", magic);
85 gLog << "is neither a CORSIKA raw nor EventIO file" << endl;
86
87 delete fileIn;
88
89 return NULL;
90}
91
92Bool_t MCorsikaFormat::Read(void *ptr, Int_t i) const
93{
94 fIn->read((char*)ptr, i);
95 return !fIn->fail();
96
97}
98// --------------------------------------------------------------------------
99//
100Bool_t MCorsikaFormat::Eof() const
101{
102 return fIn->eof();
103}
104
105// --------------------------------------------------------------------------
106//
107MCorsikaFormat::~MCorsikaFormat()
108{
109 delete fIn;
110}
111
112
113// --------------------------------------------------------------------------
114//
115// After a call to this function, the file pointer is located after the
116// header of the block. As the event block has no header it is located
117// at the beginning of this block, which is as the beginning of the data
118Bool_t MCorsikaFormatRaw::NextBlock(Int_t readState,
119 Int_t & blockType,
120 Int_t & blockVersion,
121 Int_t & blockIdentifier,
122 Int_t & blockLength) const
123{
124
125 uint32_t blockHeader = fBlockLength;
126 while (blockHeader==fBlockLength)
127 {
128 fIn->read((char*)&blockHeader, 4);
129 if (fIn->eof())
130 return kFALSE;
131 }
132
133 blockVersion = 0;
134 blockIdentifier = 0;
135 blockLength = fBlockLength/21 - 4;
136
137 switch(blockHeader)
138 {
139 case kRUNH:
140 blockType = 1200;
141 break;
142
143 case kRUNE:
144 blockType = 1210;
145 break;
146
147 case kEVTH:
148 if (readState != 10)
149 blockType = 1202; // event header (where readstate := 2)
150 else
151 {
152 blockType = 1105; // raw data
153 fIn->seekg(-4, ios::cur);
154 blockLength += 4;
155 }
156 break;
157
158 case kEVTE:
159 blockType = 1209;
160 break;
161
162 default: // the events, they don't have a specific header
163 blockType = 1105; // raw data
164 fIn->seekg(-4, ios::cur);
165 blockLength += 4;
166 }
167 return kTRUE;
168}
169// --------------------------------------------------------------------------
170//
171Bool_t MCorsikaFormatRaw::SeekEvtEnd()
172{
173 const uint32_t step = fBlockLength/21;
174 const uint32_t offset = (fBlockLength?4:0);
175
176 // Search subblockwise backward (Block: 5733*4 = 21*273*4)
177 for (uint32_t i=1; i<22; i++)
178 {
179 const int32_t L = i*step + offset;
180 fIn->seekg(-L, ios::end);
181
182 uint32_t magic = 0;
183 fIn->read((char*)&magic, 4);
184
185 if (magic==kRUNE)
186 {
187// fIn->seekg(-4, ios::cur);
188 return kTRUE;
189 }
190 }
191
192 return kFALSE;
193}
194///////////////////////////////////////////////////////////////////////////////
195///////////////////////////////////////////////////////////////////////////////
196
197Bool_t MCorsikaFormatEventIO::NextBlock(Int_t readState,
198 Int_t & blockType,
199 Int_t & blockVersion,
200 Int_t & blockIdentifier,
201 Int_t & blockLength) const
202{
203// we read - synchronisation markerif not subBlock
204// - type / version field
205// - identification field
206// - length
207// - unknown field
208// - id (first 4 bytes of data field)
209
210 if (readState == 4)
211// if (subBlock)
212 {
213 // this is a sub-block
214 int blockHeader[3];
215 fIn->read((char*)blockHeader, 3 * sizeof(int));
216
217 if (fIn->eof())
218 return kFALSE;
219
220
221 blockType = blockHeader[0] & 0xFFFF;
222 blockVersion = (blockHeader[0] & 0xFFF00000) >> 20;
223 blockIdentifier = blockHeader[1];
224 blockLength = blockHeader[2] & 0x3FFFFFFF;
225 }
226 else
227 {
228 int blockHeader[4];
229 fIn->read((char*)blockHeader, 4 * sizeof(int));
230
231 if (fIn->eof())
232 return kFALSE;
233
234
235 blockType = blockHeader[1] & 0xFFFF;
236 blockVersion = (blockHeader[1] & 0xFFF00000) >> 20;
237 blockIdentifier = blockHeader[2];
238 blockLength = blockHeader[3] & 0x3FFFFFFF;
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.