ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/fitNewRelIso.C
(Generate patch)

Comparing UserCode/Jeng/scripts/fitNewRelIso.C (file contents):
Revision 1.1 by jengbou, Mon Apr 27 22:03:02 2009 UTC vs.
Revision 1.2 by jengbou, Tue Apr 28 04:20:18 2009 UTC

# Line 14 | Line 14
14   #include "TH1D.h"
15   #include "TTree.h"
16   using namespace RooFit;
17 <
18 <
17 > // .493/.2114 j1
18 > //.773/.3257  j2
19 > // .863/.353 j3
20 > //.808/.294 j4
21   void fitNewRelIso()
22   {
23 <  TString fileName = "skimmed/QCDbin2_005.root";
23 >  TString fileName = "skimmed226/QCDbin4_005.root";
24    //   TString fileName = "skimmed/QCDbinall_NewRelIso.root";
25    //   TString fileName = "skimmed/QCDbin4_NewRelIso_CD30_rebin2.root";
26    //   TString fileName = "skimmed/QCDbin3_NewRelIso_CD.root";
27 <  //TString fileName = "skimmed/QCDbin4_rebin.root";
28 <  //TString fileName = "skimmed/QCDbin4_NewRelIso_FastSim.root";
27 >  //   TString fileName = "skimmed/QCDbin4_rebin.root";
28 >  //   TString fileName = "skimmed/QCDbin4_NewRelIso_FastSim.root";
29    
30    // Dynamic(true) or static(false) range
31    bool dynamic = true;
# Line 31 | Line 33 | void fitNewRelIso()
33    double epslon = 1.e-5;
34    double epslon1 = 1.e-6;
35    // Dynamic fit region weight
36 <  double wxmin = 1.7; // 1.81 1.7
37 <  double wxmax = .94; // 0.9 .95
36 >  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    
43    Double_t ax0=0.;
44    Double_t axf=0.05;
# Line 59 | Line 65 | void fitNewRelIso()
65    tree->SetBranchAddress("jbin",&jbin);
66    tree->SetBranchAddress("na",&na);
67    tree->GetEntry(0);
68 <
68 >  
69    TH1D *h_all = (TH1D*)h0->Clone();
70    TH1D *h_qcd = (TH1D*)h1->Clone();
71    Int_t nbxMax = h_all->GetXaxis()->FindBin(2.0);
# Line 72 | Line 78 | void fitNewRelIso()
78    cout << "=========> bwidth = " << bwidth << endl;
79    
80    cout << "\n>>>>> Iteration step: "<< niter << endl;
81 <
81 >  
82    RooRealVar x("x","x",0.,2.);
83    RooRealVar y("y","y",0.,2.);
84    RooDataHist *dh0 = new RooDataHist("dh0","dh0",x,Import(*h0));
# Line 88 | Line 94 | void fitNewRelIso()
94    // ---------------------
95    
96    // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
97 <  // Gaussian
97 >  // Exponential parameters
98 >  RooRealVar lamda("lamda","decay constant of exponential",1,-100.,100.);
99 >  // Gaussian parameters
100    RooRealVar mean("mean","mean of gaussian",1.5,0.,2.);
101    RooRealVar sigma("sigma","width of gaussian",1,0.,100.);
102 <  // Landau
102 >  // Landau parameters
103    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 +  RooExponential expo("expo","exponential PDF",x,lamda);
107    RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma);
108    RooLandau landau("landau","landau PDF",x,MPV,Sig);
109 +  // 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    
114    // Construct plot frame in 'x'
115    RooPlot* xframe = x.frame(Title("Landau p.d.f. with data"));
# Line 105 | Line 118 | void fitNewRelIso()
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 +  //   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    
109  // G e n e r a t e   e v e n t s
110  // -----------------------------
111
128    // Make a second plot frame in x and draw both the
129    // data and the p.d.f in the frame
130 <  dh0->plotOn(xframe);
131 <  dh1->plotOn(xframe,MarkerColor(9));
132 <  dhy0->plotOn(yframe);
133 <  dhy1->plotOn(yframe,MarkerColor(9));
130 >  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    
135    // F i t   m o d e l   t o   d a t a
136    // -----------------------------
137    
138    // Fit pdf to data
123  //   MPV.setRange(0.4,1.);
124  //   Sig.setRange(0.1,0.5);
125  //   Sig.setConstant(kFALSE);
139    RooFitResult*rest = landau.fitTo(*dh0,Range("ctrlregion"),PrintLevel(-1),Save());
140    rest->Print("v");
141    niter++;
129  // Print values of mean and sigma (that now reflect fitted values and errors)
130  
131  Double_t xb0 = MPV.getVal()-wxmin*Sig.getVal();
132  Double_t xbf = MPV.getVal()+wxmax*Sig.getVal();
133  
142    RooAbsReal* nQCDsigw = landau.createIntegral(x,Range("signalregion"));
143    RooAbsReal* nQCDbkgw = landau.createIntegral(x,Range("ctrlregion"));
144    // Draw all frames on a canvas
145 <  TCanvas* c = new TCanvas("fitNewRelIso","fitNewRelIso",1000,600);
146 <  c->Divide(2,1);
139 <  c->cd(1);
145 >  Double_t xb0 = bx0;
146 >  Double_t xbf = bxf;
147    
148 <  for (;niter <= np && nsiter < 2; niter++){
148 >  if (dynamic) {
149 >    TCanvas* c = new TCanvas("fitNewRelIso","fitNewRelIso",1000,600);
150 >    c->Divide(2,1);
151 >    c->cd(1);
152 >    
153      xb0 = MPV.getVal()-wxmin*Sig.getVal();
154      xbf = MPV.getVal()+wxmax*Sig.getVal();
155 <    cout << "\n>>>>> Iteration step: "<< niter << endl;
156 <    cout << "=========> xb0, xbf = " << xb0 << ", " << xbf << endl;
157 <    x.setRange("ctrlregion",xb0,xbf);
158 <    
159 <    // Construct a chi^2 of the data and the model,
160 <    // which is the input probability density scaled
161 <    // by the number of events in the dataset
162 <    RooChi2Var chi2("chi2","chi2",landau,*dh0,Range("ctrlregion"));
163 <    // Use RooMinuit interface to minimize chi^2
164 <    RooMinuit m(chi2);
165 <    m.migrad();
166 <    m.hesse();
167 <    
168 <    if ((fabs(chi2.getVal() - bound[0]) <= epslon) ||
169 <        (fabs(chi2.getVal() - temp) <= epslon) ||
170 <        (fabs(xb0 - tempb) < = epslon1 )
171 <        ){
172 <      nsiter++;
173 <      cout << "=========> Delta chi2 converges for " << nsiter << "time(s)" << endl;
174 <    }
175 <    if (chi2.getVal() < bound[0]){
176 <      bound[0] = chi2.getVal();
177 <      bound[1] = xb0;
178 <      bound[2] = xbf;
179 <      pass[1] = MPV.getVal();
180 <      pass[2] = Sig.getVal();
181 <      nQCDsigw = landau.createIntegral(x,NormSet(x),Range("signalregion"));
182 <      nQCDbkgw = landau.createIntegral(x,NormSet(x),Range("ctrlregion"));
183 <      cout << "=========> New Min chi2 = " << chi2.getVal() <<endl;
184 <      cout << "=========> Fitting range = " << bound[1] << " to " << bound[2] << endl;
185 <      landau.plotOn(xframe,NormRange("ctrlregion"),Range("ctrlregion"),LineColor(niter));
186 <      landau.paramOn(xframe,Layout(0.28,0.75,0.42));
187 <      landau.plotOn(xframe,NormRange("ctrlregion"),Range("signalregion"),LineColor(niter));
188 <      xframe->Draw();
189 <      nQCDsigw->Print();
190 <      nQCDbkgw->Print();
155 >    
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      }
201 <    temp = chi2.getVal();
202 <    tempb = xb0;
201 >    // 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    }
184  // plot
185  Double_t mpv1 = pass[1];
186  Double_t sig1 = pass[2];
187  RooRealVar MPV1("MPV1","MPV1",mpv1);
188  RooRealVar Sig1("Sig1","Sig1",sig1);
189  RooLandau landau1("landau1","landau1",y,MPV1,Sig1);
190  
191  y.setRange("signalregion",ax0,axf);
192  y.setRange("centralregion",axf,bound[1]);
193  y.setRange("ctrlregion",bound[1],bound[2]);
194    
195  landau1.plotOn(yframe,NormRange("ctrlregion"),Range("ctrlregion"));
196  landau1.plotOn(yframe,NormRange("ctrlregion"),Range("centralregion"),
197                 LineColor(3),LineStyle(kDashed));
198  landau1.plotOn(yframe,NormRange("ctrlregion"),Range("signalregion"),LineColor(2));
199  c->cd(2);
200  yframe->Draw();
201
202    
203  cout << "=========> chi2 of the last iteration = " << chi2.getVal() << endl;
204  cout << "=========> Fit range of the last iteration = " << xb0 << " to " << xbf << endl;  
205  cout << "================================================================" << endl;
206  cout << "=========> Minimum chi2 = " << bound[0] << endl;
255    cout << "=========> Final fit range = " << bound[1] << " to " << bound[2] << endl;  
256    cout << "=========> MPV = " << pass[1] << "; Sig = " << pass[2] << endl;
209  
257    // Print values of mean and sigma (that now reflect fitted values and errors)
211  //   mean.Print();
212  //   sigma.Print();
213  //   MPV.Print();
214  //   Sig.Print();
258    cout << "=========> Final MPV = pass[1] = " << pass[1] << endl;
259 <  cout << "=========> Final MPV = pass[2] = " << pass[2] << endl;
259 >  cout << "=========> Final SIG = pass[2] = " << pass[2] << endl;
260    
261    // Get number of events within fitted region ==> cross check
262    TAxis *axis = h0->GetXaxis();
# Line 223 | Line 266 | void fitNewRelIso()
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;
226  
269    nQCDsigw->Print();
270    nQCDbkgw->Print();
271    Double_t nQCDsig = nfac * nQCDsigw->getVal()/nQCDbkgw->getVal();
# Line 234 | Line 276 | void fitNewRelIso()
276    cout << ">>>>> Observed Ns = " << nQCDsig << endl;
277    cout << ">>>>> Expected Na = " << na << endl;
278    cout << "Deviation = " << (nQCDsig-na)/na*100.0 << " %" <<endl;
279 <
279 >  
280   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines