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): Thomas Bretz 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2004
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | //////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MRawFileRead
|
---|
28 | //
|
---|
29 | // This tasks reads the raw binary file like specified in the TDAS???
|
---|
30 | // and writes the data in the corresponding containers which are
|
---|
31 | // either retrieved from the parameter list or created and added.
|
---|
32 | //
|
---|
33 | // Use SetInterleave() if you don't want to read all events, eg
|
---|
34 | // SetInterleave(5) reads only each 5th event.
|
---|
35 | //
|
---|
36 | // Input Containers:
|
---|
37 | // -/-
|
---|
38 | //
|
---|
39 | // Output Containers:
|
---|
40 | // MRawRunHeader, MRawEvtHeader, MRawEvtData, MRawCrateArray, MRawEvtTime
|
---|
41 | //
|
---|
42 | //////////////////////////////////////////////////////////////////////////////
|
---|
43 | #include "MRawFileRead.h"
|
---|
44 |
|
---|
45 | #include <errno.h>
|
---|
46 | #include <fstream>
|
---|
47 |
|
---|
48 | #include <TSystem.h>
|
---|
49 |
|
---|
50 | #include "MLog.h"
|
---|
51 | #include "MLogManip.h"
|
---|
52 |
|
---|
53 | #include "MTime.h"
|
---|
54 | #include "MParList.h"
|
---|
55 |
|
---|
56 | #include "MRawRunHeader.h"
|
---|
57 | #include "MRawEvtHeader.h"
|
---|
58 | #include "MRawEvtData.h"
|
---|
59 | #include "MRawCrateData.h"
|
---|
60 | #include "MRawCrateArray.h"
|
---|
61 |
|
---|
62 | ClassImp(MRawFileRead);
|
---|
63 |
|
---|
64 | using namespace std;
|
---|
65 |
|
---|
66 | /* ----------- please don't delete and don't care about (Thomas) ------------
|
---|
67 | #define kBUFSZ 64 //1024*1024*64
|
---|
68 | #include <iomanip.h>
|
---|
69 | class bifstream : public istream, public streambuf
|
---|
70 | {
|
---|
71 | private:
|
---|
72 | char fBuffer[kBUFSZ]; //!
|
---|
73 | FILE *fd;
|
---|
74 |
|
---|
75 | int sync()
|
---|
76 | {
|
---|
77 | memset(fBuffer, 0, kBUFSZ);
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 | int underflow()
|
---|
81 | {
|
---|
82 | int sz=fread(fBuffer, kBUFSZ, 1, fd);
|
---|
83 | //int sz=fread(fBuffer, 1, kBUFSZ, fd);
|
---|
84 | setg(fBuffer, fBuffer, fBuffer+kBUFSZ);
|
---|
85 |
|
---|
86 | return sz==1 ? *(unsigned char*)fBuffer : EOF;//EOF;
|
---|
87 | //return sz==kBUFSZ ? *(unsigned char*)fBuffer : EOF;//EOF;
|
---|
88 | }
|
---|
89 | public:
|
---|
90 | bifstream(const char *name) : istream(this)
|
---|
91 | {
|
---|
92 | fd = fopen(name, "rb");
|
---|
93 | setbuf(fBuffer, kBUFSZ);
|
---|
94 | }
|
---|
95 | };
|
---|
96 | */
|
---|
97 |
|
---|
98 | // --------------------------------------------------------------------------
|
---|
99 | //
|
---|
100 | // Default constructor. It tries to open the given file.
|
---|
101 | //
|
---|
102 | MRawFileRead::MRawFileRead(const char *fname, const char *name, const char *title)
|
---|
103 | : fFileNames(NULL), fNumFile(0), fIn(NULL), fParList(NULL), fInterleave(1)
|
---|
104 | {
|
---|
105 | fName = name ? name : "MRawFileRead";
|
---|
106 | fTitle = title ? title : "Read task to read DAQ binary files";
|
---|
107 |
|
---|
108 | fFileNames = new TList;
|
---|
109 | fFileNames->SetOwner();
|
---|
110 |
|
---|
111 | AddFile(fname);
|
---|
112 | }
|
---|
113 |
|
---|
114 | // --------------------------------------------------------------------------
|
---|
115 | //
|
---|
116 | // Destructor. Delete input stream.
|
---|
117 | //
|
---|
118 | MRawFileRead::~MRawFileRead()
|
---|
119 | {
|
---|
120 | delete fFileNames;
|
---|
121 | if (fIn)
|
---|
122 | delete fIn;
|
---|
123 | }
|
---|
124 |
|
---|
125 | // --------------------------------------------------------------------------
|
---|
126 | //
|
---|
127 | // Add a new file to a list of files to be processed, Returns the number
|
---|
128 | // of files added. (We can enhance this with a existance chack and
|
---|
129 | // wildcard support)
|
---|
130 | //
|
---|
131 | Int_t MRawFileRead::AddFile(const char *fname)
|
---|
132 | {
|
---|
133 | TNamed *name = new TNamed(fname, "");
|
---|
134 | fFileNames->AddLast(name);
|
---|
135 | return 1;
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | // --------------------------------------------------------------------------
|
---|
140 | //
|
---|
141 | // This opens the next file in the list and deletes its name from the list.
|
---|
142 | //
|
---|
143 | Bool_t MRawFileRead::OpenNextFile()
|
---|
144 | {
|
---|
145 | //
|
---|
146 | // open the input stream and check if it is really open (file exists?)
|
---|
147 | //
|
---|
148 | if (fIn)
|
---|
149 | delete fIn;
|
---|
150 | fIn = NULL;
|
---|
151 |
|
---|
152 | //
|
---|
153 | // Check for the existance of a next file to read
|
---|
154 | //
|
---|
155 | TObject *file = fFileNames->At(fNumFile);
|
---|
156 | if (!file)
|
---|
157 | return kFALSE;
|
---|
158 |
|
---|
159 | //
|
---|
160 | // open the file which is the first one in the chain
|
---|
161 | //
|
---|
162 | const char *name = file->GetName();
|
---|
163 |
|
---|
164 | const char *expname = gSystem->ExpandPathName(name);
|
---|
165 | fIn = new ifstream(expname);
|
---|
166 |
|
---|
167 | const Bool_t noexist = !(*fIn);
|
---|
168 | if (noexist)
|
---|
169 | {
|
---|
170 | *fLog << err << "Cannot open file " << expname << ": ";
|
---|
171 | *fLog << strerror(errno) << endl;
|
---|
172 | }
|
---|
173 | else
|
---|
174 | *fLog << inf << "Open file: '" << name << "'" << endl;
|
---|
175 |
|
---|
176 | delete [] expname;
|
---|
177 |
|
---|
178 | if (noexist)
|
---|
179 | return kFALSE;
|
---|
180 |
|
---|
181 | fNumFile++;
|
---|
182 |
|
---|
183 | //
|
---|
184 | // Read RUN HEADER (see specification) from input stream
|
---|
185 | //
|
---|
186 | if (!fRawRunHeader->ReadEvt(*fIn))
|
---|
187 | return kFALSE;
|
---|
188 |
|
---|
189 | if (!(*fIn))
|
---|
190 | {
|
---|
191 | *fLog << err << "Error: Accessing file '" << name << "'" << endl;
|
---|
192 | return kFALSE;
|
---|
193 | }
|
---|
194 |
|
---|
195 | //
|
---|
196 | // Print Run Header
|
---|
197 | //
|
---|
198 | fRawRunHeader->Print();
|
---|
199 | *fRawEvtTime = fRawRunHeader->GetRunStart();
|
---|
200 |
|
---|
201 | fNumEvents += fRawRunHeader->GetNumEvents();
|
---|
202 |
|
---|
203 | //
|
---|
204 | // Give the run header information to the 'sub-classes'
|
---|
205 | // Run header must be valid!
|
---|
206 | //
|
---|
207 | fRawEvtHeader->Init(fRawRunHeader, fRawEvtTime);
|
---|
208 | fRawEvtData ->Init(fRawRunHeader, fRawCrateArray);
|
---|
209 |
|
---|
210 | //
|
---|
211 | // Search for MTaskList
|
---|
212 | //
|
---|
213 | MTask *tlist = (MTask*)fParList->FindObject("MTaskList");
|
---|
214 | if (!tlist)
|
---|
215 | {
|
---|
216 | *fLog << err << dbginf << "MTaskList not found... abort." << endl;
|
---|
217 | return kFALSE;
|
---|
218 | }
|
---|
219 |
|
---|
220 | //
|
---|
221 | // A new file has been opened and new headers have been read.
|
---|
222 | // --> ReInit tasklist
|
---|
223 | //
|
---|
224 | return tlist->ReInit(fParList);
|
---|
225 | }
|
---|
226 |
|
---|
227 | // --------------------------------------------------------------------------
|
---|
228 | //
|
---|
229 | // Return file name of current file.
|
---|
230 | //
|
---|
231 | const TString MRawFileRead::GetFileName() const
|
---|
232 | {
|
---|
233 | const TObject *file = fFileNames->At(fNumFile-1);
|
---|
234 | return file ? file->GetName() : "";
|
---|
235 | }
|
---|
236 |
|
---|
237 | // --------------------------------------------------------------------------
|
---|
238 | //
|
---|
239 | // Restart with the first file
|
---|
240 | //
|
---|
241 | Bool_t MRawFileRead::Rewind()
|
---|
242 | {
|
---|
243 | fNumFile=0;
|
---|
244 | fNumEvents=0;
|
---|
245 | return OpenNextFile();
|
---|
246 | }
|
---|
247 |
|
---|
248 | // --------------------------------------------------------------------------
|
---|
249 | //
|
---|
250 | // The PreProcess of this task checks for the following containers in the
|
---|
251 | // list:
|
---|
252 | // MRawRunHeader <output> if not found it is created
|
---|
253 | // MRawEvtHeader <output> if not found it is created
|
---|
254 | // MRawEvtData <output> if not found it is created
|
---|
255 | // MRawCrateArray <output> if not found it is created
|
---|
256 | // MRawEvtTime <output> if not found it is created (MTime)
|
---|
257 | //
|
---|
258 | // If all containers are found or created the run header is read from the
|
---|
259 | // binary file and printed. If the Magic-Number (file identification)
|
---|
260 | // doesn't match we stop the eventloop.
|
---|
261 | //
|
---|
262 | // Now the EvtHeader and EvtData containers are initialized.
|
---|
263 | //
|
---|
264 | Int_t MRawFileRead::PreProcess(MParList *pList)
|
---|
265 | {
|
---|
266 | fParList = pList;
|
---|
267 |
|
---|
268 | //
|
---|
269 | // open the input stream
|
---|
270 | // first of all check if opening the file in the constructor was
|
---|
271 | // successfull
|
---|
272 | //
|
---|
273 | if (!MRawRead::PreProcess(pList))
|
---|
274 | return kFALSE;
|
---|
275 |
|
---|
276 | //
|
---|
277 | // Now open next (first) file
|
---|
278 | //
|
---|
279 | if (!Rewind())
|
---|
280 | return kFALSE;
|
---|
281 |
|
---|
282 | return kTRUE;
|
---|
283 | }
|
---|
284 |
|
---|
285 | // --------------------------------------------------------------------------
|
---|
286 | //
|
---|
287 | // The Process reads one event from the binary file:
|
---|
288 | // - The event header is read
|
---|
289 | // - the run header is read
|
---|
290 | // - all crate information is read
|
---|
291 | // - the raw data information of one event is read
|
---|
292 | //
|
---|
293 | Int_t MRawFileRead::Process()
|
---|
294 | {
|
---|
295 | while (1)
|
---|
296 | {
|
---|
297 | //
|
---|
298 | // skip events if requested
|
---|
299 | //
|
---|
300 | if (fInterleave>1 && GetNumExecutions()%fInterleave>0 && fIn->peek()!=EOF)
|
---|
301 | {
|
---|
302 | SkipEvent(*fIn);
|
---|
303 | return kCONTINUE;
|
---|
304 | }
|
---|
305 |
|
---|
306 | //
|
---|
307 | // Read a single event from file
|
---|
308 | //
|
---|
309 | const Bool_t rc = ReadEvent(*fIn);
|
---|
310 | if (rc!=kFALSE)
|
---|
311 | return rc;
|
---|
312 |
|
---|
313 | //
|
---|
314 | // If an event could not be read from file try to open new file
|
---|
315 | //
|
---|
316 | if (!OpenNextFile())
|
---|
317 | return kFALSE;
|
---|
318 | }
|
---|
319 | return kTRUE;
|
---|
320 | }
|
---|
321 |
|
---|
322 | // --------------------------------------------------------------------------
|
---|
323 | //
|
---|
324 | // Close the file. Check whether the number of read events differs from
|
---|
325 | // the number the file should containe (MRawRunHeader). Prints a warning
|
---|
326 | // if it doesn't match.
|
---|
327 | //
|
---|
328 | Int_t MRawFileRead::PostProcess()
|
---|
329 | {
|
---|
330 | //
|
---|
331 | // Sanity check for the number of events
|
---|
332 | //
|
---|
333 | if (fNumEvents==GetNumExecutions()-1 || GetNumExecutions()==0)
|
---|
334 | return kTRUE;
|
---|
335 |
|
---|
336 | *fLog << warn << dec;
|
---|
337 | *fLog << "Warning - number of read events (" << GetNumExecutions()-1;
|
---|
338 | *fLog << ") doesn't match number in run header(s) (";
|
---|
339 | *fLog << fNumEvents << ")." << endl;
|
---|
340 |
|
---|
341 | return kTRUE;
|
---|
342 | }
|
---|