GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
MonteCarlo.cc
Go to the documentation of this file.
1#include "MonteCarlo.hh"
2
3#include <TStyle.h>
4#include <TSystem.h>
5
6namespace GamR {
7namespace TK {
8
9std::shared_ptr<TH2D> MonteCarlo(std::function<std::pair<double, double>(TH1 *, TRandom3 &)> func, TH1 *system,
10 size_t ntoys, Bool_t draw, const Bool_t weight, TCanvas *c1)
11{
12 TRandom3 rndgen;
13 rndgen.SetSeed(0430204771);
14
15 if (!c1) {
16 c1 = new TCanvas("montecarlo", "Monte-Carlo Experiment", 1280, 720);
17 }
18 gStyle->SetOptStat();
19
20 auto UpdateCanvas = [&]() {
21 c1->Modified();
22 c1->Update();
23 c1->Draw();
24 gSystem->ProcessEvents();
25 };
26
27 UpdateCanvas();
28
29 if (draw) {
30 c1->SetBatch(kFALSE);
31 } else {
32 c1->SetBatch(kTRUE);
33 }
34
35 auto pad_input = std::make_unique<TPad>("pad_input", "Experiment", 0, 0, 0.5, 1);
36 pad_input->Draw();
37 auto pad_results = std::make_unique<TPad>("pad_results", "Observations", 0.5, 0, 1, 1);
38 pad_results->Draw();
39 auto hist_results = std::make_shared<TH2D>("mc_results", "Monte-Carlo Results", 100, 0, 0, 100, 0, 0);
40 hist_results->SetCanExtend(TH1::kAllAxes);
41 hist_results->Sumw2();
42
43 for (size_t i = 0; i < ntoys; ++i) {
44 TH1 *copy = static_cast<TH1 *>(system->Clone());
45 std::pair<double, double> result = func(copy, rndgen);
46 if (weight) {
47 hist_results->Fill(result.first, result.second, result.second);
48 } else {
49 hist_results->Fill(result.first, result.second);
50 }
51
52 if ((i && (i % 200 == 0)) || i == ntoys) {
53 pad_input->cd();
54 double min = system->GetMinimum() == 0 ? system->GetMinimum(0) : system->GetMinimum();
55 double max = system->GetMaximum() == 0 ? system->GetMaximum(0) : system->GetMaximum();
56 int minbin;
57 system->GetBinWithContent(min, minbin);
58 int maxbin;
59 system->GetBinWithContent(max, maxbin);
60 copy->SetMinimum(min - 5 * system->GetBinError(minbin));
61 copy->SetMaximum(max + 3 * system->GetBinError(maxbin));
62
63 copy->Draw("HIST");
64
65 pad_results->cd();
66 // double rxmin = hist_results -> GetXaxis() -> GetXmin();
67 // double rxmax = hist_results -> GetXaxis() -> GetXmax();
68
69 // double rymin = hist_results -> GetYaxis() -> GetXmin();
70 // double rymax = hist_results -> GetYaxis() -> GetXmax();
71
72 // int dx = static_cast<int>(rxmax - rxmin);
73 // int dy = static_cast<int>(rymax - rymin);
74 // int square = gcd(dx,dy);
75 // hist_results -> GetXaxis() -> SetRangeUser(rxmin, rxmin*square);
76 // hist_results -> GetYaxis() -> SetRangeUser(rymin, rymin*square);
77
78 hist_results->Draw("COLZ 0");
79 // pad_project -> cd();
80 // hist_results -> ProjectionX() -> Draw("HIST");
81 UpdateCanvas();
82 }
83 delete copy;
84 }
85 return hist_results;
86}
87
88/* Tim: this is copy/pasted from one of my macros. Obviously designed for
89 the specific purpose of Monte Carlo fitting ratio functions, but it's easily
90 adaptable to be pretty general I think. Pass it a histogram and any TF1, it
91 fetches parameter names, makes a TNtuple, and does the Monte Carlo fit, using
92 the parameters of the TF1 as initial guesses and storing the results of each
93 iteration in the TNTuple, which is written to disk.
94
95 Obviously heaps that's beyond that though, making pictures etc.
96
97*/
98/*
99 void monteCarlo_fit(TH1 *hist, TF1 *function, int iterations, const char
100 *name, const char *fileName){
101 //float start, tau, amplitude, omega, phase;
102 const int nParams=function->GetNpar();
103 cout << nParams << " parameters" << endl;
104
105 float parameters[nParams];
106 float * paramPoint;
107
108 TString s(function->GetParName(0));
109
110 for (int i=1; i<nParams; i++){
111 s=s.Append(":");
112 s=s.Append(function->GetParName(i));
113 }
114
115 cout << s.Data() << endl;
116
117 TFile *outFile=new TFile(fileName, "recreate");
118
119 TNtuple *fits= new TNtuple(name, hist->GetTitle(), s.Data());
120 TRandom3 *random = new TRandom3();
121
122 TH1D *fitHist=static_cast<TH1D*>(hist->Clone("fitHist"));
123 TH1D *rebinHist;
124 TH1D *temp=new TH1D("temp", "Frequency Histogram", 200, 0.47, 0.57);
125
126 int nBins=hist->GetNbinsX();
127
128 TCanvas *simulCanvas=new TCanvas("simulCanvas", "Monte Carlo Simulation", 900,
129 450);
130 //c1->Divide(2,1);
131 TPad *ratiopad=new TPad("ratiopad", "Ratio Function", 0, 0, 0.5, 1);
132 ratiopad->Draw();
133 TPad *histpad=new TPad("histpad", "Frequency Histogram", 0.5, 0, 1, 1);
134 histpad->Draw();
135
136 ratiopad->cd();
137 rebinHist = dynamic_cast<TH1D*>(fitHist->Rebin(8, "rebinHist"));
138 rebinHist->SetTitle("Ratio Function");
139 rebinHist->Scale(1./8.);
140 rebinHist->Draw("hist");
141
142 histpad->cd();
143 temp->Draw();
144
145 rebinHist->GetXaxis()->SetRangeUser(680, 775);
146 rebinHist->SetMaximum(0.15);
147 rebinHist->SetMinimum(-0.15);
148 rebinHist->SetStats(kFALSE);
149
150 fitHist->GetXaxis()->SetRangeUser(680, 775);
151 fitHist->SetMaximum(0.15);
152 fitHist->SetMinimum(-0.15);
153 fitHist->SetStats(kFALSE);
154
155 for (int i=0; i<iterations; i++){
156 //fitHist->Reset();
157
158 TF1 *fitFunc = (TF1*)function->Clone("fitFunc");
159
160 //fill histogram with new data
161 for (int bin=1; bin<nBins; bin++) {
162 fitHist->SetBinContent(bin, random->Gaus(hist->GetBinContent(bin),
163 hist->GetBinError(bin)));
164 }
165
166 int fitStat = fitHist->Fit(fitFunc, "RWQ");
167 if ( fitStat != 0 ){
168 cout << "status is " << fitStat << endl;
169 }
170 else{
171 for (int param=0; param<nParams; param++){
172 parameters[param]=fitFunc->GetParameter(param);
173 }
174
175 paramPoint=parameters;
176
177 fits->Fill(paramPoint);
178 temp->Fill(parameters[3]);
179
180 if (i%100==0){
181 ratiopad->cd();
182 //rebinHist->Reset();
183
184 rebinHist = dynamic_cast<TH1D*>(fitHist->Rebin(8, "rebinHist"));
185 rebinHist->Scale(1./8.);
186 rebinHist->SetTitle("Ratio Function");
187
188 rebinHist->GetXaxis()->SetRangeUser(680, 775);
189 rebinHist->SetMaximum(0.15);
190 rebinHist->SetMinimum(-0.15);
191 rebinHist->SetStats(kFALSE);
192
193 rebinHist->Draw("hist");
194
195 fitFunc->Draw("same");
196
197 histpad->cd();
198 //temp->Reset();
199 //fits->Draw("p3>>temp");
200 temp->Draw("E1");
201
202 simulCanvas->Draw();
203 simulCanvas->Modified();
204 simulCanvas->Update();
205
206 TString gifFrame;
207
208 gifFrame.Form("ratioMonteCarlo%04d.gif",i);
209
210 simulCanvas->Print(gifFrame.Data());
211 }
212
213 }
214
215 }
216
217 //:simulCanvas->Print("/home/tjg103/ratioMonteCarlo.gif++");
218
219
220 fits->Write();
221
222 outFile->Close();
223 delete outFile;
224
225 }
226*/
227} /* namespace TK */
228} /* namespace GamR */
std::shared_ptr< TH2D > MonteCarlo(std::function< std::pair< double, double >(TH1 *, TRandom3 &)> func, TH1 *system, size_t ntoys, Bool_t draw, const Bool_t weight, TCanvas *c1)
Definition MonteCarlo.cc:9
Definition Gain.cc:19