forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro9.C
76 lines (67 loc) · 2.15 KB
/
macro9.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Toy Monte Carlo example.
// Check pull distribution to compare chi2 and binned
// log-likelihood methods.
void pull( int n_toys = 10000,
int n_tot_entries = 100,
int nbins = 40,
bool do_chi2=true ){
TString method_prefix("Log-Likelihood ");
if (do_chi2)
method_prefix="#chi^{2} ";
// Create histo
TH1F h4(method_prefix+"h4",
method_prefix+" Random Gauss",
nbins,-4,4);
h4.SetMarkerStyle(21);
h4.SetMarkerSize(0.8);
h4.SetMarkerColor(kRed);
// Histogram for sigma and pull
TH1F sigma(method_prefix+"sigma",
method_prefix+"sigma from gaus fit",
50,0.5,1.5);
TH1F pull(method_prefix+"pull",
method_prefix+"pull from gaus fit",
50,-4.,4.);
// Make nice canvases
auto c0 = new TCanvas(method_prefix+"Gauss",
method_prefix+"Gauss",0,0,320,240);
c0->SetGrid();
// Make nice canvases
auto c1 = new TCanvas(method_prefix+"Result",
method_prefix+"Sigma-Distribution",
0,300,600,400);
c0->cd();
float sig, mean;
for (int i=0; i<n_toys; i++){
// Reset histo contents
h4.Reset();
// Fill histo
for ( int j = 0; j<n_tot_entries; j++ )
h4.Fill(gRandom->Gaus());
// perform fit
if (do_chi2) h4.Fit("gaus","q"); // Chi2 fit
else h4.Fit("gaus","lq"); // Likelihood fit
// some control output on the way
if (!(i%100)){
h4.Draw("ep");
c0->Update();}
// Get sigma from fit
TF1 *fit = h4.GetFunction("gaus");
sig = fit->GetParameter(2);
mean= fit->GetParameter(1);
sigma.Fill(sig);
pull.Fill(mean/sig * sqrt(n_tot_entries));
} // end of toy MC loop
// print result
c1->cd();
pull.DrawClone();
}
void macro9(){
int n_toys=10000;
int n_tot_entries=100;
int n_bins=40;
cout << "Performing Pull Experiment with chi2 \n";
pull(n_toys,n_tot_entries,n_bins,true);
cout << "Performing Pull Experiment with Log Likelihood\n";
pull(n_toys,n_tot_entries,n_bins,false);
}