source: trunk/MagicSoft/Mars/msignal/MExtractAmplitudeSpline.cc@ 4374

Last change on this file since 4374 was 4344, checked in by gaug, 21 years ago
*** empty log message ***
File size: 15.5 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 05/2004 <mailto:markus@ifae.es>
18!
19! Copyright: MAGIC Software Development, 2002-2004
20!
21!
22\* ======================================================================== */
23
24//////////////////////////////////////////////////////////////////////////////
25//
26// MExtractTimeAndChargeSpline
27//
28// Fast Spline extractor using a cubic spline algorithm of Numerical Recipes.
29// It returns the integral below the interpolating spline.
30//
31// Call: SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast)
32// to modify the ranges. Ranges have to be an even number. In case of odd
33// ranges, the last slice will be reduced by one.
34//
35// Defaults are:
36//
37// fHiGainFirst = fgHiGainFirst = 3
38// fHiGainLast = fgHiGainLast = 14
39// fLoGainFirst = fgLoGainFirst = 3
40// fLoGainLast = fgLoGainLast = 14
41//
42//////////////////////////////////////////////////////////////////////////////
43#include "MExtractAmplitudeSpline.h"
44
45#include "MExtractedSignalCam.h"
46
47#include "MLog.h"
48#include "MLogManip.h"
49
50ClassImp(MExtractAmplitudeSpline);
51
52using namespace std;
53
54const Byte_t MExtractAmplitudeSpline::fgHiGainFirst = 2;
55const Byte_t MExtractAmplitudeSpline::fgHiGainLast = 14;
56const Byte_t MExtractAmplitudeSpline::fgLoGainFirst = 3;
57const Byte_t MExtractAmplitudeSpline::fgLoGainLast = 14;
58const Float_t MExtractAmplitudeSpline::fgResolution = 0.003;
59// --------------------------------------------------------------------------
60//
61// Default constructor.
62//
63// Calls:
64// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
65//
66MExtractAmplitudeSpline::MExtractAmplitudeSpline(const char *name, const char *title)
67 : fHiGainSignal(NULL), fLoGainSignal(NULL),
68 fHiGainFirstDeriv(NULL), fLoGainFirstDeriv(NULL),
69 fHiGainSecondDeriv(NULL), fLoGainSecondDeriv(NULL)
70{
71
72 fName = name ? name : "MExtractAmplitudeSpline";
73 fTitle = title ? title : "Signal Extractor for a fixed FADC window using a fast spline";
74
75 SetResolution();
76 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
77}
78
79MExtractAmplitudeSpline::~MExtractAmplitudeSpline()
80{
81
82 if (fHiGainSignal)
83 delete fHiGainSignal;
84 if (fLoGainSignal)
85 delete fLoGainSignal;
86 if (fHiGainFirstDeriv)
87 delete fHiGainFirstDeriv;
88 if (fLoGainFirstDeriv)
89 delete fLoGainFirstDeriv;
90 if (fHiGainSecondDeriv)
91 delete fHiGainSecondDeriv;
92 if (fLoGainSecondDeriv)
93 delete fLoGainSecondDeriv;
94
95}
96
97// --------------------------------------------------------------------------
98//
99// SetRange:
100//
101// Checks:
102// - if the window defined by (fHiGainLast-fHiGainFirst-1) are odd, subtract one
103// - if the window defined by (fLoGainLast-fLoGainFirst-1) are odd, subtract one
104// - if the Hi Gain window is smaller than 2, set fHiGainLast to fHiGainFirst+1
105// - if the Lo Gain window is smaller than 2, set fLoGainLast to fLoGainFirst+1
106//
107// Calls:
108// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
109//
110// Sets:
111// - fNumHiGainSamples to: (Float_t)(fHiGainLast-fHiGainFirst+1)
112// - fNumLoGainSamples to: (Float_t)(fLoGainLast-fLoGainFirst+1)
113// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
114// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)
115//
116void MExtractAmplitudeSpline::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
117{
118
119
120 MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
121
122 fNumHiGainSamples = 2.;
123 fNumLoGainSamples = 2.;
124
125 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
126 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
127
128 fHiLoLast = 0;
129
130}
131
132// --------------------------------------------------------------------------
133//
134// ReInit
135//
136// Calls:
137// - MExtractor::ReInit(pList);
138// - fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
139// fLoGainFirst, fLoGainLast, fNumLoGainSamples);
140//
141// Deletes all arrays, if not NULL
142// Creates new arrays according to the extraction range
143//
144Bool_t MExtractAmplitudeSpline::ReInit(MParList *pList)
145{
146
147 if (!MExtractor::ReInit(pList))
148 return kFALSE;
149
150 fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
151 fLoGainFirst, fLoGainLast, fNumLoGainSamples);
152
153 if (fHiGainSignal)
154 delete fHiGainSignal;
155 if (fLoGainSignal)
156 delete fLoGainSignal;
157 if (fHiGainFirstDeriv)
158 delete fHiGainFirstDeriv;
159 if (fLoGainFirstDeriv)
160 delete fLoGainFirstDeriv;
161 if (fHiGainSecondDeriv)
162 delete fHiGainSecondDeriv;
163 if (fLoGainSecondDeriv)
164 delete fLoGainSecondDeriv;
165
166 Int_t range = fHiGainLast - fHiGainFirst + 1 + fHiLoLast;
167
168 fHiGainSignal = new Float_t[range];
169 memset(fHiGainSignal,0,range*sizeof(Float_t));
170 fHiGainFirstDeriv = new Float_t[range];
171 memset(fHiGainFirstDeriv,0,range*sizeof(Float_t));
172 fHiGainSecondDeriv = new Float_t[range];
173 memset(fHiGainSecondDeriv,0,range*sizeof(Float_t));
174
175 *fLog << endl;
176 *fLog << inf << GetDescriptor() << ": Using for High-Gain Extraction " << range
177 << " FADC samples from "
178 << Form("%s%2i%s"," High Gain slice ",(Int_t)fHiGainFirst," to (including) ")
179 << Form("%s%2i",fHiLoLast ? "Low Gain slice " : " High Gain slice ",
180 fHiLoLast ? (Int_t)fHiLoLast : (Int_t)fHiGainLast )
181 << endl;
182
183 range = fLoGainLast - fLoGainFirst + 1;
184
185 fLoGainSignal = new Float_t[range];
186 memset(fLoGainSignal,0,range*sizeof(Float_t));
187 fLoGainFirstDeriv = new Float_t[range];
188 memset(fLoGainFirstDeriv,0,range*sizeof(Float_t));
189 fLoGainSecondDeriv = new Float_t[range];
190 memset(fLoGainSecondDeriv,0,range*sizeof(Float_t));
191
192 *fLog << endl;
193 *fLog << inf << GetDescriptor() << ": Using for Low-Gain Extraction " << range
194 << " FADC samples from "
195 << Form("%s%2i%s%2i"," Low Gain slice ",(Int_t)fLoGainFirst,
196 " to (including) ",(Int_t)fLoGainLast) << endl;
197
198 return kTRUE;
199}
200
201
202// --------------------------------------------------------------------------
203//
204// FindSignalHiGain:
205//
206// - Loop from ptr to (ptr+fHiGainLast-fHiGainFirst)
207// - Sum up contents of *ptr
208// - If *ptr is greater than fSaturationLimit, raise sat by 1
209//
210// - If fHiLoLast is not 0, loop also from logain to (logain+fHiLoLast)
211// - Sum up contents of logain
212// - If *logain is greater than fSaturationLimit, raise sat by 1
213//
214void MExtractAmplitudeSpline::FindSignalHiGain(Byte_t *ptr, Byte_t *logain, Float_t &sum, Byte_t &sat) const
215{
216
217 Int_t count = 0;
218 Float_t abmaxpos = 0.;
219 Byte_t max = 0;
220 Byte_t maxpos = 0;
221
222 Int_t range = fHiGainLast - fHiGainFirst + 1;
223 Byte_t *end = ptr + range;
224 Byte_t *p = ptr;
225 //
226 // Check for saturation in all other slices
227 //
228 while (++p<end)
229 {
230
231 fHiGainSignal[count] = (Float_t)*p;
232
233 if (*p > max)
234 {
235 max = *p;
236 maxpos = count;
237 }
238
239 range++;
240 count++;
241
242 if (*p >= fSaturationLimit)
243 {
244 sat++;
245 break;
246 }
247 }
248
249 if (fHiLoLast != 0)
250 {
251
252 p = logain;
253 end = logain + fHiLoLast + 1;
254
255 while (p<end)
256 {
257
258 fHiGainSignal[count] = (Float_t)*p;
259
260 if (*p > max)
261 {
262 max = *p;
263 maxpos = count;
264 }
265
266 range++;
267 count++;
268
269 if (*p++ >= fSaturationLimit)
270 {
271 sat++;
272 break;
273 }
274 }
275 }
276
277 //
278 // allow one saturated slice
279 //
280 if (sat > 1)
281 return;
282
283 //
284 // Don't start if the maxpos is too close to the left limit.
285 //
286 if (maxpos < 2)
287 return;
288
289 Float_t pp;
290 fHiGainSecondDeriv[0] = 0.;
291 fHiGainFirstDeriv[0] = 0.;
292
293 for (Int_t i=1;i<range-1;i++)
294 {
295 pp = fHiGainSecondDeriv[i-1] + 4.;
296 fHiGainSecondDeriv[i] = -1.0/pp;
297 fHiGainFirstDeriv [i] = fHiGainSignal[i+1] - fHiGainSignal[i] - fHiGainSignal[i] + fHiGainSignal[i-1];
298 fHiGainFirstDeriv [i] = (6.0*fHiGainFirstDeriv[i]-fHiGainFirstDeriv[i-1])/pp;
299 p++;
300 }
301
302 fHiGainSecondDeriv[range-1] = 0.;
303 for (Int_t k=range-2;k>=0;k--)
304 fHiGainSecondDeriv[k] = (fHiGainSecondDeriv[k]*fHiGainSecondDeriv[k+1] + fHiGainFirstDeriv[k])/6.;
305
306 //
307 // Now find the maximum
308 //
309 Float_t step = 0.2; // start with step size of 1ns and loop again with the smaller one
310 Float_t lower = (Float_t)maxpos-1.;
311 Float_t upper = (Float_t)maxpos;
312 Float_t x = lower;
313 Float_t y = 0.;
314 Float_t a = 1.;
315 Float_t b = 0.;
316 Int_t klo = maxpos-1;
317 Int_t khi = maxpos;
318 Float_t klocont = fHiGainSignal[klo];
319 Float_t khicont = fHiGainSignal[khi];
320 sum = khicont;
321 abmaxpos = lower;
322
323 //
324 // Search for the maximum, starting in interval maxpos-1. If no maximum is found, go to
325 // interval maxpos+1.
326 //
327 while (x<upper-0.3)
328 {
329
330 x += step;
331 a -= step;
332 b += step;
333
334 y = a*klocont
335 + b*khicont
336 + (a*a*a-a)*fHiGainSecondDeriv[klo]
337 + (b*b*b-b)*fHiGainSecondDeriv[khi];
338
339 if (y > sum)
340 {
341 sum = y;
342 abmaxpos = x;
343 }
344 }
345
346 if (abmaxpos > upper-0.1)
347 {
348
349 upper = (Float_t)maxpos+1;
350 lower = (Float_t)maxpos;
351 x = lower;
352 a = 1.;
353 b = 0.;
354 khi = maxpos+1;
355 klo = maxpos;
356 klocont = fHiGainSignal[klo];
357 khicont = fHiGainSignal[khi];
358
359 while (x<upper-0.3)
360 {
361
362 x += step;
363 a -= step;
364 b += step;
365
366 y = a* klocont
367 + b* khicont
368 + (a*a*a-a)*fHiGainSecondDeriv[klo]
369 + (b*b*b-b)*fHiGainSecondDeriv[khi];
370
371 if (y > sum)
372 {
373 sum = y;
374 abmaxpos = x;
375 }
376 }
377 }
378
379 const Float_t up = abmaxpos+step-0.055;
380 const Float_t lo = abmaxpos-step+0.055;
381 const Float_t maxpossave = abmaxpos;
382
383 x = abmaxpos;
384 a = upper - x;
385 b = x - lower;
386
387 step = 0.04; // step size of 83 ps
388
389 while (x<up)
390 {
391
392 x += step;
393 a -= step;
394 b += step;
395
396 y = a* klocont
397 + b* khicont
398 + (a*a*a-a)*fHiGainSecondDeriv[klo]
399 + (b*b*b-b)*fHiGainSecondDeriv[khi];
400
401 if (y > sum)
402 {
403 sum = y;
404 abmaxpos = x;
405 }
406 }
407
408 if (abmaxpos < klo + 0.02)
409 {
410 klo--;
411 khi--;
412 klocont = fHiGainSignal[klo];
413 khicont = fHiGainSignal[khi];
414 upper--;
415 lower--;
416 }
417
418 x = maxpossave;
419 a = upper - x;
420 b = x - lower;
421
422 while (x>lo)
423 {
424
425 x -= step;
426 a += step;
427 b -= step;
428
429 y = a* klocont
430 + b* khicont
431 + (a*a*a-a)*fHiGainSecondDeriv[klo]
432 + (b*b*b-b)*fHiGainSecondDeriv[khi];
433
434 if (y > sum)
435 sum = y;
436 }
437}
438
439
440// --------------------------------------------------------------------------
441//
442// FindSignalLoGain:
443//
444// - Loop from ptr to (ptr+fLoGainLast-fLoGainFirst)
445// - Sum up contents of *ptr
446// - If *ptr is greater than fSaturationLimit, raise sat by 1
447//
448void MExtractAmplitudeSpline::FindSignalLoGain(Byte_t *ptr, Float_t &sum, Byte_t &sat) const
449{
450
451 Int_t count = 0;
452 Float_t abmaxpos = 0.;
453 Byte_t max = 0;
454 Byte_t maxpos = 0;
455
456 Int_t range = fLoGainLast - fLoGainFirst + 1;
457 Byte_t *end = ptr + range;
458 Byte_t *p = ptr;
459 //
460 // Check for saturation in all other slices
461 //
462 while (++p<end)
463 {
464
465 fLoGainSignal[count] = (Float_t)*p;
466
467 if (*p > max)
468 {
469 max = *p;
470 maxpos = count;
471 }
472
473 range++;
474 count++;
475
476 if (*p >= fSaturationLimit)
477 {
478 sat++;
479 break;
480 }
481 }
482
483
484 //
485 // allow one saturated slice
486 //
487 if (sat > 1)
488 return;
489
490 //
491 // Don't start if the maxpos is too close to the left limit.
492 //
493 if (maxpos < 2)
494 return;
495
496 Float_t pp;
497 fLoGainSecondDeriv[0] = 0.;
498 fLoGainFirstDeriv[0] = 0.;
499
500 for (Int_t i=1;i<range-1;i++)
501 {
502 pp = fLoGainSecondDeriv[i-1] + 4.;
503 fLoGainSecondDeriv[i] = -1.0/pp;
504 fLoGainFirstDeriv [i] = fLoGainSignal[i+1] - fLoGainSignal[i] - fLoGainSignal[i] + fLoGainSignal[i-1];
505 fLoGainFirstDeriv [i] = (6.0*fLoGainFirstDeriv[i]-fLoGainFirstDeriv[i-1])/pp;
506 p++;
507 }
508
509 fLoGainSecondDeriv[range-1] = 0.;
510 for (Int_t k=range-2;k>=0;k--)
511 fLoGainSecondDeriv[k] = (fLoGainSecondDeriv[k]*fLoGainSecondDeriv[k+1] + fLoGainFirstDeriv[k])/6.;
512
513 //
514 // Now find the maximum
515 //
516 Float_t step = 0.2; // start with step size of 1ns and loop again with the smaller one
517 Float_t lower = (Float_t)maxpos-1.;
518 Float_t upper = (Float_t)maxpos;
519 Float_t x = lower;
520 Float_t y = 0.;
521 Float_t a = 1.;
522 Float_t b = 0.;
523 Int_t klo = maxpos-1;
524 Int_t khi = maxpos;
525 Float_t klocont = fLoGainSignal[klo];
526 Float_t khicont = fLoGainSignal[khi];
527 sum = khicont;
528 abmaxpos = lower;
529
530 //
531 // Search for the maximum, starting in interval maxpos-1. If no maximum is found, go to
532 // interval maxpos+1.
533 //
534 while (x<upper-0.3)
535 {
536
537 x += step;
538 a -= step;
539 b += step;
540
541 y = a*klocont
542 + b*khicont
543 + (a*a*a-a)*fLoGainSecondDeriv[klo]
544 + (b*b*b-b)*fLoGainSecondDeriv[khi];
545
546 if (y > sum)
547 {
548 sum = y;
549 abmaxpos = x;
550 }
551 }
552
553 if (abmaxpos > upper-0.1)
554 {
555
556 upper = (Float_t)maxpos+1;
557 lower = (Float_t)maxpos;
558 x = lower;
559 a = 1.;
560 b = 0.;
561 khi = maxpos+1;
562 klo = maxpos;
563 klocont = fLoGainSignal[klo];
564 khicont = fLoGainSignal[khi];
565
566 while (x<upper-0.3)
567 {
568
569 x += step;
570 a -= step;
571 b += step;
572
573 y = a* klocont
574 + b* khicont
575 + (a*a*a-a)*fLoGainSecondDeriv[klo]
576 + (b*b*b-b)*fLoGainSecondDeriv[khi];
577
578 if (y > sum)
579 {
580 sum = y;
581 abmaxpos = x;
582 }
583 }
584 }
585
586 const Float_t up = abmaxpos+step-0.055;
587 const Float_t lo = abmaxpos-step+0.055;
588 const Float_t maxpossave = abmaxpos;
589
590 x = abmaxpos;
591 a = upper - x;
592 b = x - lower;
593
594 step = 0.04; // step size of 83 ps
595
596 while (x<up)
597 {
598
599 x += step;
600 a -= step;
601 b += step;
602
603 y = a* klocont
604 + b* khicont
605 + (a*a*a-a)*fLoGainSecondDeriv[klo]
606 + (b*b*b-b)*fLoGainSecondDeriv[khi];
607
608 if (y > sum)
609 {
610 sum = y;
611 abmaxpos = x;
612 }
613 }
614
615 if (abmaxpos < klo + 0.02)
616 {
617 klo--;
618 khi--;
619 klocont = fLoGainSignal[klo];
620 khicont = fLoGainSignal[khi];
621 upper--;
622 lower--;
623 }
624
625 x = maxpossave;
626 a = upper - x;
627 b = x - lower;
628
629 while (x>lo)
630 {
631
632 x -= step;
633 a += step;
634 b -= step;
635
636 y = a* klocont
637 + b* khicont
638 + (a*a*a-a)*fLoGainSecondDeriv[klo]
639 + (b*b*b-b)*fLoGainSecondDeriv[khi];
640
641 if (y > sum)
642 sum = y;
643 }
644}
645
Note: See TracBrowser for help on using the repository browser.