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

Last change on this file since 1846 was 1082, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 7.6 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@uni-sw.gwdg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2001
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// Input Containers: //
34// -/- //
35// //
36// Output Containers: //
37// MRawRunHeader, MRawEvtHeader, MRawEvtData, MRawCrateArray, MRawEvtTime //
38// //
39//////////////////////////////////////////////////////////////////////////////
40
41#include "MRawFileRead.h"
42
43#include <fstream.h>
44
45#include "MLog.h"
46#include "MLogManip.h"
47
48#include "MTime.h"
49#include "MParList.h"
50#include "MRawRunHeader.h"
51#include "MRawEvtHeader.h"
52#include "MRawEvtData.h"
53#include "MRawCrateData.h"
54#include "MRawCrateArray.h"
55
56ClassImp(MRawFileRead);
57
58
59/* ----------- please don't delete and don't care about (Thomas) ------------
60#define kBUFSZ 64 //1024*1024*64
61#include <iomanip.h>
62class bifstream : public istream, public streambuf
63{
64private:
65 char fBuffer[kBUFSZ]; //!
66 FILE *fd;
67
68 int sync()
69 {
70 memset(fBuffer, 0, kBUFSZ);
71 return 0;
72 }
73 int underflow()
74 {
75 int sz=fread(fBuffer, kBUFSZ, 1, fd);
76 //int sz=fread(fBuffer, 1, kBUFSZ, fd);
77 setg(fBuffer, fBuffer, fBuffer+kBUFSZ);
78
79 return sz==1 ? *(unsigned char*)fBuffer : EOF;//EOF;
80 //return sz==kBUFSZ ? *(unsigned char*)fBuffer : EOF;//EOF;
81 }
82public:
83 bifstream(const char *name) : istream(this)
84 {
85 fd = fopen(name, "rb");
86 setbuf(fBuffer, kBUFSZ);
87 }
88};
89*/
90
91// --------------------------------------------------------------------------
92//
93// Default constructor. It tries to open the given file.
94//
95MRawFileRead::MRawFileRead(const char *fname, const char *name, const char *title)
96 : fFileName(fname), fIn(NULL)
97{
98 fName = name ? name : "MRawFileRead";
99 fTitle = title ? title : "Read task to read DAQ binary files";
100
101 fIn = new ifstream;
102}
103
104// --------------------------------------------------------------------------
105//
106// Destructor. Delete input stream.
107//
108MRawFileRead::~MRawFileRead()
109{
110 delete fIn;
111}
112
113// --------------------------------------------------------------------------
114//
115// The PreProcess of this task checks for the following containers in the
116// list:
117// MRawRunHeader <output> if not found it is created
118// MRawEvtHeader <output> if not found it is created
119// MRawEvtData <output> if not found it is created
120// MRawCrateArray <output> if not found it is created
121// MRawEvtTime <output> if not found it is created (MTime)
122//
123// If all containers are found or created the run header is read from the
124// binary file and printed. If the Magic-Number (file identification)
125// doesn't match we stop the eventloop.
126//
127// Now the EvtHeader and EvtData containers are initialized.
128//
129Bool_t MRawFileRead::PreProcess(MParList *pList)
130{
131 //
132 // open the input stream
133 // first of all check if opening the file in the constructor was
134 // successfull
135 //
136 fIn->open(fFileName);
137 if (!(*fIn))
138 {
139 *fLog << err << "Error: Cannot open file '" << fFileName << "'" << endl;
140 return kFALSE;
141 }
142
143 //
144 // check if all necessary containers exist in the Parameter list.
145 // if not create one and add them to the list
146 //
147 fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader");
148 if (!fRawRunHeader)
149 return kFALSE;
150
151 fRawEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
152 if (!fRawEvtHeader)
153 return kFALSE;
154
155 fRawEvtData = (MRawEvtData*)pList->FindCreateObj("MRawEvtData");
156 if (!fRawEvtData)
157 return kFALSE;
158
159 fRawCrateArray = (MRawCrateArray*)pList->FindCreateObj("MRawCrateArray");
160 if (!fRawCrateArray)
161 return kFALSE;
162
163 fRawEvtTime = (MTime*)pList->FindCreateObj("MTime", "MRawEvtTime");
164 if (!fRawEvtTime)
165 return kTRUE;
166
167 //
168 // Read RUN HEADER (see specification) from input stream
169 //
170 fRawRunHeader->ReadEvt(*fIn);
171 fRawRunHeader->Print();
172
173 if (fRawRunHeader->GetMagicNumber()!=kMagicNumber)
174 return kFALSE;
175
176 //
177 // Give the run header information to the 'sub-classes'
178 //
179 fRawEvtHeader->Init(fRawRunHeader, fRawEvtTime);
180 fRawEvtData ->Init(fRawRunHeader);
181
182 return kTRUE;
183}
184
185// --------------------------------------------------------------------------
186//
187// The Process reads one event from the binary file:
188// - The event header is read
189// - the run header is read
190// - all crate information is read
191// - the raw data information of one event is read
192//
193Bool_t MRawFileRead::Process()
194{
195 //
196 // Read in the next EVENT HEADER (see specification),
197 // if there is no next event anymore stop eventloop
198 //
199 if (!fRawEvtHeader->ReadEvt(*fIn))
200 return kFALSE;
201
202 //
203 // Get number of crates from the run header
204 //
205 const UShort_t nc = fRawRunHeader->GetNumCrates();
206
207 //
208 // Delete arrays which stores the pixel information (time slices)
209 //
210 fRawEvtData->ResetPixels();
211
212 //
213 // clear the TClonesArray which stores the Crate Information
214 // and create a new array of the correct size
215 //
216 fRawCrateArray->SetSize(nc);
217
218 //
219 // read the CRATE DATA (see specification) from file
220 //
221 for (int i=0; i<nc; i++)
222 {
223 fRawCrateArray->GetEntry(i)->ReadEvt(*fIn);
224
225 fRawEvtData->ReadEvt(*fIn);
226 }
227
228 return kTRUE;
229}
230
231// --------------------------------------------------------------------------
232//
233// Close the file. Check whether the number of read events differs from
234// the number the file should containe (MRawRunHeader). Prints a warning
235// if it doesn't match.
236//
237Bool_t MRawFileRead::PostProcess()
238{
239 //
240 // Sanity check for the number of events
241 //
242 if (fRawRunHeader->GetNumEvents() == GetNumExecutions()-1)
243 return kTRUE;
244
245 *fLog << warn << "Warning - number of read events (" << GetNumExecutions()-1;
246 *fLog << ") doesn't match number in run header (";
247 *fLog << fRawRunHeader->GetNumEvents() << ")." << endl;
248
249 return kTRUE;
250}
Note: See TracBrowser for help on using the repository browser.