source: trunk/MagicSoft/Mars/mextralgo/MExtralgoDigitalFilter.cc@ 8094

Last change on this file since 8094 was 8072, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 12.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): Thomas Bretz, 08/2006 <mailto:tbretz@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2006
22!
23!
24\* ======================================================================== */
25
26//////////////////////////////////////////////////////////////////////////////
27//
28// MExtralgoDigitalFilter
29//
30//////////////////////////////////////////////////////////////////////////////
31#include "MExtralgoDigitalFilter.h"
32
33using namespace std;
34
35Float_t MExtralgoDigitalFilter::ExtractNoise(Int_t iter) const
36{
37 return Eval(fWeightsAmp, 0, iter-fWeightsPerBin/2);
38}
39
40#include <iostream>
41void MExtralgoDigitalFilter::Extract()
42{
43 fSignal = 0; // default is: no pulse found
44 fTime = -1; // default is: out if range (--> random)
45 fSignalDev = 0; // default is: valid
46 fTimeDev = 0; // default is: valid
47
48 // FIXME: How to handle saturation?
49
50 Double_t maxamp = -FLT_MAX;
51 Int_t maxp = -1;
52
53 //
54 // Calculate the sum of the first fWindowSize slices
55 //
56
57 // For the case of an even numberof weights/bin there is
58 // no central bin.So we create an artificial central bin.
59 for (Int_t i=0; i<fNum-fWindowSize+1; i++)
60 {
61 const Double_t sumamp = Eval(fWeightsAmp, i);
62 if (sumamp>maxamp)
63 {
64 maxamp = sumamp;
65 maxp = i;
66 }
67 }
68 // The number of available slices were smaller than the
69 // extraction window size of the extractor
70 if (maxp<0)
71 {
72 fSignalDev = -1; // means: is invalid
73 fTimeDev = -1; // means: is invalid
74 return;
75 }
76
77 // For some reason (by chance or because all slices contain 0)
78 // maxamp is 0. This means the signal is zero and no arrival
79 // time can be extracted (but both informations are valid)
80 if (maxamp==0)
81 return;
82
83 Int_t frac = 0;
84 const Int_t shift = AlignExtractionWindow(maxp, frac, maxamp);
85
86 // For safety we do another iteration if we have
87 // shifted the extraction window
88 if (TMath::Abs(shift)>0)
89 AlignExtractionWindow(maxp, frac);
90
91 // Now we have found the "final" position: extract time and charge
92 const Double_t sumamp = Eval(fWeightsAmp, maxp, frac);
93
94 fSignal = sumamp;
95 if (sumamp == 0)
96 return;
97
98 const Double_t sumtime = Eval(fWeightsTime, maxp, frac);
99
100 // This is used to align the weights to bins between
101 // -0.5/fWeightsPerBin and 0.5/fWeightsPerBin instead of
102 // 0 and 1./fWeightsPerBin
103 const Double_t binoffset = TMath::Even(fWeightsPerBin) ? 0.5 : 0;
104
105 fTime = maxp /*- 0.5*/ - Double_t(frac+binoffset)/fWeightsPerBin;
106
107 // To let the lowest value which can be safely extracted be>0:
108 // Take also a possible offset from timefineadjust into account
109 // Sould it be: fTime += fWindowSize/2; ???
110 fTime += 0.5 + 0.5/fWeightsPerBin;
111 // In the ideal case the would never get <0
112 // But timefineadjust can be >0.05 (in 60% of the cases)
113
114 // Define in each extractor a lowest and highest extracted value!
115
116 // here, the first high-gain slice is already included
117 // in the fTimeShiftHiGain this shifts the time to the
118 // start of the rising edge
119
120 // This is from MExtractTimeAndChargeDigitalFilter
121 // fTime += 0.5 + 1./fWeightsPerBin;
122
123 // Now there is a difference between the rising edge and
124 // the extracted time. This must be applied after
125 // the randomization in case of wrog values
126
127 const Float_t timefineadjust = sumtime/sumamp;
128
129 // FIXME: Is 3 a good value?
130 //if (TMath::Abs(timefineadjust)*fWeightsPerBin < 3.)
131 // fTime -= timefineadjust;
132
133 // if (TMath::FloorNint(timefineadjust+0.5)==0)
134
135 //if (TMath::Abs(timefineadjust) < 0.2)
136 fTime -= timefineadjust;
137}
138
139#include <TH1.h>
140#include <TH2.h>
141#include <TMatrixD.h>
142#include <TArrayF.h>
143#include <iostream>
144#include <TSpline.h>
145#include <TProfile.h>
146
147Bool_t MExtralgoDigitalFilter::CalculateWeights(TH1 &shape, const TH2 &autocorr, TArrayF &weightsamp, TArrayF &weightstime, Int_t wpb)
148{
149 const Int_t weightsperbin = wpb<=0?shape.GetNbinsX()/autocorr.GetNbinsX():wpb;
150
151 if (wpb<=0 && weightsperbin*autocorr.GetNbinsX()!=shape.GetNbinsX())
152 {
153 cout << "ERROR - Number of bins mismatch..." << endl;
154 cout << " Shape: " << shape.GetNbinsX() << endl;
155 cout << " ACorr: " << autocorr.GetNbinsX() << endl;
156 return kFALSE;
157 }
158
159 const TAxis &axe = *shape.GetXaxis();
160
161 const Int_t first = axe.GetFirst()/weightsperbin;
162 const Int_t last = axe.GetLast() /weightsperbin;
163
164 const Int_t width = last-first;
165
166 cout << "Range: " << first << " <= bin < " << last << endl;
167 cout << "Window: " << width << endl;
168 cout << "W/Bin: " << weightsperbin << endl;
169
170 // ---------------------------------------------
171
172 const Float_t sum = shape.Integral(first*weightsperbin, last*weightsperbin-1)/weightsperbin;
173 shape.Scale(1./sum);
174
175 cout << "Sum: " << sum << endl;
176
177// TGraph gr(&shape);
178// TSpline5 val("Signal", &gr);
179
180 // FIXME: DELETE!!!
181 TH1 &derivative = *static_cast<TH1*>(shape.Clone());
182 derivative.Reset();
183
184 for (int i=0; i<derivative.GetNbinsX(); i++)
185 {
186// const Float_t x = derivative.GetBinCenter(i+1);
187// derivative.SetBinContent(i+1, val.Derivative(x));
188
189 const Float_t binm = shape.GetBinContent(i+1-1);
190 const Float_t binp = shape.GetBinContent(i+1+1);
191
192 const Float_t der = (binp-binm)/2./shape.GetBinWidth(1);
193
194 derivative.SetBinContent(i+1, der);
195
196 if (derivative.InheritsFrom(TProfile::Class()))
197 static_cast<TProfile&>(derivative).SetBinEntries(i+1,1);
198 }
199
200 // ---------------------------------------------
201
202 TMatrixD B(width, width);
203 for (Int_t i=0; i<width; i++)
204 for (Int_t j=0; j<width; j++)
205 B[i][j]=autocorr.GetBinContent(i+1/*first*/, j+1/*first*/);
206
207 const TMatrixD Binv(TMatrixD::kInverted, B);
208
209 // ---------------------------------------------
210
211 weightsamp.Set(width*weightsperbin);
212 weightstime.Set(width*weightsperbin);
213
214 for (Int_t i=0; i<weightsperbin; i++)
215 {
216 TMatrixD g(width, 1);
217 TMatrixD d(width, 1);
218
219 for (Int_t bin=0; bin<width; bin++)
220 {
221 const Int_t idx = weightsperbin*(bin+first) + i;
222
223 g[bin][0]=shape.GetBinContent(idx+1);
224 d[bin][0]=derivative.GetBinContent(idx+1);
225 }
226
227 const TMatrixD gT(TMatrixD::kTransposed, g);
228 const TMatrixD dT(TMatrixD::kTransposed, d);
229
230 const TMatrixD denom = (gT*(Binv*g))*(dT*(Binv*d)) - (dT*(Binv*g))*(dT*(Binv*g));
231
232 if (denom[0][0]==0)
233 {
234 cout << "ERROR - Division by zero: denom[0][0]==0 for i=" << i << "." << endl;
235 return kFALSE;
236 }
237
238 const TMatrixD w_amp = (dT*(Binv*d))*(gT*Binv) - (gT*(Binv*d))*(dT*Binv);
239 const TMatrixD w_time = (gT*(Binv*g))*(dT*Binv) - (gT*(Binv*d))*(gT*Binv);
240
241 for (Int_t bin=0; bin<width; bin++)
242 {
243 const Int_t idx = weightsperbin*bin + i;
244
245 weightsamp[idx] = w_amp [0][bin]/denom[0][0];
246 weightstime[idx] = w_time[0][bin]/denom[0][0];
247 }
248 }
249
250 return kTRUE;
251}
252
253void MExtralgoDigitalFilter::CalculateWeights2(TH1F &shape, const TH2F &autocorr, TArrayF &weightsamp, TArrayF &weightstime, Int_t wpb)
254{
255 const Int_t weightsperbin = wpb<=0?shape.GetNbinsX()/autocorr.GetNbinsX():wpb;
256
257 if (wpb<=0 && weightsperbin*autocorr.GetNbinsX()!=shape.GetNbinsX())
258 {
259 cout << "ERROR - Number of bins mismatch..." << endl;
260 cout << " Shape: " << shape.GetNbinsX() << endl;
261 cout << " ACorr: " << autocorr.GetNbinsX() << endl;
262 return;
263 }
264
265 const TAxis &axe = *shape.GetXaxis();
266
267 const Int_t first = axe.GetFirst()/weightsperbin;
268 const Int_t last = axe.GetLast() /weightsperbin;
269
270 const Int_t width = last-first;
271
272 cout << "Range: " << first << " <= bin < " << last << endl;
273 cout << "Window: " << width << endl;
274 cout << "W/Bin: " << weightsperbin << endl;
275
276 // ---------------------------------------------
277
278 const Float_t sum = shape.Integral(first*weightsperbin, last*weightsperbin-1)/weightsperbin;
279 shape.Scale(1./sum);
280
281 TGraph gr(&shape);
282 TSpline5 val("Signal", &gr);
283
284 TH1F derivative(shape);
285 derivative.Reset();
286
287 for (int i=0; i<derivative.GetNbinsX(); i++)
288 {
289 const Float_t x = derivative.GetBinCenter(i+1);
290 derivative.SetBinContent(i+1, val.Derivative(x));
291
292 /*
293 const Float_t binm = shape.GetBinContent(i+1-1);
294 const Float_t binp = shape.GetBinContent(i+1+1);
295
296 const Float_t der = (binp-binm)/2./shape.GetBinWidth(1);
297
298 derivative.SetBinContent(i+1, der);
299 */
300 }
301
302 // ---------------------------------------------
303
304 TMatrixD B(width, width);
305 for (Int_t i=0; i<width; i++)
306 for (Int_t j=0; j<width; j++)
307 B[i][j]=autocorr.GetBinContent(i+first, j+first);
308 B.Invert();
309
310 // ---------------------------------------------
311
312 weightsamp.Set(width*weightsperbin);
313 weightstime.Set(width*weightsperbin);
314
315 for (Int_t i=0; i<weightsperbin; i++)
316 {
317 TMatrixD g(width, 1);
318 TMatrixD d(width, 1);
319
320 for (Int_t bin=0; bin<width; bin++)
321 {
322 const Int_t idx = weightsperbin*(bin+first) + i;
323
324 g[bin][0]=shape.GetBinContent(idx+1);
325 d[bin][0]=derivative.GetBinContent(idx+1);
326 }
327
328 const TMatrixD gT(TMatrixD::kTransposed, g);
329 const TMatrixD dT(TMatrixD::kTransposed, d);
330
331 const TMatrixD denom = (gT*(B*g))*(dT*(B*d)) - (dT*(B*g))*(dT*(B*g));
332
333 const TMatrixD w_amp = (dT*(B*d))*(gT*B) - (gT*(B*d))*(dT*B);
334 const TMatrixD w_time = (gT*(B*g))*(dT*B) - (gT*(B*d))*(gT*B);
335
336 for (Int_t bin=0; bin<width; bin++)
337 {
338 const Int_t idx = weightsperbin*bin + i;
339
340 weightsamp[idx] = w_amp [0][bin]/denom[0][0];
341 weightstime[idx] = w_time[0][bin]/denom[0][0];
342 }
343 }
344}
345/*
346Int_t start = fBinningResolutionHiGain*(fSignalStartBinHiGain + 1);
347for (Int_t i = -fBinningResolutionHiGain/2+1; i<=fBinningResolutionHiGain/2; i++)
348{
349 // i = -4...5
350 for (Int_t count=0; count < fWindowSizeHiGain; count++)
351 {
352 // count=0: pos=10*(start+0) + 10 + i
353 // count=1: pos=10*(start+1) + 10 + i
354 // count=2: pos=10*(start+2) + 10 + i
355 // count=3: pos=10*(start+3) + 10 + i
356 }
357
358 for (Int_t count=0; count < fWindowSizeHiGain; count++)
359 {
360 // count=0: pos = 10*0 + 5 +i-1
361 // count=1: pos = 10*1 + 5 +i-1
362 // count=2: pos = 10*2 + 5 +i-1
363 // count=3: pos = 10*3 + 5 +i-1
364 }
365}
366
367Int_t start = fBinningResolutionLoGain*fSignalStartBinLoGain + fBinningResolutionLoGain/2;
368for (Int_t i = -fBinningResolutionLoGain/2+1; i<=fBinningResolutionLoGain/2; i++)
369{
370 // i=-4..5
371 for (Int_t count=0; count < fWindowSizeLoGain; count++)
372 {
373 // count=0: pos = 10*(start+0) + 5 + i
374 // count=1: pos = 10*(start+1) + 5 + i
375 // count=2: pos = 10*(start+2) + 5 + i
376 // count=3: pos = 10*(start+3) + 5 + i
377 }
378
379 for (Int_t count=0; count < fWindowSizeLoGain; count++)
380 {
381 // count=0: pos = 10*0 + 5 +i-1
382 // count=1: pos = 10*1 + 5 +i-1
383 // count=2: pos = 10*2 + 5 +i-1
384 // count=3: pos = 10*3 + 5 +i-1
385 }
386}
387*/
Note: See TracBrowser for help on using the repository browser.