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

Last change on this file since 5790 was 5734, checked in by gaug, 20 years ago
*** empty log message ***
File size: 12.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!
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 = 254;
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
142Int_t MExtractor::PreProcessStd(MParList *pList)
143{
144 fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
145 if (!fRawEvt)
146 {
147 *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
148 return kFALSE;
149 }
150
151 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
152 if (!fRunHeader)
153 {
154 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
155 return kFALSE;
156 }
157
158
159 if (fPedestals)
160 return kTRUE;
161
162 fPedestals = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedestalCam), "MPedestalCam");
163 if (!fPedestals)
164 {
165 *fLog << err << fNamePedestalCam << " not found... aborting" << endl;
166 return kFALSE;
167 }
168
169 return kTRUE;
170}
171
172// --------------------------------------------------------------------------
173//
174// The PreProcess searches for the following input containers:
175// - MRawEvtData
176// - MRawRunHeader
177// - MPedestalCam
178//
179// The following output containers are also searched and created if
180// they were not found:
181//
182// - MExtractedSignalCam
183//
184Int_t MExtractor::PreProcess(MParList *pList)
185{
186 fSignals = (MExtractedSignalCam*)pList->FindCreateObj("MExtractedSignalCam",AddSerialNumber(fNameSignalCam));
187 if (!fSignals)
188 return kFALSE;
189
190 return PreProcessStd(pList);
191}
192
193// --------------------------------------------------------------------------
194//
195// The ReInit searches for:
196// - MRawRunHeader::GetNumSamplesHiGain()
197// - MRawRunHeader::GetNumSamplesLoGain()
198//
199// In case that the variable fLoGainLast is smaller than
200// the even part of the number of samples obtained from the run header, a
201// warning is given an the range is set back accordingly. A call to:
202// - SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff)
203// is performed in that case. The variable diff means here the difference
204// between the requested range (fLoGainLast) and the available one. Note that
205// the functions SetRange() are mostly overloaded and perform more checks,
206// modifying the ranges again, if necessary.
207//
208// In case that the variable fHiGainLast is smaller than the available range
209// obtained from the run header, a warning is given that a part of the low-gain
210// samples are used for the extraction of the high-gain signal.
211//
212Bool_t MExtractor::ReInit(MParList *pList)
213{
214
215 const Int_t logainsamples = fRunHeader->GetNumSamplesLoGain();
216
217 Int_t lastdesired;
218 Int_t lastavailable;
219
220 if (logainsamples)
221 {
222
223 lastdesired = (Int_t)(fLoGainLast);
224 lastavailable = logainsamples-1;
225
226 if (lastavailable < 0)
227 *fLog << warn << GetDescriptor() << " - WARNING: Number of available Low-Gain Slices is smaller than or equal zero!" << endl;
228
229 if (lastdesired > lastavailable)
230 {
231 const Int_t diff = lastdesired - lastavailable;
232
233 *fLog << endl;
234 *fLog << warn << GetDescriptor() << ": Selected Lo Gain FADC Window [";
235 *fLog << Form("%2i,%2i", (int)fLoGainFirst, lastdesired);
236 *fLog << "] ranges out of the available limits: [0," << Form("%2i", lastavailable) << "]" << endl;
237 *fLog << GetDescriptor() << ": Will reduce the upper edge to " << (int)(fLoGainLast - diff) << endl;
238 SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff);
239 }
240 }
241 else
242 SetRange(fHiGainFirst, fHiGainLast, 0,0);
243
244 const Int_t higainsamples = fRunHeader->GetNumSamplesHiGain();
245
246 if (higainsamples <= 0)
247 {
248 *fLog << err << GetDescriptor();
249 *fLog << " - ERROR: Number of available High-Gain Slices is smaller than or equal zero!" << endl;
250 return kFALSE;
251 }
252
253 lastdesired = (Int_t)fHiGainLast;
254 lastavailable = higainsamples-1;
255
256 if (lastdesired > lastavailable)
257 {
258 const Int_t diff = lastdesired - lastavailable;
259
260 *fLog << endl;
261 *fLog << warn << GetDescriptor() << ": Selected Hi Gain FADC Window [";
262 *fLog << Form("%2i,%2i", (int)fHiGainFirst,lastdesired);
263 *fLog << "] ranges out of the available limits: [0," << Form("%2i", lastavailable) << "]" << endl;
264 *fLog << warn << GetDescriptor() << ": Will use ";
265 *fLog << Form("%2i", diff) << " samples from the Low-Gain for the High-Gain extraction";
266 *fLog << endl;
267
268 fHiGainLast -= diff;
269 fHiLoLast = diff;
270 }
271
272 return kTRUE;
273}
274
275// --------------------------------------------------------------------------
276//
277// Calculate the integral of the FADC time slices and store them as a new
278// pixel in the MExtractedSignalCam container.
279//
280Int_t MExtractor::Process()
281{
282
283 MRawEvtPixelIter pixel(fRawEvt);
284 fSignals->Clear();
285
286 while (pixel.Next())
287 {
288 Float_t sumhi = 0.;
289 Byte_t sathi = 0;
290
291 FindSignalHiGain(pixel.GetHiGainSamples()+fHiGainFirst, pixel.GetLoGainSamples(), sumhi, sathi);
292
293 Float_t sumlo = 0.;
294 Byte_t satlo = 0;
295
296 if (pixel.HasLoGain())
297 FindSignalLoGain(pixel.GetLoGainSamples()+fLoGainFirst, sumlo, satlo);
298
299 const Int_t pixid = pixel.GetPixelId();
300
301 const MPedestalPix &ped = (*fPedestals)[pixid];
302 MExtractedSignalPix &pix = (*fSignals)[pixid];
303
304 const Float_t pedes = ped.GetPedestal();
305 const Float_t pedrms = ped.GetPedestalRms();
306
307 pix.SetExtractedSignal(sumhi - pedes*fNumHiGainSamples, pedrms*fSqrtHiGainSamples,
308 sumlo - pedes*fNumLoGainSamples, pedrms*fSqrtLoGainSamples);
309
310 pix.SetGainSaturation(sathi, sathi, satlo);
311
312 } /* while (pixel.Next()) */
313
314 fSignals->SetReadyToSave();
315
316 return kTRUE;
317}
318
319// --------------------------------------------------------------------------
320//
321// Implementation of SavePrimitive. Used to write the call to a constructor
322// to a macro. In the original root implementation it is used to write
323// gui elements to a macro-file.
324//
325void MExtractor::StreamPrimitive(ofstream &out) const
326{
327 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
328 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
329
330 if (fSaturationLimit!=fgSaturationLimit)
331 {
332 out << " " << GetUniqueName() << ".SetSaturationLimit(";
333 out << (int)fSaturationLimit << ");" << endl;
334 }
335
336 out << " " << GetUniqueName() << ".SetRange(";
337 out << (int)fHiGainFirst;
338 out << ", " << (int)fHiGainLast;
339 out << ", " << (int)fLoGainFirst;
340 out << ", " << (int)fLoGainLast;
341 out << ");" << endl;
342}
343
344// --------------------------------------------------------------------------
345//
346// Read the setup from a TEnv, eg:
347// MJPedestal.MExtractor.HiGainFirst: 5
348// MJPedestal.MExtractor.LoGainFirst: 5
349// MJPedestal.MExtractor.HiGainLast: 10
350// MJPedestal.MExtractor.LoGainLast: 10
351// MJPedestal.MExtractor.SaturationLimit: 88
352//
353Int_t MExtractor::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
354{
355 Byte_t hf = fHiGainFirst;
356 Byte_t lf = fLoGainFirst;
357 Byte_t hl = fHiGainLast;
358 Byte_t ll = fLoGainLast;
359
360 Bool_t rc = kFALSE;
361
362 if (IsEnvDefined(env, prefix, "HiGainFirst", print))
363 {
364 hf = GetEnvValue(env, prefix, "HiGainFirst", hf);
365 rc = kTRUE;
366 }
367 if (IsEnvDefined(env, prefix, "LoGainFirst", print))
368 {
369 lf = GetEnvValue(env, prefix, "LoGainFirst", lf);
370 rc = kTRUE;
371 }
372
373 if (IsEnvDefined(env, prefix, "HiGainLast", print))
374 {
375 hl = GetEnvValue(env, prefix, "HiGainLast", hl);
376 rc = kTRUE;
377 }
378 if (IsEnvDefined(env, prefix, "LoGainLast", print))
379 {
380 ll = GetEnvValue(env, prefix, "LoGainLast", ll);
381 rc = kTRUE;
382 }
383
384 SetRange(hf, hl, lf, ll);
385
386 if (IsEnvDefined(env, prefix, "OffsetLoGain", print))
387 {
388 SetOffsetLoGain(GetEnvValue(env, prefix, "OffsetLoGain", fOffsetLoGain));
389 rc = kTRUE;
390 }
391
392 if (IsEnvDefined(env, prefix, "SaturationLimit", print))
393 {
394 SetSaturationLimit(GetEnvValue(env, prefix, "SaturationLimit", fSaturationLimit));
395 rc = kTRUE;
396 }
397
398 if (IsEnvDefined(env, prefix, "NoiseCalculation", print))
399 {
400 SetNoiseCalculation(GetEnvValue(env, prefix, "NoiseCalculation", fNoiseCalculation));
401 rc = kTRUE;
402 }
403
404 // Be carefull: Returning kERROR is not forseen in derived classes
405 return rc;
406}
407
408void MExtractor::Print(Option_t *o) const
409{
410 if (IsA()==MExtractor::Class())
411 *fLog << GetDescriptor() << ":" << endl;
412
413 *fLog << " Hi Gain Range: " << (int)fHiGainFirst << " " << (int)fHiGainLast << endl;
414 *fLog << " Lo Gain Range: " << (int)fLoGainFirst << " " << (int)fLoGainLast << endl;
415 *fLog << " Saturation Lim: " << (int)fSaturationLimit << endl;
416 *fLog << " Num Samples HiGain: " << fNumHiGainSamples << " LoGain: " << fNumLoGainSamples << endl;
417}
Note: See TracBrowser for help on using the repository browser.