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

Comparing UserCode/Jeng/scripts/fitLandau.C (file contents):
Revision 1.3 by jengbou, Mon Apr 27 22:37:00 2009 UTC vs.
Revision 1.4 by jengbou, Mon May 4 16:49:27 2009 UTC

# Line 17 | Line 17 | using namespace std;
17   #include "Math/GSLIntegrator.h"
18   #include "Math/WrappedTF1.h"
19  
20 < TString fileName = "skimmed/QCDbin4_005.root";
20 > // Full Sim
21 > // TString fileName = "skimmed226/QCDbin4_NewRelIso_rebin1_TandL.root";
22 > // TString fileName = "skimmed226/QCDbin123_NewRelIso_0053.root";
23 > // TString fileName = "skimmed226/QCDbin1_NewRelIso_011.root";
24 > TString fileName = "skimmed226/QCDbin4_NewRelIso_rebin_TandL.root";
25 > // Fast Sim
26 > // TString fileName = "skimmed226/QCDbin1_NewRelIso_FastSim_0053.root";
27 > // TString fileName = "skimmed226/QCDbin4_NewRelIso_rebin_FastSim_0053.root";
28   // Dynamic(true) or static(false) range
29 +
30   bool dynamic = true;
31   // Dynamic fit region weight
32 < double wxmin = 2.; // 1.82 for loose 2.1 for tight
33 < double wxmax = 0.8; // 0.6 for loose 0.8 for tight
32 > double wxmin = 1.81875; // 1.82 for loose 2.1 for tight 1.81875 for full
33 > double wxmax = wxmin/2.; // 0.6 for loose 0.8 for tight
34   // Select fitting function:
35   TString s0 = "landau";
36   // Select New or Old reliso:
37 < TString isoname = "New";
37 > TString isoname = "New"; // Only New for Landau
38  
39   void fitLandau()
40   {
41 +  gROOT->SetStyle("CMS");
42 +  //   gStyle->SetOptStat("n");
43 +  //   gStyle->SetOptTitle(1);
44    gStyle->SetOptFit(01111);
45    
46    TFile *file = TFile::Open(fileName);
# Line 37 | Line 48 | void fitLandau()
48    TH1D *h0 = (TH1D*)file->Get("histos/h_RelIso_all");
49    TH1D *h1 = (TH1D*)file->Get("histos/h_RelIso_qcd");
50    Int_t jbin;
51 <  Double_t na;
51 >  Double_t na,na1;
52    tree->SetBranchAddress("jbin",&jbin);
53    tree->SetBranchAddress("na",&na);
54 <  //   Int_t nentries = (Int_t)tree->GetEntries();
54 >  if (fileName.Contains("TandL"))
55 >    tree->SetBranchAddress("na1",&na1);  
56    tree->GetEntry(0);
57    // define fit region:
58    double ax0 = 0.;
59    double axf = 0.;
60    double bx0= 0.;
61    double bxf = 0.;
62 +  double loosecut = 0.;
63    
64    cout<<"Use "<<isoname<<" RelIso"<<endl;
65    
66    if ( isoname.Contains("Old") ) {
67      // old
68 +    loosecut = 0.9;
69      ax0 = 0.95;
70      axf = 1.0;
71      bx0 = 0.0;
# Line 60 | Line 74 | void fitLandau()
74    else if ( isoname.Contains("New") ) {
75      // new
76      ax0 = 0.0;
77 <    axf = 0.05; // default: 0.053; loose:0.11
78 <    bx0 = 0.2;
79 <    bxf = 2.;
77 >    axf = 0.053; // default: 0.053; loose:0.11
78 >    loosecut = 0.11;
79 >    bx0 = 0.2; // 0.2
80 >    bxf = 2.; // 1.2
81    }
82    
83    int niter = 1;
84    double pass[3] = {0.,0.,0.};
85 <  double ns, ns2;
86 <  ns = ns2 = 0.;
85 >  double bound[2] = {0.,0.};
86 >  double ns[2] = {0.,0.};
87 >  double ns2 = 0.;
88    
89    TCanvas *cv = new TCanvas("cv","cv",800,600);
90    
# Line 94 | Line 110 | void fitLandau()
110    else{
111      cout << "Wrong bin number!" << endl;
112    }
97  h_all->Draw();
113    
114    Int_t nbxMax = h_all->GetXaxis()->FindBin(2.0);
115    h_all->GetXaxis()->SetRange(2,h_all->GetXaxis()->FindBin(bxf));
# Line 105 | Line 120 | void fitLandau()
120    double bwidth = axis0->GetBinWidth(nbMax);
121    cout << "bwidth = " << bwidth << endl;
122    
123 +  TString title = "RelIso'";
124 +  if( jbin == 4)
125 +    title += " (#geq 4 jets)";
126 +  else if ( jbin == 1 ) {
127 +    title += " (";
128 +    title += jbin;
129 +    title += " jet)";
130 +  }
131 +  else if ( jbin < 4 ) {
132 +    title += " (";
133 +    title += jbin;
134 +    title += " jets)";
135 +    h_RelIso_all->SetTitle(title);
136 +  }
137 +  else if ( jbin == 5 ) {
138 +    h_RelIso_all->SetTitle( isoname+" RelIso distribution (#geq 1 jets (up to 3 jets))");
139 +  }
140 +  else {
141 +    cout<<"Don't mess around!!! jbin should be less than 6!"<<endl;
142 +    return;
143 +  }
144 +
145 +  h_all->GetXaxis()->SetTitle(title);
146 +  h_all->GetXaxis()->SetLabelFont(42);
147 +  h_all->GetXaxis()->SetLabelSize(0.04);
148 +  h_all->GetXaxis()->SetTitleFont(42);
149 +  h_all->GetXaxis()->SetTitleSize(0.04);
150 +  h_all->GetXaxis()->SetTitleOffset(1.2);
151 +  h_all->GetYaxis()->SetTitle("Events");
152 +  h_all->GetYaxis()->SetLabelFont(42);
153 +  h_all->GetYaxis()->SetLabelSize(0.04);
154 +  h_all->GetYaxis()->SetTitleFont(42);
155 +  h_all->GetYaxis()->SetTitleSize(0.04);
156 +  h_all->GetYaxis()->SetTitleOffset(1.2);
157 +  h_all->GetXaxis()->SetRangeUser(0.,1.6);
158 +  h_all->Draw();
159 +  
160    cout << "\n>>>>> Iteration step: "<< niter << endl;
161    h_all->Fit(s0,"0","",bx0,bxf);
162    
# Line 115 | Line 167 | void fitLandau()
167    Int_t ndof = f0->GetNDF();
168    pass[0] = chi2/ndof;
169    pass[1] = pass[0];
170 <  cout << ">> Norm chi2 = " << pass[0] << endl;
170 >  cout << ">>>> Norm chi2 = " << pass[0] << endl;
171      
172    double * par0 = new double [npar];
173    double * parErr0 = new double [npar];
# Line 129 | Line 181 | void fitLandau()
181      par0[i] = f0->GetParameter(i);
182      parErr0[i] = f0->GetParError(i);
183    }
184 <  
184 >  bound[0] = bx0;
185 >  bound[1] = bxf;  
186    if(dynamic) {
187      bx0 = f0->GetParameter(1) - wxmin*f0->GetParameter(2);
188      bxf = f0->GetParameter(1) + wxmax*f0->GetParameter(2);
189    }
190    double delta = pass[1]-pass[2];
191    
192 <  while (dynamic && niter <= 20 && abs(pass[1]-pass[2]) > 0.00001) {
192 >  while (dynamic && niter <= 20 && abs(delta) > 0.00001) {
193      if (niter > 1)
194 <      pass[1]=pass[2];
195 <    if (delta >= 0){
194 >      pass[1] = pass[2];
195 >    if (delta != 0){
196        niter++;
197        cout << "\n>>>>> Iteration step: "<< niter << endl;
198 <      cout << ">> Pass[1] = " << pass[1] << endl;
198 >      cout << ">> Previous step norm. chi2 = " << pass[1] << endl;
199 >      cout << ">>>> Starting fitting new range (bx0, bxf) = (" << bx0;
200 >      cout << ", " << bxf << ")" << endl;
201        
202        h_all->Fit(s0,"0","",bx0,bxf);
203        TF1 *fi = (TF1*)h_all->GetFunction(s0);    
204        pass[2] = fi->GetChisquare()/fi->GetNDF();
205 <      cout << ">> Norm chi2 = pass[2] = " << pass[2] << endl;
205 >      cout << ">> Current step norm. chi2  = " << pass[2] << endl;
206        delta = pass[1]-pass[2];
207        cout << ">> Delta = " << delta << endl;
208        
209 <      if (delta >= 0) {
209 >      if (delta > 0) {
210 >        bound[0] = bx0;
211 >        bound[1] = bxf;
212          printf("Function has %i parameters. Chisquare = %g\n",
213                 npar,
214                 fi->GetChisquare());
# Line 167 | Line 224 | void fitLandau()
224          }
225          bx0 = fi->GetParameter(1) - wxmin*fi->GetParameter(2);
226          bxf = fi->GetParameter(1) + wxmax*fi->GetParameter(2);
227 <        cout << ">> bx0, bxf = " << bx0 << ", " << bxf << endl;
227 >        cout << ">> Range for next iteration (bx0, bxf) = (" << bx0;
228 >        cout << ", " << bxf << ")" << endl;
229 >      }
230 >      else if (delta < 0) {
231 >        cout << " Use previous fit parameters for extrapolation fit" << endl;
232 >        delta = 0;
233        }
234        else {
235 <        cout << ">> Use previous fit!" << endl;
235 >        bound[0] = (bound[0]<bx0)?bound[0]:bx0;
236 >        bound[1] = (bound[1]>bxf)?bound[1]:bxf;
237 >        cout << ">> Fit converges." << endl;
238 >        cout << ">> Best fitting region = (" << bound[0];
239 >        cout << ", " << bound[1] << ")" << endl;
240        }
241      }
242    }
243 +  cout << ">>> First fit norm. chi2 = " << pass[0] << endl;
244 +  cout << ">>> Min norm. chi2       = " << pass[1] << endl;
245 +  cout << ">>> Last fitting region (bx0, bxf) = (" << bx0 << ", " << bxf << ")"<< endl;
246 +  cout << ">>> Best fitting region (bx0, bxf) = (" << bound[0] << ", " << bound[1] << ")"<< endl;
247    
248 <  cout << ">>> Final fitting resion (bx0, bxf) = " << bx0 << ", " << bxf << endl;
179 <  
180 <  // Get number of events within fitted region
248 >  // Get number of events within fitted (control) region
249    TAxis *axis = h_all->GetXaxis();
250 <  int bmin = axis->FindBin(bx0);
251 <  int bmax = axis->FindBin(bxf);
250 >  int bmin = axis->FindBin(bound[0]);
251 >  int bmax = axis->FindBin(bound[1]);
252    double nfac1 = h_all->Integral(bmin,bmax);
253 <  nfac1 -= (h_all->GetBinContent(bmin))*(bx0-axis->GetBinLowEdge(bmin))/axis->GetBinWidth(bmin);
254 <  nfac1 -= (h_all->GetBinContent(bmax))*(axis->GetBinUpEdge(bmax)-bxf)/axis->GetBinWidth(bmax);
253 >  nfac1 -= (h_all->GetBinContent(bmin))*(bound[0]-axis->GetBinLowEdge(bmin))/axis->GetBinWidth(bmin);
254 >  nfac1 -= (h_all->GetBinContent(bmax))*(axis->GetBinUpEdge(bmax)-bound[1])/axis->GetBinWidth(bmax);
255    cout << ">>> Final Nfac = " << nfac1 << endl;
256    
257    // ////////////////////////
258    // Histos and extrapolation
259    
260    TF1 *f1 = (TF1*)f0->Clone();
261 <  f1->SetRange(bx0,bxf);
261 >  f1->SetRange(bound[0],bound[1]);
262    for (int i=0;i<npar;i++) {
263      cout<<" >> Par["<<i<<"]="<<par0[i]<<"; Error="<<parErr0[i]<<endl;
264      f1->SetParameter(i,par0[i]);
265    }
198  
266    f1->SetLineColor(2);
267 +  
268 +  TPaveStats *p2 = (TPaveStats*)h_qcd->GetListOfFunctions()->FindObject("stats");
269 +  h_qcd->GetListOfFunctions()->Remove(p2);
270 +  h_qcd->SetStats(0);
271    h_qcd->SetLineColor(40);
272    h_qcd->SetFillStyle(3018);
273    h_qcd->SetFillColor(38);
274 <  h_qcd->Draw("same");
275 <  f1->Draw("same");
274 >  h_qcd->Draw("sames");
275 >  f1->Draw("sames");
276    
277    // Connect fitted and extrapolated region
278    TF1 *fin = (TF1*)f1->Clone();
279 <  fin->SetRange(axf,bx0);
279 >  fin->SetRange(axf,bound[0]);
280    fin->SetLineStyle(2);
281    fin->SetLineColor(8);
282 <  fin->Draw("same");
282 >  fin->Draw("sames");
283 >
284 >  TF1 *fine = (TF1*)f1->Clone();
285 >  fine->SetRange(bound[1],2.0);
286 >  fine->SetLineStyle(2);
287 >  fine->SetLineColor(8);
288 >  fine->Draw("sames");
289    
290    TF1 *f2 = (TF1*)f1->Clone();
291    f2->SetRange(ax0,axf);
292    f2->SetLineColor(4);
293 <  f2->Draw("same");
293 >  f2->Draw("sames");
294    int np = 100;
295    double *x=new double[np];
296    double *w=new double[np];
297    f2->CalcGaussLegendreSamplingPoints(np,x,w,1e-15);
298 <  ns = f2->IntegralFast(np,x,w,ax0,axf);
299 <  ns/=(f2->Integral(bx0,bxf));
300 <  ns*=nfac1;
301 <  cout<<">>> Ns by usual integral = "<<ns<<endl;
298 >
299 >  if (fileName.Contains("0053") || fileName.Contains("TandL")) {
300 >    ns[0] = f2->IntegralFast(np,x,w,ax0,axf);
301 >    ns[0]/=(f2->Integral(bound[0],bound[1]));
302 >    ns[0]*=nfac1;
303 >  }
304 >  else if (fileName.Contains("011")) {
305 >    ns[0] = f2->IntegralFast(np,x,w,ax0,loosecut);
306 >    ns[0]/=(f2->Integral(bound[0],bound[1]));
307 >    ns[0]*=nfac1;
308 >  }
309 >  
310 >  ns[1] = f2->IntegralFast(np,x,w,ax0,loosecut);
311 >  ns[1]/=(f2->Integral(bound[0],bound[1]));
312 >  ns[1]*=nfac1;
313 >  
314 >  if (fileName.Contains("TandL") || fileName.Contains("011"))
315 >    cout << ">>> Loose Ns by usual integral    = " << ns[1] << endl;
316 >  else
317 >    cout << ">>> Tight Ns by usual integral    = " << ns[0] << endl;
318 >
319    delete [] x;
320    delete [] w;
321    
# Line 230 | Line 324 | void fitLandau()
324    ROOT::Math::GSLIntegrator ig(1.E-8,1.E-8,1000);
325    ROOT::Math::WrappedTF1 wf(*g);
326    ig.SetFunction(wf);
327 <  ns2 = ig.Integral(ax0,axf);
328 <  ns2/=(g->Integral(bx0,bxf));
327 >  if (fileName.Contains("0053") || fileName.Contains("TandL"))
328 >    ns2 = ig.Integral(ax0,axf);
329 >  else if (fileName.Contains("011"))
330 >    ns2 = ig.Integral(ax0,loosecut);
331 >  
332 >  ns2/=(g->Integral(bound[0],bound[1]));
333    ns2*=nfac1;
334 <  cout<<">>> Ns by MathMore integral = "<<ns2<<endl;
335 <    
334 >  cout<<">>> Ns by MathMore integral       = "<<ns2<<endl;
335 >  
336    cv->Update();
337    
338 <  TPaveStats *p1 = (TPaveStats*)h_all->GetListOfFunctions()->FindObject("stats");
339 <  p1->SetTextColor(kBlue);
340 <  p1->SetX1NDC(0.6);
338 >  //   TPaveStats *p1 = (TPaveStats*)h_all->GetListOfFunctions()->FindObject("stats");
339 >  //   p1->SetTextColor(kBlue);
340 >  TPaveStats *p1 = (TPaveStats*)cv->GetPrimitive("stats");
341 >  p1->SetName("Landau fit");
342 >  TList *list = p1->GetListOfLines();
343 >  //   TText *tconst = p1->GetLineWith("Constant");
344 >  //   list->Remove(tconst);
345 >  TLatex *myt = new TLatex(0,0,"Landau fit");
346 >  list->AddFirst(myt);
347 >  h_all->SetStats(0);
348 >  cv->Modified();
349 >  
350 >  p1->SetTextFont(42);
351 >  p1->SetFillColor(0);
352 >  p1->SetFillStyle(0);
353 >  p1->SetBorderSize(0);
354 >  p1->SetX1NDC(0.65);
355    p1->SetX2NDC(0.88);
356 <  p1->SetY1NDC(0.62);
357 <  p1->SetY2NDC(0.88);
358 <  p1->Draw();
356 >  p1->SetY1NDC(0.65);
357 >  p1->SetY2NDC(0.85);
358 >  gPad->Update();
359    
360    // Label histo
361 <  TLegend * leg1 = new TLegend(0.3,0.15,0.68,0.45);
362 <  leg1->AddEntry(h_all,"All events (S+B)");
363 <  leg1->AddEntry(h_qcd,"QCD events");
361 >  TLegend * leg1 = new TLegend(0.25,0.65,0.5,0.85);
362 >  leg1->SetFillColor(0);
363 >  leg1->SetFillStyle(0);
364    leg1->AddEntry(f1,"Fit of all events in control region");
365    leg1->AddEntry(f2,"Extrapolation to signal region");
366 +  leg1->AddEntry(h_all,"All events (S+B)");
367 +  leg1->AddEntry(h_qcd,"QCD events");
368    leg1->Draw();
369 +  
370    // Print results
371    cout << "\n";
372    cout << ">>>>> Jet bin " << jbin << ":" << endl;
373 <  cout << ">>>>> Observed Ns = " << ns << endl;
373 >  cout << ">>>>> Bin number @ peak = " << nbMax << endl;
374 >  cout << ">>>>> bwidth = " << bwidth << endl;
375 >  cout << ">>>>> First fit norm. chi2 = " << pass[0] << endl;
376 >  cout << ">>>>> Min norm. chi2       = " << pass[1] << endl;
377 >  cout << ">>>>> Last fitting region (bx0, bxf) = (" << bx0 << ", " << bxf << ")"<< endl;
378 >  cout << ">>>>> Best fitting region (bx0, bxf) = (" << bound[0] << ", " << bound[1] << ")"<< endl;
379 >  cout << ">>>>> Observed Ns = " << ns[0] << endl;
380    cout << ">>>>> Expected Na = " << na << endl;
381 <  cout << "Deviation = " << (ns-na)/na*100.0 << " %" <<endl;
381 >  cout << "Deviation = " << (ns[0]-na)/na*100.0 << " %" <<endl;
382  
383 +  if (fileName.Contains("TandL")) {
384 +    cout << "====================================" << endl;
385 +    cout << ">>>>> Observed Loose Ns = " << ns[1] << endl;
386 +    cout << ">>>>> Expected Loose Na = " << na1 << endl;
387 +    cout << "Deviation = " << (ns[1]-na1)/na1*100.0 << " %" <<endl;
388 +  }
389   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines