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

Last change on this file since 6742 was 6254, 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 = 250;
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 fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
154 if (!fRawEvt)
155 {
156 *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
157 return kFALSE;
158 }
159
160 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
161 if (!fRunHeader)
162 {
163 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
164 return kFALSE;
165 }
166
167
168 if (fPedestals)
169 return kTRUE;
170
171 fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedestalCam), "MPedestalCam");
172 if (!fPedestals)
173 {
174 *fLog << err << fNamePedestalCam << " not found... aborting" << endl;
175 return kFALSE;
176 }
177
178 return kTRUE;
179}
180
181// --------------------------------------------------------------------------
182//
183// The PreProcess searches for the following input containers:
184// - MRawEvtData
185// - MRawRunHeader
186// - MPedestalCam
187//
188// The following output containers are also searched and created if
189// they were not found:
190//
191// - MExtractedSignalCam
192//
193Int_t MExtractor::PreProcess(MParList *pList)
194{
195 fSignals = (MExtractedSignalCam*)pList->FindCreateObj("MExtractedSignalCam",AddSerialNumber(fNameSignalCam));
196 if (!fSignals)
197 return kFALSE;
198
199 return PreProcessStd(pList);
200}
201
202// --------------------------------------------------------------------------
203//
204// The ReInit searches for:
205// - MRawRunHeader::GetNumSamplesHiGain()
206// - MRawRunHeader::GetNumSamplesLoGain()
207//
208// In case that the variable fLoGainLast is smaller than
209// the even part of the number of samples obtained from the run header, a
210// warning is given an the range is set back accordingly. A call to:
211// - SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff)
212// is performed in that case. The variable diff means here the difference
213// between the requested range (fLoGainLast) and the available one. Note that
214// the functions SetRange() are mostly overloaded and perform more checks,
215// modifying the ranges again, if necessary.
216//
217// In case that the variable fHiGainLast is smaller than the available range
218// obtained from the run header, a warning is given that a part of the low-gain
219// samples are used for the extraction of the high-gain signal.
220//
221Bool_t MExtractor::ReInit(MParList *pList)
222{
223
224 const Int_t logainsamples = fRunHeader->GetNumSamplesLoGain();
225
226 Int_t lastdesired;
227 Int_t lastavailable;
228
229 if (logainsamples)
230 {
231
232 lastdesired = (Int_t)(fLoGainLast);
233 lastavailable = logainsamples-1;
234
235 if (lastavailable < 0)
236 *fLog << warn << GetDescriptor() << " - WARNING: Number of available Low-Gain Slices is smaller than or equal zero!" << endl;
237
238 if (lastdesired > lastavailable)
239 {
240 const Int_t diff = lastdesired - lastavailable;
241
242 *fLog << endl;
243 *fLog << warn << GetDescriptor() << ": Selected Lo Gain FADC Window [";
244 *fLog << Form("%2i,%2i", (int)fLoGainFirst, lastdesired);
245 *fLog << "] ranges out of the available limits: [0," << Form("%2i", lastavailable) << "]" << endl;
246 *fLog << GetDescriptor() << ": Will reduce the upper edge to " << (int)(fLoGainLast - diff) << endl;
247 SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff);
248 }
249 }
250 else
251 SetRange(fHiGainFirst, fHiGainLast, 0,0);
252
253 const Int_t higainsamples = fRunHeader->GetNumSamplesHiGain();
254
255 if (higainsamples <= 0)
256 {
257 *fLog << err << GetDescriptor();
258 *fLog << " - ERROR: Number of available High-Gain Slices is smaller than or equal zero!" << endl;
259 return kFALSE;
260 }
261
262 lastdesired = (Int_t)fHiGainLast;
263 lastavailable = higainsamples-1;
264
265 if (lastdesired > lastavailable)
266 {
267 const Int_t diff = lastdesired - lastavailable;
268
269 *fLog << endl;
270 *fLog << inf << GetDescriptor() << ": Selected Hi Gain FADC Window [";
271 *fLog << Form("%2i,%2i", (int)fHiGainFirst,lastdesired);
272 *fLog << "] ranges out of the available limits: [0," << Form("%2i", lastavailable) << "]" << endl;
273 *fLog << inf << GetDescriptor() << ": Will use ";
274 *fLog << Form("%2i", diff) << " samples from the Low-Gain for the High-Gain extraction";
275 *fLog << endl;
276
277 fHiGainLast -= diff;
278 fHiLoLast = diff;
279 }
280
281 return kTRUE;
282}
283
284// --------------------------------------------------------------------------
285//
286// Calculate the integral of the FADC time slices and store them as a new
287// pixel in the MExtractedSignalCam container.
288//
289Int_t MExtractor::Process()
290{
291
292 MRawEvtPixelIter pixel(fRawEvt);
293
294 while (pixel.Next())
295 {
296 Float_t sumhi = 0.;
297 Byte_t sathi = 0;
298
299 FindSignalHiGain(pixel.GetHiGainSamples()+fHiGainFirst, pixel.GetLoGainSamples(), sumhi, sathi);
300
301 Float_t sumlo = 0.;
302 Byte_t satlo = 0;
303
304 if (pixel.HasLoGain())
305 FindSignalLoGain(pixel.GetLoGainSamples()+fLoGainFirst, sumlo, satlo);
306
307 const Int_t pixid = pixel.GetPixelId();
308
309 const MPedestalPix &ped = (*fPedestals)[pixid];
310 MExtractedSignalPix &pix = (*fSignals)[pixid];
311
312 const Float_t pedes = ped.GetPedestal();
313 const Float_t pedrms = ped.GetPedestalRms();
314
315 pix.SetExtractedSignal(sumhi - pedes*fNumHiGainSamples, pedrms*fSqrtHiGainSamples,
316 sumlo - pedes*fNumLoGainSamples, pedrms*fSqrtLoGainSamples);
317
318 pix.SetGainSaturation(sathi, sathi, satlo);
319
320 } /* while (pixel.Next()) */
321
322 fSignals->SetReadyToSave();
323
324 return kTRUE;
325}
326
327// --------------------------------------------------------------------------
328//
329// Implementation of SavePrimitive. Used to write the call to a constructor
330// to a macro. In the original root implementation it is used to write
331// gui elements to a macro-file.
332//
333void MExtractor::StreamPrimitive(ofstream &out) const
334{
335 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
336 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
337
338 if (fSaturationLimit!=fgSaturationLimit)
339 {
340 out << " " << GetUniqueName() << ".SetSaturationLimit(";
341 out << (int)fSaturationLimit << ");" << endl;
342 }
343
344 out << " " << GetUniqueName() << ".SetRange(";
345 out << (int)fHiGainFirst;
346 out << ", " << (int)fHiGainLast;
347 out << ", " << (int)fLoGainFirst;
348 out << ", " << (int)fLoGainLast;
349 out << ");" << endl;
350}
351
352// --------------------------------------------------------------------------
353//
354// Read the setup from a TEnv, eg:
355// MJPedestal.MExtractor.HiGainFirst: 5
356// MJPedestal.MExtractor.LoGainFirst: 5
357// MJPedestal.MExtractor.HiGainLast: 10
358// MJPedestal.MExtractor.LoGainLast: 10
359// MJPedestal.MExtractor.SaturationLimit: 88
360//
361Int_t MExtractor::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
362{
363 Byte_t hf = fHiGainFirst;
364 Byte_t lf = fLoGainFirst;
365 Byte_t hl = fHiGainLast;
366 Byte_t ll = fLoGainLast;
367
368 Bool_t rc = kFALSE;
369
370 if (IsEnvDefined(env, prefix, "HiGainFirst", print))
371 {
372 hf = GetEnvValue(env, prefix, "HiGainFirst", hf);
373 rc = kTRUE;
374 }
375 if (IsEnvDefined(env, prefix, "LoGainFirst", print))
376 {
377 lf = GetEnvValue(env, prefix, "LoGainFirst", lf);
378 rc = kTRUE;
379 }
380
381 if (IsEnvDefined(env, prefix, "HiGainLast", print))
382 {
383 hl = GetEnvValue(env, prefix, "HiGainLast", hl);
384 rc = kTRUE;
385 }
386 if (IsEnvDefined(env, prefix, "LoGainLast", print))
387 {
388 ll = GetEnvValue(env, prefix, "LoGainLast", ll);
389 rc = kTRUE;
390 }
391
392 SetRange(hf, hl, lf, ll);
393
394 if (IsEnvDefined(env, prefix, "OffsetLoGain", print))
395 {
396 SetOffsetLoGain(GetEnvValue(env, prefix, "OffsetLoGain", fOffsetLoGain));
397 rc = kTRUE;
398 }
399
400 if (IsEnvDefined(env, prefix, "SaturationLimit", print))
401 {
402 SetSaturationLimit(GetEnvValue(env, prefix, "SaturationLimit", fSaturationLimit));
403 rc = kTRUE;
404 }
405
406 if (IsEnvDefined(env, prefix, "NoiseCalculation", print))
407 {
408 SetNoiseCalculation(GetEnvValue(env, prefix, "NoiseCalculation", fNoiseCalculation));
409 rc = kTRUE;
410 }
411
412 // Be carefull: Returning kERROR is not forseen in derived classes
413 return rc;
414}
415
416void MExtractor::Print(Option_t *o) const
417{
418 if (IsA()==MExtractor::Class())
419 *fLog << GetDescriptor() << ":" << endl;
420
421 *fLog << " Hi Gain Range: " << (int)fHiGainFirst << " " << (int)fHiGainLast << endl;
422 *fLog << " Lo Gain Range: " << (int)fLoGainFirst << " " << (int)fLoGainLast << endl;
423 *fLog << " Gain Overlap to Lo: " << (int)fHiLoLast << endl;
424 *fLog << " Saturation Lim: " << (int)fSaturationLimit << endl;
425 *fLog << " Num Samples HiGain: " << fNumHiGainSamples << " LoGain: " << fNumLoGainSamples << endl;
426}
Note: See TracBrowser for help on using the repository browser.