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

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