source: trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc@ 8551

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