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

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