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

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