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

Last change on this file since 4949 was 4738, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 9.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 : "MRawFileRead";
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
126// --------------------------------------------------------------------------
127//
128// Add a new file to a list of files to be processed, Returns the number
129// of files added. (We can enhance this with a existance chack and
130// wildcard support)
131//
132Int_t MRawFileRead::AddFile(const char *fname, Int_t entries)
133{
134 TNamed *name = new TNamed(fname, "");
135 fFileNames->AddLast(name);
136 return 1;
137
138}
139
140// --------------------------------------------------------------------------
141//
142// This opens the next file in the list and deletes its name from the list.
143//
144Bool_t MRawFileRead::OpenNextFile()
145{
146 //
147 // open the input stream and check if it is really open (file exists?)
148 //
149 if (fIn)
150 delete fIn;
151 fIn = NULL;
152
153 //
154 // Check for the existance of a next file to read
155 //
156 TObject *file = fFileNames->At(fNumFile);
157 if (!file)
158 return kFALSE;
159
160 //
161 // open the file which is the first one in the chain
162 //
163 const char *name = file->GetName();
164
165 const char *expname = gSystem->ExpandPathName(name);
166 fIn = new ifstream(expname);
167
168 const Bool_t noexist = !(*fIn);
169 if (noexist)
170 {
171 *fLog << err << "Cannot open file " << expname << ": ";
172 *fLog << strerror(errno) << endl;
173 }
174 else
175 *fLog << inf << "Open file: '" << name << "'" << endl;
176
177 delete [] expname;
178
179 if (noexist)
180 return kFALSE;
181
182 fNumFile++;
183
184 //
185 // Read RUN HEADER (see specification) from input stream
186 //
187 if (!fRawRunHeader->ReadEvt(*fIn))
188 return kFALSE;
189
190 if (!(*fIn))
191 {
192 *fLog << err << "Error: Accessing file '" << name << "'" << endl;
193 return kFALSE;
194 }
195
196 //
197 // Print Run Header
198 //
199 fRawRunHeader->Print();
200 *fRawEvtTime = fRawRunHeader->GetRunStart();
201
202 fNumEvents += fRawRunHeader->GetNumEvents();
203
204 //
205 // Give the run header information to the 'sub-classes'
206 // Run header must be valid!
207 //
208 fRawEvtHeader->InitRead(fRawRunHeader, fRawEvtTime);
209 fRawEvtData1 ->InitRead(fRawRunHeader);
210 fRawEvtData2 ->InitRead(fRawRunHeader);
211
212 //
213 // Search for MTaskList
214 //
215 MTask *tlist = (MTask*)fParList->FindObject("MTaskList");
216 if (!tlist)
217 {
218 *fLog << err << dbginf << "MTaskList not found... abort." << endl;
219 return kFALSE;
220 }
221
222 //
223 // A new file has been opened and new headers have been read.
224 // --> ReInit tasklist
225 //
226 return tlist->ReInit(fParList);
227}
228
229// --------------------------------------------------------------------------
230//
231// Return file name of current file.
232//
233TString MRawFileRead::GetFileName() const
234{
235 const TObject *file = fFileNames->At(fNumFile-1);
236 return file ? file->GetName() : "";
237}
238
239// --------------------------------------------------------------------------
240//
241// Restart with the first file
242//
243Bool_t MRawFileRead::Rewind()
244{
245 fNumFile=0;
246 fNumEvents=0;
247 return OpenNextFile();
248}
249
250// --------------------------------------------------------------------------
251//
252// The PreProcess of this task checks for the following containers in the
253// list:
254// MRawRunHeader <output> if not found it is created
255// MRawEvtHeader <output> if not found it is created
256// MRawEvtData <output> if not found it is created
257// MRawCrateArray <output> if not found it is created
258// MRawEvtTime <output> if not found it is created (MTime)
259//
260// If all containers are found or created the run header is read from the
261// binary file and printed. If the Magic-Number (file identification)
262// doesn't match we stop the eventloop.
263//
264// Now the EvtHeader and EvtData containers are initialized.
265//
266Int_t MRawFileRead::PreProcess(MParList *pList)
267{
268 fParList = pList;
269
270 //
271 // open the input stream
272 // first of all check if opening the file in the constructor was
273 // successfull
274 //
275 if (!MRawRead::PreProcess(pList))
276 return kFALSE;
277
278 //
279 // Now open next (first) file
280 //
281// if (!Rewind())
282// return kFALSE;
283
284
285 fNumFile=0;
286 fNumEvents=0;
287
288 return kTRUE;
289}
290
291// --------------------------------------------------------------------------
292//
293// The Process reads one event from the binary file:
294// - The event header is read
295// - the run header is read
296// - all crate information is read
297// - the raw data information of one event is read
298//
299Int_t MRawFileRead::Process()
300{
301 while (1)
302 {
303 if (fIn)
304 {
305 //
306 // skip events if requested
307 //
308 if (fInterleave>1 && GetNumExecutions()%fInterleave>0 && fIn->peek()!=EOF)
309 {
310 SkipEvent(*fIn);
311 return kCONTINUE;
312 }
313
314 //
315 // Read a single event from file
316 //
317 const Bool_t rc = ReadEvent(*fIn);
318 if (rc!=kFALSE)
319 return rc;
320 }
321
322 //
323 // If an event could not be read from file try to open new file
324 //
325 if (!OpenNextFile())
326 return kFALSE;
327 }
328 return kTRUE;
329}
330
331// --------------------------------------------------------------------------
332//
333// Close the file. Check whether the number of read events differs from
334// the number the file should containe (MRawRunHeader). Prints a warning
335// if it doesn't match.
336//
337Int_t MRawFileRead::PostProcess()
338{
339 //
340 // Sanity check for the number of events
341 //
342 if (fNumEvents==GetNumExecutions()-1 || GetNumExecutions()==0)
343 return kTRUE;
344
345 *fLog << warn << dec;
346 *fLog << "Warning - number of read events (" << GetNumExecutions()-1;
347 *fLog << ") doesn't match number in run header(s) (";
348 *fLog << fNumEvents << ")." << endl;
349
350 return kTRUE;
351}
Note: See TracBrowser for help on using the repository browser.