source: trunk/MagicSoft/Mars/mraw/MRawRead.cc@ 9614

Last change on this file since 9614 was 8961, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 8.4 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 10/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2007
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MRawRead
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
38// MRawEvtHeader
39// MRawEvtData
40// MRawEvtData2 [MRawEvtData]
41// MRawCrateArray
42// MTime
43//
44//////////////////////////////////////////////////////////////////////////////
45#include "MRawRead.h"
46
47#include <fstream>
48
49#include "MLog.h"
50#include "MLogManip.h"
51
52#include "MTime.h"
53#include "MParList.h"
54#include "MRawRunHeader.h"
55#include "MRawEvtHeader.h"
56#include "MRawEvtData.h"
57#include "MRawCrateData.h"
58#include "MRawCrateArray.h"
59
60ClassImp(MRawRead);
61
62using namespace std;
63
64// --------------------------------------------------------------------------
65//
66// Default constructor. It tries to open the given file.
67//
68MRawRead::MRawRead(const char *name, const char *title)
69 : fForceMode(kFALSE)
70{
71 fName = name ? name : "MRawRead";
72 fTitle = title ? title : "Bas class for reading DAQ files";
73}
74
75// --------------------------------------------------------------------------
76//
77// The PreProcess of this task checks for the following containers in the
78// list:
79// MRawRunHeader <output> if not found it is created
80// MRawEvtHeader <output> if not found it is created
81// MRawEvtData <output> if not found it is created
82// MRawCrateArray <output> if not found it is created
83// MRawEvtTime <output> if not found it is created (MTime)
84//
85// If all containers are found or created the run header is read from the
86// binary file and printed. If the Magic-Number (file identification)
87// doesn't match we stop the eventloop.
88//
89// Now the EvtHeader and EvtData containers are initialized.
90//
91Int_t MRawRead::PreProcess(MParList *pList)
92{
93 if (!OpenStream())
94 return kFALSE;
95
96 //
97 // check if all necessary containers exist in the Parameter list.
98 // if not create one and add them to the list
99 //
100 fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader");
101 if (!fRawRunHeader)
102 return kFALSE;
103
104 fRawEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
105 if (!fRawEvtHeader)
106 return kFALSE;
107
108 fRawEvtData1 = (MRawEvtData*)pList->FindCreateObj("MRawEvtData");
109 if (!fRawEvtData1)
110 return kFALSE;
111
112 fRawEvtData2 = (MRawEvtData*)pList->FindCreateObj("MRawEvtData", "MRawEvtData2");
113 if (!fRawEvtData2)
114 return kFALSE;
115
116 fRawCrateArray = (MRawCrateArray*)pList->FindCreateObj("MRawCrateArray");
117 if (!fRawCrateArray)
118 return kFALSE;
119
120 fRawEvtTime = (MTime*)pList->FindCreateObj("MTime");
121 if (!fRawEvtTime)
122 return kFALSE;
123
124 return kTRUE;
125}
126
127// --------------------------------------------------------------------------
128//
129// This is a workaround for the oldest runs (file version<3)
130// for which no time stamp was available.
131// For this runs a fake time stamp is created
132//
133// Be carefull: This is NOT thread safe!
134//
135void MRawRead::CreateFakeTime() const
136{
137 static Double_t tm = 0; // Range of roughly 8min
138 const UInt_t ct = (*fRawCrateArray)[0]->GetFADCClockTick();
139
140 tm = ct<tm ? fmod(tm, (UInt_t)(-1))+(UInt_t)(-1)+ct : ct;
141
142 const Double_t mhz = 9.375; // [1e6 ticks/s]
143 const Double_t t = tm/mhz; // [us]
144 const UInt_t ns = (UInt_t)fmod(t*1e3, 1e6);
145 //const UShort_t ms = (UShort_t)fmod(t/1e3, 1e3);
146 const Byte_t s = (Byte_t)fmod(t/1e6, 60);
147
148 // Create an artificial time stamp!
149 UInt_t m = (Byte_t)fmod(t/60e6, 60);
150 //const Byte_t h = (Byte_t)(t/3600e6);
151 m += fRawRunHeader->GetRunNumber()*10;
152 m %= 360; // 6h
153
154 fRawEvtTime->UpdMagicTime(m/60, m%60, s, ns);
155}
156
157// --------------------------------------------------------------------------
158//
159// Read a single event from the stream
160//
161Bool_t MRawRead::ReadEvent(istream &fin)
162{
163 //
164 // Get file format version
165 //
166 const UShort_t ver = fRawRunHeader->GetFormatVersion();
167
168 //
169 // Read in the next EVENT HEADER (see specification),
170 // if there is no next event anymore stop eventloop
171 //
172 const Int_t rc = fRawEvtHeader->ReadEvt(fin, ver, fRawRunHeader->GetHeaderSizeEvt());
173 if (rc==kCONTINUE && fForceMode==kFALSE)
174 {
175 *fLog << err << "Problem found reading the event header... abort." << endl;
176 return kFALSE;
177 }
178 if (rc==kFALSE)
179 return kFALSE;
180
181 //
182 // Get number of crates from the run header
183 //
184 const UShort_t nc = fRawRunHeader->GetNumCrates();
185
186 //
187 // Delete arrays which stores the pixel information (time slices)
188 //
189 fRawEvtData1->ResetPixels(fRawRunHeader->GetNumNormalPixels(), fRawRunHeader->GetMaxPixId());
190 fRawEvtData2->ResetPixels(fRawRunHeader->GetNumSpecialPixels(), fRawRunHeader->GetMinPixId());
191
192 //
193 // clear the TClonesArray which stores the Crate Information
194 // and create a new array of the correct size
195 //
196 fRawCrateArray->SetSize(nc);
197
198 //
199 // Calculate the number of byte to be skipped in a file if a pixel is not connected
200 //
201 const UShort_t nskip = fRawRunHeader->GetNumSamples()*fRawRunHeader->GetNumBytesPerSample();
202
203 //
204 // read the CRATE DATA (see specification) from file
205 //
206 Int_t posinarray=0;
207 for (int i=0; i<nc; i++)
208 {
209 if (!fRawCrateArray->GetEntry(i)->ReadEvt(fin, ver, fRawRunHeader->GetHeaderSizeCrate()))
210 return kFALSE;
211
212 //fRawEvtData1->ReadEvt(fin, posinarray++);
213
214 const UShort_t npic = fRawRunHeader->GetNumPixInCrate();
215 const Byte_t ab = fRawCrateArray->GetEntry(posinarray)->GetABFlags();
216 for (int j=0; j<npic; j++)
217 {
218 // calc the spiral hardware pixel number
219 const UShort_t ipos = posinarray*npic+j;
220
221 // Get Hardware Id
222 const Short_t hwid = fRawRunHeader->GetPixAssignment(ipos);
223
224 // Check whether the pixel is connected or not
225 if (hwid==0)
226 {
227 fin.seekg(nskip, ios::cur);
228 continue;
229 }
230
231 // -1 converts the hardware pixel Id into the software pixel index
232 if (hwid>0)
233 fRawEvtData1->ReadPixel(fin, hwid-1);
234 else
235 fRawEvtData2->ReadPixel(fin, -(hwid+1));
236
237 if (ver>=7)
238 continue;
239
240 if (hwid>0)
241 fRawEvtData1->SetABFlag( hwid-1, TESTBIT(ab, j));
242 else
243 fRawEvtData1->SetABFlag(-(hwid+1), TESTBIT(ab, j));
244 }
245 if (!fin)
246 return kFALSE;
247
248 posinarray++;
249 }
250
251 // This is a workaround for the oldest runs (version<3)
252 // for which no time stamp was available.
253 // For this runs a fake time stamp is created
254 if (ver<3)
255 CreateFakeTime();
256
257 if (rc==kCONTINUE && fForceMode==kTRUE)
258 return kCONTINUE;
259
260 fRawEvtData1->SetReadyToSave();
261 fRawEvtData2->SetReadyToSave();
262
263 return kTRUE;
264}
265
266void MRawRead::SkipEvent(istream &fin)
267{
268 //
269 // Get file format version
270 //
271 const UShort_t ver = fRawRunHeader->GetFormatVersion();
272 fRawEvtHeader->SkipEvt(fin, ver);
273
274 const UShort_t nc = fRawRunHeader->GetNumCrates();
275 for (int i=0; i<nc; i++)
276 {
277 fRawCrateArray->GetEntry(i)->SkipEvt(fin, ver);
278 fRawEvtData1->SkipEvt(fin);
279 }
280}
Note: See TracBrowser for help on using the repository browser.