1 |
buchmann |
1.1 |
/*
|
2 |
|
|
|
3 |
|
|
g++ ShapeFit.C -o ShapeFit.exec `root-config --glibs --cflags` && ./ShapeFit.exec
|
4 |
|
|
|
5 |
|
|
*/
|
6 |
|
|
|
7 |
|
|
#include <iostream>
|
8 |
|
|
#include <vector>
|
9 |
|
|
#include <fstream>
|
10 |
|
|
|
11 |
|
|
#include <TH1.h>
|
12 |
|
|
#include <TCanvas.h>
|
13 |
|
|
#include <TObjArray.h>
|
14 |
|
|
#include <TF1.h>
|
15 |
|
|
#include <TError.h>
|
16 |
|
|
#include <TFractionFitter.h>
|
17 |
|
|
#include <TMath.h>
|
18 |
|
|
#include <TROOT.h>
|
19 |
|
|
#include <THStack.h>
|
20 |
|
|
#include <TLine.h>
|
21 |
|
|
#include <TColor.h>
|
22 |
|
|
|
23 |
|
|
#ifndef ValueClassLoaded
|
24 |
|
|
#include "ValueClass.C"
|
25 |
|
|
#endif
|
26 |
|
|
#ifndef GeneralToolBoxLoaded
|
27 |
|
|
#include "GeneralToolBox.C"
|
28 |
|
|
#endif
|
29 |
|
|
using namespace std;
|
30 |
|
|
|
31 |
|
|
void check_compatibility_with_zero(Value sigresult, ofstream &file) {
|
32 |
|
|
cout << "Checking compatibility with zero for f=" << sigresult << endl;
|
33 |
|
|
cout << " The result is compatible with zero to " << 1-(TMath::Erf((sigresult.getValue()/(TMath::Sqrt(2)*sigresult.getError())))) << endl;
|
34 |
|
|
file << "Checking compatibility with zero for f=" << sigresult << endl;
|
35 |
|
|
file << " The result is compatible with zero to " << 1-(TMath::Erf((sigresult.getValue()/(TMath::Sqrt(2)*sigresult.getError())))) << endl;
|
36 |
|
|
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
/*
|
40 |
|
|
vector<float> compute_sigmas(TH1 *signal, float weight) {
|
41 |
|
|
TH1F *histo = (TH1F*)signal->Clone();
|
42 |
|
|
histo->Scale(weight);
|
43 |
|
|
float integral[7];
|
44 |
|
|
for(int i=0;i<4;i++) integral[i]=0;
|
45 |
|
|
for(int i=0;i<=histo->GetNbinsX()+1;i++) {
|
46 |
|
|
integral[0]+=histo->GetBinContent(i);
|
47 |
|
|
integral[1]+=histo->GetBinContent(i)+histo->GetBinError(i);
|
48 |
|
|
integral[2]+=histo->GetBinContent(i)+2*histo->GetBinError(i);
|
49 |
|
|
integral[3]+=histo->GetBinContent(i)+3*histo->GetBinError(i);
|
50 |
|
|
integral[4]+=histo->GetBinContent(i)+4*histo->GetBinError(i);
|
51 |
|
|
integral[5]+=histo->GetBinContent(i)+5*histo->GetBinError(i);
|
52 |
|
|
integral[6]+=histo->GetBinContent(i)+6*histo->GetBinError(i);
|
53 |
|
|
}
|
54 |
|
|
float meanintegral = signal->Integral();
|
55 |
|
|
vector<float> results;
|
56 |
|
|
results.push_back(integral[0]/meanintegral);
|
57 |
|
|
results.push_back(integral[1]/meanintegral);
|
58 |
|
|
results.push_back(integral[2]/meanintegral);
|
59 |
|
|
results.push_back(integral[3]/meanintegral);
|
60 |
|
|
results.push_back(integral[4]/meanintegral);
|
61 |
|
|
results.push_back(integral[5]/meanintegral);
|
62 |
|
|
results.push_back(integral[6]/meanintegral);
|
63 |
|
|
return results;
|
64 |
|
|
}
|
65 |
|
|
*/
|
66 |
|
|
void present_limit_fitting_results(TH1 *result,TH1 *data, TH1 *background, TH1 *signal, float bgw, float siw,string filename) {
|
67 |
|
|
TH1F *bg = (TH1F*)background->Clone();
|
68 |
|
|
bg->Scale(bgw);
|
69 |
|
|
TH1F *si = (TH1F*)signal->Clone();
|
70 |
|
|
si->Scale(siw);
|
71 |
|
|
// TCanvas *c1 = new TCanvas("c1","c1",500,900);
|
72 |
|
|
TCanvas *c1 = new TCanvas("c1","c1",1500,500);
|
73 |
|
|
c1->Divide(3,1);
|
74 |
|
|
// c1->Divide(1,3);
|
75 |
|
|
|
76 |
|
|
c1->cd(1);
|
77 |
|
|
data->SetTitle("");
|
78 |
|
|
data->SetStats(0);
|
79 |
|
|
background->SetLineColor(kBlue);
|
80 |
|
|
data->SetLineColor(kBlack);
|
81 |
|
|
signal->SetLineColor(kRed);
|
82 |
|
|
// background->SetFillColor(TColor::GetColor("#5882FA"));
|
83 |
|
|
background->SetMarkerSize(0);
|
84 |
|
|
// background->SetFillColor(TColor::GetColor("#5882FA"));
|
85 |
|
|
// signal->SetFillColor(TColor::GetColor("#81F781"));
|
86 |
|
|
signal->SetMarkerSize(0);
|
87 |
|
|
// signal->SetFillColor(TColor::GetColor("#FE2E2E"));
|
88 |
|
|
TLegend *leg = new TLegend(0.7,0.7,0.89,0.89);
|
89 |
|
|
leg->AddEntry(data,"Data","p");
|
90 |
|
|
leg->AddEntry(bg,"Z+Jets","f");
|
91 |
|
|
leg->AddEntry(si,"signal","f");
|
92 |
|
|
leg->SetLineColor(kWhite);
|
93 |
|
|
leg->SetFillColor(kWhite);
|
94 |
|
|
data->Draw();
|
95 |
|
|
background->Draw("histo,same");
|
96 |
|
|
signal->Draw("histo,same");
|
97 |
|
|
data->Draw("same");
|
98 |
|
|
leg->Draw("same");
|
99 |
|
|
TText* title = write_title("Before fitting");
|
100 |
|
|
title->Draw();
|
101 |
|
|
|
102 |
|
|
c1->cd(2);
|
103 |
|
|
c1->cd(2)->SetLogy(1);
|
104 |
|
|
TH1F *datac = (TH1F*)data->Clone();
|
105 |
|
|
bg->SetFillColor(TColor::GetColor("#5882FA"));
|
106 |
|
|
bg->SetLineColor(kBlue);
|
107 |
|
|
// si->SetLineColor(kGreen);
|
108 |
|
|
// si->SetFillColor(TColor::GetColor("#81F781"));
|
109 |
|
|
si->SetLineColor(kRed);
|
110 |
|
|
si->SetFillColor(TColor::GetColor("#FE2E2E"));
|
111 |
|
|
THStack fresult("fresult","");
|
112 |
|
|
fresult.Add(bg);
|
113 |
|
|
fresult.Add(si);
|
114 |
|
|
fresult.Draw("histo");
|
115 |
|
|
datac->SetMinimum(0.4);
|
116 |
|
|
datac->SetMaximum(data->GetMaximum()*10);
|
117 |
|
|
datac->Draw();
|
118 |
|
|
fresult.Draw("histo,same");
|
119 |
|
|
datac->Draw("same");
|
120 |
|
|
leg->Draw("same");
|
121 |
|
|
TText* title2 = write_title("After fitting (composition)");
|
122 |
|
|
title2->Draw();
|
123 |
|
|
|
124 |
|
|
c1->cd(3);
|
125 |
|
|
TH1F *ratio = (TH1F*)result->Clone();
|
126 |
|
|
ratio->Divide(data);
|
127 |
|
|
ratio->GetYaxis()->SetRangeUser(0,2);
|
128 |
|
|
TLine *oneline = new TLine(data->GetBinLowEdge(1),1,data->GetBinLowEdge(data->GetNbinsX())+data->GetBinWidth(data->GetNbinsX()),1);
|
129 |
|
|
oneline->SetLineColor(kBlue);
|
130 |
|
|
ratio->SetFillColor(TColor::GetColor("#FA8258"));
|
131 |
|
|
ratio->SetTitle("");
|
132 |
|
|
ratio->SetStats(0);
|
133 |
|
|
ratio->GetYaxis()->SetTitle("(Fit result)/data");
|
134 |
|
|
ratio->GetYaxis()->CenterTitle();
|
135 |
|
|
ratio->SetMarkerSize(0);
|
136 |
|
|
ratio->Draw("E5");
|
137 |
|
|
oneline->Draw();
|
138 |
|
|
TText* title3 = write_title("Ratio");
|
139 |
|
|
title3->Draw();
|
140 |
|
|
CompleteSave(c1,("upper_limit/"+filename).c_str());
|
141 |
|
|
CompleteSave(c1->cd(1),("upper_limit/"+filename+"_cd1").c_str());
|
142 |
|
|
CompleteSave(c1->cd(2),("upper_limit/"+filename+"_cd2").c_str());
|
143 |
|
|
CompleteSave(c1->cd(3),("upper_limit/"+filename+"_cd3").c_str());
|
144 |
|
|
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
float compute_upper_limit_based_on_fit(Value signalresult, Value originalfactor, int nsigma) {
|
148 |
|
|
float stretchby=signalresult.getValue()*(signalresult.getValue()+nsigma*signalresult.getError())/(signalresult.getValue());
|
149 |
|
|
stretchby/=originalfactor.getValue();
|
150 |
|
|
return stretchby;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
vector<float> establish_upper_limits(TH1 *data, TH1 *background, TH1 *signal,string filename, ofstream &file) {
|
155 |
|
|
TObjArray *allmc = new TObjArray(2);
|
156 |
|
|
allmc->Add(background);
|
157 |
|
|
allmc->Add(signal);
|
158 |
|
|
|
159 |
|
|
TCanvas *c1 = new TCanvas("c1","c1");
|
160 |
|
|
background->SetMarkerColor(kBlue);
|
161 |
|
|
background->Draw();
|
162 |
|
|
signal->SetMarkerColor(kGreen);
|
163 |
|
|
signal->Draw("same");
|
164 |
|
|
data->Draw("same");
|
165 |
|
|
c1->SaveAs("Input_for_establish_upper_limits.png");
|
166 |
|
|
|
167 |
|
|
TFractionFitter* fit = new TFractionFitter(data, allmc);
|
168 |
buchmann |
1.2 |
|
169 |
|
|
fit->Constrain(1,1.0,1.0);
|
170 |
|
|
fit->Constrain(2,0.0,1.1);
|
171 |
|
|
//fit->Constrain(1,0.0,10.0);
|
172 |
|
|
TFile *rawinput = new TFile("rawtfitterinput.root","RECREATE");
|
173 |
|
|
data->Write();
|
174 |
|
|
signal->Write();
|
175 |
|
|
background->Write();
|
176 |
|
|
rawinput->Close();
|
177 |
buchmann |
1.1 |
|
178 |
|
|
Int_t status = fit->Fit();
|
179 |
|
|
vector<float> results;
|
180 |
|
|
if(status!=0) return results;
|
181 |
|
|
|
182 |
|
|
Double_t mean[2],error[2];
|
183 |
|
|
fit->GetResult(0,mean[0],error[0]);
|
184 |
|
|
Value SMresult(mean[0],error[0]);
|
185 |
|
|
fit->GetResult(1,mean[1],error[1]);
|
186 |
|
|
Value sigresult(mean[1],error[1]);
|
187 |
|
|
cout << "Parameters: " << mean[0] << " , " << mean[1] << endl;
|
188 |
|
|
if(mean[0]<0||mean[1]<0) {
|
189 |
|
|
cout << "Fitting failed, one of the parameters is negative !!!!" << endl;
|
190 |
|
|
return results;
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
Value vdata(data->Integral(),TMath::Sqrt(data->Integral()));
|
194 |
|
|
Value vsig(signal->Integral(),TMath::Sqrt(signal->Integral()));
|
195 |
|
|
Value vbg(background->Integral(),TMath::Sqrt(background->Integral()));
|
196 |
|
|
float originalfactor=(signal->Integral())/(data->Integral());
|
197 |
|
|
float originalfactorSM=(background->Integral())/(data->Integral());
|
198 |
|
|
|
199 |
|
|
Value ofac = vsig/vdata;
|
200 |
|
|
Value ofacsm = vbg /vdata;
|
201 |
|
|
|
202 |
|
|
Value stretchsm = SMresult/ofacsm;
|
203 |
|
|
Value stretchsig = sigresult/ofac;
|
204 |
|
|
cout << "Data consists of: " << endl;
|
205 |
|
|
cout << " SM : " << SMresult << endl;
|
206 |
|
|
cout << " signal: " << sigresult << endl;
|
207 |
|
|
cout << "This means that the components have been scaled by: " << endl;
|
208 |
|
|
cout << " SM: has been stretched by " << (mean[0])/originalfactorSM << " "<< stretchsm<<endl;
|
209 |
|
|
cout << " Signal: has been stretched by " << (mean[1])/originalfactor << " "<< stretchsig<<endl;
|
210 |
|
|
|
211 |
|
|
file << "------------------------------------------"<<endl;
|
212 |
|
|
file << "Now considering: Everything pertaining to " << filename << endl;
|
213 |
|
|
file << "Data consists of: " << endl;
|
214 |
|
|
file << " SM : " << SMresult << endl;
|
215 |
|
|
file << " signal: " << sigresult << endl;
|
216 |
|
|
file << "This means that the components have been scaled by: " << endl;
|
217 |
|
|
file << " SM: has been stretched by " << stretchsm<<endl;
|
218 |
|
|
file << " Signal: has been stretched by " << stretchsig<<endl;
|
219 |
|
|
|
220 |
|
|
present_limit_fitting_results((TH1F*)fit->GetPlot(),data,background,signal,(mean[0])/originalfactorSM,(mean[1])/originalfactor,filename);
|
221 |
|
|
|
222 |
|
|
check_compatibility_with_zero(sigresult,file);
|
223 |
|
|
//vector<float> results = compute_sigmas(signal,(mean[1])/originalfactor);
|
224 |
|
|
|
225 |
|
|
cout << "\\Chi^2 / ndf = " << fit->GetChisquare() << "/" << fit->GetNDF() << endl;
|
226 |
|
|
|
227 |
|
|
results.push_back(compute_upper_limit_based_on_fit(sigresult,ofac,0));
|
228 |
|
|
results.push_back(compute_upper_limit_based_on_fit(sigresult,ofac,1));
|
229 |
|
|
results.push_back(compute_upper_limit_based_on_fit(sigresult,ofac,2));
|
230 |
|
|
results.push_back(compute_upper_limit_based_on_fit(sigresult,ofac,3));
|
231 |
|
|
|
232 |
|
|
cout << "Limits: (in terms of x prediction) " << endl;
|
233 |
|
|
file << "Limits: (in terms of x prediction) " << endl;
|
234 |
|
|
for (int i=0;i<=3;i++) {
|
235 |
|
|
cout << i << " sigma : " << results[i] << endl;
|
236 |
|
|
file << i << " sigma : " << results[i] << endl;
|
237 |
|
|
}
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
cout << endl << endl;
|
241 |
|
|
file << endl << endl;
|
242 |
|
|
|
243 |
|
|
if(results[3]<1) {
|
244 |
|
|
cout << "The point has been excluded, since the upper limit corresponding to 3 sigma, " << results[3] << " is < 1" << endl;
|
245 |
|
|
file << "The point has been excluded, since the upper limit corresponding to 3 sigma, " << results[3] << " is < 1" << endl;
|
246 |
|
|
//the ANSII code below will make an X for the excluded point (to celebrate)
|
247 |
|
|
cout << " __ __" << endl;
|
248 |
|
|
cout << " \\ \\ / /" << endl;
|
249 |
|
|
cout << " \\ V / " << endl;
|
250 |
|
|
cout << " / \\ " << endl;
|
251 |
|
|
cout << " / /^\\ \\" << endl;
|
252 |
|
|
cout << " \\/ \\/" << endl;
|
253 |
|
|
|
254 |
|
|
file << " __ __" << endl;
|
255 |
|
|
file << " \\ \\ / /" << endl;
|
256 |
|
|
file << " \\ V / " << endl;
|
257 |
|
|
file << " / \\ " << endl;
|
258 |
|
|
file << " / /^\\ \\" << endl;
|
259 |
|
|
file << " \\/ \\/" << endl;
|
260 |
|
|
|
261 |
|
|
|
262 |
|
|
}
|
263 |
|
|
else {
|
264 |
|
|
cout << "The 3 sigma limit on this point is " << results[3] << " x prediction" << endl;
|
265 |
|
|
file << "The point has been excluded, since " << results[3] << " x prediction" << endl;
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
return results;
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
/*
|
273 |
|
|
|
274 |
|
|
Please use the function like this :
|
275 |
|
|
|
276 |
|
|
int main() {
|
277 |
|
|
gROOT->SetStyle("Plain");
|
278 |
|
|
ofstream myfile;
|
279 |
|
|
myfile.open ("ShapeFit_log.txt");
|
280 |
|
|
TH1F *data = new TH1F("data","data",10,0,10);
|
281 |
|
|
data->GetXaxis()->SetTitle("some variable");
|
282 |
|
|
data->GetXaxis()->CenterTitle();
|
283 |
|
|
data->GetYaxis()->SetTitle("events");
|
284 |
|
|
data->GetYaxis()->CenterTitle();
|
285 |
|
|
data->Sumw2();
|
286 |
|
|
TH1F *mc = new TH1F("mc","mc",10,0,10);
|
287 |
|
|
mc->Sumw2();
|
288 |
|
|
TH1F *si = new TH1F("si","signal",10,0,10);
|
289 |
|
|
si->Sumw2();
|
290 |
|
|
TF1 *f1 = new TF1("f1", "[0] +[1]*x +gaus(2)", 0, 5);
|
291 |
|
|
f1->SetParameters(6, -1,8, 0.1, 0.05);
|
292 |
|
|
TF1 *f2 = new TF1("f2", "gaus(2)", 0, 5);
|
293 |
|
|
f2->SetParameters(5, 0.1, 0.05);
|
294 |
|
|
TF1 *f3 = new TF1("f3", "[0] +[1]*x", 0, 5);
|
295 |
|
|
f3->SetParameters(6, -1,8);
|
296 |
|
|
|
297 |
|
|
data->FillRandom("f1",10000);
|
298 |
|
|
mc->FillRandom("f3",6000);
|
299 |
|
|
si->FillRandom("f2",2000);
|
300 |
|
|
|
301 |
|
|
establish_upper_limits(data,mc,si,"test2.png",myfile);
|
302 |
|
|
myfile.close();
|
303 |
|
|
return 0;
|
304 |
|
|
}
|
305 |
|
|
*/ |