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

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