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 | // --------------------------------------------------------------------------
|
---|
43 | //
|
---|
44 | MCorsikaFormat *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 |
|
---|
92 | Bool_t MCorsikaFormat::Read(void *ptr, Int_t i) const
|
---|
93 | {
|
---|
94 | fIn->read((char*)ptr, i);
|
---|
95 | return !fIn->fail();
|
---|
96 |
|
---|
97 | }
|
---|
98 | // --------------------------------------------------------------------------
|
---|
99 | //
|
---|
100 | Bool_t MCorsikaFormat::Eof() const
|
---|
101 | {
|
---|
102 | return fIn->eof();
|
---|
103 | }
|
---|
104 |
|
---|
105 | // --------------------------------------------------------------------------
|
---|
106 | //
|
---|
107 | MCorsikaFormat::~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
|
---|
118 | Bool_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 | // cout << "P=" << fIn->tellg() << " L=" <<blockLength << " " << hex << blockHeader << dec << " " << fIn->eof() << endl;
|
---|
138 |
|
---|
139 | switch(blockHeader)
|
---|
140 | {
|
---|
141 | case kRUNH:
|
---|
142 | blockType = 1200;
|
---|
143 | break;
|
---|
144 |
|
---|
145 | case kRUNE:
|
---|
146 | blockType = 1210;
|
---|
147 | break;
|
---|
148 |
|
---|
149 | case kEVTH:
|
---|
150 | if (readState != 10)
|
---|
151 | blockType = 1202; // event header (where readstate := 2)
|
---|
152 | else
|
---|
153 | {
|
---|
154 | blockType = 1105; // raw data
|
---|
155 | fIn->seekg(-4, ios::cur);
|
---|
156 | blockLength += 4;
|
---|
157 | }
|
---|
158 | break;
|
---|
159 |
|
---|
160 | case kEVTE:
|
---|
161 | blockType = 1209;
|
---|
162 | break;
|
---|
163 |
|
---|
164 | default: // the events, they don't have a specific header
|
---|
165 | blockType = 1105; // raw data
|
---|
166 | fIn->seekg(-4, ios::cur);
|
---|
167 | blockLength += 4;
|
---|
168 | }
|
---|
169 | return kTRUE;
|
---|
170 | }
|
---|
171 | // --------------------------------------------------------------------------
|
---|
172 | //
|
---|
173 | Bool_t MCorsikaFormatRaw::SeekEvtEnd()
|
---|
174 | {
|
---|
175 | const uint32_t step = fBlockLength/21;
|
---|
176 | const uint32_t offset = fBlockLength?4:0;
|
---|
177 |
|
---|
178 | // cout << fIn->bad() << " " << fIn->eof() << " " << fIn->tellg() << endl;
|
---|
179 |
|
---|
180 | // Search subblockwise backward (Block: 5733*4 = 21*273*4)
|
---|
181 | for (uint32_t i=1; i<22; i++)
|
---|
182 | {
|
---|
183 | const int32_t L = i*step + offset;
|
---|
184 | fIn->seekg(-L, ios::end);
|
---|
185 |
|
---|
186 | uint32_t magic = 0;
|
---|
187 | fIn->read((char*)&magic, 4);
|
---|
188 |
|
---|
189 | // cout << dec << L << " " << fIn->tellg() << " " << hex << magic << endl;
|
---|
190 |
|
---|
191 | if (magic==kRUNE)
|
---|
192 | {
|
---|
193 | // fIn->seekg(-4, ios::cur);
|
---|
194 | return kTRUE;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | return kFALSE;
|
---|
199 | }
|
---|
200 | ///////////////////////////////////////////////////////////////////////////////
|
---|
201 | ///////////////////////////////////////////////////////////////////////////////
|
---|
202 |
|
---|
203 | Bool_t MCorsikaFormatEventIO::NextBlock(Int_t readState,
|
---|
204 | Int_t & blockType,
|
---|
205 | Int_t & blockVersion,
|
---|
206 | Int_t & blockIdentifier,
|
---|
207 | Int_t & blockLength) const
|
---|
208 | {
|
---|
209 | // we read - synchronisation markerif not subBlock
|
---|
210 | // - type / version field
|
---|
211 | // - identification field
|
---|
212 | // - length
|
---|
213 | // - unknown field
|
---|
214 | // - id (first 4 bytes of data field)
|
---|
215 |
|
---|
216 | if (readState == 4)
|
---|
217 | // if (subBlock)
|
---|
218 | {
|
---|
219 | // this is a sub-block
|
---|
220 | int blockHeader[3];
|
---|
221 | fIn->read((char*)blockHeader, 3 * sizeof(int));
|
---|
222 |
|
---|
223 | if (fIn->eof())
|
---|
224 | return kFALSE;
|
---|
225 |
|
---|
226 |
|
---|
227 | blockType = blockHeader[0] & 0xFFFF;
|
---|
228 | blockVersion = (blockHeader[0] & 0xFFF00000) >> 20;
|
---|
229 | blockIdentifier = blockHeader[1];
|
---|
230 | blockLength = blockHeader[2] & 0x3FFFFFFF;
|
---|
231 | }
|
---|
232 | else
|
---|
233 | {
|
---|
234 | int blockHeader[4];
|
---|
235 | fIn->read((char*)blockHeader, 4 * sizeof(int));
|
---|
236 |
|
---|
237 | if (fIn->eof())
|
---|
238 | return kFALSE;
|
---|
239 |
|
---|
240 |
|
---|
241 | blockType = blockHeader[1] & 0xFFFF;
|
---|
242 | blockVersion = (blockHeader[1] & 0xFFF00000) >> 20;
|
---|
243 | blockIdentifier = blockHeader[2];
|
---|
244 | blockLength = blockHeader[3] & 0x3FFFFFFF;
|
---|
245 |
|
---|
246 | if (blockType == 1200 || blockType == 1210 ||
|
---|
247 | blockType == 1202 || blockType == 1209 )
|
---|
248 | {
|
---|
249 | // read the "old" corsika header plus additional 4 unknown byte
|
---|
250 | char tmp[8];
|
---|
251 | fIn->read(tmp, 8);
|
---|
252 | blockLength -= 8;
|
---|
253 | }
|
---|
254 |
|
---|
255 | }
|
---|
256 | return kTRUE;
|
---|
257 | }
|
---|
258 |
|
---|
259 | // --------------------------------------------------------------------------
|
---|
260 | //
|
---|
261 | Bool_t MCorsikaFormatEventIO::SeekEvtEnd()
|
---|
262 | {
|
---|
263 |
|
---|
264 | // the RUNE block it at the very end of the file.
|
---|
265 | fIn->seekg(-32, ios::end);
|
---|
266 |
|
---|
267 | unsigned int blockHeader[4];
|
---|
268 | fIn->read((char*)blockHeader, 4 * sizeof(int));
|
---|
269 |
|
---|
270 | if ( blockHeader[0] == kSyncMarker &&
|
---|
271 | (blockHeader[1] & 0xffff) == 1210 &&
|
---|
272 | (blockHeader[3] & 0x3fffffff) == 16)
|
---|
273 | {
|
---|
274 | // this seams to be a RUNE (1210) block
|
---|
275 | fIn->seekg( 8, ios::cur);
|
---|
276 | return kTRUE;
|
---|
277 | }
|
---|
278 |
|
---|
279 | return kFALSE;
|
---|
280 | }
|
---|
281 |
|
---|