CMS 3D CMS Logo

PulseFitWithShape.cc
Go to the documentation of this file.
1 /*
2  * \class PulseFitWithShape
3  *
4  * \author: Julie Malcles - CEA/Saclay
5  */
6 
7 
8 // File PulseFitWithShape.cxx
9 
11 
12 #include <iostream>
13 #include "TMath.h"
14 #include <cmath>
15 
16 //ClassImp(PulseFitWithShape)
17 
18 
19 // Constructor...
21 {
22 
23  fNsamples = 0;
24  fNsamplesShape = 0;
27  fNoise = 0.0;
28 }
29 
30 // Destructor
32 {
33 }
34 
35 // Initialisation
36 
37 void PulseFitWithShape::init(int n_samples,int samplb,int sampla,int niter,int n_samplesShape, const std::vector<double>& shape, double nois)
38 {
39 
40  fNsamples = n_samples ;
41  fNsamplesShape = n_samplesShape ;
42  fNb_iter = niter ;
43  fNum_samp_bef_max = samplb ;
44  fNum_samp_after_max = sampla ;
45 
46 
48  std::cout<<"PulseFitWithShape::init: Error! Configuration samples in fit greater than total number of samples!" << std::endl;
49  }
50 
51  for(int i=0;i<fNsamplesShape;i++){
52  pshape.push_back(shape[i]);
53  dshape.push_back(0.0);
54  }
55 
56  fNoise=nois;
57  return ;
58  }
59 
60 // Compute the amplitude using as input the Crystaldata
61 
62 double PulseFitWithShape::doFit(double *adc, double *cova)
63 {
64 
65  // xpar = fit paramaters
66  // [0] = signal amplitude
67  // [1] = residual pedestal
68  // [2] = clock phase
69 
70  bool useCova=true;
71  if(cova==nullptr) useCova=false;
72 
73  double xpar[3];
74  double chi2;
75 
76  fAmp_fitted_max = 0. ;
77  fTim_fitted_max = 0. ;
78 
79  // for now don't fit pedestal
80 
81  xpar[1]=0.0;
82 
83  // Sample noise. If the cova matrix is defined, use it :
84 
85  double noise=fNoise;
86  //if(cova[0] > 0.) noise=1./sqrt(cova[0]);
87 
88  xpar[0]=0.;
89  xpar[2]=0.;
90 
91 
92  // first locate max:
93 
94  int imax=0;
95  double amax=0.0;
96  for(int i=0; i<fNsamples; i++){
97  if (adc[i]>amax){
98  amax=adc[i];
99  imax=i;
100  }
101  }
102 
103  // Shift pulse shape and calculate its derivative:
104 
105  double qms=0.;
106  int ims=0;
107 
108  // 1) search for maximum
109 
110  for(int is=0; is<fNsamplesShape; is++){
111  if(pshape[is] > qms){
112  qms=pshape[is];
113  ims=is;
114  }
115 
116  // 2) compute shape derivative :
117 
118  if(is < fNsamplesShape-2)
119  dshape[is]= (pshape[is+2]-pshape[is])*12.5;
120  else
121  dshape[is]=dshape[is-1];
122  }
123 
124  // 3) compute pol2 max
125 
126  double sq1=pshape[ims-1];
127  double sq2=pshape[ims];
128  double sq3=pshape[ims+1];
129 
130  double a2=(sq3+sq1)/2.0-sq2;
131  double a1=sq2-sq1+a2*(1-2*ims);
132 
133 
134  double t_ims=0;
135  if(a2!=0) t_ims=-a1/(2.0*a2);
136 
137 
138  // Starting point of the fit : qmax and tmax given by a
139  // P2 fit on 3 max samples.
140 
141  double qm=0.;
142  int im=0;
143 
144  int nsamplef=fNum_samp_bef_max + fNum_samp_after_max +1 ; // number of samples used in the fit
145  int nsampleo=imax-fNum_samp_bef_max; // first sample number = sample max-fNum_samp_bef_max
146 
147  for(int is=0; is<nsamplef; is++){
148 
149  if(adc[is+nsampleo] > qm){
150  qm=adc[is+nsampleo];
151  im=nsampleo+is;
152  }
153  }
154 
155  double tm;
156  if(qm > 5.*noise){
157  if(im >= nsamplef+nsampleo) im=nsampleo+nsamplef-1;
158  double q1=adc[im-1];
159  double q2=adc[im];
160  double q3=adc[im+1];
161  double y2=(q1+q3)/2.-q2;
162  double y1=q2-q1+y2*(1-2*im);
163  double y0=q2-y1*(double)im-y2*(double)(im*im);
164  tm=-y1/2./y2;
165  xpar[0]=y0+y1*tm+y2*tm*tm;
166  xpar[2]=(double)ims/25.-tm;
167  }
168 
169  double chi2old=999999.;
170  chi2=99999.;
171  int nsfit=nsamplef;
172  int iloop=0;
173  int nloop=fNb_iter;
174  if(qm <= 5*noise)nloop=1; // Just one iteration for very low signal
175 
176  std::vector<double> resi(fNsamples, 0.0);
177 
178  while(std::fabs(chi2old-chi2) > 0.1 && iloop < nloop)
179  {
180  iloop++;
181  chi2old=chi2;
182 
183  double c=0.;
184  double y1=0.;
185  double s1=0.;
186  double s2=0.;
187  double ys1=0.;
188  double sp1=0.;
189  double sp2=0.;
190  double ssp=0.;
191  double ysp=0.;
192 
193  for(int is=0; is<nsfit; is++)
194  {
195  int iis=is;
196  iis=is+nsampleo;
197 
198  double t1=(double)iis+xpar[2];
199  double xbin=t1*25.;
200  int ibin1=(int)xbin;
201 
202  if(ibin1 < 0) ibin1=0;
203 
204  double amp1,amp11,amp12,der1,der11,der12;
205 
206  if(ibin1 <= fNsamplesShape-2){ // Interpolate shape values to get the right number :
207 
208  int ibin2=ibin1+1;
209  double xfrac=xbin-ibin1;
210  amp11=pshape[ibin1];
211  amp12=pshape[ibin2];
212  amp1=(1.-xfrac)*amp11+xfrac*amp12;
213  der11=dshape[ibin1];
214  der12=dshape[ibin2];
215  der1=(1.-xfrac)*der11+xfrac*der12;
216 
217  }else{ // Or extraoplate if we are outside the array :
218 
219  amp1=pshape[fNsamplesShape-1]+dshape[fNsamplesShape-1]*
220  (xbin-double(fNsamplesShape-1))/25.;
221  der1=dshape[fNsamplesShape-1];
222  }
223 
224  if( useCova ){ // Use covariance matrix:
225  for(int js=0; js<nsfit; js++)
226  {
227  int jjs=js;
228  jjs+=nsampleo;
229 
230  t1=(double)jjs+xpar[2];
231  xbin=t1*25.;
232  ibin1=(int)xbin;
233  if(ibin1 < 0) ibin1=0;
234  if(ibin1 > fNsamplesShape-2)ibin1=fNsamplesShape-2;
235  int ibin2=ibin1+1;
236  double xfrac=xbin-ibin1;
237  amp11=pshape[ibin1];
238  amp12=pshape[ibin2];
239  double amp2=(1.-xfrac)*amp11+xfrac*amp12;
240  der11=dshape[ibin1];
241  der12=dshape[ibin2];
242  double der2=(1.-xfrac)*der11+xfrac*der12;
243  c=c+cova[js*fNsamples+is];
244  y1=y1+adc[iis]*cova[js*fNsamples+is];
245  s1=s1+amp1*cova[js*fNsamples+is];
246  s2=s2+amp1*amp2*cova[js*fNsamples+is];
247  ys1=ys1+adc[iis]*amp2*cova[js*fNsamples+is];
248  sp1=sp1+der1*cova[is*fNsamples+js];
249  sp2=sp2+der1*der2*cova[js*fNsamples+is];
250  ssp=ssp+amp1*der2*cova[js*fNsamples+is];
251  ysp=ysp+adc[iis]*der2*cova[js*fNsamples+is];
252  }
253  }else { // Don't use covariance matrix:
254  c++;
255  y1=y1+adc[iis];
256  s1=s1+amp1;
257  s2=s2+amp1*amp1;
258  ys1=ys1+amp1*adc[iis];
259  sp1=sp1+der1;
260  sp2=sp2+der1*der1;
261  ssp=ssp+der1*amp1;
262  ysp=ysp+der1*adc[iis];
263  }
264  }
265  xpar[0]=(ysp*ssp-ys1*sp2)/(ssp*ssp-s2*sp2);
266  xpar[2]+=(ysp/xpar[0]/sp2-ssp/sp2);
267 
268  for(int is=0; is<nsfit; is++){
269  int iis=is;
270  iis=is+nsampleo;
271 
272  double t1=(double)iis+xpar[2];
273  double xbin=t1*25.;
274  int ibin1=(int)xbin;
275  if(ibin1 < 0) ibin1=0;
276 
277  if(ibin1 < 0) ibin1=0;
278  if(ibin1 > fNsamplesShape-2)ibin1=fNsamplesShape-2;
279  int ibin2=ibin1+1;
280  double xfrac=xbin-ibin1;
281  double amp11=xpar[0]*pshape[ibin1];
282  double amp12=xpar[0]*pshape[ibin2];
283  double amp1=xpar[1]+(1.-xfrac)*amp11+xfrac*amp12;
284  resi[iis]=adc[iis]-amp1;
285  }
286 
287  chi2=0.;
288  for(int is=0; is<nsfit; is++){
289  int iis=is;
290  iis+=nsampleo;
291 
292  if( useCova ){
293  for(int js=0; js<nsfit; js++){
294  int jjs=js;
295  jjs+=nsampleo;
296  chi2+=resi[iis]*resi[jjs]*cova[js*fNsamples+is];
297  }
298 
299  }else chi2+=resi[iis]*resi[iis];
300  }
301  }
302 
303  fAmp_fitted_max = xpar[0];
304  fTim_fitted_max = (double)t_ims/25.-xpar[2];
305 
306  return chi2 ;
307 
308 }
309 
int adc(sample_type sample)
get the ADC sample (12 bits)
std::vector< double > pshape
double q2[4]
Definition: TauolaWrapper.h:88
return((rh^lh)&mask)
virtual double doFit(double *, double *cova=nullptr)
virtual void init(int, int, int, int, int, const std::vector< double > &, double)
double q1[4]
Definition: TauolaWrapper.h:87
std::vector< double > dshape
~PulseFitWithShape() override