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

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