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

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