def main(): '''This is the entry point to execution.''' global data, model, x, fitresult data = getdata() x = data['raw'].get()[variable] if fit_range: x.setRange('fit', *fit_range) if plot_range: x.setRange('plot', *plot_range) initialize_fit_parameters(data['raw'], x) old_precision = set_default_integrator_precision(1e-8, 1e-8) ## Reduce data for model building data_kde_training = data['raw'] if data['raw'].numEntries() > kde_training_max_events: resampler = Resampler(data['raw']) data['raw_reduced'] = resampler.bootstrap( name = data['raw'].GetName() + '_boot', title = data['raw'].GetTitle() + ' Bootstrap Replica', size = kde_training_max_events, seed = 12345, ) data_kde_training = data['raw_reduced'] model = ParameterizedKeysPdf('model', 'model', x, mode, effsigma, data_kde_training, rho=0.3, forcerange=True) fitresult = {} for name, dataset in data.items(): dataset.SetName(name + 'Data') ## Reduce data for debugging # dataset = dataset.reduce(roo.EventRange(0, 10000)) mode.setVal(model.shapemodevar.getVal()) effsigma.setVal(model.shapewidthvar.getVal()) fitresult[name] = model.fitTo(dataset, roo.SumW2Error(True), roo.NumCPU(8), roo.Strategy(2), roo.Save(), roo.Range("fit")) make_plot(x, dataset, model) print '\n== Fitted parameters ==' mode.Print() effsigma.Print() save_result(fitresult[name]) set_default_integrator_precision(*old_precision) x_corr = get_correction(x, fitresult['raw'].floatParsFinal(), fitresult['target'].floatParsFinal()) make_comparison_plot(data['raw'], data['target'], x_corr)
def dofit(self): mi = ModalInterval(self.data) mi.setSigmaLevel(1) self.mode.setVal(mi.halfSampleMode()) self.effsigma.setVal(0.5 * mi.length()) self.bootdata.add(self.bootset) resampler = Resampler(self.data) for iboot in range(self.nboot): mi = ModalInterval(resampler.bootstrap()) mi.setSigmaLevel(1) self.mode.setVal(mi.halfSampleMode()) self.effsigma.setVal(0.5 * mi.length()) self.bootdata.add(self.bootset) self.mode.setVal(self.bootdata.mean(self.mode)) self.effsigma.setVal(self.bootdata.mean(self.effsigma)) self.mode.setError(self.bootdata.rmsVar(self.mode).getVal()) self.effsigma.setError(self.bootdata.rmsVar(self.effsigma).getVal())
def bootstrap(self, repeat=10): nbinsx = len(self.fractions) xlow = 0.5 * self.fractions[0] xup = self.fractions[-1] + xlow errors_rms = ROOT.TProfile(self.name + '_errors_rms', self.title, nbinsx, xlow, xup, 's') errors_mi1 = ROOT.TGraphAsymmErrors(nbinsx) errors_mi2 = ROOT.TGraphAsymmErrors(nbinsx) for graph in [errors_mi1, errors_mi2]: graph.SetTitle(self.title) resampler = Resampler(self.data) bootdata = {} for iteration in range(repeat): replica = resampler.bootstrap() boot = self.get_width_ratio(replica, self.fractions) for i in range(boot.GetN()): x = boot.GetX()[i] y = boot.GetY()[i] #y = boot.GetY()[i] - self.width_ratio.GetY()[i] errors_rms.Fill(x, y) bootdata.setdefault(x, []).append(y) for i, (x, ydist) in enumerate(sorted(bootdata.items())): ysize = len(ydist) yarray = array.array('d', ydist) y = ROOT.TMath.Median(ysize, yarray) errors_mi1.SetPoint(i, x, y) errors_mi2.SetPoint(i, x, y) exh = exl = 0. mi = ModalInterval(ysize, yarray) mi.setSigmaLevel(1) eyl = y - mi.lowerBound() eyh = mi.upperBound() - y errors_mi1.SetPointError(i, exl, exh, eyl, eyh) mi.setSigmaLevel(2) eyl = y - mi.lowerBound() eyh = mi.upperBound() - y errors_mi2.SetPointError(i, exl, exh, eyl, eyh) return errors_rms, errors_mi1, errors_mi2