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

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