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

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