Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
macro9.C
Go to the documentation of this file.
1// Toy Monte Carlo example.
2// Check pull distribution to compare chi2 and binned
3// log-likelihood methods.
4
5void pull( int n_toys = 10000,
6 int n_tot_entries = 100,
7 int nbins = 40,
8 bool do_chi2=true ){
9
10 TString method_prefix("Log-Likelihood ");
11 if (do_chi2)
12 method_prefix="#chi^{2} ";
13
14 // Create histo
15 TH1F h4(method_prefix+"h4",
16 method_prefix+" Random Gauss",
17 nbins,-4,4);
18 h4.SetMarkerStyle(21);
19 h4.SetMarkerSize(0.8);
20 h4.SetMarkerColor(kRed);
21
22 // Histogram for sigma and pull
23 TH1F sigma(method_prefix+"sigma",
24 method_prefix+"sigma from gaus fit",
25 50,0.5,1.5);
26 TH1F pull(method_prefix+"pull",
27 method_prefix+"pull from gaus fit",
28 50,-4.,4.);
29
30 // Make nice canvases
31 auto c0 = new TCanvas(method_prefix+"Gauss",
32 method_prefix+"Gauss",0,0,320,240);
33 c0->SetGrid();
34
35 // Make nice canvases
36 auto c1 = new TCanvas(method_prefix+"Result",
37 method_prefix+"Sigma-Distribution",
38 0,300,600,400);
39 c0->cd();
40
41 float sig, mean;
42 for (int i=0; i<n_toys; i++){
43 // Reset histo contents
44 h4.Reset();
45 // Fill histo
46 for ( int j = 0; j<n_tot_entries; j++ )
47 h4.Fill(gRandom->Gaus());
48 // perform fit
49 if (do_chi2) h4.Fit("gaus","q"); // Chi2 fit
50 else h4.Fit("gaus","lq"); // Likelihood fit
51 // some control output on the way
52 if (!(i%100)){
53 h4.Draw("ep");
54 c0->Update();}
55
56 // Get sigma from fit
57 TF1 *fit = h4.GetFunction("gaus");
58 sig = fit->GetParameter(2);
59 mean= fit->GetParameter(1);
60 sigma.Fill(sig);
61 pull.Fill(mean/sig * sqrt(n_tot_entries));
62 } // end of toy MC loop
63 // print result
64 c1->cd();
65 pull.DrawClone();
66}
67
68void macro9(){
69 int n_toys=10000;
70 int n_tot_entries=100;
71 int n_bins=40;
72 cout << "Performing Pull Experiment with chi2 \n";
73 pull(n_toys,n_tot_entries,n_bins,true);
74 cout << "Performing Pull Experiment with Log Likelihood\n";
75 pull(n_toys,n_tot_entries,n_bins,false);
76}
@ kRed
Definition Rtypes.h:67
externTRandom * gRandom
Definition TRandom.h:62
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:182
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
Basic string class.
Definition TString.h:138
const Double_t sigma
Definition h1analysis.C:161
return c1
Definition legend1.C:41
void macro9()
Definition macro9.C:68
void pull(int n_toys=10000, int n_tot_entries=100, int nbins=40, bool do_chi2=true)
Definition macro9.C:5