source: trunk/MagicSoft/Mars/mextralgo/MExtralgoDigitalFilter.h@ 8220

Last change on this file since 8220 was 8154, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 5.1 KB
Line 
1#ifndef MARS_MExtralgoDigitalFilter
2#define MARS_MExtralgoDigitalFilter
3
4#ifndef ROOT_TMatrix
5#include <TMatrix.h>
6#endif
7
8class TH1;
9class TH2;
10class TH1F;
11class TH2F;
12class TArrayF;
13
14//#include <TMatrix.h>
15
16class MExtralgoDigitalFilter
17{
18private:
19 // Input
20 const Float_t *fVal;
21 Int_t fNum;
22
23 Float_t const *fWeightsAmp;
24 Float_t const *fWeightsTime;
25 Float_t const *fPulseShape;
26
27 const TMatrix *fAinv;
28
29 const Int_t fWeightsPerBin; // Number of weights per data bin
30 const Int_t fWindowSize;
31
32 // Result
33 Float_t fTime;
34 Float_t fTimeDev;
35 Float_t fSignal;
36 Float_t fSignalDev;
37
38 Float_t GetChisq(const Int_t maxp, const Int_t frac, const Float_t sum) const;
39
40 inline Double_t ChiSq(const Double_t sum, const Int_t startv, const Int_t startw=0) const
41 {
42 //
43 // Slide with a window of size windowsize over the sample
44 // and multiply the entries with the corresponding weights
45 //
46 Double_t chisq = 0;
47
48 // Shift the start of the weight to the center of sample 0
49 Float_t const *w = fPulseShape + startw;
50
51 const Float_t *beg = fVal+startv;
52 for (Float_t const *pex=beg; pex<beg+fWindowSize; pex++)
53 {
54 const Double_t c = *w - *pex/sum;
55 chisq += c*c;
56 w += fWeightsPerBin;
57 }
58 return chisq;
59 }
60
61 // Weights: Weights to evaluate
62 // Startv: Index of first bin of data
63 // startw: Offset on the weights
64 inline Double_t Eval(Float_t const *weights, const Int_t startv, const Int_t startw=0) const
65 {
66 //
67 // Slide with a window of size windowsize over the sample
68 // and multiply the entries with the corresponding weights
69 //
70 Double_t sum = 0;
71
72 // Shift the start of the weight to the center of sample 0
73 Float_t const *w = weights + startw;
74
75 const Float_t *beg = fVal+startv;
76 for (Float_t const *pex=beg; pex<beg+fWindowSize; pex++)
77 {
78 sum += *w * *pex;
79 w += fWeightsPerBin;
80 }
81 return sum;
82 }
83
84 inline void AlignIntoLimits(Int_t &maxp, Int_t &frac) const
85 {
86 // Align maxp into available range (TO BE CHECKED)
87 if (maxp < 0)
88 {
89 maxp = 0;
90 frac = fWeightsPerBin/2-1; // Assume peak at the end of the last slice
91 }
92 if (maxp > fNum-fWindowSize)
93 {
94 maxp = fNum-fWindowSize;
95 frac = -fWeightsPerBin/2; // Assume peak at the beginning of the first slice
96 }
97 }
98
99 inline Int_t AlignExtractionWindow(Int_t &maxp, Int_t &frac, const Double_t ampsum)
100 {
101 // Align extraction window to signal position
102
103 const Double_t timesum = Eval(fWeightsTime, maxp, frac);
104
105 // Because fWeightsPerBin/2 doesn't correspond to the center
106 // of a bin the time-values extracted are slightly positive.
107 // They are roughly between -0.45 and 0.55
108 const Double_t binoffset = TMath::Even(fWeightsPerBin) ? 0.5 : 0;
109
110 // This is the time offset from the extraction position
111 Double_t tmoffset = (frac+binoffset)/fWeightsPerBin + timesum/ampsum;
112
113 // Convert the residual fraction of one slice into an
114 // offset position in the extraction weights
115 const Int_t integ = TMath::FloorNint(tmoffset+0.5);
116
117 /*
118 if (integ>0)
119 tmoffset=0.49-0.05;
120 if (integ<0)
121 tmoffset=-0.49-0.05;
122 integ=0;
123 */
124
125 // move the extractor by an offset number of slices
126 // determined by the extracted time
127 maxp -= integ;
128
129 frac = TMath::FloorNint((tmoffset-integ)*fWeightsPerBin);
130
131 // Align maxp into available range (TO BE CHECKED)
132 AlignIntoLimits(maxp, frac);
133
134 return integ;
135 }
136
137 inline void AlignExtractionWindow(Int_t &maxp, Int_t &frac)
138 {
139 const Double_t amp = Eval(fWeightsAmp, maxp, frac);
140 if (amp!=0)
141 AlignExtractionWindow(maxp, frac, amp);
142 }
143
144public:
145 MExtralgoDigitalFilter(Int_t res, Int_t windowsize, Float_t *wa, Float_t *wt, Float_t *ps=0, TMatrix *ainv=0)
146 : fVal(0), fNum(0), fWeightsAmp(wa+res/2), fWeightsTime(wt+res/2),
147 fPulseShape(ps), fAinv(ainv), fWeightsPerBin(res), fWindowSize(windowsize),
148 fTime(0), fTimeDev(-1), fSignal(0), fSignalDev(-1)
149 {
150 }
151
152 void SetData(Int_t n, Float_t const *val) { fNum=n; fVal=val; }
153
154 Float_t GetTime() const { return fTime; }
155 Float_t GetSignal() const { return fSignal; }
156
157 Float_t GetTimeDev() const { return fTimeDev; }
158 Float_t GetSignalDev() const { return fSignalDev; }
159
160 void GetSignal(Float_t &sig, Float_t &dsig) const { sig=fSignal; dsig=fSignalDev; }
161 void GetTime(Float_t &sig, Float_t &dsig) const { sig=fTime; dsig=fTimeDev; }
162
163 Float_t ExtractNoise(Int_t iter) const;
164 void Extract(Int_t maxpos=-1);
165
166 static Int_t CalculateWeights(TH1 &shape, const TH2 &autocorr, TArrayF &wa, TArrayF &wt, Int_t wpb=-1);
167 static Int_t CalculateWeights2(TH1 &shape, const TH2 &autocorr, TArrayF &wa, TArrayF &wt, Int_t wpb=-1);
168};
169
170
171#endif
Note: See TracBrowser for help on using the repository browser.