source: trunk/MagicSoft/Mars/msignal/MExtractor.cc@ 4829

Last change on this file since 4829 was 4723, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 11.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): Markus Gaug, 04/2004 <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MExtractor
28// ==========
29//
30// Base class for the signal extractors, used the functions
31// FindSignalHiGain() and FindSignalLoGain() to extract the signal and
32// substract the pedestal value
33//
34// The following variables have to be set by the derived class and
35// do not have defaults:
36// - fNumHiGainSamples
37// - fNumLoGainSamples
38// - fSqrtHiGainSamples
39// - fSqrtLoGainSamples
40//
41// The signal extractor classes can be setup from an environmental
42// setup file. For more information see ReadEnv and MEvtLoop::ReadEnv
43//
44//
45// IMPORTANT: For all classes you derive from MExtractor make sure that:
46// - Print() is correctly implemented
47// - Clone() works
48// - Class Version number != 0 and the I/O works PERFECTLY
49// - only data members which are necessary for the setup (not the ones
50// created in PreProcess and Process) are written
51// - the version number is maintained!
52//
53//
54// Input Containers:
55// MRawEvtData
56// MRawRunHeader
57// MPedestalCam
58//
59// Output Containers:
60// MExtractedSignalCam
61//
62//////////////////////////////////////////////////////////////////////////////
63#include "MExtractor.h"
64
65#include <fstream>
66
67#include "MLog.h"
68#include "MLogManip.h"
69
70#include "MParList.h"
71
72#include "MRawEvtData.h"
73#include "MRawEvtPixelIter.h"
74#include "MRawRunHeader.h"
75
76#include "MPedestalCam.h"
77#include "MPedestalPix.h"
78
79#include "MExtractedSignalCam.h"
80#include "MExtractedSignalPix.h"
81
82ClassImp(MExtractor);
83
84using namespace std;
85
86const Byte_t MExtractor::fgSaturationLimit = 254;
87const TString MExtractor::fgNamePedestalCam = "MPedestalCam";
88
89// --------------------------------------------------------------------------
90//
91// Default constructor.
92//
93// Set:
94// - all pointers to NULL
95// - all variables to 0
96// - fSaturationLimit to fgSaturationLimit
97// - fNamePedestalCam to fgNamePedestalCam
98//
99// Call:
100// - AddToBranchList("MRawEvtData.*")
101//
102MExtractor::MExtractor(const char *name, const char *title)
103 : fPedestals(NULL), fSignals(NULL), fRawEvt(NULL), fRunHeader(NULL),
104 fHiLoLast(0), fNumHiGainSamples(0.), fNumLoGainSamples(0.)
105{
106 fName = name ? name : "MExtractor";
107 fTitle = title ? title : "Base class for signal extractors";
108
109 AddToBranchList("MRawEvtData.*");
110
111 SetNamePedestalCam();
112 SetRange();
113 SetSaturationLimit();
114}
115
116void MExtractor::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
117{
118 fHiGainFirst = hifirst;
119 fHiGainLast = hilast;
120
121 fLoGainFirst = lofirst;
122 fLoGainLast = lolast;
123}
124
125// --------------------------------------------------------------------------
126//
127// The PreProcess searches for the following input containers:
128// - MRawEvtData
129// - MRawRunHeader
130// - MPedestalCam
131//
132// The following output containers are also searched and created if
133// they were not found:
134//
135// - MExtractedSignalCam
136//
137Int_t MExtractor::PreProcess(MParList *pList)
138{
139 fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
140 if (!fRawEvt)
141 {
142 *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
143 return kFALSE;
144 }
145
146 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
147 if (!fRunHeader)
148 {
149 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
150 return kFALSE;
151 }
152
153
154 fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedestalCam), "MPedestalCam");
155 if (!fPedestals)
156 {
157 *fLog << err << AddSerialNumber("MPedestalCam") << " not found... aborting" << endl;
158 return kFALSE;
159 }
160
161 fSignals = (MExtractedSignalCam*)pList->FindCreateObj(AddSerialNumber("MExtractedSignalCam"));
162 if (!fSignals)
163 return kFALSE;
164
165 return kTRUE;
166}
167
168// --------------------------------------------------------------------------
169//
170// The ReInit searches for:
171// - MRawRunHeader::GetNumSamplesHiGain()
172// - MRawRunHeader::GetNumSamplesLoGain()
173//
174// In case that the variable fLoGainLast is smaller than
175// the even part of the number of samples obtained from the run header, a
176// warning is given an the range is set back accordingly. A call to:
177// - SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff)
178// is performed in that case. The variable diff means here the difference
179// between the requested range (fLoGainLast) and the available one. Note that
180// the functions SetRange() are mostly overloaded and perform more checks,
181// modifying the ranges again, if necessary.
182//
183// In case that the variable fHiGainLast is smaller than the available range
184// obtained from the run header, a warning is given that a part of the low-gain
185// samples are used for the extraction of the high-gain signal.
186//
187// Call:
188// - MExtractedSignalCam::SetUsedFADCSlices(fHiGainFirst, fHiGainLast, fNumHiGainSamples,
189// fLoGainFirst, fLoGainLast, fNumLoGainSamples);
190//
191Bool_t MExtractor::ReInit(MParList *pList)
192{
193 Int_t lastdesired = (Int_t)(fLoGainLast);
194 Int_t lastavailable = (Int_t)fRunHeader->GetNumSamplesLoGain()-1;
195
196 if (lastavailable < 0)
197 *fLog << warn << GetDescriptor() << " - WARNING: Number of available Low-Gain Slices is smaller than or equal zero!" << endl;
198
199 if (lastdesired > lastavailable)
200 {
201 const Int_t diff = lastdesired - lastavailable;
202
203 *fLog << endl;
204 *fLog << warn << GetDescriptor() << ": Selected Lo Gain FADC Window [";
205 *fLog << Form("%2i,%2i", (int)fLoGainFirst, lastdesired);
206 *fLog << "] ranges out of the available limits: [0," << Form("%2i", lastavailable) << "]" << endl;
207 *fLog << GetDescriptor() << ": Will reduce the upper edge to " << (int)(fLoGainLast - diff) << endl;
208 SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff);
209 }
210
211 lastdesired = (Int_t)fHiGainLast;
212 lastavailable = (Int_t)fRunHeader->GetNumSamplesHiGain()-1;
213
214 if (lastavailable < 0)
215 {
216 *fLog << err << GetDescriptor();
217 *fLog << " - ERROR: Number of available High-Gain Slices is smaller than or equal zero!" << endl;
218 return kFALSE;
219 }
220
221 if (lastdesired > lastavailable)
222 {
223 const Int_t diff = lastdesired - lastavailable;
224
225 *fLog << endl;
226 *fLog << warn << GetDescriptor() << ": Selected Hi Gain FADC Window [";
227 *fLog << Form("%2i,%2i", (int)fHiGainFirst,lastdesired);
228 *fLog << "] ranges out of the available limits: [0," << Form("%2i", lastavailable) << "]" << endl;
229 *fLog << warn << GetDescriptor() << ": Will use ";
230 *fLog << Form("%2i", diff) << " samples from the Low-Gain for the High-Gain extraction";
231 *fLog << endl;
232
233 fHiGainLast -= diff;
234 fHiLoLast = diff;
235 }
236
237 return kTRUE;
238}
239
240
241/*
242void MExtractor::FindPedestalLoGain(Byte_t *maxpos, Float_t &sum) const
243{
244
245 Byte_t *p = maxpos-4;
246 Byte_t *end = maxpos-2;
247
248 Int_t summ = 0;
249
250 while (p<end)
251 summ += *p++;
252
253 sum = summ/2.;
254
255 return;
256}
257*/
258// --------------------------------------------------------------------------
259//
260// Calculate the integral of the FADC time slices and store them as a new
261// pixel in the MExtractedSignalCam container.
262//
263Int_t MExtractor::Process()
264{
265 MRawEvtPixelIter pixel(fRawEvt);
266 fSignals->Clear();
267
268 while (pixel.Next())
269 {
270 Float_t sumhi = 0.;
271 Byte_t sathi = 0;
272
273 FindSignalHiGain(pixel.GetHiGainSamples()+fHiGainFirst, pixel.GetLoGainSamples(), sumhi, sathi);
274
275 Float_t sumlo = 0.;
276 Byte_t satlo = 0;
277
278 if (pixel.HasLoGain())
279 FindSignalLoGain(pixel.GetLoGainSamples()+fLoGainFirst, sumlo, satlo);
280
281 const Int_t pixid = pixel.GetPixelId();
282
283 const MPedestalPix &ped = (*fPedestals)[pixid];
284 MExtractedSignalPix &pix = (*fSignals)[pixid];
285
286 const Float_t pedes = ped.GetPedestal();
287 const Float_t pedrms = ped.GetPedestalRms();
288
289 pix.SetExtractedSignal(sumhi - pedes*fNumHiGainSamples, pedrms*fSqrtHiGainSamples,
290 sumlo - pedes*fNumLoGainSamples, pedrms*fSqrtLoGainSamples);
291
292 pix.SetGainSaturation(sathi, sathi, satlo);
293
294 } /* while (pixel.Next()) */
295
296 fSignals->SetReadyToSave();
297
298 return kTRUE;
299}
300
301// --------------------------------------------------------------------------
302//
303// Implementation of SavePrimitive. Used to write the call to a constructor
304// to a macro. In the original root implementation it is used to write
305// gui elements to a macro-file.
306//
307void MExtractor::StreamPrimitive(ofstream &out) const
308{
309 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
310 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
311
312 if (fSaturationLimit!=fgSaturationLimit)
313 {
314 out << " " << GetUniqueName() << ".SetSaturationLimit(";
315 out << (int)fSaturationLimit << ");" << endl;
316 }
317
318 out << " " << GetUniqueName() << ".SetRange(";
319 out << (int)fHiGainFirst;
320 out << ", " << (int)fHiGainLast;
321 out << ", " << (int)fLoGainFirst;
322 out << ", " << (int)fLoGainLast;
323 out << ");" << endl;
324}
325
326// --------------------------------------------------------------------------
327//
328// Read the setup from a TEnv, eg:
329// MJPedestal.MExtractor.HiGainFirst: 5
330// MJPedestal.MExtractor.LoGainFirst: 5
331// MJPedestal.MExtractor.HiGainLast: 10
332// MJPedestal.MExtractor.LoGainLast: 10
333// MJPedestal.MExtractor.SaturationLimit: 88
334//
335Int_t MExtractor::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
336{
337 Byte_t hf = fHiGainFirst;
338 Byte_t lf = fLoGainFirst;
339 Byte_t hl = fHiGainLast;
340 Byte_t ll = fLoGainLast;
341
342 Bool_t rc = kFALSE;
343
344 if (IsEnvDefined(env, prefix, "HiGainFirst", print))
345 {
346 hf = GetEnvValue(env, prefix, "HiGainFirst", hf);
347 rc = kTRUE;
348 }
349 if (IsEnvDefined(env, prefix, "LoGainFirst", print))
350 {
351 lf = GetEnvValue(env, prefix, "LoGainFirst", lf);
352 rc = kTRUE;
353 }
354
355 if (IsEnvDefined(env, prefix, "HiGainLast", print))
356 {
357 hl = GetEnvValue(env, prefix, "HiGainLast", hl);
358 rc = kTRUE;
359 }
360 if (IsEnvDefined(env, prefix, "LoGainLast", print))
361 {
362 ll = GetEnvValue(env, prefix, "LoGainLast", ll);
363 rc = kTRUE;
364 }
365
366 SetRange(hf, hl, lf, ll);
367
368 if (IsEnvDefined(env, prefix, "SaturationLimit", print))
369 {
370 SetSaturationLimit(GetEnvValue(env, prefix, "SaturationLimit", fSaturationLimit));
371 rc = kTRUE;
372 }
373
374 return rc;
375}
376
377void MExtractor::Print(Option_t *o) const
378{
379 *fLog << all;
380
381 if (IsA()==MExtractor::Class())
382 *fLog << GetDescriptor() << ":" << endl;
383
384 *fLog << " Hi Gain Range: " << (int)fHiGainFirst << " " << (int)fHiGainLast << endl;
385 *fLog << " Lo Gain Range: " << (int)fLoGainFirst << " " << (int)fLoGainLast << endl;
386 *fLog << " Saturation Lim: " << (int)fSaturationLimit << endl;
387}
Note: See TracBrowser for help on using the repository browser.