source: trunk/MagicSoft/Mars/mraw/MRawFileRead.cc@ 6478

Last change on this file since 6478 was 6459, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 10.0 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): 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
62ClassImp(MRawFileRead);
63
64using 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>
69class bifstream : public istream, public streambuf
70{
71private:
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 }
89public:
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//
102MRawFileRead::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 : "MRead";
106 fTitle = title ? title : "Read task to read DAQ binary files";
107
108 fFileNames = new TList;
109 fFileNames->SetOwner();
110
111 if (fname!=NULL)
112 AddFile(fname);
113}
114
115// --------------------------------------------------------------------------
116//
117// Destructor. Delete input stream.
118//
119MRawFileRead::~MRawFileRead()
120{
121 delete fFileNames;
122 if (fIn)
123 delete fIn;
124}
125
126Byte_t MRawFileRead::IsFileValid(const char *name)
127{
128 ifstream fin(name);
129 if (!fin)
130 return 0;
131
132 Byte_t c[4];
133 fin.read((char*)c, 4);
134 if (!fin)
135 return 0;
136
137 if (c[0]!=0xc0)
138 return 0;
139
140 if (c[1]==0xc0)
141 return 1;
142
143 if (c[1]==0xc1)
144 return 2;
145
146 return 0;
147}
148
149// --------------------------------------------------------------------------
150//
151// Add a new file to a list of files to be processed, Returns the number
152// of files added. (We can enhance this with a existance chack and
153// wildcard support)
154//
155Int_t MRawFileRead::AddFile(const char *fname, Int_t entries)
156{
157 TNamed *name = new TNamed(fname, "");
158 fFileNames->AddLast(name);
159 return 1;
160
161}
162
163// --------------------------------------------------------------------------
164//
165// This opens the next file in the list and deletes its name from the list.
166//
167Int_t MRawFileRead::OpenNextFile(Bool_t print)
168{
169 //
170 // open the input stream and check if it is really open (file exists?)
171 //
172 if (fIn)
173 delete fIn;
174 fIn = NULL;
175
176 //
177 // Check for the existance of a next file to read
178 //
179 TObject *file = fFileNames->At(fNumFile);
180 if (!file)
181 return kFALSE;
182
183 //
184 // open the file which is the first one in the chain
185 //
186 const char *name = file->GetName();
187
188 const char *expname = gSystem->ExpandPathName(name);
189 fIn = new ifstream(expname);
190
191 const Bool_t noexist = !(*fIn);
192 if (noexist)
193 {
194 *fLog << err << "Cannot open file " << expname << ": ";
195 *fLog << strerror(errno) << endl;
196 }
197 else
198 *fLog << inf << "Open file: '" << name << "'" << endl;
199
200 delete [] expname;
201
202 if (noexist)
203 return kFALSE;
204
205 fNumFile++;
206
207 //
208 // Read RUN HEADER (see specification) from input stream
209 //
210 if (!fRawRunHeader->ReadEvt(*fIn))
211 return kERROR;
212
213 if (!(*fIn))
214 {
215 *fLog << err << "Error: Accessing file '" << name << "'" << endl;
216 return kERROR;
217 }
218
219 if (!print)
220 return kTRUE;
221
222 //
223 // Print Run Header
224 //
225 fRawRunHeader->Print();
226 *fRawEvtTime = fRawRunHeader->GetRunStart();
227
228 fNumEvents += fRawRunHeader->GetNumEvents();
229
230 fRawRunHeader->SetReadyToSave();
231
232 //
233 // Give the run header information to the 'sub-classes'
234 // Run header must be valid!
235 //
236 fRawEvtHeader->InitRead(fRawRunHeader, fRawEvtTime);
237 fRawEvtData1 ->InitRead(fRawRunHeader);
238 fRawEvtData2 ->InitRead(fRawRunHeader);
239
240 //
241 // Search for MTaskList
242 //
243 MTask *tlist = (MTask*)fParList->FindObject("MTaskList");
244 if (!tlist)
245 {
246 *fLog << err << dbginf << "MTaskList not found... abort." << endl;
247 return kERROR;
248 }
249
250 //
251 // A new file has been opened and new headers have been read.
252 // --> ReInit tasklist
253 //
254 return tlist->ReInit(fParList) ? kTRUE : kERROR;
255}
256
257// --------------------------------------------------------------------------
258//
259// Return file name of current file.
260//
261TString MRawFileRead::GetFileName() const
262{
263 const TObject *file = fFileNames->At(fNumFile-1);
264 return file ? file->GetName() : "";
265}
266
267// --------------------------------------------------------------------------
268//
269// Restart with the first file
270//
271Bool_t MRawFileRead::Rewind()
272{
273 fNumFile=0;
274 fNumEvents=0;
275 return OpenNextFile()==kTRUE;
276}
277
278Bool_t MRawFileRead::CalcNumTotalEvents()
279{
280 fNumTotalEvents = 0;
281
282 Bool_t rc = kTRUE;
283
284 while (1)
285 {
286 switch (OpenNextFile(kFALSE))
287 {
288 case kFALSE:
289 break;
290 case kERROR:
291 rc = kFALSE;
292 break;
293 case kTRUE:
294 fNumTotalEvents += fRawRunHeader->GetNumEvents();
295 continue;
296 }
297 break;
298 }
299
300 if (fIn)
301 delete fIn;
302 fIn = NULL;
303
304 return rc;
305}
306
307// --------------------------------------------------------------------------
308//
309// The PreProcess of this task checks for the following containers in the
310// list:
311// MRawRunHeader <output> if not found it is created
312// MRawEvtHeader <output> if not found it is created
313// MRawEvtData <output> if not found it is created
314// MRawCrateArray <output> if not found it is created
315// MRawEvtTime <output> if not found it is created (MTime)
316//
317// If all containers are found or created the run header is read from the
318// binary file and printed. If the Magic-Number (file identification)
319// doesn't match we stop the eventloop.
320//
321// Now the EvtHeader and EvtData containers are initialized.
322//
323Int_t MRawFileRead::PreProcess(MParList *pList)
324{
325 fParList = pList;
326
327 //
328 // open the input stream
329 // first of all check if opening the file in the constructor was
330 // successfull
331 //
332 if (!MRawRead::PreProcess(pList))
333 return kFALSE;
334
335 *fLog << inf << "Calculating number of total events..." << flush;
336 if (!CalcNumTotalEvents())
337 return kFALSE;
338 *fLog << inf << " " << fNumTotalEvents << " found." << endl;
339
340 fNumFile=0;
341 fNumEvents=0;
342
343 return kTRUE;
344}
345
346// --------------------------------------------------------------------------
347//
348// The Process reads one event from the binary file:
349// - The event header is read
350// - the run header is read
351// - all crate information is read
352// - the raw data information of one event is read
353//
354Int_t MRawFileRead::Process()
355{
356 while (1)
357 {
358 if (fIn)
359 {
360 //
361 // skip events if requested
362 //
363 if (fInterleave>1 && GetNumExecutions()%fInterleave>0 && fIn->peek()!=EOF)
364 {
365 SkipEvent(*fIn);
366 return kCONTINUE;
367 }
368
369 //
370 // Read a single event from file
371 //
372 const Bool_t rc = ReadEvent(*fIn);
373 if (rc!=kFALSE)
374 return rc;
375 }
376
377 //
378 // If an event could not be read from file try to open new file
379 //
380 const Int_t rc = OpenNextFile();
381 if (rc!=kTRUE)
382 return rc;
383 }
384 return kTRUE;
385}
386
387// --------------------------------------------------------------------------
388//
389// Close the file. Check whether the number of read events differs from
390// the number the file should containe (MRawRunHeader). Prints a warning
391// if it doesn't match.
392//
393Int_t MRawFileRead::PostProcess()
394{
395 //
396 // Sanity check for the number of events
397 //
398 if (fNumEvents==GetNumExecutions()-1 || GetNumExecutions()==0)
399 return kTRUE;
400
401 *fLog << warn << dec;
402 *fLog << "Warning - number of read events (" << GetNumExecutions()-1;
403 *fLog << ") doesn't match number in run header(s) (";
404 *fLog << fNumEvents << ")." << endl;
405
406 return kTRUE;
407}
Note: See TracBrowser for help on using the repository browser.