ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/Plotting/Modules/UpperLimitsWithShape.C
Revision: 1.1
Committed: Tue Jul 5 13:07:36 2011 UTC (13 years, 10 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Log Message:
TTbar presentation update

File Contents

# User Rev Content
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    
169     fit->Constrain(0,0.8,1.2);
170     fit->Constrain(1,0.0,10.0);
171     Int_t status = fit->Fit();
172     vector<float> results;
173     if(status!=0) return results;
174    
175     Double_t mean[2],error[2];
176     fit->GetResult(0,mean[0],error[0]);
177     Value SMresult(mean[0],error[0]);
178     fit->GetResult(1,mean[1],error[1]);
179     Value sigresult(mean[1],error[1]);
180     cout << "Parameters: " << mean[0] << " , " << mean[1] << endl;
181     if(mean[0]<0||mean[1]<0) {
182     cout << "Fitting failed, one of the parameters is negative !!!!" << endl;
183     return results;
184     }
185    
186     Value vdata(data->Integral(),TMath::Sqrt(data->Integral()));
187     Value vsig(signal->Integral(),TMath::Sqrt(signal->Integral()));
188     Value vbg(background->Integral(),TMath::Sqrt(background->Integral()));
189     float originalfactor=(signal->Integral())/(data->Integral());
190     float originalfactorSM=(background->Integral())/(data->Integral());
191    
192     Value ofac = vsig/vdata;
193     Value ofacsm = vbg /vdata;
194    
195     Value stretchsm = SMresult/ofacsm;
196     Value stretchsig = sigresult/ofac;
197     cout << "Data consists of: " << endl;
198     cout << " SM : " << SMresult << endl;
199     cout << " signal: " << sigresult << endl;
200     cout << "This means that the components have been scaled by: " << endl;
201     cout << " SM: has been stretched by " << (mean[0])/originalfactorSM << " "<< stretchsm<<endl;
202     cout << " Signal: has been stretched by " << (mean[1])/originalfactor << " "<< stretchsig<<endl;
203    
204     file << "------------------------------------------"<<endl;
205     file << "Now considering: Everything pertaining to " << filename << endl;
206     file << "Data consists of: " << endl;
207     file << " SM : " << SMresult << endl;
208     file << " signal: " << sigresult << endl;
209     file << "This means that the components have been scaled by: " << endl;
210     file << " SM: has been stretched by " << stretchsm<<endl;
211     file << " Signal: has been stretched by " << stretchsig<<endl;
212    
213     present_limit_fitting_results((TH1F*)fit->GetPlot(),data,background,signal,(mean[0])/originalfactorSM,(mean[1])/originalfactor,filename);
214    
215     check_compatibility_with_zero(sigresult,file);
216     //vector<float> results = compute_sigmas(signal,(mean[1])/originalfactor);
217    
218     cout << "\\Chi^2 / ndf = " << fit->GetChisquare() << "/" << fit->GetNDF() << endl;
219    
220     results.push_back(compute_upper_limit_based_on_fit(sigresult,ofac,0));
221     results.push_back(compute_upper_limit_based_on_fit(sigresult,ofac,1));
222     results.push_back(compute_upper_limit_based_on_fit(sigresult,ofac,2));
223     results.push_back(compute_upper_limit_based_on_fit(sigresult,ofac,3));
224    
225     cout << "Limits: (in terms of x prediction) " << endl;
226     file << "Limits: (in terms of x prediction) " << endl;
227     for (int i=0;i<=3;i++) {
228     cout << i << " sigma : " << results[i] << endl;
229     file << i << " sigma : " << results[i] << endl;
230     }
231    
232    
233     cout << endl << endl;
234     file << endl << endl;
235    
236     if(results[3]<1) {
237     cout << "The point has been excluded, since the upper limit corresponding to 3 sigma, " << results[3] << " is < 1" << endl;
238     file << "The point has been excluded, since the upper limit corresponding to 3 sigma, " << results[3] << " is < 1" << endl;
239     //the ANSII code below will make an X for the excluded point (to celebrate)
240     cout << " __ __" << endl;
241     cout << " \\ \\ / /" << endl;
242     cout << " \\ V / " << endl;
243     cout << " / \\ " << endl;
244     cout << " / /^\\ \\" << endl;
245     cout << " \\/ \\/" << endl;
246    
247     file << " __ __" << endl;
248     file << " \\ \\ / /" << endl;
249     file << " \\ V / " << endl;
250     file << " / \\ " << endl;
251     file << " / /^\\ \\" << endl;
252     file << " \\/ \\/" << endl;
253    
254    
255     }
256     else {
257     cout << "The 3 sigma limit on this point is " << results[3] << " x prediction" << endl;
258     file << "The point has been excluded, since " << results[3] << " x prediction" << endl;
259     }
260    
261    
262     return results;
263     }
264    
265     /*
266    
267     Please use the function like this :
268    
269     int main() {
270     gROOT->SetStyle("Plain");
271     ofstream myfile;
272     myfile.open ("ShapeFit_log.txt");
273     TH1F *data = new TH1F("data","data",10,0,10);
274     data->GetXaxis()->SetTitle("some variable");
275     data->GetXaxis()->CenterTitle();
276     data->GetYaxis()->SetTitle("events");
277     data->GetYaxis()->CenterTitle();
278     data->Sumw2();
279     TH1F *mc = new TH1F("mc","mc",10,0,10);
280     mc->Sumw2();
281     TH1F *si = new TH1F("si","signal",10,0,10);
282     si->Sumw2();
283     TF1 *f1 = new TF1("f1", "[0] +[1]*x +gaus(2)", 0, 5);
284     f1->SetParameters(6, -1,8, 0.1, 0.05);
285     TF1 *f2 = new TF1("f2", "gaus(2)", 0, 5);
286     f2->SetParameters(5, 0.1, 0.05);
287     TF1 *f3 = new TF1("f3", "[0] +[1]*x", 0, 5);
288     f3->SetParameters(6, -1,8);
289    
290     data->FillRandom("f1",10000);
291     mc->FillRandom("f3",6000);
292     si->FillRandom("f2",2000);
293    
294     establish_upper_limits(data,mc,si,"test2.png",myfile);
295     myfile.close();
296     return 0;
297     }
298     */