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

Last change on this file since 6000 was 5978, 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 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
296 while (pixel.Next())
297 {
298 Float_t sumhi = 0.;
299 Byte_t sathi = 0;
300
301 FindSignalHiGain(pixel.GetHiGainSamples()+fHiGainFirst, pixel.GetLoGainSamples(), sumhi, sathi);
302
303 Float_t sumlo = 0.;
304 Byte_t satlo = 0;
305
306 if (pixel.HasLoGain())
307 FindSignalLoGain(pixel.GetLoGainSamples()+fLoGainFirst, sumlo, satlo);
308
309 const Int_t pixid = pixel.GetPixelId();
310
311 const MPedestalPix &ped = (*fPedestals)[pixid];
312 MExtractedSignalPix &pix = (*fSignals)[pixid];
313
314 const Float_t pedes = ped.GetPedestal();
315 const Float_t pedrms = ped.GetPedestalRms();
316
317 pix.SetExtractedSignal(sumhi - pedes*fNumHiGainSamples, pedrms*fSqrtHiGainSamples,
318 sumlo - pedes*fNumLoGainSamples, pedrms*fSqrtLoGainSamples);
319
320 pix.SetGainSaturation(sathi, sathi, satlo);
321
322 } /* while (pixel.Next()) */
323
324 fSignals->SetReadyToSave();
325
326 return kTRUE;
327}
328
329// --------------------------------------------------------------------------
330//
331// Implementation of SavePrimitive. Used to write the call to a constructor
332// to a macro. In the original root implementation it is used to write
333// gui elements to a macro-file.
334//
335void MExtractor::StreamPrimitive(ofstream &out) const
336{
337 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
338 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
339
340 if (fSaturationLimit!=fgSaturationLimit)
341 {
342 out << " " << GetUniqueName() << ".SetSaturationLimit(";
343 out << (int)fSaturationLimit << ");" << endl;
344 }
345
346 out << " " << GetUniqueName() << ".SetRange(";
347 out << (int)fHiGainFirst;
348 out << ", " << (int)fHiGainLast;
349 out << ", " << (int)fLoGainFirst;
350 out << ", " << (int)fLoGainLast;
351 out << ");" << endl;
352}
353
354// --------------------------------------------------------------------------
355//
356// Read the setup from a TEnv, eg:
357// MJPedestal.MExtractor.HiGainFirst: 5
358// MJPedestal.MExtractor.LoGainFirst: 5
359// MJPedestal.MExtractor.HiGainLast: 10
360// MJPedestal.MExtractor.LoGainLast: 10
361// MJPedestal.MExtractor.SaturationLimit: 88
362//
363Int_t MExtractor::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
364{
365 Byte_t hf = fHiGainFirst;
366 Byte_t lf = fLoGainFirst;
367 Byte_t hl = fHiGainLast;
368 Byte_t ll = fLoGainLast;
369
370 Bool_t rc = kFALSE;
371
372 if (IsEnvDefined(env, prefix, "HiGainFirst", print))
373 {
374 hf = GetEnvValue(env, prefix, "HiGainFirst", hf);
375 rc = kTRUE;
376 }
377 if (IsEnvDefined(env, prefix, "LoGainFirst", print))
378 {
379 lf = GetEnvValue(env, prefix, "LoGainFirst", lf);
380 rc = kTRUE;
381 }
382
383 if (IsEnvDefined(env, prefix, "HiGainLast", print))
384 {
385 hl = GetEnvValue(env, prefix, "HiGainLast", hl);
386 rc = kTRUE;
387 }
388 if (IsEnvDefined(env, prefix, "LoGainLast", print))
389 {
390 ll = GetEnvValue(env, prefix, "LoGainLast", ll);
391 rc = kTRUE;
392 }
393
394 SetRange(hf, hl, lf, ll);
395
396 if (IsEnvDefined(env, prefix, "OffsetLoGain", print))
397 {
398 SetOffsetLoGain(GetEnvValue(env, prefix, "OffsetLoGain", fOffsetLoGain));
399 rc = kTRUE;
400 }
401
402 if (IsEnvDefined(env, prefix, "SaturationLimit", print))
403 {
404 SetSaturationLimit(GetEnvValue(env, prefix, "SaturationLimit", fSaturationLimit));
405 rc = kTRUE;
406 }
407
408 if (IsEnvDefined(env, prefix, "NoiseCalculation", print))
409 {
410 SetNoiseCalculation(GetEnvValue(env, prefix, "NoiseCalculation", fNoiseCalculation));
411 rc = kTRUE;
412 }
413
414 // Be carefull: Returning kERROR is not forseen in derived classes
415 return rc;
416}
417
418void MExtractor::Print(Option_t *o) const
419{
420 if (IsA()==MExtractor::Class())
421 *fLog << GetDescriptor() << ":" << endl;
422
423 *fLog << " Hi Gain Range: " << (int)fHiGainFirst << " " << (int)fHiGainLast << endl;
424 *fLog << " Lo Gain Range: " << (int)fLoGainFirst << " " << (int)fLoGainLast << endl;
425 *fLog << " Saturation Lim: " << (int)fSaturationLimit << endl;
426 *fLog << " Num Samples HiGain: " << fNumHiGainSamples << " LoGain: " << fNumLoGainSamples << endl;
427}
Note: See TracBrowser for help on using the repository browser.