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

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