def _getAutoBinning(self): 'Get binning that is uniform around that peak area and non-uniform\n' 'in the tails. It guaranties that the number of entries per bin is\n' 'in a given range [minEntriesPerBin, maxEntriesPerBin] by merging\n' 'neighboring bins in the tails. This is useful for calculation of\n' 'chi2 statistic that obeys the chi2 PDF.\n' 'Optional features:\n' ' * the bin width is a pretty number, e.g. 1, 0.5, 0.2, etc.\n' ' * calculate the median for each bin. (->better looking plot)\n' ## Get the data as an array of doubles pointed by a tree entries = self.data.tree().Draw(self.x.GetName(), '', 'goff') ## Create the DataDrivenBinning object bins = DataDrivenBinning(entries, self.data.tree().GetV1(), self.binContentMin, self.binContentMax) return bins
## Build the model # model = w.factory('Gaussian::gaus(x[-5,5],mean[0,-6,6],sigma[1,0.5,2])') model = w.factory('RooSechPdf::sech(x[-5,5],mean[0,-6,6],sigma[1,0.5,2])') x = w.factory('x') for itoy in range(ntoys): ## Use the model to generate a toy dataset data = model.generate(ROOT.RooArgSet(x), nentries, roofit.NumCPU(numCPU)) ## Fit the model to the generated data model.fitTo(data, roofit.NumCPU(numCPU), roofit.Verbose(False)) ## Get custom binning nevents = data.tree().Draw(x.GetName(), '', 'goff') bins = DataDrivenBinning(nevents, data.tree().GetV1(), minBinContent, maxBinContent) binning = bins.binning(ROOT.RooBinning()) ubinning = bins.uniformBinning(ROOT.RooUniformBinning()) ## Make sure that there are at least 1./epsilonPerBin curve points ## in each bin. The RooChi2Calculator fails if there is none perhaps ## due to a bug in RooCurve::average? Reason is not understood yet. epsilon = epsilonPerBin * ubinning.binWidth(0) / bins.length() ## Make frames log_plot = x.frame() log_plot_bins = x.frame() log_plot_medians = x.frame() bins.setSigmaLevel(3) lin_plot = x.frame(roofit.Range(-3,3))
## Plot the data and the model overlaid plot = x.frame(nbins) plot.SetTitle('Default Binning') data.plotOn(plot) model.plotOn(plot) ## Display the plot canvas1 = canvases.next('Data_Driven_Binning') canvas1.Divide(2,2) canvas1.cd(1) plot.Draw() ## Create the DataDrivenBinning object n = data.tree().Draw(x.GetName(), '', 'goff') bins = DataDrivenBinning(n, data.tree().GetV1(), minBinContent, maxBinContent) ## Use DataDrivenBinning to define a RooBinning binning = bins.binning(ROOT.RooBinning()) ubinning = bins.uniformBinning(ROOT.RooUniformBinning()) ## Make a second plot for comparison. plot2 = x.frame() plot2.SetTitle('Data-Driven Binning') ## First plot the data with uniform binning to set the per bin density data.plotOn(plot2, ROOT.RooFit.Binning(ubinning), ROOT.RooFit.Invisible()) ## Plot the data with the custom binning. data.plotOn(plot2, ROOT.RooFit.Binning(binning))
## Build the model # model = w.factory('Gaussian::gaus(x[-5,5],mean[0,-6,6],sigma[1,0.5,2])') model = w.factory('RooSechPdf::sech(x[-5,5],mean[0,-6,6],sigma[1,0.5,2])') x = w.factory('x') for itoy in range(ntoys): ## Use the model to generate a toy dataset data = model.generate(ROOT.RooArgSet(x), nentries, roofit.NumCPU(numCPU)) ## Fit the model to the generated data model.fitTo(data, roofit.NumCPU(numCPU), roofit.Verbose(False)) ## Get custom binning nevents = data.tree().Draw(x.GetName(), '', 'goff') bins = DataDrivenBinning(nevents, data.tree().GetV1(), minBinContent, maxBinContent) binning = bins.binning(ROOT.RooBinning()) ubinning = bins.uniformBinning(ROOT.RooUniformBinning()) ## Make sure that there are at least 1./epsilonPerBin curve points ## in each bin. The RooChi2Calculator fails if there is none perhaps ## due to a bug in RooCurve::average? Reason is not understood yet. epsilon = epsilonPerBin * ubinning.binWidth(0) / bins.length() ## Make frames log_plot = x.frame() log_plot_bins = x.frame() log_plot_medians = x.frame() bins.setSigmaLevel(3) lin_plot = x.frame(roofit.Range(-3, 3))