ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/fitNewRelIso.C
Revision: 1.2
Committed: Tue Apr 28 04:20:18 2009 UTC (16 years ago) by jengbou
Content type: text/plain
Branch: MAIN
Changes since 1.1: +141 -99 lines
Log Message:
update

File Contents

# User Rev Content
1 jengbou 1.1 //////////////////////////////////////////////////////////////////////////
2     //Geng-yuan Jeng/UC Riverside
3     //2009/04/20 14:41:01
4     /////////////////////////////////////////////////////////////////////////
5    
6     #ifndef __CINT__
7     #include "RooGlobalFunc.h"
8     #endif
9     #include "RooRealVar.h"
10     #include "RooDataSet.h"
11     #include "RooGaussian.h"
12     #include "TCanvas.h"
13     #include "RooPlot.h"
14     #include "TH1D.h"
15     #include "TTree.h"
16     using namespace RooFit;
17 jengbou 1.2 // .493/.2114 j1
18     //.773/.3257 j2
19     // .863/.353 j3
20     //.808/.294 j4
21 jengbou 1.1 void fitNewRelIso()
22     {
23 jengbou 1.2 TString fileName = "skimmed226/QCDbin4_005.root";
24 jengbou 1.1 // TString fileName = "skimmed/QCDbinall_NewRelIso.root";
25     // TString fileName = "skimmed/QCDbin4_NewRelIso_CD30_rebin2.root";
26     // TString fileName = "skimmed/QCDbin3_NewRelIso_CD.root";
27 jengbou 1.2 // TString fileName = "skimmed/QCDbin4_rebin.root";
28     // TString fileName = "skimmed/QCDbin4_NewRelIso_FastSim.root";
29 jengbou 1.1
30     // Dynamic(true) or static(false) range
31     bool dynamic = true;
32     int np = 100;
33     double epslon = 1.e-5;
34     double epslon1 = 1.e-6;
35     // Dynamic fit region weight
36 jengbou 1.2 double wxmin = 2.; // 1.81 1.7 1.81875 for fastsim
37     double wxmax = wxmin/2.5; // 0.9 .95
38     // Select fitting function:
39     // TString s0 = "landau";
40     // Select New or Old reliso:
41     // TString isoname = "New";
42 jengbou 1.1
43     Double_t ax0=0.;
44     Double_t axf=0.05;
45     Double_t axF=0.11;
46     Double_t bx0=0.2;
47     Double_t bxf=2.;
48     int niter = 1;
49     int nsiter = 0;
50     Double_t temp = 0.;
51     Double_t tempb = 0.;
52     Double_t pass[3] = {0.,0.,0.};
53     Double_t bound[3] = {10e6.,0.,0.};
54    
55     gStyle->SetOptStat(1);
56     gStyle->SetPalette(1);
57     // Import histos
58    
59     TFile *file = TFile::Open(fileName);
60     TTree *tree = (TTree*)file->Get("myTree");
61     TH1D *h0 = (TH1D*)file->Get("histos/h_RelIso_all");
62     TH1D *h1 = (TH1D*)file->Get("histos/h_RelIso_qcd");
63     Int_t jbin;
64     Double_t na;
65     tree->SetBranchAddress("jbin",&jbin);
66     tree->SetBranchAddress("na",&na);
67     tree->GetEntry(0);
68 jengbou 1.2
69 jengbou 1.1 TH1D *h_all = (TH1D*)h0->Clone();
70     TH1D *h_qcd = (TH1D*)h1->Clone();
71     Int_t nbxMax = h_all->GetXaxis()->FindBin(2.0);
72     h_all->GetXaxis()->SetRange(2,h_all->GetXaxis()->FindBin(bxf));
73     Int_t nbMax = h_all->GetMaximumBin();
74     cout << "=========> Bin number at peak= " << nbMax << endl;
75     h_all->GetXaxis()->SetRange(1,nbxMax);
76     TAxis *axis0 = h_all->GetXaxis();
77     double bwidth = axis0->GetBinWidth(nbMax);
78     cout << "=========> bwidth = " << bwidth << endl;
79    
80     cout << "\n>>>>> Iteration step: "<< niter << endl;
81 jengbou 1.2
82 jengbou 1.1 RooRealVar x("x","x",0.,2.);
83     RooRealVar y("y","y",0.,2.);
84     RooDataHist *dh0 = new RooDataHist("dh0","dh0",x,Import(*h0));
85     RooDataHist *dh1 = new RooDataHist("dh1","dh1",x,Import(*h1));
86     RooDataHist *dhy0 = new RooDataHist("dhy0","dhy0",y,Import(*h0));
87     RooDataHist *dhy1 = new RooDataHist("dhy1","dhy1",y,Import(*h1));
88     // Choose signal region:
89     x.setRange("signalregion",ax0,axf);
90     // x.setRange("signalregion",ax0,axF);
91     x.setRange("ctrlregion",bx0,bxf);
92    
93     // S e t u p m o d e l
94     // ---------------------
95    
96     // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
97 jengbou 1.2 // Exponential parameters
98     RooRealVar lamda("lamda","decay constant of exponential",1,-100.,100.);
99     // Gaussian parameters
100 jengbou 1.1 RooRealVar mean("mean","mean of gaussian",1.5,0.,2.);
101     RooRealVar sigma("sigma","width of gaussian",1,0.,100.);
102 jengbou 1.2 // Landau parameters
103 jengbou 1.1 RooRealVar MPV("MPV","mean of landau",1,0,2);
104     RooRealVar Sig("Sig","sigma of landau",1,0,2);
105     // Build gaussian p.d.f in terms of x,mean and sigma
106 jengbou 1.2 RooExponential expo("expo","exponential PDF",x,lamda);
107 jengbou 1.1 RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma);
108     RooLandau landau("landau","landau PDF",x,MPV,Sig);
109 jengbou 1.2 // Build model
110     // RooRealVar elfrac("elfrac","fraction of Exponential and Landau",0.05,0.,1.);
111     // RooRealVar glfrac("glfrac","fraction of Gaussian and Landau",0.,0.,1.);
112     // RooAddPdf model("model","g+l",RooArgList(gauss,landau),glfrac);
113 jengbou 1.1
114     // Construct plot frame in 'x'
115     RooPlot* xframe = x.frame(Title("Landau p.d.f. with data"));
116     RooPlot* yframe = y.frame(Title("Landau p.d.f. with data"));
117    
118     // P l o t m o d e l a n d c h a n g e p a r a m e t e r v a l u e s
119     // ---------------------------------------------------------------------------
120     // Change the value of Parameters
121 jengbou 1.2 // sigma.setVal(2.);
122     // MPV.setVal(0.8412);
123     // Sig.setVal(0.314);
124     // Plot gauss in frame (i.e. in x) and draw frame on canvas
125     // gauss.plotOn(xframe);
126     // landau.plotOn(xframe,LineColor(kRed));
127 jengbou 1.1
128     // Make a second plot frame in x and draw both the
129     // data and the p.d.f in the frame
130 jengbou 1.2 dh0->plotOn(xframe,MarkerStyle(23),MarkerColor(2),LineColor(2),XErrorSize(0));
131     dh1->plotOn(xframe,MarkerStyle(22),MarkerColor(9),LineColor(9),XErrorSize(0));
132     dhy0->plotOn(yframe,MarkerStyle(23),MarkerColor(2),LineColor(2),XErrorSize(0));
133     dhy1->plotOn(yframe,MarkerStyle(22),MarkerColor(9),LineColor(9),XErrorSize(0));
134 jengbou 1.1
135     // F i t m o d e l t o d a t a
136     // -----------------------------
137    
138     // Fit pdf to data
139     RooFitResult*rest = landau.fitTo(*dh0,Range("ctrlregion"),PrintLevel(-1),Save());
140     rest->Print("v");
141     niter++;
142     RooAbsReal* nQCDsigw = landau.createIntegral(x,Range("signalregion"));
143     RooAbsReal* nQCDbkgw = landau.createIntegral(x,Range("ctrlregion"));
144     // Draw all frames on a canvas
145 jengbou 1.2 Double_t xb0 = bx0;
146     Double_t xbf = bxf;
147 jengbou 1.1
148 jengbou 1.2 if (dynamic) {
149     TCanvas* c = new TCanvas("fitNewRelIso","fitNewRelIso",1000,600);
150     c->Divide(2,1);
151     c->cd(1);
152    
153 jengbou 1.1 xb0 = MPV.getVal()-wxmin*Sig.getVal();
154     xbf = MPV.getVal()+wxmax*Sig.getVal();
155 jengbou 1.2
156     for (;niter <= np && nsiter < 2; niter++){
157     xb0 = MPV.getVal()-wxmin*Sig.getVal();
158     xbf = MPV.getVal()+wxmax*Sig.getVal();
159     cout << "\n>>>>> Iteration step: "<< niter << endl;
160     cout << "=========> xb0, xbf = " << xb0 << ", " << xbf << endl;
161     // x.setRange("ctrlregion",xb0.getVal(),xbf.getVal());
162     x.setRange("ctrlregion",xb0,xbf);
163    
164     // Construct a chi^2 of the data and the model,
165     // which is the input probability density scaled
166     // by the number of events in the dataset
167     // RooChi2Var chi2("chi2","chi2",landau,*dh0,Range(xb0.getVal(),xbf.getVal()));
168     RooChi2Var chi2("chi2","chi2",landau,*dh0,Range("ctrlregion"));
169     // Use RooMinuit interface to minimize chi^2
170     RooMinuit m(chi2);
171     m.migrad();
172     m.hesse();
173    
174     if ((fabs(chi2.getVal() - bound[0]) <= epslon) ||
175     (fabs(chi2.getVal() - temp) <= epslon) ||
176     (fabs(xb0 - tempb) < = epslon1 )
177     ){
178     nsiter++;
179     cout << "=========> Delta chi2 converges for " << nsiter << "time(s)" << endl;
180     }
181     if (chi2.getVal() < bound[0]){
182     bound[0] = chi2.getVal();
183     bound[1] = xb0;
184     bound[2] = xbf;
185     pass[1] = MPV.getVal();
186     pass[2] = Sig.getVal();
187     nQCDsigw = landau.createIntegral(x,NormSet(x),Range("signalregion"));
188     nQCDbkgw = landau.createIntegral(x,NormSet(x),Range("ctrlregion"));
189     cout << "=========> New Min chi2 = " << chi2.getVal() <<endl;
190     cout << "=========> Fitting range = " << bound[1] << " to " << bound[2] << endl;
191     landau.plotOn(xframe,NormRange("ctrlregion"),Range("ctrlregion"),LineColor(niter));
192     landau.paramOn(xframe,Layout(0.28,0.75,0.42));
193     landau.plotOn(xframe,NormRange("ctrlregion"),Range("signalregion"),LineColor(niter));
194     xframe->Draw();
195     nQCDsigw->Print();
196     nQCDbkgw->Print();
197     }
198     temp = chi2.getVal();
199     tempb = xb0;
200 jengbou 1.1 }
201 jengbou 1.2 // plot
202     Double_t mpv1 = pass[1];
203     Double_t sig1 = pass[2];
204     RooRealVar MPV1("MPV1","MPV1",mpv1);
205     RooRealVar Sig1("Sig1","Sig1",sig1);
206     RooLandau landau1("landau1","landau1",y,MPV1,Sig1);
207    
208     y.setRange("signalregion",ax0,axf);
209     y.setRange("centralregion",axf,bound[1]);
210     y.setRange("ctrlregion",bound[1],bound[2]);
211    
212     landau1.plotOn(yframe,NormRange("ctrlregion"),Range("ctrlregion"));
213     landau1.plotOn(yframe,NormRange("ctrlregion"),Range("centralregion"),
214     LineColor(3),LineStyle(kDashed));
215     landau1.plotOn(yframe,NormRange("ctrlregion"),Range("signalregion"),LineColor(2));
216     c->cd(2);
217     yframe->Draw();
218    
219     }
220     else { // Fixed range
221    
222     TCanvas* c = new TCanvas("fitNewRelIso","fitNewRelIso",800,600);
223     pass[1] = MPV.getVal();
224     pass[2] = Sig.getVal();
225     bound[1] = bx0;
226     bound[2] = bxf;
227     x.setRange("signalregion",ax0,axf);
228     x.setRange("centralregion",axf,bound[1]);
229     x.setRange("ctrlregion",bound[1],bound[2]);
230    
231     landau.plotOn(xframe,NormRange("ctrlregion"),Range("ctrlregion"));
232     landau.plotOn(xframe,NormRange("ctrlregion"),Range("centralregion"),
233     LineColor(3),LineStyle(kDashed));
234     landau.plotOn(xframe,NormRange("ctrlregion"),Range("signalregion"),LineColor(2));
235     xframe->Draw();
236    
237     }
238     // h0->SetLineColor(9);
239     // h0->SetLineStyle(2);
240     // h0->SetLineWidth(2);
241     h0->Draw("same");
242     // h1->SetLineStyle(3);
243     // h1->SetLineWidth(2);
244     h1->SetLineColor(9); //40
245     // h1->SetFillStyle(3020); // 3018
246     // h1->SetFillColor(41); //38
247     h1->Draw("same");
248    
249     if (dynamic){
250     cout << "=========> chi2 of the last iteration = " << chi2.getVal() << endl;
251     cout << "=========> Fit range of the last iteration = " << xb0 << " to " << xbf << endl;
252     cout << "================================================================" << endl;
253     cout << "=========> Minimum chi2 = " << bound[0] << endl;
254 jengbou 1.1 }
255     cout << "=========> Final fit range = " << bound[1] << " to " << bound[2] << endl;
256     cout << "=========> MPV = " << pass[1] << "; Sig = " << pass[2] << endl;
257     // Print values of mean and sigma (that now reflect fitted values and errors)
258     cout << "=========> Final MPV = pass[1] = " << pass[1] << endl;
259 jengbou 1.2 cout << "=========> Final SIG = pass[2] = " << pass[2] << endl;
260 jengbou 1.1
261     // Get number of events within fitted region ==> cross check
262     TAxis *axis = h0->GetXaxis();
263     int bmin = axis->FindBin(xb0);
264     int bmax = axis->FindBin(xbf);
265     double nfac = h0->Integral(bmin,bmax);
266     nfac -= (h0->GetBinContent(bmin))*(xb0-axis->GetBinLowEdge(bmin))/axis->GetBinWidth(bmin);
267     nfac -= (h0->GetBinContent(bmax))*(axis->GetBinUpEdge(bmax)-xbf)/axis->GetBinWidth(bmax);
268     cout << "=========> Final Nfac = " << nfac << endl;
269     nQCDsigw->Print();
270     nQCDbkgw->Print();
271     Double_t nQCDsig = nfac * nQCDsigw->getVal()/nQCDbkgw->getVal();
272     cout << "=========> Number of observed QCD evts = " << nQCDsig << endl;
273     // Print results
274     cout << "\n";
275     cout << ">>>>> Jet bin " << jbin << ":" << endl;
276     cout << ">>>>> Observed Ns = " << nQCDsig << endl;
277     cout << ">>>>> Expected Na = " << na << endl;
278     cout << "Deviation = " << (nQCDsig-na)/na*100.0 << " %" <<endl;
279 jengbou 1.2
280 jengbou 1.1 }