52 void TestBinomial(
int nloop = 100,
int nevts = 100,
bool plot =
false,
bool debug =
false,
int seed = 111)
54 gStyle->SetMarkerStyle(20);
55 gStyle->SetLineWidth(2.0);
56 gStyle->SetOptStat(11);
59 hbiasNorm.Add(
new TH1D(
"h0Norm",
"Bias Histogram fit",100,-5,5));
60 hbiasNorm.Add(
new TH1D(
"h1Norm",
"Bias Binomial fit",100,-5,5));
61 TObjArray hbiasThreshold;
62 hbiasThreshold.Add(
new TH1D(
"h0Threshold",
"Bias Histogram fit",100,-5,5));
63 hbiasThreshold.Add(
new TH1D(
"h1Threshold",
"Bias Binomial fit",100,-5,5));
65 hbiasWidth.Add(
new TH1D(
"h0Width",
"Bias Histogram fit",100,-5,5));
66 hbiasWidth.Add(
new TH1D(
"h1Width",
"Bias Binomial fit",100,-5,5));
67 TH1D* hChisquared =
new TH1D(
"hChisquared",
68 "#chi^{2} probability (Baker-Cousins)", 200, 0.0, 1.0);
70 TVirtualFitter::SetDefaultFitter(
"Minuit2");
71 ROOT::Math::IntegratorOneDimOptions::SetDefaultIntegrator(
"Gauss");
83 Double_t xmin =10, xmax = 100;
84 TH1D* hM2D =
new TH1D(
"hM2D",
"x^(-2) denominator distribution",
86 TH1D* hM2N =
new TH1D(
"hM2N",
"x^(-2) numerator distribution",
88 TH1D* hM2E =
new TH1D(
"hM2E",
"x^(-2) efficiency",
91 TF1* fM2D =
new TF1(
"fM2D",
"(1-[0]/(1+exp(([1]-x)/[2])))/(x*x)",
93 TF1* fM2N =
new TF1(
"fM2N",
"[0]/(1+exp(([1]-x)/[2]))/(x*x)",
95 TF1* fM2Fit =
new TF1(
"fM2Fit",
"[0]/(1+exp(([1]-x)/[2]))",
104 Double_t normalization = 0.80;
105 Double_t threshold = 25.0;
106 Double_t width = 5.0;
108 fM2D->SetParameter(0, normalization);
109 fM2D->SetParameter(1, threshold);
110 fM2D->SetParameter(2, width);
111 fM2N->SetParameter(0, normalization);
112 fM2N->SetParameter(1, threshold);
113 fM2N->SetParameter(2, width);
114 Double_t integralN = fM2N->Integral(xmin, xmax);
115 Double_t integralD = fM2D->Integral(xmin, xmax);
116 Double_t fracN = integralN/(integralN+integralD);
117 Int_t nevtsN = rb.Binomial(nevts, fracN);
118 Int_t nevtsD = nevts - nevtsN;
120 std::cout << nevtsN <<
" " << nevtsD << std::endl;
122 gStyle->SetOptFit(1111);
125 for (
int iloop = 0; iloop < nloop; ++iloop) {
130 hM2D->FillRandom(fM2D->GetName(), nevtsD);
131 hM2N->FillRandom(fM2N->GetName(), nevtsN);
136 hM2E->Divide(hM2N, hM2D, 1, 1,
"b");
145 fM2Fit->SetParameter(0, 0.5);
146 fM2Fit->SetParameter(1, 15.0);
147 fM2Fit->SetParameter(2, 2.0);
148 fM2Fit->SetParError(0, 0.1);
149 fM2Fit->SetParError(1, 1.0);
150 fM2Fit->SetParError(2, 0.2);
151 TH1 * hf = fM2Fit->GetHistogram();
159 cEvt =
new TCanvas(Form(
"cEnv%d",iloop),
160 Form(
"plots for experiment %d", iloop),
164 hM2D->DrawCopy(
"HIST");
165 hM2N->SetLineColor(kRed);
166 hM2N->DrawCopy(
"HIST SAME");
169 for (
int fit = 0; fit < 2; ++fit) {
178 TString optFit =
"RN";
179 if (debug) optFit += TString(
"SV");
180 TFitResultPtr res = hM2E->Fit(fM2Fit, optFit);
183 fM2Fit->SetLineColor(kBlue);
184 fM2Fit->DrawCopy(
"SAME");
186 if (debug) res->Print();
195 if (fM2Fit2->GetParameter(0) >= 1.0)
196 fM2Fit2->SetParameter(0, 0.95);
197 fM2Fit2->SetParLimits(0, 0.0, 1.0);
204 TBinomialEfficiencyFitter bef(hM2N, hM2D);
205 TString optFit =
"RI S";
206 if (debug) optFit += TString(
"V");
207 TFitResultPtr res = bef.Fit(fM2Fit2,optFit);
210 std::cerr <<
"Error performing binomial efficiency fit, result = "
211 << status << std::endl;
216 fM2Fit2->SetLineColor(kRed);
217 fM2Fit2->DrawCopy(
"SAME");
219 bool confint = (status == 0);
222 auto htemp = fM2Fit2->GetHistogram();
223 ROOT::Fit::BinData xdata;
224 ROOT::Fit::FillData(xdata, fM2Fit2->GetHistogram() );
225 TGraphErrors gr(fM2Fit2->GetHistogram() );
226 res->GetConfidenceIntervals(xdata, gr.GetEY(), 0.68,
false);
228 gr.SetFillStyle(3005);
229 gr.DrawClone(
"4 same");
238 if (status != 0)
break;
240 Double_t fnorm = fM2Fit->GetParameter(0);
241 Double_t enorm = fM2Fit->GetParError(0);
242 Double_t fthreshold = fM2Fit->GetParameter(1);
243 Double_t ethreshold = fM2Fit->GetParError(1);
244 Double_t fwidth = fM2Fit->GetParameter(2);
245 Double_t ewidth = fM2Fit->GetParError(2);
247 fnorm = fM2Fit2->GetParameter(0);
248 enorm = fM2Fit2->GetParError(0);
249 fthreshold = fM2Fit2->GetParameter(1);
250 ethreshold = fM2Fit2->GetParError(1);
251 fwidth = fM2Fit2->GetParameter(2);
252 ewidth = fM2Fit2->GetParError(2);
253 hChisquared->Fill(fM2Fit2->GetProb());
256 TH1D* h =
dynamic_cast<TH1D*
>(hbiasNorm[fit]);
257 h->Fill((fnorm-normalization)/enorm);
258 h =
dynamic_cast<TH1D*
>(hbiasThreshold[fit]);
259 h->Fill((fthreshold-threshold)/ethreshold);
260 h =
dynamic_cast<TH1D*
>(hbiasWidth[fit]);
261 h->Fill((fwidth-width)/ewidth);
266 TCanvas* c1 =
new TCanvas(
"c1",
267 "Efficiency fit biases",10,10,1000,800);
272 h0 =
dynamic_cast<TH1D*
>(hbiasNorm[0]);
274 h1 =
dynamic_cast<TH1D*
>(hbiasNorm[1]);
275 h1->SetLineColor(kRed);
276 h1->Draw(
"HIST SAMES");
277 TLegend* l1 =
new TLegend(0.1, 0.75, 0.5, 0.9,
278 "plateau parameter",
"ndc");
279 l1->AddEntry(h0, Form(
"histogram: mean = %4.2f RMS = \
280 %4.2f", h0->GetMean(), h0->GetRMS()),
"l");
281 l1->AddEntry(h1, Form(
"binomial : mean = %4.2f RMS = \
282 %4.2f", h1->GetMean(), h1->GetRMS()),
"l");
286 h0 =
dynamic_cast<TH1D*
>(hbiasThreshold[0]);
288 h1 =
dynamic_cast<TH1D*
>(hbiasThreshold[1]);
289 h1->SetLineColor(kRed);
290 h1->Draw(
"HIST SAMES");
291 TLegend* l2 =
new TLegend(0.1, 0.75, 0.5, 0.9,
292 "threshold parameter",
"ndc");
293 l2->AddEntry(h0, Form(
"histogram: mean = %4.2f RMS = \
294 %4.2f", h0->GetMean(), h0->GetRMS()),
"l");
295 l2->AddEntry(h1, Form(
"binomial : mean = %4.2f RMS = \
296 %4.2f", h1->GetMean(), h1->GetRMS()),
"l");
300 h0 =
dynamic_cast<TH1D*
>(hbiasWidth[0]);
302 h1 =
dynamic_cast<TH1D*
>(hbiasWidth[1]);
303 h1->SetLineColor(kRed);
304 h1->Draw(
"HIST SAMES");
305 TLegend* l3 =
new TLegend(0.1, 0.75, 0.5, 0.9,
"width parameter",
"ndc");
306 l3->AddEntry(h0, Form(
"histogram: mean = %4.2f RMS = \
307 %4.2f", h0->GetMean(), h0->GetRMS()),
"l");
308 l3->AddEntry(h1, Form(
"binomial : mean = %4.2f RMS = \
309 %4.2f", h1->GetMean(), h1->GetRMS()),
"l");
313 hChisquared->Draw(
"HIST");