-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit1.c
193 lines (154 loc) · 5.49 KB
/
fit1.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
TCanvas *c1;
Double_t langaufun(Double_t *x, Double_t *par) {
//Fit parameters:
//par[0]=Width (scale) parameter of Landau density
//par[1]=Most Probable (MP, location) parameter of Landau density
//par[2]=Total area (integral -inf to inf, normalization constant)
//par[3]=Width (sigma) of convoluted Gaussian function
//
//In the Landau distribution (represented by the CERNLIB approximation),
//the maximum is located at x=-0.22278298 with the location parameter=0.
//This shift is corrected within this function, so that the actual
//maximum is identical to the MP parameter.
// Numeric constants
Double_t invsq2pi = 0.3989422804014; // (2 pi)^(-1/2)
Double_t mpshift = 0; //-0.22278298; // Landau maximum location
// Control constants
Double_t np = 300; //nstp; //100.0; // number of convolution steps
Double_t sc = 5.0; // convolution extends to +-sc Gaussian sigmas
// Variables
Double_t xx;
Double_t mpc;
Double_t fland;
Double_t sum = 0.0;
Double_t xlow,xupp;
Double_t step;
Double_t i;
// MP shift correction
mpc = par[1] - mpshift * par[0];
// Range of convolution integral
xlow = x[0] - sc * par[3];
xupp = x[0] + sc * par[3];
step = (xupp-xlow) / np;
// Convolution integral of Landau and Gaussian by sum
for(i=1.0; i<=np/2; i++) {
xx = xlow + (i-.5) * step;
fland = TMath::Landau(xx,mpc,par[0]) / par[0];
sum += fland * TMath::Gaus(x[0],xx,par[3]);
xx = xupp - (i-.5) * step;
fland = TMath::Landau(xx,mpc,par[0]) / par[0];
sum += fland * TMath::Gaus(x[0],xx,par[3]);
}
return (par[2] * step * sum * invsq2pi / par[3]);
}
void fit1() { // After Digi
const char *fname = "./up/dmp_ag10um_4.00_1.6_x5.5_z5.5mm_ds1.00_3000evt.root";
const char *fOutName = "./up/out/out_dmp_ag10um_4.00_1.6_x5.5_z5.5mm_ds1.00_3000evt.root";
TRandom2 *ranGen = new TRandom2();
ranGen->SetSeed(0);
if ( gROOT->GetListOfCanvases()->FindObject("c1") == NULL ) c1 = new TCanvas("c1", "c1", 1);
//
// Some common variables
//
const char *treeName = "DRTile";
const int nBins = 400;
const float binLo = 0.0;
const float binHi = 200.0;
const float gMean = 3.0;
const float gSigma = 1.5;
const float eff = 0.15;
//
// Open file
//
std::cout << "Trying to open file... ";
TFile *file = new TFile(fname, "READ");
if (!file) {
std::cout << "[FAIL]" << std::endl;
return;
}
std::cout << "[OK]" << std::endl;
//
// Create an output file
//
std::cout << "Trying to create output file... ";
TFile *fOut = new TFile(fOutName, "RECREATE");
if (!fOut) {
file->Close();
std::cout << "[FAIL]" << std::endl;
return;
}
std::cout << "[OK]" << std::endl;
//
// Setup a TTree
//
std::cout << "Setup a tree... ";
TTree *tree = (TTree *)file->Get(treeName);
TH1F *hist = (TH1F *)file->Get("fCounterSipm");
if (!tree) {
std::cout << "[FAIL]" << std::endl;
file->Close();
return;
}
std::cout << "[OK]" << std::endl;
unsigned int nEvents = tree->GetEntries();
//
// Setup a branch
//
Double_t photonsInSipm = 0;
Double_t trigger = 0;
tree->SetBranchAddress("photonsInSipm", &photonsInSipm);
tree->SetBranchAddress("trigger", &trigger);
//
// Create a histogram and random generator
//
TH1F *hPhotonInSipm = new TH1F("hPhotonInSipm", "hPhotonInSipm", nBins, binLo, binHi);
TH1F *hPhotonInSipm_without_trigger = new TH1F("hPhotonInSipm_without_trigger", "hPhotonInSipm_without_trigger", nBins, binLo, binHi);
for (unsigned int i = 0; i <nEvents; i++) {
tree->GetEntry(i);
hPhotonInSipm_without_trigger->Fill(photonsInSipm);
if (trigger == 1) {
float nPhCorr = ranGen->Poisson(eff * photonsInSipm) + ranGen->Gaus(gMean, gSigma);
hPhotonInSipm->Fill(nPhCorr);
}
}
TH1F *copy_from_in_file=(TH1F*)hist->Clone();
fOut->cd();
hPhotonInSipm_without_trigger->Write();
hPhotonInSipm->Write();
copy_from_in_file->Write();
fOut->Write();
fOut->Close();
file->Close();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TFile *filenew = new TFile("./up/out/out_dmp_ag10um_4.00_1.6_x5.5_z5.5mm_ds1.00_3000evt.root");
TH1F *histnew;
printf("File open... ");
if (!filenew) {
printf("[FAIL]\n");
return 0;
} else printf("[OK]\n");
printf("Loading histogram... ");
histnew = (TH1F *)filenew->Get("hPhotonInSipm");
if (!histnew) {
printf("[FAIL]\n");
return 0;
} else printf("[OK]\n");
TF1 *ff1 = new TF1("ff1",langaufun,0,80,4);
ff1->SetParameters(0.1*histnew->GetMean(),
histnew->GetMean(),
double(histnew->GetEntries()),
0.1*histnew->GetMean());
ff1->SetParNames("Width","MP","Area","GSigma");
histnew->Fit(ff1,"Q0R");
std::cout << "Fit result for 1: " << std::endl
<< " MPV = " << ff1->GetParameter(1) << "+-" << ff1->GetParError(1) << std::endl
<< " Lwidth = " << ff1->GetParameter(0) << "+-" << ff1->GetParError(0) << std::endl
<< " Gwidth = " << ff1->GetParameter(3) << "+-" << ff1->GetParError(3) << std::endl
<< " Scale = " << ff1->GetParameter(2) << "+-" << ff1->GetParError(2) << std::endl
<< " chi2/ndf = " << ff1->GetChisquare() << " / " << ff1->GetNDF()
<< " = " << ff1->GetChisquare()/ff1->GetNDF()
<< std::endl;
c1->cd();
histnew->DrawCopy();
ff1->Draw("SAME");
}