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

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