source: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeDigitalFilter.cc@ 6000

Last change on this file since 6000 was 5992, checked in by mazin, 20 years ago
*** empty log message ***
File size: 32.8 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 analysing 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!
18! Author(s): Hendrik Bartko, 09/2004 <mailto:hbartko@mppmu.mpg.de>
19! Author(s): Markus Gaug, 05/2004 <mailto:markus@ifae.es>
20! Author(s): Diego Tescaro, 05/2004 <mailto:tescaro@pd.infn.it>
21!
22! Copyright: MAGIC Software Development, 2000-2004
23!
24!
25\* ======================================================================== */
26//////////////////////////////////////////////////////////////////////////////
27//
28// MExtractTimeAndChargeDigitalFilter
29//
30// Hendrik has promised to write more documentation
31//
32//
33// The following variables have to be set by the derived class and
34// do not have defaults:
35// - fNumHiGainSamples
36// - fNumLoGainSamples
37// - fSqrtHiGainSamples
38// - fSqrtLoGainSamples
39//
40// Input Containers:
41// MRawEvtData
42// MRawRunHeader
43// MPedestalCam
44//
45// Output Containers:
46// MArrivalTimeCam
47// MExtractedSignalCam
48//
49//////////////////////////////////////////////////////////////////////////////
50#include "MExtractTimeAndChargeDigitalFilter.h"
51
52#include <errno.h>
53#include <fstream>
54
55#include <TFile.h>
56#include <TH1F.h>
57#include <TH2F.h>
58#include <TString.h>
59#include <TMatrix.h>
60
61#include "MLog.h"
62#include "MLogManip.h"
63
64#include "MPedestalPix.h"
65
66ClassImp(MExtractTimeAndChargeDigitalFilter);
67
68using namespace std;
69
70const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainFirst = 0;
71const Byte_t MExtractTimeAndChargeDigitalFilter::fgHiGainLast = 14;
72const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainFirst = 3;
73const Byte_t MExtractTimeAndChargeDigitalFilter::fgLoGainLast = 14;
74const Int_t MExtractTimeAndChargeDigitalFilter::fgWindowSizeHiGain = 6;
75const Int_t MExtractTimeAndChargeDigitalFilter::fgWindowSizeLoGain = 6;
76const Int_t MExtractTimeAndChargeDigitalFilter::fgBinningResolutionHiGain = 10;
77const Int_t MExtractTimeAndChargeDigitalFilter::fgBinningResolutionLoGain = 10;
78const Int_t MExtractTimeAndChargeDigitalFilter::fgSignalStartBinHiGain = 4;
79const Int_t MExtractTimeAndChargeDigitalFilter::fgSignalStartBinLoGain = 4;
80const TString MExtractTimeAndChargeDigitalFilter::fgNameWeightsFile = "msignal/cosmics_weights.dat";
81const Float_t MExtractTimeAndChargeDigitalFilter::fgOffsetLoGain = 1.8; // 5 ns
82// --------------------------------------------------------------------------
83//
84// Default constructor.
85//
86// Calls:
87// - SetWindowSize();
88// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
89// - SetBinningResolution();
90//
91// Sets all weights to 1.
92//
93MExtractTimeAndChargeDigitalFilter::MExtractTimeAndChargeDigitalFilter(const char *name, const char *title)
94 : fWeightsSet(kFALSE), fRandomIter(0)
95{
96 fName = name ? name : "MExtractTimeAndChargeDigitalFilter";
97 fTitle = title ? title : "Digital Filter";
98
99 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
100 SetWindowSize();
101 SetBinningResolution();
102 SetSignalStartBin();
103
104 SetNameWeightsFile();
105 SetOffsetLoGain(fgOffsetLoGain);
106}
107
108// ---------------------------------------------------------------------------------------
109//
110// Checks:
111// - if a window is bigger than the one defined by the ranges, set it to the available range
112//
113// Sets:
114// - fNumHiGainSamples to: (Float_t)fWindowSizeHiGain
115// - fNumLoGainSamples to: (Float_t)fWindowSizeLoGain
116//
117void MExtractTimeAndChargeDigitalFilter::SetWindowSize(Int_t windowh, Int_t windowl)
118{
119
120 if (windowh != fgWindowSizeHiGain)
121 *fLog << warn << GetDescriptor()
122 << ": ATTENTION!!! If you are not Hendrik Bartko, do NOT use a different window size than the default." << endl;
123 if (windowl != fgWindowSizeLoGain)
124 *fLog << warn << GetDescriptor()
125 << ": ATTENTION!!! If you are not Hendrik Bartko, do NOT use a different window size than the default" << endl;
126
127 fWindowSizeHiGain = windowh;
128 fWindowSizeLoGain = windowl;
129
130 const Int_t availhirange = (Int_t)(fHiGainLast-fHiGainFirst+1);
131
132 if (fWindowSizeHiGain > availhirange)
133 {
134 // Please simplify this!
135 *fLog << warn << GetDescriptor()
136 << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",fWindowSizeHiGain,
137 " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl;
138 fHiGainLast = fHiGainFirst + fWindowSizeHiGain;
139 *fLog << warn << GetDescriptor()
140 << ": Will set the upper range to: " << (int)fHiGainLast << endl;
141 }
142
143 if (fWindowSizeHiGain < 2)
144 {
145 fWindowSizeHiGain = 2;
146 *fLog << warn << GetDescriptor() << ": High Gain window size set to two samples" << endl;
147 }
148
149 if (fLoGainLast != 0 && fWindowSizeLoGain != 0)
150 {
151 const Int_t availlorange = (Int_t)(fLoGainLast-fLoGainFirst+1);
152
153 if (fWindowSizeLoGain > availlorange)
154 {
155 // Please simplify this!
156 *fLog << warn << GetDescriptor()
157 << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",fWindowSizeLoGain,
158 " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl;
159 fLoGainLast = fLoGainFirst + fWindowSizeLoGain;
160 *fLog << warn << GetDescriptor()
161 << ": Will set the upper range to: " << (int)fLoGainLast << endl;
162 }
163
164 if (fWindowSizeLoGain<2)
165 {
166 fWindowSizeLoGain = 2;
167 *fLog << warn << GetDescriptor() << ": Low Gain window size set to two samples" << endl;
168 }
169 }
170 //
171 // We need here the effective number of samples which is about 2.5 in the case of a window
172 // size of 6. The exact numbers have to be found still.
173 //
174 fNumHiGainSamples = (Float_t)fWindowSizeHiGain/2.4;
175 fNumLoGainSamples = (Float_t)fWindowSizeLoGain/2.4;
176 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
177 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
178
179}
180
181// --------------------------------------------------------------------------
182//
183// InitArrays
184//
185// Gets called in the ReInit() and initialized the arrays
186//
187Bool_t MExtractTimeAndChargeDigitalFilter::InitArrays()
188{
189
190 Int_t range = (Int_t)(fHiGainLast - fHiGainFirst + 1 + fHiLoLast);
191
192 fHiGainSignal.Set(range);
193
194 range = (Int_t)(fLoGainLast - fLoGainFirst + 1);
195
196 fLoGainSignal.Set(range);
197
198 if (!fWeightsSet)
199 if (!ReadWeightsFile(fNameWeightsFile))
200 return kFALSE;
201
202 fTimeShiftHiGain = (Float_t)fHiGainFirst + 0.5 + 1./fBinningResolutionHiGain;
203 fTimeShiftLoGain = 0.5 + 1./fBinningResolutionLoGain;
204 //
205 // We need here the effective number of samples which is about 2.5 in the case of a window
206 // size of 6. The exact numbers have to be found still.
207 //
208 fNumHiGainSamples = (Float_t)fWindowSizeHiGain/2.4;
209 fNumLoGainSamples = (Float_t)fWindowSizeLoGain/2.4;
210 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
211 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
212
213 return kTRUE;
214}
215
216void MExtractTimeAndChargeDigitalFilter::CalcBinningResArrays()
217{
218
219 fArrBinningResHiGain.Set(fWindowSizeHiGain);
220 fArrBinningResHalfHiGain.Set(fWindowSizeHiGain);
221
222 for (int i=0; i<fWindowSizeHiGain; i++)
223 {
224 fArrBinningResHiGain[i] = fBinningResolutionHiGain*i;
225 fArrBinningResHalfHiGain[i] = fArrBinningResHiGain[i] + fBinningResolutionHalfHiGain;
226 }
227
228 fArrBinningResLoGain.Set(fWindowSizeLoGain);
229 fArrBinningResHalfLoGain.Set(fWindowSizeLoGain);
230
231 for (int i=0; i<fWindowSizeLoGain; i++)
232 {
233 fArrBinningResLoGain[i] = fBinningResolutionLoGain*i;
234 fArrBinningResHalfLoGain[i] = fArrBinningResLoGain[i] + fBinningResolutionHalfLoGain;
235 }
236}
237
238// --------------------------------------------------------------------------
239//
240// Apply the digital filter algorithm to the high-gain slices.
241//
242void MExtractTimeAndChargeDigitalFilter::FindTimeAndChargeHiGain(Byte_t *ptr, Byte_t *logain, Float_t &sum, Float_t &dsum,
243 Float_t &time, Float_t &dtime,
244 Byte_t &sat, const MPedestalPix &ped, const Bool_t abflag)
245{
246
247 Int_t range = fHiGainLast - fHiGainFirst + 1;
248
249 const Byte_t *end = ptr + range;
250 Byte_t *p = ptr;
251 Byte_t maxpos = 0;
252
253 //
254 // Preparations for the pedestal subtraction (with AB-noise correction)
255 //
256 const Float_t pedes = ped.GetPedestal();
257 const Float_t ABoffs = ped.GetPedestalABoffset();
258
259 const Float_t pedmean[2] = { pedes + ABoffs, pedes - ABoffs };
260
261 range += fHiLoLast;
262 fMaxBinContent = 0;
263 //
264 // Check for saturation in all other slices
265 //
266 Int_t ids = fHiGainFirst;
267 Float_t *sample = fHiGainSignal.GetArray();
268 while (p<end)
269 {
270 *sample++ = (Float_t)*p - pedmean[(ids++ + abflag) & 0x1];
271
272 if (*p > fMaxBinContent)
273 {
274 maxpos = p-ptr;
275 if (maxpos > 1 && maxpos < (range - fWindowSizeHiGain + 1))
276 fMaxBinContent = *p;
277 }
278
279 if (*p++ >= fSaturationLimit)
280 if (!sat)
281 sat = ids-4;
282 }
283
284 if (fHiLoLast != 0)
285 {
286
287 end = logain + fHiLoLast;
288
289 while (logain<end)
290 {
291
292 *sample++ = (Float_t)*logain - pedmean[(ids++ + abflag) & 0x1];
293
294 if (*logain++ >= fSaturationLimit)
295 if (!sat)
296 sat = ids-4;
297 }
298 }
299
300 //
301 // allow no saturated slice
302 //
303 if (sat > 0)
304 return;
305
306 //
307 // Slide with a window of size fWindowSizeHiGain over the sample
308 // and multiply the entries with the corresponding weights
309 //
310 if (IsNoiseCalculation())
311 {
312 if (fRandomIter == fBinningResolutionHiGain)
313 fRandomIter = 0;
314 for (Int_t ids=0; ids < fWindowSizeHiGain; ids++)
315 {
316 const Int_t idx = fArrBinningResHiGain[ids] + fRandomIter;
317 sum += fAmpWeightsHiGain [idx]*fHiGainSignal[ids];
318 }
319 fRandomIter++;
320 return;
321 }
322
323 Float_t time_sum = 0.;
324 Float_t fmax = 0.;
325 Float_t ftime_max = 0.;
326 Int_t max_p = 0;
327
328 //
329 // Calculate the sum of the first fWindowSize slices
330 //
331 for (Int_t i=0;i<range-fWindowSizeHiGain+1;i++)
332 {
333 sum = 0.;
334 time_sum = 0.;
335
336 //
337 // Slide with a window of size fWindowSizeHiGain over the sample
338 // and multiply the entries with the corresponding weights
339 //
340 for (Int_t sample=0; sample < fWindowSizeHiGain; sample++)
341 {
342 const Int_t idx = fBinningResolutionHiGain*sample+fBinningResolutionHalfHiGain;
343 const Float_t pex = fHiGainSignal[sample+i];
344 sum += fAmpWeightsHiGain [idx]*pex;
345 time_sum += fTimeWeightsHiGain[idx]*pex;
346 }
347
348 if (sum>fmax)
349 {
350 fmax = sum;
351 ftime_max = time_sum;
352 max_p = i;
353 }
354 } /* for (Int_t i=0;i<range-fWindowSizeHiGain+1;i++) */
355
356 if (fmax==0)
357 return;
358
359 ftime_max /= fmax;
360 Int_t t_iter = Int_t(ftime_max*fBinningResolutionHiGain);
361 Int_t sample_iter = 0;
362
363 while ( t_iter > fBinningResolutionHalfHiGain-1 || t_iter < -fBinningResolutionHalfHiGain )
364 {
365 if (t_iter > fBinningResolutionHalfHiGain-1)
366 {
367 t_iter -= fBinningResolutionHiGain;
368 max_p--;
369 sample_iter--;
370 }
371 if (t_iter < -fBinningResolutionHalfHiGain)
372 {
373 t_iter += fBinningResolutionHiGain;
374 max_p++;
375 sample_iter++;
376 }
377 }
378
379 sum = 0.;
380 time_sum = 0.;
381 //
382 // Slide with a window of size fWindowSizeHiGain over the sample
383 // and multiply the entries with the corresponding weights
384 //
385 for (Int_t sample=0; sample < fWindowSizeHiGain; sample++)
386 {
387 const Int_t idx = fArrBinningResHalfHiGain[sample] + t_iter;
388 const Int_t ids = max_p + sample;
389 const Float_t pex = ids < 0 ? 0. : ( ids >= range ? 0. : fHiGainSignal[ids]);
390 sum += fAmpWeightsHiGain [idx]*pex;
391 time_sum += fTimeWeightsHiGain[idx]*pex;
392 }
393
394 if (sum == 0)
395 return;
396
397 time = max_p + fTimeShiftHiGain /* this shifts the time to the start of the rising edge */
398 - ((Float_t)t_iter)/fBinningResolutionHiGain;
399
400 const Float_t timefineadjust = time_sum/sum;
401
402 if (timefineadjust < 2./fBinningResolutionHiGain)
403 time -= timefineadjust;
404
405}
406
407// --------------------------------------------------------------------------
408//
409// Apply the digital filter algorithm to the low-gain slices.
410//
411void MExtractTimeAndChargeDigitalFilter::FindTimeAndChargeLoGain(Byte_t *ptr, Float_t &sum, Float_t &dsum,
412 Float_t &time, Float_t &dtime,
413 Byte_t &sat, const MPedestalPix &ped, const Bool_t abflag)
414{
415
416 const Int_t range = fLoGainLast - fLoGainFirst + 1;
417
418 const Byte_t *end = ptr + range;
419 Byte_t *p = ptr;
420 //
421 // Prepare the low-gain pedestal
422 //
423 const Float_t pedes = ped.GetPedestal();
424 const Float_t ABoffs = ped.GetPedestalABoffset();
425
426 const Float_t pedmean[2] = { pedes + ABoffs, pedes - ABoffs };
427
428 //
429 // Check for saturation in all other slices
430 //
431 Float_t *sample = fLoGainSignal.GetArray();
432 Int_t ids = fLoGainFirst;
433 while (p<end)
434 {
435 *sample++ = (Float_t)*p - pedmean[(ids++ + abflag) & 0x1];
436
437 if (*p++ >= fSaturationLimit)
438 sat++;
439 }
440
441 //
442 // Slide with a window of size fWindowSizeLoGain over the sample
443 // and multiply the entries with the corresponding weights
444 //
445 if (IsNoiseCalculation())
446 {
447 if (fRandomIter == fBinningResolutionLoGain)
448 fRandomIter = 0;
449 for (Int_t ids=0; ids < fWindowSizeLoGain; ids++)
450 {
451 const Int_t idx = fArrBinningResLoGain[ids] + fRandomIter;
452 sum += fAmpWeightsLoGain [idx]*fLoGainSignal[ids];
453 }
454 return;
455 }
456
457 Float_t time_sum = 0.;
458 Float_t fmax = 0.;
459 Float_t ftime_max = 0.;
460 Int_t max_p = 0;
461
462 //
463 // Calculate the sum of the first fWindowSize slices
464 //
465 for (Int_t i=0;i<range-fWindowSizeLoGain+1;i++)
466 {
467 sum = 0.;
468 time_sum = 0.;
469
470 //
471 // Slide with a window of size fWindowSizeLoGain over the sample
472 // and multiply the entries with the corresponding weights
473 //
474 for (Int_t sample=0; sample < fWindowSizeLoGain; sample++)
475 {
476 const Int_t idx = fArrBinningResHalfLoGain[sample];
477 const Float_t pex = fLoGainSignal[sample+i];
478 sum += fAmpWeightsLoGain [idx]*pex;
479 time_sum += fTimeWeightsLoGain[idx]*pex;
480 }
481
482 if (sum>fmax)
483 {
484 fmax = sum;
485 ftime_max = time_sum;
486 max_p = i;
487 }
488 } /* for (Int_t i=0;i<range-fWindowSizeLoGain+1;i++) */
489
490 time = 0;
491 if (fmax==0)
492 return;
493
494 ftime_max /= fmax;
495 Int_t t_iter = Int_t(ftime_max*fBinningResolutionLoGain);
496 Int_t sample_iter = 0;
497
498 while ( t_iter > fBinningResolutionHalfLoGain-1 || t_iter < -fBinningResolutionHalfLoGain )
499 {
500 if (t_iter > fBinningResolutionHalfLoGain-1)
501 {
502 t_iter -= fBinningResolutionLoGain;
503 max_p--;
504 sample_iter--;
505 }
506 if (t_iter < -fBinningResolutionHalfLoGain)
507 {
508 t_iter += fBinningResolutionLoGain;
509 max_p++;
510 sample_iter++;
511 }
512 }
513
514 sum = 0.;
515 time_sum = 0.;
516
517 //
518 // Slide with a window of size fWindowSizeLoGain over the sample
519 // and multiply the entries with the corresponding weights
520 //
521 for (Int_t sample=0; sample < fWindowSizeLoGain; sample++)
522 {
523 const Int_t idx = fArrBinningResHalfLoGain[sample] + t_iter;
524 const Int_t ids = max_p + sample;
525 const Float_t pex = ids < 0 ? 0. : ( ids >= range ? 0. : fLoGainSignal[ids]);
526 sum += fAmpWeightsLoGain [idx]*pex;
527 time_sum += fTimeWeightsLoGain[idx]*pex;
528 }
529
530 if (sum == 0)
531 return;
532
533 time = max_p + fTimeShiftLoGain + (Float_t)fLoGainFirst /* this shifts the time to the start of the rising edge */
534 - ((Float_t)t_iter)/fBinningResolutionLoGain - time_sum/sum;
535}
536
537// --------------------------------------------------------------------------
538//
539// Read the setup from a TEnv, eg:
540// MJPedestal.MExtractor.WindowSizeHiGain: 6
541// MJPedestal.MExtractor.WindowSizeLoGain: 6
542// MJPedestal.MExtractor.BinningResolutionHiGain: 10
543// MJPedestal.MExtractor.BinningResolutionLoGain: 10
544// MJPedestal.MExtractor.WeightsFile: filename
545//
546Int_t MExtractTimeAndChargeDigitalFilter::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
547{
548
549 Byte_t hw = fWindowSizeHiGain;
550 Byte_t lw = fWindowSizeLoGain;
551 Bool_t rc = kFALSE;
552
553 if (IsEnvDefined(env, prefix, "WindowSizeHiGain", print))
554 {
555 hw = GetEnvValue(env, prefix, "WindowSizeHiGain", hw);
556 rc = kTRUE;
557 }
558 if (IsEnvDefined(env, prefix, "WindowSizeLoGain", print))
559 {
560 lw = GetEnvValue(env, prefix, "WindowSizeLoGain", lw);
561 rc = kTRUE;
562 }
563
564 if (rc)
565 SetWindowSize(hw, lw);
566
567 Bool_t rc2 = kFALSE;
568 Int_t brh = fBinningResolutionHiGain;
569 Int_t brl = fBinningResolutionLoGain;
570
571 if (IsEnvDefined(env, prefix, "BinningResolutionHiGain", print))
572 {
573 brh = GetEnvValue(env, prefix, brh);
574 rc2 = kTRUE;
575 }
576 if (IsEnvDefined(env, prefix, "BinningResolutionLoGain", print))
577 {
578 brl = GetEnvValue(env, prefix, brl);
579 rc2 = kTRUE;
580 }
581
582 if (rc2)
583 {
584 SetBinningResolution(brh, brl);
585 rc = kTRUE;
586 }
587
588 if (IsEnvDefined(env, prefix, "WeightsFile", print))
589 {
590 if (!ReadWeightsFile(GetEnvValue(env, prefix, "WeightsFile", "")))
591 return kERROR;
592 rc = kTRUE;
593 }
594
595 return MExtractTimeAndCharge::ReadEnv(env, prefix, print) ? kTRUE : rc;
596}
597
598//----------------------------------------------------------------------------
599//
600// Read a pre-defined weights file into the class.
601// This is mandatory for the extraction
602//
603// If filenname is empty, then all weights will be set to 1.
604//
605Bool_t MExtractTimeAndChargeDigitalFilter::ReadWeightsFile(TString filename)
606{
607
608 // This is a fix for TEnv files edited with windows editors
609 filename.ReplaceAll("\015", "");
610
611 SetNameWeightsFile(filename);
612
613 fAmpWeightsHiGain .Set(fBinningResolutionHiGain*fWindowSizeHiGain);
614 fAmpWeightsLoGain .Set(fBinningResolutionLoGain*fWindowSizeLoGain);
615 fTimeWeightsHiGain.Set(fBinningResolutionHiGain*fWindowSizeHiGain);
616 fTimeWeightsLoGain.Set(fBinningResolutionLoGain*fWindowSizeLoGain);
617
618 if (fNameWeightsFile.IsNull())
619 {
620 fAmpWeightsHiGain.Reset(1);
621 fTimeWeightsHiGain.Reset(1);
622 fAmpWeightsLoGain.Reset(1);
623 fTimeWeightsLoGain.Reset(1);
624 return kTRUE;
625 }
626
627 ifstream fin(filename.Data());
628 if (!fin)
629 {
630 *fLog << err << GetDescriptor() << ": ERROR - Cannot open file " << filename << ": ";
631 *fLog << strerror(errno) << endl;
632 return kFALSE;
633 }
634
635 *fLog << inf << "Reading weights file " << filename << "..." << flush;
636
637 Int_t len = 0;
638 Int_t cnt = 0;
639 Int_t line = 0;
640 Bool_t hi = kFALSE;
641 Bool_t lo = kFALSE;
642
643 TString str;
644
645 while (1)
646 {
647 str.ReadLine(fin);
648 if (!fin)
649 break;
650
651 line++;
652
653 if (str.Contains("# High Gain Weights:"))
654 {
655 if (hi)
656 {
657 *fLog << err << "ERROR - 'High Gain Weights' found twice in line #" << line << "." << endl;
658 return kFALSE;
659 }
660
661 if (2!=sscanf(str.Data(), "# High Gain Weights:%2i %2i", &fWindowSizeHiGain, &fBinningResolutionHiGain))
662 {
663 *fLog << err << "ERROR - Wrong number of arguments in line #" << line << ":" << endl;
664 *fLog << str << endl;
665 return kFALSE;
666 }
667
668 len = fBinningResolutionHiGain*fWindowSizeHiGain;
669 fAmpWeightsHiGain .Set(len);
670 fTimeWeightsHiGain.Set(len);
671 hi = kTRUE;
672 continue;
673 }
674
675 if (str.Contains("# Low Gain Weights:"))
676 {
677 if (lo)
678 {
679 *fLog << err << "ERROR - 'Lo Gain Weights' found twice in line #" << line << "." << endl;
680 return kFALSE;
681 }
682
683 if (2!=sscanf(str.Data(),"# Low Gain Weights:%2i %2i", &fWindowSizeLoGain, &fBinningResolutionLoGain))
684 {
685 *fLog << err << "ERROR - Wrong number of arguments in line #" << line << ":" << endl;
686 *fLog << str << endl;
687 return kFALSE;
688 }
689
690 len = fBinningResolutionLoGain*fWindowSizeLoGain;
691 fAmpWeightsLoGain .Set(len);
692 fTimeWeightsLoGain.Set(len);
693 lo = kTRUE;
694 continue;
695 }
696
697 // Handle lines with comments
698 if (str.Contains("#"))
699 continue;
700
701 // Nothing found so far
702 if (len == 0)
703 continue;
704
705 if (2!=sscanf(str.Data(), "%f %f",
706 lo ? &fAmpWeightsLoGain [cnt] : &fAmpWeightsHiGain [cnt],
707 lo ? &fTimeWeightsLoGain[cnt] : &fTimeWeightsHiGain[cnt]))
708 {
709 *fLog << err << "ERROR - Wrong number of arguments in line #" << line << ":" << endl;
710 *fLog << str << endl;
711 return kFALSE;
712 }
713
714 if (++cnt == len)
715 {
716 len = 0;
717 cnt = 0;
718 }
719 }
720
721 if (cnt != len)
722 {
723 *fLog << err << "Size mismatch in weights file " << filename << endl;
724 return kFALSE;
725 }
726
727 if (!hi)
728 {
729 *fLog << err << "No correct header found in weights file " << filename << endl;
730 return kFALSE;
731 }
732
733 *fLog << "done." << endl;
734
735 *fLog << inf << " File contains " << fWindowSizeHiGain << " hi-gain slices ";
736 *fLog << "with a resolution of " << fBinningResolutionHiGain << endl;
737
738 *fLog << inf << " File contains " << fWindowSizeLoGain << " lo-gain slices ";
739 *fLog << "with a resolution of " << fBinningResolutionLoGain << endl;
740
741 CalcBinningResArrays();
742
743 fWeightsSet = kTRUE;
744
745 return kTRUE;
746}
747
748//----------------------------------------------------------------------------
749//
750// Create the weights file
751// Beware that the shape-histogram has to contain the pulse starting at bin 1
752//
753Bool_t MExtractTimeAndChargeDigitalFilter::WriteWeightsFile(TString filename, TH1F *shapehi, TH2F *autocorrhi,
754 TH1F *shapelo, TH2F *autocorrlo )
755{
756
757 const Int_t nbinshi = shapehi->GetNbinsX();
758 Float_t binwidth = shapehi->GetBinWidth(1);
759
760 TH1F *derivativehi = new TH1F(Form("%s%s",shapehi->GetName(),"_der"),
761 Form("%s%s",shapehi->GetTitle()," derivative"),
762 nbinshi,
763 shapehi->GetBinLowEdge(1),
764 shapehi->GetBinLowEdge(nbinshi)+binwidth);
765
766 //
767 // Calculate the derivative of shapehi
768 //
769 for (Int_t i = 1; i<nbinshi+1;i++)
770 {
771 derivativehi->SetBinContent(i,
772 ((shapehi->GetBinContent(i+1)-shapehi->GetBinContent(i-1))/2./binwidth));
773 derivativehi->SetBinError(i,
774 (sqrt(shapehi->GetBinError(i+1)*shapehi->GetBinError(i+1)
775 +shapehi->GetBinError(i-1)*shapehi->GetBinError(i-1))/2./binwidth));
776 }
777
778 //
779 // normalize the shapehi, such that the integral for fWindowSize slices is one!
780 //
781 Float_t sum = 0;
782 Int_t lasttemp = fBinningResolutionHiGain * (fSignalStartBinHiGain + fWindowSizeHiGain);
783 lasttemp = lasttemp > nbinshi ? nbinshi : lasttemp;
784
785 for (Int_t i=fBinningResolutionHiGain*fSignalStartBinHiGain; i<lasttemp; i++) {
786 sum += shapehi->GetBinContent(i);
787 }
788 sum /= fBinningResolutionHiGain;
789
790 shapehi->Scale(1./sum);
791 derivativehi->Scale(1./sum);
792
793 //
794 // read in the noise auto-correlation function:
795 //
796 TMatrix Bhi(fWindowSizeHiGain,fWindowSizeHiGain);
797
798 for (Int_t i=0; i<fWindowSizeHiGain; i++){
799 for (Int_t j=0; j<fWindowSizeHiGain; j++){
800 Bhi[i][j]=autocorrhi->GetBinContent(i+1,j+1); //+fSignalStartBinHiGain +fSignalStartBinHiGain
801 }
802 }
803 Bhi.Invert();
804
805 const Int_t nsizehi = fWindowSizeHiGain*fBinningResolutionHiGain;
806 fAmpWeightsHiGain.Set(nsizehi);
807 fTimeWeightsHiGain.Set(nsizehi);
808
809 //
810 // Loop over relative time in one BinningResolution interval
811 //
812 Int_t start = fBinningResolutionHiGain*(fSignalStartBinHiGain + 1);
813
814 for (Int_t i = -fBinningResolutionHalfHiGain+1; i<=fBinningResolutionHalfHiGain; i++)
815 {
816
817 TMatrix g(fWindowSizeHiGain,1);
818 TMatrix gT(1,fWindowSizeHiGain);
819 TMatrix d(fWindowSizeHiGain,1);
820 TMatrix dT(1,fWindowSizeHiGain);
821
822 for (Int_t count=0; count < fWindowSizeHiGain; count++){
823
824 g[count][0]=shapehi->GetBinContent(start
825 +fBinningResolutionHiGain*count+i);
826 gT[0][count]=shapehi->GetBinContent(start
827 +fBinningResolutionHiGain*count+i);
828 d[count][0]=derivativehi->GetBinContent(start
829 +fBinningResolutionHiGain*count+i);
830 dT[0][count]=derivativehi->GetBinContent(start
831 +fBinningResolutionHiGain*count+i);
832 }
833
834 TMatrix m_denom = (gT*(Bhi*g))*(dT*(Bhi*d)) - (dT*(Bhi*g))*(dT*(Bhi*g));
835 Float_t denom = m_denom[0][0]; // ROOT thinks, m_denom is still a matrix
836
837 TMatrix m_first = dT*(Bhi*d); // ROOT thinks, m_first is still a matrix
838 Float_t first = m_first[0][0]/denom;
839
840 TMatrix m_last = gT*(Bhi*d); // ROOT thinks, m_last is still a matrix
841 Float_t last = m_last[0][0]/denom;
842
843 TMatrix m1 = gT*Bhi;
844 m1 *= first;
845
846 TMatrix m2 = dT*Bhi;
847 m2 *=last;
848
849 TMatrix w_amp = m1 - m2;
850
851 TMatrix m_first1 = gT*(Bhi*g);
852 Float_t first1 = m_first1[0][0]/denom;
853
854 TMatrix m_last1 = gT*(Bhi*d);
855 Float_t last1 = m_last1 [0][0]/denom;
856
857 TMatrix m11 = dT*Bhi;
858 m11 *=first1;
859
860 TMatrix m21 = gT*Bhi;
861 m21 *=last1;
862
863 TMatrix w_time= m11 - m21;
864
865 for (Int_t count=0; count < fWindowSizeHiGain; count++)
866 {
867 const Int_t idx = i+fBinningResolutionHalfHiGain+fBinningResolutionHiGain*count-1;
868 fAmpWeightsHiGain [idx] = w_amp [0][count];
869 fTimeWeightsHiGain[idx] = w_time[0][count];
870 }
871
872 } // end loop over i
873
874 //
875 // Low Gain histograms
876 //
877 TH1F *derivativelo = NULL;
878 if (shapelo)
879 {
880 const Int_t nbinslo = shapelo->GetNbinsX();
881 binwidth = shapelo->GetBinWidth(1);
882
883 derivativelo = new TH1F(Form("%s%s",shapelo->GetName(),"_der"),
884 Form("%s%s",shapelo->GetTitle()," derivative"),
885 nbinslo,
886 shapelo->GetBinLowEdge(1),
887 shapelo->GetBinLowEdge(nbinslo)+binwidth);
888
889 //
890 // Calculate the derivative of shapelo
891 //
892 for (Int_t i = 1; i<nbinslo+1;i++)
893 {
894 derivativelo->SetBinContent(i,
895 ((shapelo->GetBinContent(i+1)-shapelo->GetBinContent(i-1))/2./binwidth));
896 derivativelo->SetBinError(i,
897 (sqrt(shapelo->GetBinError(i+1)*shapelo->GetBinError(i+1)
898 +shapelo->GetBinError(i-1)*shapelo->GetBinError(i-1))/2./binwidth));
899 }
900
901 //
902 // normalize the shapelo, such that the integral for fWindowSize slices is one!
903 //
904 sum = 0;
905 lasttemp = fBinningResolutionLoGain * (fSignalStartBinLoGain + fWindowSizeLoGain);
906 lasttemp = lasttemp > nbinslo ? nbinslo : lasttemp;
907
908 for (Int_t i=fBinningResolutionLoGain*fSignalStartBinLoGain; i<lasttemp; i++)
909 sum += shapelo->GetBinContent(i);
910
911 sum /= fBinningResolutionLoGain;
912
913 shapelo->Scale(1./sum);
914 derivativelo->Scale(1./sum);
915
916 //
917 // read in the noise auto-correlation function:
918 //
919 TMatrix Blo(fWindowSizeLoGain,fWindowSizeLoGain);
920
921 for (Int_t i=0; i<fWindowSizeLoGain; i++){
922 for (Int_t j=0; j<fWindowSizeLoGain; j++){
923 Blo[i][j]=autocorrlo->GetBinContent(i+1+fSignalStartBinLoGain,j+1+fSignalStartBinLoGain);
924 }
925 }
926 Blo.Invert();
927
928 const Int_t nsizelo = fWindowSizeLoGain*fBinningResolutionLoGain;
929 fAmpWeightsLoGain.Set(nsizelo);
930 fTimeWeightsLoGain.Set(nsizelo);
931
932 //
933 // Loop over relative time in one BinningResolution interval
934 //
935 Int_t start = fBinningResolutionLoGain*fSignalStartBinLoGain + fBinningResolutionHalfLoGain;
936
937 for (Int_t i = -fBinningResolutionHalfLoGain+1; i<=fBinningResolutionHalfLoGain; i++)
938 {
939
940 TMatrix g(fWindowSizeLoGain,1);
941 TMatrix gT(1,fWindowSizeLoGain);
942 TMatrix d(fWindowSizeLoGain,1);
943 TMatrix dT(1,fWindowSizeLoGain);
944
945 for (Int_t count=0; count < fWindowSizeLoGain; count++){
946
947 g[count][0] = shapelo->GetBinContent(start
948 +fBinningResolutionLoGain*count+i);
949 gT[0][count]= shapelo->GetBinContent(start
950 +fBinningResolutionLoGain*count+i);
951 d[count][0] = derivativelo->GetBinContent(start
952 +fBinningResolutionLoGain*count+i);
953 dT[0][count]= derivativelo->GetBinContent(start
954 +fBinningResolutionLoGain*count+i);
955 }
956
957 TMatrix m_denom = (gT*(Blo*g))*(dT*(Blo*d)) - (dT*(Blo*g))*(dT*(Blo*g));
958 Float_t denom = m_denom[0][0]; // ROOT thinks, m_denom is still a matrix
959
960 TMatrix m_first = dT*(Blo*d); // ROOT thinks, m_first is still a matrix
961 Float_t first = m_first[0][0]/denom;
962
963 TMatrix m_last = gT*(Blo*d); // ROOT thinks, m_last is still a matrix
964 Float_t last = m_last[0][0]/denom;
965
966 TMatrix m1 = gT*Blo;
967 m1 *= first;
968
969 TMatrix m2 = dT*Blo;
970 m2 *=last;
971
972 TMatrix w_amp = m1 - m2;
973
974 TMatrix m_first1 = gT*(Blo*g);
975 Float_t first1 = m_first1[0][0]/denom;
976
977 TMatrix m_last1 = gT*(Blo*d);
978 Float_t last1 = m_last1 [0][0]/denom;
979
980 TMatrix m11 = dT*Blo;
981 m11 *=first1;
982
983 TMatrix m21 = gT*Blo;
984 m21 *=last1;
985
986 TMatrix w_time= m11 - m21;
987
988 for (Int_t count=0; count < fWindowSizeLoGain; count++)
989 {
990 const Int_t idx = i+fBinningResolutionHalfLoGain+fBinningResolutionLoGain*count-1;
991 fAmpWeightsLoGain [idx] = w_amp [0][count];
992 fTimeWeightsLoGain[idx] = w_time[0][count];
993 }
994
995 } // end loop over i
996 }
997
998 ofstream fn(filename.Data());
999
1000 fn << "# High Gain Weights: " << fWindowSizeHiGain << " " << fBinningResolutionHiGain << endl;
1001 fn << "# (Amplitude) (Time) " << endl;
1002
1003 for (Int_t i=0; i<nsizehi; i++)
1004 fn << "\t" << fAmpWeightsHiGain[i] << "\t" << fTimeWeightsHiGain[i] << endl;
1005
1006 fn << "# Low Gain Weights: " << fWindowSizeLoGain << " " << fBinningResolutionLoGain << endl;
1007 fn << "# (Amplitude) (Time) " << endl;
1008
1009 for (Int_t i=0; i<nsizehi; i++)
1010 fn << "\t" << fAmpWeightsLoGain[i] << "\t" << fTimeWeightsLoGain[i] << endl;
1011
1012 delete derivativehi;
1013 if (derivativelo)
1014 delete derivativelo;
1015
1016 return kTRUE;
1017}
1018
1019void MExtractTimeAndChargeDigitalFilter::Print(Option_t *o) const
1020{
1021 if (IsA()==Class())
1022 *fLog << GetDescriptor() << ":" << endl;
1023
1024 MExtractTimeAndCharge::Print(o);
1025 *fLog << " Time Shift HiGain: " << fTimeShiftHiGain << " LoGain: " << fTimeShiftLoGain << endl;
1026 *fLog << " Window Size HiGain: " << fWindowSizeHiGain << " LoGain: " << fWindowSizeLoGain << endl;
1027 *fLog << " Binning Res HiGain: " << fBinningResolutionHiGain << " LoGain: " << fBinningResolutionHiGain << endl;
1028 *fLog << " Weights File: " << fNameWeightsFile << endl;
1029
1030 TString opt(o);
1031 if (!opt.Contains("weights"))
1032 return;
1033
1034 *fLog << endl;
1035 *fLog << inf << "Using the following weights: " << endl;
1036 *fLog << "Hi-Gain:" << endl;
1037 for (Int_t i=0; i<fBinningResolutionHiGain*fWindowSizeHiGain; i++)
1038 *fLog << " " << fAmpWeightsHiGain[i] << " \t " << fTimeWeightsHiGain[i] << endl;
1039
1040 *fLog << "Lo-Gain:" << endl;
1041 for (Int_t i=0; i<fBinningResolutionLoGain*fWindowSizeLoGain; i++)
1042 *fLog << " " << fAmpWeightsLoGain[i] << " \t " << fTimeWeightsLoGain[i] << endl;
1043}
Note: See TracBrowser for help on using the repository browser.