-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglePEAnalysis_LedScan_Simultaneous.C
306 lines (256 loc) · 8.39 KB
/
SinglePEAnalysis_LedScan_Simultaneous.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "TH1F.h"
#include "TF1.h"
#include "TFile.h"
#include "TString.h"
#include "TTree.h"
#include "TStyle.h"
#include "TGraphErrors.h"
#include "TCanvas.h"
#include "TMath.h"
#include "TH2D.h"
#include "TPaveText.h"
#include "TLegend.h"
#include "TRandom.h"
#include <vector>
#include <iostream>
#include "Fit/Fitter.h"
#include "Fit/BinData.h"
#include "Fit/Chi2FCN.h"
#include "TH1.h"
#include "TList.h"
#include "Math/WrappedMultiTF1.h"
#include "HFitInterface.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "Math/GSLMinimizer.h"
//#include "Style.C"
#define START_ORDER 0
#define NORDERS 15
#define OFFSET 755
struct FitResults {
std::vector<float> norm;
std::vector<float> mu;
std::vector<float> mu_err;
float Q1;
float Q1_err;
float Q1_sigma;
float Q1_sigma_err;
float ped;
float ped_err;
float ped_sigma;
float ped_sigma_err;
float min_sigma;
float min_sigma_err;
};
struct GlobalChi2 {
GlobalChi2( std::vector< ROOT::Math::IMultiGenFunction* > f ) :
f_(f) { };
// parameter vector is first background (in common 1 and 2) and then is signal (only in 2)
double operator() (const double *par) const {
double retval=0;
int nfun=f_.size();
for (int ifun=0;ifun<nfun;++ifun)
{
double p1[7];
p1[0] = par[2*ifun];
p1[1] = par[2*ifun+1];
p1[2] = par[2*nfun];
p1[3] = par[2*nfun+1];
p1[4] = par[2*nfun+2];
p1[5] = par[2*nfun+3];
p1[6] = par[2*nfun+4];
retval += (*f_[ifun])(p1);
}
retval += (par[2*nfun+4] - 2.)*(par[2*nfun+4] - 2.) * 4.;
return retval;
}
std::vector< ROOT::Math::IMultiGenFunction* > f_;
};
Double_t PMTFunction(Double_t *x, Double_t *par)
{
float N = par[0];
float mu = par[1];
float Q1 = par[2];
float sigma = par[3];
float ped = par[4];
float sigmaped = par[5];
float xx = x[0];
double value = 0.;
for( unsigned i=START_ORDER; i<NORDERS; ++i ) {
double sigma_n = sqrt( (double)(i)*sigma*sigma + sigmaped*sigmaped);
double gauss = TMath::Gaus( xx, ((double)i*Q1 + ped), sigma_n, kTRUE) ;
if (i!=0 && xx<(ped-par[6]*sigmaped))
gauss = 0;
value += N*(TMath::Poisson( i, mu ) * gauss );
}
return value;
}
FitResults fitSimultaneous( std::vector<TH1F*> histos, double xMin, double xMax )
{
FitResults fr;
int nHistos=histos.size();
std::vector<TF1*> fun;
std::vector<ROOT::Math::WrappedMultiTF1*> wf;
std::vector<ROOT::Fit::BinData*> data;
std::vector< ROOT::Math::IMultiGenFunction* > chi2;
ROOT::Fit::DataOptions opt;
ROOT::Fit::DataRange range;
range.SetRange(xMin,xMax);
const int Npar = 2*nHistos + 5; //norm and mu for each histo, Q1, Q1_sigma, ped, ped_sigma, min_gain
double par0[Npar];
ROOT::Fit::Fitter fitter;
int nBins=0;
for (int ifun=0;ifun<nHistos;++ifun)
{
TF1* f = new TF1( Form("fPMT_%d",ifun) , PMTFunction, xMin, xMax, 7 );
fun.push_back(f);
ROOT::Math::WrappedMultiTF1* wf1= new ROOT::Math::WrappedMultiTF1(*f,1);
wf.push_back(wf1);
ROOT::Fit::BinData* data1=new ROOT::Fit::BinData(opt,range);
data.push_back(data1);
ROOT::Fit::FillData(*(data[ifun]), histos[ifun]);
ROOT::Fit::Chi2Function* chi2_1=new ROOT::Fit::Chi2Function(*(data[ifun]), *(wf[ifun]));
chi2.push_back((ROOT::Math::IMultiGenFunction*) chi2_1);
par0[2*ifun]= histos[ifun]->Integral(); //N
par0[2*ifun+1]= 1; //mu
nBins += (*(data[ifun])).Size();
}
GlobalChi2 globalChi2(chi2);
par0[2*nHistos]=22; //Q1
par0[2*nHistos+1]=11; //Q1 sigma
par0[2*nHistos+2]=30;
par0[2*nHistos+3]=4.2;
par0[2*nHistos+4]=2;
// create before the parameter settings in order to fix or set range on them
fitter.Config().SetParamsSettings(Npar,par0);
// fitter.GetMinimizer()->SetTolerance(0.001);
// fix 5-th parameter
//fitter.Config().ParSettings(4).Fix();
for (int ifun=0;ifun<nHistos;++ifun)
fitter.Config().ParSettings(2*ifun+1).SetLimits(0.1, 5);
fitter.Config().ParSettings(2*nHistos).SetLimits(15,30);
fitter.Config().ParSettings(2*nHistos+1).SetLimits(10,20);
fitter.Config().ParSettings(2*nHistos+2).SetLimits(25,32);
fitter.Config().ParSettings(2*nHistos+3).SetLimits(3.5,4.5);
fitter.Config().ParSettings(2*nHistos+4).SetLimits(1.,5.);
ROOT::Math::MinimizerOptions fitopt;
fitopt.SetTolerance(0.001);
// fitopt.Print();
fitter.Config().SetMinimizerOptions(fitopt);
fitter.FitFCN(Npar,globalChi2,par0,nBins,kTRUE);
ROOT::Fit::FitResult result = fitter.Result();
result.Print(std::cout);
for (int ifun=0;ifun<nHistos;++ifun)
{
fr.norm.push_back(result.Value(2*ifun));
fr.mu.push_back(result.Value(2*ifun+1));
fr.mu_err.push_back(result.Error(2*ifun+1));
}
fr.Q1= result.Value(2*nHistos);
fr.Q1_err= result.Error(2*nHistos);
fr.Q1_sigma= result.Value(2*nHistos+1);
fr.Q1_sigma_err= result.Error(2*nHistos+1);
fr.ped= result.Value(2*nHistos+2);
fr.ped_err= result.Error(2*nHistos+2);
fr.ped_sigma= result.Value(2*nHistos+3);
fr.ped_sigma_err= result.Error(2*nHistos+3);
fr.min_sigma= result.Value(2*nHistos+4);
fr.min_sigma_err= result.Error(2*nHistos+4);
return fr;
}
void SinglePEAnalysis_LedScan_Simultaneous()
{
TCanvas *c=new TCanvas("c","c",800,700);
TFile* out=TFile::Open("SinglePEAnalysis_ledScan.root","RECREATE");
// TFile *f=TFile::Open("h4Reco_test100kevents.root");
int led[7];
int led_err[7];
double x[7];
double x_err[7];
double pe[7];
double pe_err[7];
double gain[7];
double gain_err[7];
double peres[7];
double peres_err[7];
double mu[7];
double mu_err[7];
std::vector<TH1F*> adcData;
TFile* f[7];
for (int i=0;i<7;++i)
{
led[i]=4200+20*i;
led_err[i]=0;
x[i]=led[i];
x_err[i]=led_err[i];
f[i]=TFile::Open(Form("h4Reco_pmt1350led%d.root",led[i]));
TTree* tree=(TTree*)f[i]->Get("h4");
adcData.push_back(new TH1F(Form("ledData_led%d",led[i]),Form("ledData_led%d",led[i]),1200,0,300));
tree->Project(Form("ledData_led%d",led[i]),"charge_tot[C0]");
adcData[i]->Print();
}
// std::cout << "FIT RANGE " << adcData[i]->GetMean()-3*adcData[i]->GetRMS() << "," << adcData[i]->GetMean()+3*adcData[i]->GetRMS() << std::endl;
FitResults fr=fitSimultaneous(adcData,0,300);
for (int i=0;i<7;++i)
{
pe[i]=fr.Q1;
pe_err[i]=fr.Q1_err;
gain[i]=fr.Q1*5E-10*1E-3/50/1.6E-19;
gain_err[i]=fr.Q1_err*5E-10*1E-3/50/1.6E-19;
peres[i]=fr.Q1_sigma/fr.Q1;
peres_err[i]=fr.Q1_sigma/fr.Q1*sqrt((fr.Q1_err*fr.Q1_err)/(fr.Q1*fr.Q1)+(fr.Q1_sigma_err*fr.Q1_sigma_err)/(fr.Q1_sigma*fr.Q1_sigma));
mu[i]=fr.mu[i];
mu_err[i]=fr.mu_err[i];
c->SetLogy(1);
gStyle->SetOptStat(0);
gStyle->SetOptFit(11111);
adcData[i]->SetMarkerStyle(20);
adcData[i]->SetMarkerSize(0.6);
adcData[i]->SetMarkerColor(kBlack);
adcData[i]->SetLineColor(kBlack);
adcData[i]->Draw("PE");
adcData[i]->GetXaxis()->SetTitle("Charge [ADC Counts]");
TF1* f = new TF1( Form("PMT_%d",i) , PMTFunction, 0, 300, 7 );
f->SetParameter(0,fr.norm[i]);
f->SetParameter(1,fr.mu[i]);
f->SetParameter(2,fr.Q1);
f->SetParameter(3,fr.Q1_sigma);
f->SetParameter(4,fr.ped);
f->SetParameter(5,fr.ped_sigma);
f->SetParameter(6,fr.min_sigma);
f->SetLineColor(kOrange);
f->SetLineWidth(3);
f->SetNpx(1000);
f->Draw("SAME");
for (int ipe=0; ipe<10;++ipe)
{
TF1* peFunc=new TF1(Form("peFunc_%d",ipe),"gaus",ipe!=0 ? fr.ped-fr.ped_sigma*fr.min_sigma : 0.,300);
peFunc->SetLineColor(1+ipe);
peFunc->SetLineWidth(2);
float mean=ipe*fr.Q1+fr.ped;
float sigma=sqrt(ipe*fr.Q1_sigma*fr.Q1_sigma+fr.ped_sigma*fr.ped_sigma);
peFunc->SetParameter(0,fr.norm[i]*TMath::Poisson(ipe,fr.mu[i])/(sqrt(2*TMath::Pi())*sigma));
peFunc->SetParameter(1,mean);
peFunc->SetParameter(2,sigma);
peFunc->SetNpx(1000);
peFunc->Draw("SAME");
}
out->cd();
adcData[i]->Write(Form("adcData_led%d",led[i]));
f->Write();
c->Write(Form("singlePEfit_led%d.pdf",led[i]));
c->SaveAs(Form("singlePEfit_led%d.pdf",led[i]));
}
c->SetLogy(0);
TGraphErrors* muVsLed=new TGraphErrors(7,x,mu,x_err,mu_err);
muVsLed->SetMarkerStyle(20);
muVsLed->SetMarkerSize(1.1);
muVsLed->GetXaxis()->SetTitle("Led (V)");
muVsLed->GetYaxis()->SetTitle("#mu");
muVsLed->GetYaxis()->SetRangeUser(0,7);
muVsLed->Draw("APE");
muVsLed->Fit("pol2");
// muVsLed->Write();
c->SaveAs("muVsLed.pdf");
out->Write();
}