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

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