source: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeSpline.cc@ 5601

Last change on this file since 5601 was 5593, checked in by gaug, 20 years ago
*** empty log message ***
File size: 33.0 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 analyzing 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! Author(s): Markus Gaug 09/2004 <mailto:markus@ifae.es>
18!
19! Copyright: MAGIC Software Development, 2002-2004
20!
21!
22\* ======================================================================== */
23//////////////////////////////////////////////////////////////////////////////
24//
25// MExtractTimeAndChargeSpline
26//
27// Fast Spline extractor using a cubic spline algorithm, adapted from
28// Numerical Recipes in C++, 2nd edition, pp. 116-119.
29//
30// The coefficients "ya" are here denoted as "fHiGainSignal" and "fLoGainSignal"
31// which means the FADC value subtracted by the clock-noise corrected pedestal.
32//
33// The coefficients "y2a" get immediately divided 6. and are called here
34// "fHiGainSecondDeriv" and "fLoGainSecondDeriv" although they are now not exactly
35// the second derivative coefficients any more.
36//
37// The calculation of the cubic-spline interpolated value "y" on a point
38// "x" along the FADC-slices axis becomes:
39//
40// y = a*fHiGainSignal[klo] + b*fHiGainSignal[khi]
41// + (a*a*a-a)*fHiGainSecondDeriv[klo] + (b*b*b-b)*fHiGainSecondDeriv[khi]
42//
43// with:
44// a = (khi - x)
45// b = (x - klo)
46//
47// and "klo" being the lower bin edge FADC index and "khi" the upper bin edge FADC index.
48// fHiGainSignal[klo] and fHiGainSignal[khi] are the FADC values at "klo" and "khi".
49//
50// An analogues formula is used for the low-gain values.
51//
52// The coefficients fHiGainSecondDeriv and fLoGainSecondDeriv are calculated with the
53// following simplified algorithm:
54//
55// for (Int_t i=1;i<range-1;i++) {
56// pp = fHiGainSecondDeriv[i-1] + 4.;
57// fHiGainFirstDeriv[i] = fHiGainSignal[i+1] - 2.*fHiGainSignal[i] + fHiGainSignal[i-1]
58// fHiGainFirstDeriv[i] = (6.0*fHiGainFirstDeriv[i]-fHiGainFirstDeriv[i-1])/pp;
59// }
60//
61// for (Int_t k=range-2;k>=0;k--)
62// fHiGainSecondDeriv[k] = (fHiGainSecondDeriv[k]*fHiGainSecondDeriv[k+1] + fHiGainFirstDeriv[k])/6.;
63//
64//
65// This algorithm takes advantage of the fact that the x-values are all separated by exactly 1
66// which simplifies the Numerical Recipes algorithm.
67// (Note that the variables "fHiGainFirstDeriv" are not real first derivative coefficients.)
68//
69//
70// The algorithm to search the time proceeds as follows:
71//
72// 1) Calculate all fHiGainSignal from fHiGainFirst to fHiGainLast
73// (note that an "overlap" to the low-gain arrays is possible: i.e. fHiGainLast>14 in the case of
74// the MAGIC FADCs).
75// 2) Remember the position of the slice with the highest content "fAbMax" at "fAbMaxPos".
76// 3) If one or more slices are saturated or fAbMaxPos is less than 2 slices from fHiGainFirst,
77// return fAbMaxPos as time and fAbMax as charge (note that the pedestal is subtracted here).
78// 4) Calculate all fHiGainSecondDeriv from the fHiGainSignal array
79// 5) Search for the maximum, starting in interval fAbMaxPos-1 in steps of 0.2 till fAbMaxPos-0.2.
80// If no maximum is found, go to interval fAbMaxPos+1.
81// --> 4 function evaluations
82// 6) Search for the absolute maximum from fAbMaxPos to fAbMaxPos+1 in steps of 0.2
83// --> 4 function evaluations
84// 7) Try a better precision searching from new max. position fAbMaxPos-0.2 to fAbMaxPos+0.2
85// in steps of 0.025 (83 psec. in the case of the MAGIC FADCs).
86// --> 14 function evaluations
87// 8) If Time Extraction Type kMaximum has been chosen, the position of the found maximum is
88// returned, else:
89// 9) The Half Maximum is calculated.
90// 10) fHiGainSignal is called beginning from fAbMaxPos-1 backwards until a value smaller than fHalfMax
91// is found at "klo".
92// 11) Then, the spline value between "klo" and "klo"+1 is halfed by means of bisection as long as
93// the difference between fHalfMax and spline evaluation is less than fResolution (default: 0.01).
94// --> maximum 12 interations.
95//
96// The algorithm to search the charge proceeds as follows:
97//
98// 1) If Charge Type: kAmplitude was chosen, return the Maximum of the spline, found during the
99// time search.
100// 2) If Charge Type: kIntegral was chosen, sum the fHiGainSignal between:
101// (Int_t)(fAbMaxPos - fRiseTime) and
102// (Int_t)(fAbMaxPos + fFallTime)
103// (default: fRiseTime: 1.5, fFallTime: 4.5)
104// 3) Sum only half the values of the edge slices
105// 4) Sum 1.5*fHiGainSecondDeriv of the not-edge slices using the "natural cubic
106// spline with second derivatives set to 0. at the edges.
107// (Remember that fHiGainSecondDeriv had been divided by 6.)
108//
109// The values: fNumHiGainSamples and fNumLoGainSamples are set to:
110// 1) If Charge Type: kAmplitude was chosen: 1.
111// 2) If Charge Type: kIntegral was chosen: TMath::Floor(fRiseTime + fFallTime)
112// or: TMath::Floor(fRiseTime + fFallTime + 1.) in the case of the low-gain
113//
114// Call: SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast)
115// to modify the ranges.
116//
117// Defaults:
118// fHiGainFirst = 2
119// fHiGainLast = 14
120// fLoGainFirst = 2
121// fLoGainLast = 14
122//
123// Call: SetResolution() to define the resolution of the half-maximum search.
124// Default: 0.01
125//
126// Call: SetRiseTime() and SetFallTime() to define the integration ranges
127// for the case, the extraction type kIntegral has been chosen.
128//
129// Call: - SetTimeType(MExtractTimeAndChargeSpline::kMaximum) for extraction
130// the position of the maximum (default)
131// --> needs 22 function evaluations
132// - SetTimeType(MExtractTimeAndChargeSpline::kHalfMaximum) for extraction
133// the position of the half maximum at the rising edge.
134// --> needs max. 34 function evaluations
135// - SetChargeType(MExtractTimeAndChargeSpline::kAmplitude) for the
136// computation of the amplitude at the maximum (default)
137// --> no further function evaluation needed
138// - SetChargeType(MExtractTimeAndChargeSpline::kIntegral) for the
139// computation of the integral beneith the spline between fRiseTime
140// from the position of the maximum to fFallTime after the position of
141// the maximum. The Low Gain is computed with one more slice at the falling
142// edge.
143// --> needs one more simple summation loop over 7 slices.
144//
145//////////////////////////////////////////////////////////////////////////////
146#include "MExtractTimeAndChargeSpline.h"
147
148#include "MPedestalPix.h"
149
150#include "MLog.h"
151#include "MLogManip.h"
152
153ClassImp(MExtractTimeAndChargeSpline);
154
155using namespace std;
156
157const Byte_t MExtractTimeAndChargeSpline::fgHiGainFirst = 2;
158const Byte_t MExtractTimeAndChargeSpline::fgHiGainLast = 14;
159const Byte_t MExtractTimeAndChargeSpline::fgLoGainFirst = 2;
160const Byte_t MExtractTimeAndChargeSpline::fgLoGainLast = 14;
161const Float_t MExtractTimeAndChargeSpline::fgResolution = 0.025;
162const Float_t MExtractTimeAndChargeSpline::fgRiseTime = 1.5;
163const Float_t MExtractTimeAndChargeSpline::fgFallTime = 4.5;
164// --------------------------------------------------------------------------
165//
166// Default constructor.
167//
168// Calls:
169// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
170//
171// Initializes:
172// - fResolution to fgResolution
173// - fRiseTime to fgRiseTime
174// - fFallTime to fgFallTime
175// - Time Extraction Type to kMaximum
176// - Charge Extraction Type to kAmplitude
177//
178MExtractTimeAndChargeSpline::MExtractTimeAndChargeSpline(const char *name, const char *title)
179 : fAbMax(0.), fAbMaxPos(0.), fHalfMax(0.), fRandomIter(0)
180{
181
182 fName = name ? name : "MExtractTimeAndChargeSpline";
183 fTitle = title ? title : "Calculate photons arrival time using a fast spline";
184
185 SetResolution();
186 SetRiseTime();
187 SetFallTime();
188
189 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
190
191 SetTimeType();
192 SetChargeType();
193
194}
195
196
197//-------------------------------------------------------------------
198//
199// Set the ranges
200// In order to set the fNum...Samples variables correctly for the case,
201// the integral is computed, have to overwrite this function and make an
202// explicit call to SetChargeType().
203//
204void MExtractTimeAndChargeSpline::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
205{
206
207 MExtractor::SetRange(hifirst, hilast, lofirst, lolast);
208
209 if (IsExtractionType(kIntegral))
210 SetChargeType(kIntegral);
211 if (IsExtractionType(kAmplitude))
212 SetChargeType(kAmplitude);
213
214}
215
216
217//-------------------------------------------------------------------
218//
219// Set the Time Extraction type. Possible are:
220// - kMaximum: Search the maximum of the spline and return its position
221// - kHalfMaximum: Search the half maximum left from the maximum and return
222// its position
223//
224void MExtractTimeAndChargeSpline::SetTimeType( ExtractionType_t typ )
225{
226
227 CLRBIT(fFlags,kMaximum);
228 CLRBIT(fFlags,kHalfMaximum);
229 SETBIT(fFlags,typ);
230
231}
232
233//-------------------------------------------------------------------
234//
235// Set the Charge Extraction type. Possible are:
236// - kAmplitude: Search the value of the spline at the maximum
237// - kIntegral: Integral the spline from fHiGainFirst to fHiGainLast,
238// by counting the edge bins only half and setting the
239// second derivative to zero, there.
240//
241void MExtractTimeAndChargeSpline::SetChargeType( ExtractionType_t typ )
242{
243
244 CLRBIT(fFlags,kAmplitude);
245 CLRBIT(fFlags,kIntegral );
246
247 SETBIT(fFlags,typ);
248
249}
250
251// --------------------------------------------------------------------------
252//
253// InitArrays
254//
255// Gets called in the ReInit() and initialized the arrays
256//
257Bool_t MExtractTimeAndChargeSpline::InitArrays()
258{
259
260 Int_t range = fHiGainLast - fHiGainFirst + 1 + fHiLoLast;
261
262 fHiGainSignal .Set(range);
263 fHiGainFirstDeriv .Set(range);
264 fHiGainSecondDeriv.Set(range);
265
266 range = fLoGainLast - fLoGainFirst + 1;
267
268 fLoGainSignal .Set(range);
269 fLoGainFirstDeriv .Set(range);
270 fLoGainSecondDeriv.Set(range);
271
272 fHiGainSignal .Reset();
273 fHiGainFirstDeriv .Reset();
274 fHiGainSecondDeriv.Reset();
275
276 fLoGainSignal .Reset();
277 fLoGainFirstDeriv .Reset();
278 fLoGainSecondDeriv.Reset();
279
280 if (IsExtractionType(kAmplitude))
281 {
282 fNumHiGainSamples = 1.;
283 fNumLoGainSamples = fLoGainLast ? 1. : 0.;
284 fSqrtHiGainSamples = 1.;
285 fSqrtLoGainSamples = 1.;
286 fWindowSizeHiGain = 1;
287 fWindowSizeLoGain = 1;
288 }
289
290 if (IsExtractionType(kIntegral))
291 {
292 fNumHiGainSamples = fRiseTime + fFallTime;
293 fNumLoGainSamples = fLoGainLast ? fNumHiGainSamples + 1. : 0.;
294 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
295 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
296 fWindowSizeHiGain = (Int_t)(fRiseTime + fFallTime);
297 fWindowSizeLoGain = (Int_t)(fRiseTime + fFallTime+1);
298 }
299
300 return kTRUE;
301
302}
303
304// --------------------------------------------------------------------------
305//
306// Calculates the arrival time and charge for each pixel
307//
308void MExtractTimeAndChargeSpline::FindTimeAndChargeHiGain(Byte_t *first, Byte_t *logain, Float_t &sum, Float_t &dsum,
309 Float_t &time, Float_t &dtime,
310 Byte_t &sat, const MPedestalPix &ped, const Bool_t abflag)
311{
312
313 Int_t range = fHiGainLast - fHiGainFirst + 1;
314 const Byte_t *end = first + range;
315 Byte_t *p = first;
316 Int_t count = 0;
317
318 const Float_t pedes = ped.GetPedestal();
319 const Float_t ABoffs = ped.GetPedestalABoffset();
320
321 Float_t pedmean[2];
322 pedmean[0] = pedes + ABoffs;
323 pedmean[1] = pedes - ABoffs;
324
325 fAbMax = 0.;
326 fAbMaxPos = 0.;
327 Int_t maxpos = 0;
328
329 //
330 // Check for saturation in all other slices
331 //
332 while (p<end)
333 {
334
335 const Int_t ids = fHiGainFirst + count ;
336 const Float_t signal = (Float_t)*p - pedmean[(ids+abflag) & 0x1];
337 fHiGainSignal[count] = signal;
338
339 if (signal > fAbMax + 0.1) /* the 0.1 is necessary for the ultra-high enery events saturating many slices */
340 {
341 fAbMax = signal;
342 maxpos = count;
343 }
344
345 if (*p++ >= fSaturationLimit)
346 sat++;
347
348 count++;
349 }
350
351 if (fHiLoLast != 0)
352 {
353
354 end = logain + fHiLoLast;
355
356 while (logain<end)
357 {
358
359 const Int_t ids = fHiGainFirst + range ;
360 const Float_t signal = (Float_t)*logain - pedmean[(ids+abflag) & 0x1];
361 fHiGainSignal[range] = signal;
362
363 if (signal > fAbMax)
364 {
365 fAbMax = signal;
366 maxpos = range;
367 }
368
369 if (*logain >= fSaturationLimit)
370 sat++;
371
372 range++;
373 logain++;
374 }
375 }
376
377 Float_t pp;
378
379 fHiGainSecondDeriv[0] = 0.;
380 fHiGainFirstDeriv[0] = 0.;
381
382 for (Int_t i=1;i<range-1;i++)
383 {
384 pp = fHiGainSecondDeriv[i-1] + 4.;
385 fHiGainSecondDeriv[i] = -1.0/pp;
386 fHiGainFirstDeriv [i] = fHiGainSignal[i+1] - fHiGainSignal[i] - fHiGainSignal[i] + fHiGainSignal[i-1];
387 fHiGainFirstDeriv [i] = (6.0*fHiGainFirstDeriv[i]-fHiGainFirstDeriv[i-1])/pp;
388 }
389
390 fHiGainSecondDeriv[range-1] = 0.;
391
392 for (Int_t k=range-2;k>=0;k--)
393 fHiGainSecondDeriv[k] = fHiGainSecondDeriv[k]*fHiGainSecondDeriv[k+1] + fHiGainFirstDeriv[k];
394 for (Int_t k=range-2;k>=0;k--)
395 fHiGainSecondDeriv[k] /= 6.;
396
397 if (IsNoiseCalculation())
398 {
399 if (fRandomIter == int(1./fResolution))
400 fRandomIter = 0;
401
402 const Float_t nsx = fRandomIter * fResolution;
403
404 if (IsExtractionType(kAmplitude))
405 {
406 const Float_t b = nsx;
407 const Float_t a = 1. - nsx;
408
409 sum = a*fHiGainSignal[1]
410 + b*fHiGainSignal[2]
411 + (a*a*a-a)*fHiGainSecondDeriv[1]
412 + (b*b*b-b)*fHiGainSecondDeriv[2];
413 }
414 else
415 {
416 Float_t start = 2. + nsx;
417 Float_t last = start + fRiseTime + fFallTime;
418
419 if (int(last) > range)
420 {
421 const Int_t diff = range - int(last);
422 last -= diff;
423 start -= diff;
424 }
425
426 CalcIntegralHiGain(sum, start, last);
427 }
428 fRandomIter++;
429 return;
430 }
431
432 //
433 // Allow one saturated slice
434 // and
435 // Don't start if the maxpos is too close to the limits.
436 //
437 if (sat > 1 || maxpos < 2 || maxpos > range-2)
438 {
439 time = IsExtractionType(kMaximum)
440 ? (Float_t)(fHiGainFirst + maxpos)
441 : (Float_t)(fHiGainFirst + maxpos - 1);
442
443 if (IsExtractionType(kAmplitude))
444 {
445 sum = fAbMax;
446 return;
447 }
448
449 if (maxpos > range - 2)
450 CalcIntegralHiGain(sum, (Float_t)range - fRiseTime - fFallTime, (Float_t)range - 0.001);
451 else
452 CalcIntegralHiGain(sum, 0.001, fRiseTime + fFallTime);
453
454 return;
455 }
456
457 //
458 // Now find the maximum
459 //
460 Float_t step = 0.2; // start with step size of 1ns and loop again with the smaller one
461 Float_t lower = -1. + maxpos;
462 Float_t upper = (Float_t)maxpos;
463 fAbMaxPos = upper;
464 Float_t x = lower;
465 Float_t y = 0.;
466 Float_t a = 1.;
467 Float_t b = 0.;
468 Int_t klo = maxpos-1;
469 Int_t khi = maxpos;
470
471 //
472 // Search for the maximum, starting in interval maxpos-1 in steps of 0.2 till maxpos-0.2.
473 // If no maximum is found, go to interval maxpos+1.
474 //
475 while ( x < upper - 0.3 )
476 {
477
478 x += step;
479 a -= step;
480 b += step;
481
482 y = a*fHiGainSignal[klo]
483 + b*fHiGainSignal[khi]
484 + (a*a*a-a)*fHiGainSecondDeriv[klo]
485 + (b*b*b-b)*fHiGainSecondDeriv[khi];
486
487 if (y > fAbMax)
488 {
489 fAbMax = y;
490 fAbMaxPos = x;
491 }
492
493 }
494
495 //
496 // Search for the absolute maximum from maxpos to maxpos+1 in steps of 0.2
497 //
498 if (fAbMaxPos > upper-0.1)
499 {
500
501 upper = 1. + maxpos;
502 lower = (Float_t)maxpos;
503 x = lower;
504 a = 1.;
505 b = 0.;
506 khi = maxpos+1;
507 klo = maxpos;
508
509 while (x<upper-0.3)
510 {
511
512 x += step;
513 a -= step;
514 b += step;
515
516 y = a*fHiGainSignal[klo]
517 + b*fHiGainSignal[khi]
518 + (a*a*a-a)*fHiGainSecondDeriv[klo]
519 + (b*b*b-b)*fHiGainSecondDeriv[khi];
520
521 if (y > fAbMax)
522 {
523 fAbMax = y;
524 fAbMaxPos = x;
525 }
526 }
527 }
528
529 //
530 // Now, the time, abmax and khicont and klocont are set correctly within the previous precision.
531 // Try a better precision.
532 //
533 const Float_t up = fAbMaxPos+step - 1.5*fResolution;
534 const Float_t lo = fAbMaxPos-step + 1.5*fResolution;
535 const Float_t maxpossave = fAbMaxPos;
536
537 x = fAbMaxPos;
538 a = upper - x;
539 b = x - lower;
540
541 step = fResolution; // step size of 83 ps
542
543 while (x<up)
544 {
545
546 x += step;
547 a -= step;
548 b += step;
549
550 y = a*fHiGainSignal[klo]
551 + b*fHiGainSignal[khi]
552 + (a*a*a-a)*fHiGainSecondDeriv[klo]
553 + (b*b*b-b)*fHiGainSecondDeriv[khi];
554
555 if (y > fAbMax)
556 {
557 fAbMax = y;
558 fAbMaxPos = x;
559 }
560 }
561
562 //
563 // Second, try from time down to time-0.2 in steps of fResolution.
564 //
565 x = maxpossave;
566
567 //
568 // Test the possibility that the absolute maximum has not been found between
569 // maxpos and maxpos+0.025, then we have to look between maxpos-0.025 and maxpos
570 // which requires new setting of klocont and khicont
571 //
572 if (x < lower + fResolution/2.)
573 {
574 klo--;
575 khi--;
576 upper += 1.;
577 lower -= 1.;
578 }
579
580 a = upper - x;
581 b = x - lower;
582
583 while (x>lo)
584 {
585
586 x -= step;
587 a += step;
588 b -= step;
589
590 y = a*fHiGainSignal[klo]
591 + b*fHiGainSignal[khi]
592 + (a*a*a-a)*fHiGainSecondDeriv[klo]
593 + (b*b*b-b)*fHiGainSecondDeriv[khi];
594
595 if (y > fAbMax)
596 {
597 fAbMax = y;
598 fAbMaxPos = x;
599 }
600 }
601
602 if (IsExtractionType(kMaximum))
603 {
604 time = (Float_t)fHiGainFirst + fAbMaxPos;
605 dtime = fResolution;
606 }
607 else
608 {
609 fHalfMax = fAbMax/2.;
610
611 //
612 // Now, loop from the maximum bin leftward down in order to find the position of the half maximum.
613 // First, find the right FADC slice:
614 //
615 klo = maxpos - 1;
616 while (klo >= 0)
617 {
618 if (fHiGainSignal[klo] < fHalfMax)
619 break;
620 klo--;
621 }
622
623 //
624 // Loop from the beginning of the slice upwards to reach the fHalfMax:
625 // With means of bisection:
626 //
627 x = (Float_t)klo;
628 a = 1.;
629 b = 0.;
630
631 step = 0.5;
632 Bool_t back = kFALSE;
633
634 Int_t maxcnt = 50;
635 Int_t cnt = 0;
636
637 while (TMath::Abs(y-fHalfMax) > fResolution)
638 {
639
640 if (back)
641 {
642 x -= step;
643 a += step;
644 b -= step;
645 }
646 else
647 {
648 x += step;
649 a -= step;
650 b += step;
651 }
652
653 y = a*fHiGainSignal[klo]
654 + b*fHiGainSignal[khi]
655 + (a*a*a-a)*fHiGainSecondDeriv[klo]
656 + (b*b*b-b)*fHiGainSecondDeriv[khi];
657
658 if (y > fHalfMax)
659 back = kTRUE;
660 else
661 back = kFALSE;
662
663 if (++cnt > maxcnt)
664 {
665 // *fLog << inf << x << " " << y << " " << fHalfMax << endl;
666 break;
667 }
668
669 step /= 2.;
670 }
671
672 time = (Float_t)fHiGainFirst + x;
673 dtime = fResolution;
674 }
675
676 if (IsExtractionType(kAmplitude))
677 {
678 sum = fAbMax;
679 return;
680 }
681
682 if (IsExtractionType(kIntegral))
683 {
684 //
685 // Now integrate the whole thing!
686 //
687
688 Float_t start = fAbMaxPos - fRiseTime;
689 Float_t last = fAbMaxPos + fFallTime;
690
691 const Int_t diff = int(last) - range;
692
693 if (diff > 0)
694 {
695 last -= diff;
696 start -= diff;
697 }
698
699 CalcIntegralHiGain(sum, start, last);
700 }
701}
702
703
704// --------------------------------------------------------------------------
705//
706// Calculates the arrival time and charge for each pixel
707//
708void MExtractTimeAndChargeSpline::FindTimeAndChargeLoGain(Byte_t *first, Float_t &sum, Float_t &dsum,
709 Float_t &time, Float_t &dtime,
710 Byte_t &sat, const MPedestalPix &ped, const Bool_t abflag)
711{
712
713 Int_t range = fLoGainLast - fLoGainFirst + 1;
714 const Byte_t *end = first + range;
715 Byte_t *p = first;
716 Int_t count = 0;
717
718 const Float_t pedes = ped.GetPedestal();
719 const Float_t ABoffs = ped.GetPedestalABoffset();
720
721 Float_t pedmean[2];
722 pedmean[0] = pedes + ABoffs;
723 pedmean[1] = pedes - ABoffs;
724
725 fAbMax = 0.;
726 fAbMaxPos = 0.;
727 Int_t maxpos = 0;
728
729 //
730 // Check for saturation in all other slices
731 //
732 while (p<end)
733 {
734
735 const Int_t ids = count + fLoGainFirst;
736 const Float_t signal = (Float_t)*p - pedmean[(ids+abflag) & 0x1];
737 fLoGainSignal[count] = signal;
738
739 if (signal > fAbMax + 0.1)
740 {
741 fAbMax = signal;
742 maxpos = count;
743 }
744
745 if (*p++ >= fSaturationLimit)
746 sat++;
747
748 count++;
749 }
750
751 Float_t pp;
752
753 fLoGainSecondDeriv[0] = 0.;
754 fLoGainFirstDeriv[0] = 0.;
755
756 for (Int_t i=1;i<range-1;i++)
757 {
758 pp = fLoGainSecondDeriv[i-1] + 4.;
759 fLoGainSecondDeriv[i] = -1.0/pp;
760 fLoGainFirstDeriv [i] = fLoGainSignal[i+1] - fLoGainSignal[i] - fLoGainSignal[i] + fLoGainSignal[i-1];
761 fLoGainFirstDeriv [i] = (6.0*fLoGainFirstDeriv[i]-fLoGainFirstDeriv[i-1])/pp;
762 }
763
764 fLoGainSecondDeriv[range-1] = 0.;
765
766 for (Int_t k=range-2;k>=0;k--)
767 fLoGainSecondDeriv[k] = fLoGainSecondDeriv[k]*fLoGainSecondDeriv[k+1] + fLoGainFirstDeriv[k];
768 for (Int_t k=range-2;k>=0;k--)
769 fLoGainSecondDeriv[k] /= 6.;
770
771 if (IsNoiseCalculation())
772 {
773 if (fRandomIter == int(1./fResolution))
774 fRandomIter = 0;
775
776 const Float_t nsx = fRandomIter * fResolution;
777
778 if (IsExtractionType(kAmplitude))
779 {
780 const Float_t b = nsx;
781 const Float_t a = 1. - nsx;
782
783 sum = a*fLoGainSignal[1]
784 + b*fLoGainSignal[2]
785 + (a*a*a-a)*fLoGainSecondDeriv[1]
786 + (b*b*b-b)*fLoGainSecondDeriv[2];
787 }
788 else
789 {
790 Float_t start = 2. + nsx;
791 Float_t last = start + fRiseTime + fFallTime;
792
793 if (int(last) > range)
794 {
795 const Int_t diff = range - int(last);
796 last -= diff;
797 start -= diff;
798 }
799
800 CalcIntegralLoGain(sum, start, last);
801 }
802 fRandomIter++;
803 return;
804 }
805 //
806 // Allow no saturated slice
807 // and
808 // Don't start if the maxpos is too close to the limits.
809 //
810 if (sat || maxpos < 2 || maxpos > range-2)
811 {
812 time = IsExtractionType(kMaximum)
813 ? (Float_t)(fLoGainFirst + maxpos)
814 : (Float_t)(fLoGainFirst + maxpos - 1);
815
816 if (IsExtractionType(kAmplitude))
817 {
818 sum = fAbMax;
819 return;
820 }
821
822 if (maxpos > range-2)
823 CalcIntegralLoGain(sum, (Float_t)range - fRiseTime - fFallTime-1., (Float_t)range - 0.001);
824 else
825 CalcIntegralLoGain(sum, 0.001, fRiseTime + fFallTime + 1.);
826
827 return;
828 }
829
830 //
831 // Now find the maximum
832 //
833 Float_t step = 0.2; // start with step size of 1ns and loop again with the smaller one
834 Float_t lower = -1. + maxpos;
835 Float_t upper = (Float_t)maxpos;
836 fAbMaxPos = upper;
837 Float_t x = lower;
838 Float_t y = 0.;
839 Float_t a = 1.;
840 Float_t b = 0.;
841 Int_t klo = maxpos-1;
842 Int_t khi = maxpos;
843
844 //
845 // Search for the maximum, starting in interval maxpos-1 in steps of 0.2 till maxpos-0.2.
846 // If no maximum is found, go to interval maxpos+1.
847 //
848 while ( x < upper - 0.3 )
849 {
850
851 x += step;
852 a -= step;
853 b += step;
854
855 y = a*fLoGainSignal[klo]
856 + b*fLoGainSignal[khi]
857 + (a*a*a-a)*fLoGainSecondDeriv[klo]
858 + (b*b*b-b)*fLoGainSecondDeriv[khi];
859
860 if (y > fAbMax)
861 {
862 fAbMax = y;
863 fAbMaxPos = x;
864 }
865
866 }
867
868 //
869 // Test the possibility that the absolute maximum has not been found before the
870 // maxpos and search from maxpos to maxpos+1 in steps of 0.2
871 //
872 if (fAbMaxPos > upper-0.1)
873 {
874
875 upper = 1. + maxpos;
876 lower = (Float_t)maxpos;
877 x = lower;
878 a = 1.;
879 b = 0.;
880 khi = maxpos+1;
881 klo = maxpos;
882
883 while (x<upper-0.3)
884 {
885
886 x += step;
887 a -= step;
888 b += step;
889
890 y = a*fLoGainSignal[klo]
891 + b*fLoGainSignal[khi]
892 + (a*a*a-a)*fLoGainSecondDeriv[klo]
893 + (b*b*b-b)*fLoGainSecondDeriv[khi];
894
895 if (y > fAbMax)
896 {
897 fAbMax = y;
898 fAbMaxPos = x;
899 }
900 }
901 }
902
903
904 //
905 // Now, the time, abmax and khicont and klocont are set correctly within the previous precision.
906 // Try a better precision.
907 //
908 const Float_t up = fAbMaxPos+step - 1.5*fResolution;
909 const Float_t lo = fAbMaxPos-step + 1.5*fResolution;
910 const Float_t maxpossave = fAbMaxPos;
911
912 x = fAbMaxPos;
913 a = upper - x;
914 b = x - lower;
915
916 step = fResolution; // step size of fResolution (33 ps )
917
918 while (x<up)
919 {
920
921 x += step;
922 a -= step;
923 b += step;
924
925 y = a*fLoGainSignal[klo]
926 + b*fLoGainSignal[khi]
927 + (a*a*a-a)*fLoGainSecondDeriv[klo]
928 + (b*b*b-b)*fLoGainSecondDeriv[khi];
929
930 if (y > fAbMax)
931 {
932 fAbMax = y;
933 fAbMaxPos = x;
934 }
935 }
936
937 //
938 // Second, try from time down to time-0.2 in steps of 0.025.
939 //
940 x = maxpossave;
941
942 //
943 // Test the possibility that the absolute maximum has not been found between
944 // maxpos and maxpos+0.02, then we have to look between maxpos-0.02 and maxpos
945 // which requires new setting of klocont and khicont
946 //
947 if (x < lower + fResolution/2.)
948 {
949 klo--;
950 khi--;
951 upper += 1.;
952 lower -= 1.;
953 }
954
955 a = upper - x;
956 b = x - lower;
957
958 while (x>lo)
959 {
960
961 x -= step;
962 a += step;
963 b -= step;
964
965 y = a*fLoGainSignal[klo]
966 + b*fLoGainSignal[khi]
967 + (a*a*a-a)*fLoGainSecondDeriv[klo]
968 + (b*b*b-b)*fLoGainSecondDeriv[khi];
969
970 if (y > fAbMax)
971 {
972 fAbMax = y;
973 fAbMaxPos = x;
974 }
975 }
976
977 if (IsExtractionType(kMaximum))
978 {
979 time = fAbMaxPos + (Int_t)fLoGainFirst;
980 dtime = fResolution;
981 }
982 else
983 {
984 fHalfMax = fAbMax/2.;
985
986 //
987 // Now, loop from the maximum bin leftward down in order to find the position of the half maximum.
988 // First, find the right FADC slice:
989 //
990 klo = maxpos - 1;
991 while (klo >= 0)
992 {
993 if (fLoGainSignal[klo] < fHalfMax)
994 break;
995 klo--;
996 }
997
998 //
999 // Loop from the beginning of the slice upwards to reach the fHalfMax:
1000 // With means of bisection:
1001 //
1002 x = (Float_t)klo;
1003 a = 1.;
1004 b = 0.;
1005
1006 step = 0.5;
1007 Bool_t back = kFALSE;
1008
1009 Int_t maxcnt = 50;
1010 Int_t cnt = 0;
1011
1012 while (TMath::Abs(y-fHalfMax) > fResolution)
1013 {
1014
1015 if (back)
1016 {
1017 x -= step;
1018 a += step;
1019 b -= step;
1020 }
1021 else
1022 {
1023 x += step;
1024 a -= step;
1025 b += step;
1026 }
1027
1028 y = a*fLoGainSignal[klo]
1029 + b*fLoGainSignal[khi]
1030 + (a*a*a-a)*fLoGainSecondDeriv[klo]
1031 + (b*b*b-b)*fLoGainSecondDeriv[khi];
1032
1033 if (y > fHalfMax)
1034 back = kTRUE;
1035 else
1036 back = kFALSE;
1037
1038 if (++cnt > maxcnt)
1039 {
1040 // *fLog << inf << x << " " << y << " " << fHalfMax << endl;
1041 break;
1042 }
1043
1044 step /= 2.;
1045 }
1046
1047 time = x + (Int_t)fLoGainFirst;
1048 dtime = fResolution;
1049 }
1050
1051 if (IsExtractionType(kAmplitude))
1052 {
1053 sum = fAbMax;
1054 return;
1055 }
1056
1057 if (IsExtractionType(kIntegral))
1058 {
1059 //
1060 // Now integrate the whole thing!
1061 //
1062 Float_t start = fAbMaxPos - fRiseTime;
1063 Float_t last = fAbMaxPos + fFallTime + 1.;
1064
1065 const Int_t diff = int(last) - range;
1066
1067 if (diff > 0)
1068 {
1069 last -= diff;
1070 start -= diff;
1071 }
1072 CalcIntegralLoGain(sum, start, last);
1073 }
1074}
1075
1076void MExtractTimeAndChargeSpline::CalcIntegralHiGain(Float_t &sum, Float_t start, Float_t last)
1077{
1078
1079 const Float_t step = 0.1;
1080
1081 if (start < 0)
1082 {
1083 last -= start;
1084 start = 0.;
1085 }
1086
1087 Int_t klo = int(start);
1088 Int_t khi = klo+1;
1089
1090 Float_t up = TMath::Ceil(start);
1091 Float_t lo = TMath::Floor(start);
1092
1093 const Int_t m = int((start-klo)/step);
1094 start = step*m + klo; // Correct start for the digitization due to resolution
1095
1096 Float_t x = start;
1097 Float_t a = up-start;
1098 Float_t b = start-lo;
1099
1100 while (1)
1101 {
1102
1103 while (x<up)
1104 {
1105 x += step;
1106
1107 if (x > last)
1108 {
1109 sum *= step;
1110 return;
1111 }
1112
1113 a -= step;
1114 b += step;
1115
1116 sum += a*fHiGainSignal[klo]
1117 + b*fHiGainSignal[khi]
1118 + (a*a*a-a)*fHiGainSecondDeriv[klo]
1119 + (b*b*b-b)*fHiGainSecondDeriv[khi];
1120 }
1121
1122 up += 1.;
1123 lo += 1.;
1124 klo++;
1125 khi++;
1126 start += 1.;
1127 a = 1.;
1128 b = 0.;
1129 }
1130
1131}
1132void MExtractTimeAndChargeSpline::CalcIntegralLoGain(Float_t &sum, Float_t start, Float_t last)
1133{
1134
1135 const Float_t step = 0.1;
1136
1137 if (start < 0)
1138 {
1139 last -= start;
1140 start = 0.;
1141 }
1142
1143 Int_t klo = int(start);
1144 Int_t khi = klo+1;
1145
1146 Float_t up = TMath::Ceil(start);
1147 Float_t lo = TMath::Floor(start);
1148
1149 const Int_t m = int((start-klo)/step);
1150 start = step*m + klo; // Correct start for the digitization due to resolution
1151
1152 Float_t x = start;
1153 Float_t a = up-start;
1154 Float_t b = start-lo;
1155
1156 while (1)
1157 {
1158
1159 while (x<up)
1160 {
1161 x += step;
1162
1163 if (x > last)
1164 {
1165 sum *= step;
1166 return;
1167 }
1168
1169 a -= step;
1170 b += step;
1171
1172 sum += a*fHiGainSignal[klo]
1173 + b*fHiGainSignal[khi]
1174 + (a*a*a-a)*fHiGainSecondDeriv[klo]
1175 + (b*b*b-b)*fHiGainSecondDeriv[khi];
1176 }
1177
1178 up += 1.;
1179 lo += 1.;
1180 klo++;
1181 khi++;
1182 start += 1.;
1183 a = 1.;
1184 b = 0.;
1185 }
1186}
1187
1188
1189
1190
1191// --------------------------------------------------------------------------
1192//
1193// In addition to the resources of the base-class MExtractor:
1194// MJPedestal.MExtractor.WindowSizeHiGain: 6
1195// MJPedestal.MExtractor.WindowSizeLoGain: 6
1196//
1197Int_t MExtractTimeAndChargeSpline::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
1198{
1199
1200 Bool_t rc = kFALSE;
1201
1202 if (IsEnvDefined(env, prefix, "Resolution", print))
1203 {
1204 SetResolution(GetEnvValue(env, prefix, "Resolution",fResolution));
1205 rc = kTRUE;
1206 }
1207 if (IsEnvDefined(env, prefix, "RiseTime", print))
1208 {
1209 SetRiseTime(GetEnvValue(env, prefix, "RiseTime", fRiseTime));
1210 rc = kTRUE;
1211 }
1212 if (IsEnvDefined(env, prefix, "FallTime", print))
1213 {
1214 SetFallTime(GetEnvValue(env, prefix, "FallTime", fFallTime));
1215 rc = kTRUE;
1216 }
1217
1218 Bool_t b = kFALSE;
1219
1220 if (IsEnvDefined(env, prefix, "Amplitude", print))
1221 {
1222 b = GetEnvValue(env, prefix, "Amplitude", IsExtractionType(kAmplitude));
1223 if (b)
1224 SetChargeType(kAmplitude);
1225 rc = kTRUE;
1226 }
1227 if (IsEnvDefined(env, prefix, "Integral", print))
1228 {
1229 b = GetEnvValue(env, prefix, "Integral", IsExtractionType(kIntegral));
1230 if (b)
1231 SetChargeType(kIntegral);
1232 rc = kTRUE;
1233 }
1234 if (IsEnvDefined(env, prefix, "Maximum", print))
1235 {
1236 b = GetEnvValue(env, prefix, "Maximum", IsExtractionType(kMaximum));
1237 if (b)
1238 SetTimeType(kMaximum);
1239 rc = kTRUE;
1240 }
1241 if (IsEnvDefined(env, prefix, "HalfMaximum", print))
1242 {
1243 b = GetEnvValue(env, prefix, "HalfMaximum", IsExtractionType(kHalfMaximum));
1244 if (b)
1245 SetTimeType(kHalfMaximum);
1246 rc = kTRUE;
1247 }
1248
1249 return MExtractTimeAndCharge::ReadEnv(env, prefix, print) ? kTRUE : rc;
1250
1251}
1252
1253
Note: See TracBrowser for help on using the repository browser.