1 | /* ======================================================================== *\
|
---|
2 | ! $Name: not supported by cvs2svn $:$Id: MExtractTimeAndCharge.cc,v 1.68 2007-08-10 11:21:21 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 |
|
---|
102 | ClassImp(MExtractTimeAndCharge);
|
---|
103 |
|
---|
104 | using namespace std;
|
---|
105 |
|
---|
106 | const Float_t MExtractTimeAndCharge::fgLoGainStartShift = -2.5;
|
---|
107 | const 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 | //
|
---|
118 | MExtractTimeAndCharge::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 | //
|
---|
141 | Int_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 | //
|
---|
164 | Bool_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 | //
|
---|
191 | Double_t MExtractTimeAndCharge::GetSaturationTime(Int_t sat0, const Float_t *sig, Int_t maxconthalf) const
|
---|
192 | {
|
---|
193 | const Int_t p = sat0>1 ? sat0-2 : sat0-1;
|
---|
194 | if (sat0<=0)
|
---|
195 | return 0;
|
---|
196 |
|
---|
197 | if (sat0==1)
|
---|
198 | return sig[0]>maxconthalf ? 0 : 0.5;
|
---|
199 |
|
---|
200 | if (sig[p]>sig[p+1] || sig[p+1]>sig[p+2])
|
---|
201 | return sig[p+1]>maxconthalf ? sat0-1 : sat0-0.5;
|
---|
202 |
|
---|
203 | // Find the place at which the signal is maxcont/2
|
---|
204 | const TVector3 vx(sig[p], sig[p+1], sig[p+2]);
|
---|
205 | const TVector3 vy(p, p+1, p+2);
|
---|
206 |
|
---|
207 | return MMath::InterpolParabLin(vx, vy, maxconthalf);
|
---|
208 | }
|
---|
209 |
|
---|
210 | // --------------------------------------------------------------------------
|
---|
211 | //
|
---|
212 | // Calculate the integral of the FADC time slices and store them as a new
|
---|
213 | // pixel in the MArrivalTimeCam container.
|
---|
214 | // Calculate the integral of the FADC time slices and store them as a new
|
---|
215 | // pixel in the MExtractedSignalCam container.
|
---|
216 | // The functions FindTimeAndChargeHiGain() and FindTimeAndChargeLoGain() are
|
---|
217 | // supposed to extract the signal themselves.
|
---|
218 | //
|
---|
219 | Int_t MExtractTimeAndCharge::Process()
|
---|
220 | {
|
---|
221 | const Int_t numh = fRunHeader->GetNumSamplesHiGain();
|
---|
222 | const Int_t numl = fRunHeader->GetNumSamplesLoGain();
|
---|
223 |
|
---|
224 | const UInt_t satlim = fSaturationLimit*fRunHeader->GetScale();
|
---|
225 |
|
---|
226 | MRawEvtPixelIter pixel(fRawEvt);
|
---|
227 | while (pixel.Next())
|
---|
228 | {
|
---|
229 | const Int_t pixidx = pixel.GetPixelId();
|
---|
230 |
|
---|
231 | const Float_t *sig = fSignal->GetSamples(pixidx);
|
---|
232 |
|
---|
233 | // Would it be better to take lastsat-firstsat?
|
---|
234 | Int_t sathi0 = fHiGainFirst; // First slice to extract and first saturating slice
|
---|
235 | Int_t sathi1 = fHiGainLast; // Last slice to extract and last saturating slice
|
---|
236 | Int_t numsathi = fSignal->GetSaturation(pixidx, satlim, 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 | {
|
---|
247 | const Int_t maxposhi = fSignal->GetMaxPos(pixidx, fHiGainFirst, fHiGainLast);
|
---|
248 | FindTimeAndChargeHiGain2(sig+fHiGainFirst, rangehi,
|
---|
249 | sumhi, deltasumhi, timehi, deltatimehi,
|
---|
250 | numsathi, maxposhi);
|
---|
251 | }
|
---|
252 |
|
---|
253 | // If we have saturating slices try to get a better estimate
|
---|
254 | // of the arrival time than timehi or sathi0. This is
|
---|
255 | // usefull to know where to start lo-gain extraction.
|
---|
256 | const UInt_t maxcont = fSignal->GetRawMaxVal(pixidx, fHiGainFirst, fHiGainLast);
|
---|
257 | if (numsathi>1)
|
---|
258 | {
|
---|
259 | timehi = GetSaturationTime(sathi0, sig, maxcont/2)-fHiGainFirst;
|
---|
260 | deltatimehi = 0;
|
---|
261 | }
|
---|
262 |
|
---|
263 | // Make sure that in cases the time couldn't be correctly determined
|
---|
264 | // more meaningfull default values are assigned.
|
---|
265 | // For extractors like the digital filter and the spline
|
---|
266 | // we allow extracpolation by one slice.
|
---|
267 | // FIXME: Defined Out-Of-Range better so that the extractors
|
---|
268 | // know what to return!
|
---|
269 | if (deltatimehi>-0.5 && (timehi<-1 || timehi>=rangehi))
|
---|
270 | {
|
---|
271 | // Flag this as unreliable!
|
---|
272 | timehi = gRandom->Uniform(rangehi+1)-1;
|
---|
273 | // deltatimehi=-1; // Set PIXEL to UNRELIABLE?
|
---|
274 | }
|
---|
275 |
|
---|
276 | timehi += fHiGainFirst;
|
---|
277 |
|
---|
278 | Float_t sumlo =0, deltasumlo =-1; // invalidate logain of MExtractedSignalPix
|
---|
279 | Float_t timelo=0, deltatimelo=-1; // invalidate logain of MArrivalTimePix
|
---|
280 | Int_t numsatlo=0;
|
---|
281 |
|
---|
282 | //
|
---|
283 | // Adapt the low-gain extraction range from the obtained high-gain time
|
---|
284 | //
|
---|
285 |
|
---|
286 | // IN THIS CASE THE PIXEL SHOULD BE MARKED BAD!!!!
|
---|
287 | // MEANS: Hi gain has saturated, but the signal is to dim
|
---|
288 | // to extract the lo-gain properly
|
---|
289 | // This could happen because the maxcont was not extracted from
|
---|
290 | // all slices!
|
---|
291 | // THIS produces pulse positions ~= -1
|
---|
292 | // The signal might be handled in MCalibrateData, but hwat's about
|
---|
293 | // the arrival times in MCalibrateRelTime
|
---|
294 | if (numsathi>0 && maxcont<=fLoGainSwitch)
|
---|
295 | deltasumlo=deltasumhi=deltatimelo=deltatimehi=-1;
|
---|
296 |
|
---|
297 | // If more than 8 hi-gain slices have saturated this is
|
---|
298 | // no physical event. We just assume that something with
|
---|
299 | // the extraction is wrong
|
---|
300 | if (numsathi>8) // FIXME: Should be something like 2?
|
---|
301 | deltasumhi=deltatimehi=-1;
|
---|
302 |
|
---|
303 | // FIXME: What to do with the pixel if it saturates too early???
|
---|
304 | if (numl>0 && maxcont>fLoGainSwitch)
|
---|
305 | {
|
---|
306 | Int_t first = numh+fLoGainFirst;
|
---|
307 | Int_t last = numh+fLoGainLast;
|
---|
308 |
|
---|
309 | // To determin the window in which the lo-gain is extracted
|
---|
310 | // clearly more information about the relation between the
|
---|
311 | // extraction window and the reslting time is necessary.
|
---|
312 | //
|
---|
313 | // numh + fLoGainStartShift == 14 / fLoGainStartShift=-1
|
---|
314 | //
|
---|
315 | // The lo-gain is expected to have its raising edge
|
---|
316 | // at timehi+numh+fOffsetLoGain. We start to search for the
|
---|
317 | // lo-gain fLoGainStartShift slices earlier.
|
---|
318 | //
|
---|
319 | // Instead of fLoGainStartShift the extractor should now how many
|
---|
320 | // slices before the expected raising edge the start of the
|
---|
321 | // search must be placed and from there we can step 1.5 slices
|
---|
322 | // back to be on the safe side.
|
---|
323 | //
|
---|
324 | // The jitter in the hi-/lo-gain offset ssems to be around +/-0.5
|
---|
325 | const Float_t tm = deltatimehi<0 ? -1.+fHiGainFirst : timehi;
|
---|
326 | first = TMath::FloorNint(tm+numh+fOffsetLoGain+fLoGainStartShift);
|
---|
327 |
|
---|
328 | if (first<0)
|
---|
329 | first = 0;
|
---|
330 | if (first>last)
|
---|
331 | first=last;
|
---|
332 |
|
---|
333 | /*
|
---|
334 | // Currently we have to stick to this check because at least
|
---|
335 | // the spline has arrays of this size...
|
---|
336 | if (first>last)
|
---|
337 | first = last;
|
---|
338 | if (first<numh+fLoGainFirst)
|
---|
339 | first = numh+fLoGainFirst;
|
---|
340 | */
|
---|
341 | // Would it be better to take lastsat-firstsat?
|
---|
342 | Int_t satlo0 = first; // First slice to extract and first saturating slice
|
---|
343 | Int_t satlo1 = last; // Last slice to extract and last saturating slice
|
---|
344 | numsatlo = fSignal->GetSaturation(pixidx, satlim, satlo0, satlo1);
|
---|
345 |
|
---|
346 | //if (satlo0>first && satlo1<last && numsatlo>2)
|
---|
347 | //{
|
---|
348 | // fSignal->InterpolateSaturation(pixidx, fSaturationLimit, satlo0, satlo1);
|
---|
349 | // numsatlo = 0;
|
---|
350 | //}
|
---|
351 |
|
---|
352 | const Int_t rangelo = last-first+1;
|
---|
353 | const Int_t maxposlo = fSignal->GetMaxPos(pixidx, first, last);
|
---|
354 | FindTimeAndChargeLoGain2(sig+first, rangelo,
|
---|
355 | sumlo, deltasumlo, timelo, deltatimelo,
|
---|
356 | numsatlo, maxposlo);
|
---|
357 |
|
---|
358 | // If we have saturating slices try to get a better estimate
|
---|
359 | // of the arrival time than timehi or sathi0. This is
|
---|
360 | // usefull to know where to start lo-gain extraction.
|
---|
361 | if (numsatlo>1)
|
---|
362 | {
|
---|
363 | const UInt_t maxcontlo = fSignal->GetRawMaxVal(pixidx, fHiGainFirst, fHiGainLast);
|
---|
364 | timelo = GetSaturationTime(satlo0, sig, maxcontlo/2)-numh-first;
|
---|
365 | deltatimelo = 0;
|
---|
366 | }
|
---|
367 |
|
---|
368 | // Make sure that in cases the time couldn't be correctly determined
|
---|
369 | // more meaningfull default values are assigned
|
---|
370 | // For extractors like the digital filter and the spline
|
---|
371 | // we allow extracpolation by one slice.
|
---|
372 | if (deltatimelo>-0.5 && (timelo<-1 || timelo>=rangelo))
|
---|
373 | {
|
---|
374 | // Flag this as unreliable!
|
---|
375 | timelo = gRandom->Uniform(rangelo+1)-1;
|
---|
376 | //deltatimelo=-1; // Set PIXEL to UNRELIABLE?
|
---|
377 | }
|
---|
378 |
|
---|
379 | timelo += first-numh;
|
---|
380 |
|
---|
381 | // If more than 6 lo-gain slices have saturated this is
|
---|
382 | // no physical event. We just assume that something with
|
---|
383 | // the extraction is wrong
|
---|
384 | if (numsatlo>6)
|
---|
385 | deltasumlo=deltatimelo=-1;
|
---|
386 |
|
---|
387 | // The extracted lo-gain signal cannot be zero or
|
---|
388 | // negative at all, so it must be wrong
|
---|
389 | if (sumlo<=0)
|
---|
390 | deltasumlo=-1;
|
---|
391 |
|
---|
392 | //if (TMath::Abs(timelo-fOffsetLoGain - timehi)>1.0)
|
---|
393 | // deltatimelo = -1;
|
---|
394 | }
|
---|
395 |
|
---|
396 | // Now store the result in the corresponding containers
|
---|
397 | MExtractedSignalPix &pix = (*fSignals)[pixidx];
|
---|
398 | MArrivalTimePix &tix = (*fArrTime)[pixidx];
|
---|
399 | pix.SetExtractedSignal(sumhi, deltasumhi, sumlo, deltasumlo);
|
---|
400 | pix.SetGainSaturation(numsathi, numsatlo);
|
---|
401 |
|
---|
402 | tix.SetArrivalTime(timehi, deltatimehi, timelo-fOffsetLoGain, deltatimelo);
|
---|
403 | tix.SetGainSaturation(numsathi, numsatlo);
|
---|
404 | } /* while (pixel.Next()) */
|
---|
405 |
|
---|
406 | fArrTime->SetReadyToSave();
|
---|
407 | fSignals->SetReadyToSave();
|
---|
408 |
|
---|
409 | return kTRUE;
|
---|
410 | }
|
---|
411 |
|
---|
412 | // --------------------------------------------------------------------------
|
---|
413 | //
|
---|
414 | // In addition to the resources of the base-class MExtractor:
|
---|
415 | // MJPedestal.MExtractor.LoGainStartShift: -2.8
|
---|
416 | //
|
---|
417 | Int_t MExtractTimeAndCharge::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
|
---|
418 | {
|
---|
419 | Bool_t rc = MExtractTime::ReadEnv(env, prefix, print);
|
---|
420 |
|
---|
421 | if (IsEnvDefined(env, prefix, "LoGainStartShift", print))
|
---|
422 | {
|
---|
423 | fLoGainStartShift = GetEnvValue(env, prefix, "LoGainStartShift", fgLoGainStartShift);
|
---|
424 | SetLoGainStartShift(fLoGainStartShift);
|
---|
425 | rc = kTRUE;
|
---|
426 | }
|
---|
427 |
|
---|
428 | if (IsEnvDefined(env, prefix, "LoGainSwitch", print))
|
---|
429 | {
|
---|
430 | fLoGainSwitch = GetEnvValue(env, prefix, "LoGainSwitch", (Int_t)fLoGainSwitch);
|
---|
431 | rc = kTRUE;
|
---|
432 | }
|
---|
433 |
|
---|
434 | return rc;
|
---|
435 | }
|
---|
436 |
|
---|
437 | void MExtractTimeAndCharge::Print(Option_t *o) const
|
---|
438 | {
|
---|
439 | MExtractTime::Print(o);
|
---|
440 |
|
---|
441 | if (HasLoGain())
|
---|
442 | {
|
---|
443 | *fLog << dec;
|
---|
444 | *fLog << " LoGainStartShift: " << fLoGainStartShift << endl;
|
---|
445 | *fLog << " LoGainSwitch: " << fLoGainSwitch << endl;
|
---|
446 | }
|
---|
447 | }
|
---|