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

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