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

Last change on this file since 8239 was 8154, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 14.3 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// Class Version 6:
67// +Float_t fResolutionPerPheHiGain; // Extractor-dependent charge resolution per phe for high-gain (see TDAS-0502).
68// +Float_t fResolutionPerPheLoGain; // Extractor-dependent charge resolution per phe for low-gain (see TDAS-0502).
69//
70//
71// Input Containers:
72// MRawEvtData
73// MRawRunHeader
74// MPedestalCam
75//
76// Output Containers:
77// MExtractedSignalCam
78//
79//////////////////////////////////////////////////////////////////////////////
80#include "MExtractor.h"
81
82#include <fstream>
83
84#include "MLog.h"
85#include "MLogManip.h"
86
87#include "MParList.h"
88
89#include "MRawEvtData.h"
90#include "MRawEvtPixelIter.h"
91#include "MRawRunHeader.h"
92
93#include "MPedestalCam.h"
94#include "MPedestalPix.h"
95//#include "MPedestalSubtractEvt.h"
96
97#include "MExtractedSignalCam.h"
98#include "MExtractedSignalPix.h"
99
100ClassImp(MExtractor);
101
102using namespace std;
103
104const Byte_t MExtractor::fgSaturationLimit = 245;
105const TString MExtractor::fgNamePedestalCam = "MPedestalCam";
106const TString MExtractor::fgNameSignalCam = "MExtractedSignalCam";
107const Float_t MExtractor::fgOffsetLoGain = 1.51; // 5 ns
108
109// --------------------------------------------------------------------------
110//
111// Default constructor.
112//
113// Set:
114// - all pointers to NULL
115// - all variables to 0
116// - fSaturationLimit to fgSaturationLimit
117// - fNamePedestalCam to fgNamePedestalCam
118// - fNameSignalCam to fgNameSignalCam
119// - fNoiseCalculation to kFALSE
120//
121// Call:
122// - AddToBranchList("MRawEvtData.*")
123//
124MExtractor::MExtractor(const char *name, const char *title)
125 : fResolutionPerPheHiGain(0), fResolutionPerPheLoGain(0),
126 fPedestals(NULL), fSignals(NULL), fRawEvt(NULL), fRunHeader(NULL),
127 fSignal(NULL),
128 /*fHiLoLast(0),*/ fNumHiGainSamples(0), fNumLoGainSamples(0)
129{
130 fName = name ? name : "MExtractor";
131 fTitle = title ? title : "Base class for signal extractors";
132
133 AddToBranchList("MRawEvtData.*");
134
135 SetNamePedestalCam();
136 SetNameSignalCam();
137 SetOffsetLoGain();
138 SetSaturationLimit();
139 SetNoiseCalculation(kFALSE);
140}
141
142void MExtractor::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
143{
144
145 Clear();
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// MPedestalCam
161//
162Int_t MExtractor::PreProcessStd(MParList *pList)
163{
164
165 fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
166 if (!fRawEvt)
167 {
168 *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
169 return kFALSE;
170 }
171
172 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
173 if (!fRunHeader)
174 {
175 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
176 return kFALSE;
177 }
178
179 fSignal = (MPedestalSubtractedEvt*)pList->FindObject(AddSerialNumber("MPedestalSubtractedEvt"));
180 if (!fSignal)
181 {
182 *fLog << err << AddSerialNumber("MPedestalSubtractedEvt") << " not found... aborting." << endl;
183 return kFALSE;
184 }
185
186
187 if (fPedestals)
188 return kTRUE;
189
190 fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedestalCam), "MPedestalCam");
191 if (!fPedestals)
192 {
193 *fLog << err << AddSerialNumber(fNamePedestalCam) << " [MPedestalCam] not found... aborting" << endl;
194 return kFALSE;
195 }
196
197 return kTRUE;
198}
199
200// --------------------------------------------------------------------------
201//
202// The PreProcess searches for the following input containers:
203// - MRawEvtData
204// - MRawRunHeader
205// - MPedestalCam
206//
207// The following output containers are also searched and created if
208// they were not found:
209//
210// - MExtractedSignalCam
211//
212Int_t MExtractor::PreProcess(MParList *pList)
213{
214 fSignals = (MExtractedSignalCam*)pList->FindCreateObj("MExtractedSignalCam",AddSerialNumber(fNameSignalCam));
215 if (!fSignals)
216 return kFALSE;
217
218 return PreProcessStd(pList);
219}
220
221// --------------------------------------------------------------------------
222//
223// The ReInit searches for:
224// - MRawRunHeader::GetNumSamplesHiGain()
225// - MRawRunHeader::GetNumSamplesLoGain()
226//
227// In case that the variable fLoGainLast is smaller than
228// the even part of the number of samples obtained from the run header, a
229// warning is given an the range is set back accordingly. A call to:
230// - SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff)
231// is performed in that case. The variable diff means here the difference
232// between the requested range (fLoGainLast) and the available one. Note that
233// the functions SetRange() are mostly overloaded and perform more checks,
234// modifying the ranges again, if necessary.
235//
236// In case that the variable fHiGainLast is smaller than the available range
237// obtained from the run header, a warning is given that a part of the low-gain
238// samples are used for the extraction of the high-gain signal.
239//
240Bool_t MExtractor::ReInit(MParList *pList)
241{
242 const Int_t numl = fRunHeader->GetNumSamplesLoGain();
243 const Int_t numh = fRunHeader->GetNumSamplesHiGain();
244 const Int_t num = numh+numl;
245
246 if (fHiGainLast>=num)
247 {
248 *fLog << err << "ERROR - Last hi-gain slice must not exceed " << num-1 << endl;
249 return kFALSE;
250 }
251 if (fLoGainLast>=num)
252 {
253 *fLog << err << "ERROR - Last lo-gain slice must not exceed " << num-1 << endl;
254 return kFALSE;
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
327 MRawEvtPixelIter pixel(fRawEvt);
328
329 while (pixel.Next())
330 {
331 Float_t sumhi = 0.;
332 Byte_t sathi = 0;
333
334 FindSignalHiGain(pixel.GetHiGainSamples()+fHiGainFirst, pixel.GetLoGainSamples(), sumhi, sathi);
335
336 Float_t sumlo = 0.;
337 Byte_t satlo = 0;
338
339 if (pixel.HasLoGain())
340 FindSignalLoGain(pixel.GetLoGainSamples()+fLoGainFirst, sumlo, satlo);
341
342 const Int_t pixid = pixel.GetPixelId();
343
344 const MPedestalPix &ped = (*fPedestals)[pixid];
345 MExtractedSignalPix &pix = (*fSignals)[pixid];
346
347 const Float_t pedes = ped.GetPedestal();
348 const Float_t pedrms = ped.GetPedestalRms();
349
350 pix.SetExtractedSignal(sumhi - pedes*fNumHiGainSamples, pedrms*fSqrtHiGainSamples,
351 sumlo - pedes*fNumLoGainSamples, pedrms*fSqrtLoGainSamples);
352
353 pix.SetGainSaturation(sathi, satlo);
354
355 } /* while (pixel.Next()) */
356
357 fSignals->SetReadyToSave();
358
359 return kTRUE;
360}
361
362// --------------------------------------------------------------------------
363//
364// Implementation of SavePrimitive. Used to write the call to a constructor
365// to a macro. In the original root implementation it is used to write
366// gui elements to a macro-file.
367//
368void MExtractor::StreamPrimitive(ostream &out) const
369{
370 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
371 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
372
373 if (fSaturationLimit!=fgSaturationLimit)
374 {
375 out << " " << GetUniqueName() << ".SetSaturationLimit(";
376 out << (int)fSaturationLimit << ");" << endl;
377 }
378
379 out << " " << GetUniqueName() << ".SetRange(";
380 out << (int)fHiGainFirst;
381 out << ", " << (int)fHiGainLast;
382 out << ", " << (int)fLoGainFirst;
383 out << ", " << (int)fLoGainLast;
384 out << ");" << endl;
385}
386
387// --------------------------------------------------------------------------
388//
389// Read the setup from a TEnv, eg:
390// MJPedestal.MExtractor.HiGainFirst: 5
391// MJPedestal.MExtractor.LoGainFirst: 5
392// MJPedestal.MExtractor.HiGainLast: 10
393// MJPedestal.MExtractor.LoGainLast: 10
394// MJPedestal.MExtractor.SaturationLimit: 88
395//
396Int_t MExtractor::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
397{
398 Byte_t hf = fHiGainFirst;
399 Byte_t lf = fLoGainFirst;
400 Byte_t hl = fHiGainLast;
401 Byte_t ll = fLoGainLast;
402
403 Bool_t rc = kFALSE;
404
405 if (IsEnvDefined(env, prefix, "HiGainFirst", print))
406 {
407 hf = GetEnvValue(env, prefix, "HiGainFirst", hf);
408 rc = kTRUE;
409 }
410 if (IsEnvDefined(env, prefix, "LoGainFirst", print))
411 {
412 lf = GetEnvValue(env, prefix, "LoGainFirst", lf);
413 rc = kTRUE;
414 }
415
416 if (IsEnvDefined(env, prefix, "HiGainLast", print))
417 {
418 hl = GetEnvValue(env, prefix, "HiGainLast", hl);
419 rc = kTRUE;
420 }
421 if (IsEnvDefined(env, prefix, "LoGainLast", print))
422 {
423 ll = GetEnvValue(env, prefix, "LoGainLast", ll);
424 rc = kTRUE;
425 }
426
427 SetRange(hf, hl, lf, ll);
428
429 if (IsEnvDefined(env, prefix, "OffsetLoGain", print))
430 {
431 SetOffsetLoGain(GetEnvValue(env, prefix, "OffsetLoGain", fOffsetLoGain));
432 rc = kTRUE;
433 }
434
435 if (IsEnvDefined(env, prefix, "SaturationLimit", print))
436 {
437 SetSaturationLimit(GetEnvValue(env, prefix, "SaturationLimit", fSaturationLimit));
438 rc = kTRUE;
439 }
440
441 if (IsEnvDefined(env, prefix, "NoiseCalculation", print))
442 {
443 SetNoiseCalculation(GetEnvValue(env, prefix, "NoiseCalculation", fNoiseCalculation));
444 rc = kTRUE;
445 }
446
447 // Be carefull: Returning kERROR is not forseen in derived classes
448 return rc;
449}
450
451void MExtractor::Print(Option_t *o) const
452{
453 if (IsA()==MExtractor::Class())
454 *fLog << GetDescriptor() << ":" << endl;
455
456 *fLog << " Hi Gain Range: " << Form("%2d %2d", fHiGainFirst, fHiGainLast) << endl;
457 *fLog << " Lo Gain Range: " << Form("%2d %2d", fLoGainFirst, fLoGainLast) << endl;
458// *fLog << " Gain Overlap to Lo: " << Form("%2d", fHiLoLast) << endl;
459 *fLog << " Saturation Lim: " << Form("%3d", fSaturationLimit) << endl;
460 *fLog << " Num Samples Hi/Lo: " << Form("%2.1f %2.1f", fNumHiGainSamples, fNumLoGainSamples) << endl;
461 if (fPedestals)
462 *fLog << " Pedestals: " << fPedestals->GetName() << ", " << fPedestals << endl;
463}
Note: See TracBrowser for help on using the repository browser.