source: tags/Mars-V1.1/msignal/MExtractTimeAndCharge.cc

Last change on this file was 8362, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 15.1 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MExtractTimeAndCharge.cc,v 1.64 2007-03-04 13:25:07 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Markus Gaug, 05/2004 <mailto:markus@ifae.es>
21! Author(s): Thomas Bretz, 05/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
22!
23! Copyright: MAGIC Software Development, 2000-2006
24!
25!
26\* ======================================================================== */
27
28//////////////////////////////////////////////////////////////////////////////
29//
30// MExtractTimeAndCharge
31//
32// Base class for the signal extractors which extract the arrival time
33// and the signal at the same time. Uses the functions
34// FindTimeAndChargeHiGain() and FindTimeAndChargeLoGain() to extract
35// the signal.
36//
37// The following figure gives and example of possible inheritance trees.
38// An extractor class can inherit from each of the following base classes:
39// - MExtractor
40// - MExtractTime
41// - MExtractTimeAndCharge
42//
43//Begin_Html
44/*
45<img src="images/ExtractorClasses.gif">
46*/
47//End_Html
48//
49// The following variables have to be set by the derived class and
50// do not have defaults:
51// - fNumHiGainSamples
52// - fNumLoGainSamples
53// - fSqrtHiGainSamples
54// - fSqrtLoGainSamples
55//
56//
57// Class Version 3:
58// ----------------
59// - Byte_t fMaxBinContent;
60//
61// Class Version 4:
62// ----------------
63// - Byte_t fLoGainSwitch
64// + UInt_t fLoGainSwitch
65//
66//
67// Input Containers:
68// MRawEvtData
69// MRawRunHeader
70// MPedestalCam
71//
72// Output Containers:
73// MArrivalTimeCam
74// MExtractedSignalCam
75//
76// For weired events check: Run 94127 Event 672, 1028
77//
78//////////////////////////////////////////////////////////////////////////////
79#include "MExtractTimeAndCharge.h"
80
81#include <TRandom.h>
82#include <TVector3.h>
83
84#include "MMath.h"
85
86#include "MLog.h"
87#include "MLogManip.h"
88
89#include "MParList.h"
90
91#include "MRawRunHeader.h"
92#include "MRawEvtPixelIter.h"
93
94#include "MArrivalTimeCam.h"
95#include "MArrivalTimePix.h"
96
97#include "MExtractedSignalCam.h"
98#include "MExtractedSignalPix.h"
99
100#include "MPedestalSubtractedEvt.h"
101
102ClassImp(MExtractTimeAndCharge);
103
104using namespace std;
105
106const Float_t MExtractTimeAndCharge::fgLoGainStartShift = -2.5;
107const UInt_t MExtractTimeAndCharge::fgLoGainSwitch = 120;
108
109// --------------------------------------------------------------------------
110//
111// Default constructor.
112//
113// Sets:
114// - fWindowSizeHiGain and fWindowSizeLoGain to 0
115// - fLoGainStartShift to fgLoGainStartShift
116// - fLoGainSwitch to fgLoGainSwitch
117//
118MExtractTimeAndCharge::MExtractTimeAndCharge(const char *name, const char *title)
119 : fWindowSizeHiGain(0), fWindowSizeLoGain(0)
120{
121 fName = name ? name : "MExtractTimeAndCharge";
122 fTitle = title ? title : "Base class for signal and time extractors";
123
124 SetLoGainStartShift();
125 SetLoGainSwitch();
126}
127
128// --------------------------------------------------------------------------
129//
130// The PreProcess searches for the following input containers:
131// - MRawEvtData
132// - MRawRunHeader
133// - MPedestalCam
134//
135// The following output containers are also searched and created if
136// they were not found:
137//
138// - MExtractedSignalCam
139// - MArrivalTimeCam
140//
141Int_t MExtractTimeAndCharge::PreProcess(MParList *pList)
142{
143 if (!MExtractTime::PreProcess(pList))
144 return kFALSE;
145
146 fSignals = (MExtractedSignalCam*)pList->FindCreateObj("MExtractedSignalCam",AddSerialNumber(fNameSignalCam));
147 if (!fSignals)
148 return kFALSE;
149
150 *fLog << flush << inf;
151 return kTRUE;
152}
153
154// --------------------------------------------------------------------------
155//
156// The ReInit calls:
157// - MExtractor::ReInit()
158//
159// Call:
160// - MArrivalTimeCam::SetUsedFADCSlices(fHiGainFirst, fHiGainLast, fNumHiGainSamples,
161// fLoGainFirst, fLoGainLast, fNumLoGainSamples);
162// - InitArrays();
163//
164Bool_t MExtractTimeAndCharge::ReInit(MParList *pList)
165{
166 if (!MExtractTime::ReInit(pList))
167 return kFALSE;
168
169 if (!InitArrays(fRunHeader->GetNumSamplesHiGain()+fRunHeader->GetNumSamplesLoGain()))
170 return kFALSE;
171
172 if (fSignals)
173 fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast, fNumHiGainSamples,
174 fLoGainFirst, fLoGainLast, fNumLoGainSamples);
175
176 if (!HasLoGain())
177 {
178 *fLog << inf << "No lo-gains... resetting lo-gain switch." << endl;
179 fLoGainSwitch=0xff;
180 }
181
182 return kTRUE;
183}
184
185// --------------------------------------------------------------------------
186//
187// Return the x-value lower than sat0 at which the signal has been
188// fallen bwlow maxcont/2. This time is determined using a simple second
189// order polynomial interpolation.
190//
191Double_t MExtractTimeAndCharge::GetSaturationTime(Int_t sat0, const Float_t *sig, Int_t maxcont) const
192{
193 const Int_t p = sat0>1 ? sat0-2 : sat0-1;
194 if (sat0<=0)
195 return 0;
196 if (sat0==1)
197 return sig[0]>maxcont/2 ? 0 : 0.5;
198
199 if (sig[p]>sig[p+1] || sig[p+1]>sig[p+2])
200 return sig[p+1]>maxcont/2 ? sat0-1 : sat0-0.5;
201
202 // Find the place at which the signal is maxcont/2
203 const TVector3 vx(sig[p], sig[p+1], sig[p+2]);
204 const TVector3 vy(p, p+1, p+2);
205
206 return MMath::InterpolParabLin(vx, vy, maxcont/2);
207}
208
209// --------------------------------------------------------------------------
210//
211// Calculate the integral of the FADC time slices and store them as a new
212// pixel in the MArrivalTimeCam container.
213// Calculate the integral of the FADC time slices and store them as a new
214// pixel in the MExtractedSignalCam container.
215// The functions FindTimeAndChargeHiGain() and FindTimeAndChargeLoGain() are
216// supposed to extract the signal themselves.
217//
218Int_t MExtractTimeAndCharge::Process()
219{
220 const Int_t numh = fRunHeader->GetNumSamplesHiGain();
221 const Int_t numl = fRunHeader->GetNumSamplesLoGain();
222
223 MRawEvtPixelIter pixel(fRawEvt);
224 while (pixel.Next())
225 {
226 const Int_t pixidx = pixel.GetPixelId();
227
228 const Float_t *sig = fSignal->GetSamples(pixidx);
229
230 Int_t sathi0 = fHiGainFirst; // First slice to extract and first saturating slice
231 Int_t sathi1 = fHiGainLast; // Last slice to extract and last saturating slice
232
233 UInt_t maxcont;
234 Int_t maxposhi = fSignal->GetMax(pixidx, sathi0, sathi1, maxcont);
235 // Would it be better to take lastsat-firstsat?
236 Int_t numsathi = fSignal->GetSaturation(pixidx, fSaturationLimit, sathi0, sathi1);
237
238 Float_t sumhi =0., deltasumhi =-1; // Set hi-gain of MExtractedSignalPix valid
239 Float_t timehi=0., deltatimehi=-1; // Set hi-gain of MArrivalTimePix valid
240
241 // Do not even try to extract the hi-gain if we have
242 // more than one saturating slice
243 const Int_t rangehi = fHiGainLast - fHiGainFirst + 1;
244
245 if (numsathi<2)
246 FindTimeAndChargeHiGain2(sig+fHiGainFirst, rangehi,
247 sumhi, deltasumhi, timehi, deltatimehi,
248 numsathi, maxposhi);
249
250 // If we have saturating slices try to get a better estimate
251 // of the arrival time than timehi or sathi0. This is
252 // usefull to know where to start lo-gain extraction.
253 if (numsathi>1)
254 {
255 timehi = GetSaturationTime(sathi0, sig, maxcont)-fHiGainFirst;
256 deltatimehi = 0;
257 }
258
259 // Make sure that in cases the time couldn't be correctly determined
260 // more meaningfull default values are assigned.
261 // For extractors like the digital filter and the spline
262 // we allow extracpolation by one slice.
263 // FIXME: Defined Out-Of-Range better so that the extractors
264 // know what to return!
265 if (deltatimehi>-0.5 && (timehi<-1 || timehi>=rangehi))
266 {
267 // Flag this as unreliable!
268 timehi = gRandom->Uniform(rangehi+1)-1;
269 // deltatimehi=-1; // Set PIXEL to UNRELIABLE?
270 }
271
272 timehi += fHiGainFirst;
273
274 Float_t sumlo =0, deltasumlo =-1; // invalidate logain of MExtractedSignalPix
275 Float_t timelo=0, deltatimelo=-1; // invalidate logain of MArrivalTimePix
276 Int_t numsatlo=0;
277
278 //
279 // Adapt the low-gain extraction range from the obtained high-gain time
280 //
281
282 // IN THIS CASE THE PIXEL SHOULD BE MARKED BAD!!!!
283 // MEANS: Hi gain has saturated, but the signal is to dim
284 // to extract the lo-gain properly
285 // This could happen because the maxcont was not extracted from
286 // all slices!
287 // THIS produces pulse positions ~= -1
288 // The signal might be handled in MCalibrateData, but hwat's about
289 // the arrival times in MCalibrateRelTime
290 if (numsathi>0 && maxcont<=fLoGainSwitch)
291 deltasumlo=deltasumhi=deltatimelo=deltatimehi=-1;
292
293 // If more than 8 hi-gain slices have saturated this is
294 // no physical event. We just assume that something with
295 // the extraction is wrong
296 if (numsathi>8) // FIXME: Should be something like 2?
297 deltasumhi=deltatimehi=-1;
298
299 // FIXME: What to do with the pixel if it saturates too early???
300 if (numl>0 && maxcont>fLoGainSwitch)
301 {
302 Int_t first = numh+fLoGainFirst;
303 Int_t last = numh+fLoGainLast;
304
305 // To determin the window in which the lo-gain is extracted
306 // clearly more information about the relation between the
307 // extraction window and the reslting time is necessary.
308 //
309 // numh + fLoGainStartShift == 14 / fLoGainStartShift=-1
310 //
311 // The lo-gain is expected to have its raising edge
312 // at timehi+numh+fOffsetLoGain. We start to search for the
313 // lo-gain fLoGainStartShift slices earlier.
314 //
315 // Instead of fLoGainStartShift the extractor should now how many
316 // slices before the expected raising edge the start of the
317 // search must be placed and from there we can step 1.5 slices
318 // back to be on the safe side.
319 //
320 // The jitter in the hi-/lo-gain offset ssems to be around +/-0.5
321 const Float_t tm = deltatimehi<0 ? -1.+fHiGainFirst : timehi;
322 first = TMath::FloorNint(tm+numh+fOffsetLoGain+fLoGainStartShift);
323
324 if (first<0)
325 first = 0;
326 if (first>last)
327 first=last;
328
329 /*
330 // Currently we have to stick to this check because at least
331 // the spline has arrays of this size...
332 if (first>last)
333 first = last;
334 if (first<numh+fLoGainFirst)
335 first = numh+fLoGainFirst;
336 */
337 Int_t satlo0 = first; // First slice to extract and first saturating slice
338 Int_t satlo1 = last; // Last slice to extract and last saturating slice
339
340 // Would it be better to take lastsat-firstsat?
341 UInt_t maxlo;
342 Int_t maxposlo = fSignal->GetMax(pixidx, satlo0, satlo1, maxlo);
343 numsatlo = fSignal->GetSaturation(pixidx, fSaturationLimit, satlo0, satlo1);
344
345 //if (satlo0>first && satlo1<last && numsatlo>2)
346 //{
347 // fSignal->InterpolateSaturation(pixidx, fSaturationLimit, satlo0, satlo1);
348 // numsatlo = 0;
349 //}
350
351 const Int_t rangelo = last-first+1;
352 FindTimeAndChargeLoGain2(sig+first, rangelo,
353 sumlo, deltasumlo, timelo, deltatimelo,
354 numsatlo, maxposlo);
355
356 // If we have saturating slices try to get a better estimate
357 // of the arrival time than timehi or sathi0. This is
358 // usefull to know where to start lo-gain extraction.
359 if (numsatlo>1)
360 {
361 timelo = GetSaturationTime(satlo0, sig, maxlo)-numh-first;
362 deltatimelo = 0;
363 }
364
365 // Make sure that in cases the time couldn't be correctly determined
366 // more meaningfull default values are assigned
367 // For extractors like the digital filter and the spline
368 // we allow extracpolation by one slice.
369 if (deltatimelo>-0.5 && (timelo<-1 || timelo>=rangelo))
370 {
371 // Flag this as unreliable!
372 timelo = gRandom->Uniform(rangelo+1)-1;
373 //deltatimelo=-1; // Set PIXEL to UNRELIABLE?
374 }
375
376 timelo += first-numh;
377
378 // If more than 6 lo-gain slices have saturated this is
379 // no physical event. We just assume that something with
380 // the extraction is wrong
381 if (numsatlo>6)
382 deltasumlo=deltatimelo=-1;
383
384 // The extracted lo-gain signal cannot be zero or
385 // negative at all, so it must be wrong
386 if (sumlo<=0)
387 deltasumlo=-1;
388
389 //if (TMath::Abs(timelo-fOffsetLoGain - timehi)>1.0)
390 // deltatimelo = -1;
391 }
392
393 // Now store the result in the corresponding containers
394 MExtractedSignalPix &pix = (*fSignals)[pixidx];
395 MArrivalTimePix &tix = (*fArrTime)[pixidx];
396 pix.SetExtractedSignal(sumhi, deltasumhi, sumlo, deltasumlo);
397 pix.SetGainSaturation(numsathi, numsatlo);
398
399 tix.SetArrivalTime(timehi, deltatimehi, timelo-fOffsetLoGain, deltatimelo);
400 tix.SetGainSaturation(numsathi, numsatlo);
401 } /* while (pixel.Next()) */
402
403 fArrTime->SetReadyToSave();
404 fSignals->SetReadyToSave();
405
406 return kTRUE;
407}
408
409// --------------------------------------------------------------------------
410//
411// In addition to the resources of the base-class MExtractor:
412// MJPedestal.MExtractor.LoGainStartShift: -2.8
413//
414Int_t MExtractTimeAndCharge::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
415{
416 Bool_t rc = MExtractTime::ReadEnv(env, prefix, print);
417
418 if (IsEnvDefined(env, prefix, "LoGainStartShift", print))
419 {
420 fLoGainStartShift = GetEnvValue(env, prefix, "LoGainStartShift", fgLoGainStartShift);
421 SetLoGainStartShift(fLoGainStartShift);
422 rc = kTRUE;
423 }
424
425 if (IsEnvDefined(env, prefix, "LoGainSwitch", print))
426 {
427 fLoGainSwitch = GetEnvValue(env, prefix, "LoGainSwitch", (Int_t)fLoGainSwitch);
428 rc = kTRUE;
429 }
430
431 return rc;
432}
433
434void MExtractTimeAndCharge::Print(Option_t *o) const
435{
436 MExtractTime::Print(o);
437
438 if (HasLoGain())
439 {
440 *fLog << dec;
441 *fLog << " LoGainStartShift: " << fLoGainStartShift << endl;
442 *fLog << " LoGainSwitch: " << fLoGainSwitch << endl;
443 }
444}
Note: See TracBrowser for help on using the repository browser.