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

Last change on this file since 4200 was 4040, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 7.1 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-2004
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// MRawCrateArray
41// MRawEvtTime
42//
43//////////////////////////////////////////////////////////////////////////////
44#include "MRawRead.h"
45
46#include <fstream>
47
48#include "MLog.h"
49#include "MLogManip.h"
50
51#include "MTime.h"
52#include "MParList.h"
53#include "MRawRunHeader.h"
54#include "MRawEvtHeader.h"
55#include "MRawEvtData.h"
56#include "MRawCrateData.h"
57#include "MRawCrateArray.h"
58
59ClassImp(MRawRead);
60
61using namespace std;
62
63// --------------------------------------------------------------------------
64//
65// Default constructor. It tries to open the given file.
66//
67MRawRead::MRawRead(const char *name, const char *title)
68 : fForceMode(kFALSE)
69{
70 fName = name ? name : "MRawRead";
71 fTitle = title ? title : "Bas class for reading DAQ files";
72}
73
74// --------------------------------------------------------------------------
75//
76// The PreProcess of this task checks for the following containers in the
77// list:
78// MRawRunHeader <output> if not found it is created
79// MRawEvtHeader <output> if not found it is created
80// MRawEvtData <output> if not found it is created
81// MRawCrateArray <output> if not found it is created
82// MRawEvtTime <output> if not found it is created (MTime)
83//
84// If all containers are found or created the run header is read from the
85// binary file and printed. If the Magic-Number (file identification)
86// doesn't match we stop the eventloop.
87//
88// Now the EvtHeader and EvtData containers are initialized.
89//
90Int_t MRawRead::PreProcess(MParList *pList)
91{
92 if (!OpenStream())
93 return kFALSE;
94
95 //
96 // check if all necessary containers exist in the Parameter list.
97 // if not create one and add them to the list
98 //
99 fRawRunHeader = (MRawRunHeader*)pList->FindCreateObj("MRawRunHeader");
100 if (!fRawRunHeader)
101 return kFALSE;
102
103 fRawEvtHeader = (MRawEvtHeader*)pList->FindCreateObj("MRawEvtHeader");
104 if (!fRawEvtHeader)
105 return kFALSE;
106
107 fRawEvtData = (MRawEvtData*)pList->FindCreateObj("MRawEvtData");
108 if (!fRawEvtData)
109 return kFALSE;
110
111 fRawCrateArray = (MRawCrateArray*)pList->FindCreateObj("MRawCrateArray");
112 if (!fRawCrateArray)
113 return kFALSE;
114
115 fRawEvtTime = (MTime*)pList->FindCreateObj("MTime");
116 if (!fRawEvtTime)
117 return kFALSE;
118
119 return kTRUE;
120}
121
122// --------------------------------------------------------------------------
123//
124// This is a workaround for the oldest runs (file version<3)
125// for which no time stamp was available.
126// For this runs a fake time stamp is created
127//
128// Be carefull: This is NOT thread safe!
129//
130void MRawRead::CreateFakeTime() const
131{
132 static Double_t tm = 0; // Range of roughly 8min
133 const UInt_t ct = (*fRawCrateArray)[0]->GetFADCClockTick();
134
135 tm = ct<tm ? fmod(tm, (UInt_t)(-1))+(UInt_t)(-1)+ct : ct;
136
137 const Double_t mhz = 9.375; // [1e6 ticks/s]
138 const Double_t t = tm/mhz; // [us]
139 const UInt_t ns = (UInt_t)fmod(t*1e3, 1e6);
140 //const UShort_t ms = (UShort_t)fmod(t/1e3, 1e3);
141 const Byte_t s = (Byte_t)fmod(t/1e6, 60);
142
143 // Create an artificial time stamp!
144 UInt_t m = (Byte_t)fmod(t/60e6, 60);
145 //const Byte_t h = (Byte_t)(t/3600e6);
146 m += fRawRunHeader->GetRunNumber()*10;
147 m %= 360; // 6h
148
149 fRawEvtTime->UpdMagicTime(m/60, m%60, s, ns);
150}
151
152// --------------------------------------------------------------------------
153//
154// Read a single event from the stream
155//
156Bool_t MRawRead::ReadEvent(istream &fin)
157{
158 //
159 // Get file format version
160 //
161 const UShort_t ver = fRawRunHeader->GetFormatVersion();
162
163 //
164 // Read in the next EVENT HEADER (see specification),
165 // if there is no next event anymore stop eventloop
166 //
167 const Int_t rc = fRawEvtHeader->ReadEvt(fin, ver);
168 if (rc==kCONTINUE && fForceMode==kFALSE)
169 {
170 *fLog << err << "Problem found reading the event header... abort." << endl;
171 return kFALSE;
172 }
173
174 //
175 // Get number of crates from the run header
176 //
177 const UShort_t nc = fRawRunHeader->GetNumCrates();
178
179 //
180 // Delete arrays which stores the pixel information (time slices)
181 //
182 fRawEvtData->ResetPixels();
183
184 //
185 // clear the TClonesArray which stores the Crate Information
186 // and create a new array of the correct size
187 //
188 fRawCrateArray->SetSize(nc);
189
190 //
191 // read the CRATE DATA (see specification) from file
192 //
193 for (int i=0; i<nc; i++)
194 {
195 fRawCrateArray->GetEntry(i)->ReadEvt(fin, ver);
196 if (!fin)
197 return kFALSE;
198
199 fRawEvtData->ReadEvt(fin);
200 if (!fin)
201 return kFALSE;
202 }
203
204 // This is a workaround for the oldest runs (version<3)
205 // for which no time stamp was available.
206 // For this runs a fake time stamp is created
207 if (ver<3)
208 CreateFakeTime();
209
210 if (rc==kCONTINUE && fForceMode==kTRUE)
211 return kCONTINUE;
212
213 return kTRUE;
214}
215
216void MRawRead::SkipEvent(istream &fin)
217{
218 //
219 // Get file format version
220 //
221 const UShort_t ver = fRawRunHeader->GetFormatVersion();
222 fRawEvtHeader->SkipEvt(fin, ver);
223
224 const UShort_t nc = fRawRunHeader->GetNumCrates();
225 for (int i=0; i<nc; i++)
226 {
227 fRawCrateArray->GetEntry(i)->SkipEvt(fin, ver);
228 fRawEvtData->SkipEvt(fin);
229 }
230}
Note: See TracBrowser for help on using the repository browser.