def integral(wrp, use_bin_width=False): """ Integral. Applies to HistoWrapper. Returns FloatWrapper. >>> from ROOT import TH1I >>> h1 = TH1I("h1", "", 2, .5, 4.5) >>> h1.Fill(1) 1 >>> h1.Fill(3,3) 2 >>> w1 = wrappers.HistoWrapper(h1) >>> w2 = integral(w1) >>> w2.float 4.0 >>> w3 = integral(w1, True) >>> w3.float 8.0 """ if not isinstance(wrp, wrappers.HistoWrapper): raise WrongInputError( "integral needs argument of type HistoWrapper. histo: " + str(wrp) ) option = "width" if use_bin_width else "" info = wrp.all_info() return wrappers.FloatWrapper(wrp.histo.Integral(option), **info)
def chi2(wrps, x_min=0, x_max=0): """ Expects two Histowrappers. Returns FloatWrapper. """ wrps = iterableize(wrps) wrps = iter(wrps) try: first, second = next(wrps), next(wrps) except StopIteration: raise TooFewWrpsError("chi2 needs exactly two HistoWrappers.") try: wrps.next() raise TooManyWrpsError("chi2 needs exactly two HistoWrappers.") except StopIteration: pass for w in (first, second): if not isinstance(w, wrappers.HistoWrapper): raise WrongInputError( "chi2 needs type HistoWrapper. w: " + str(w) ) if first.histo.GetNbinsX() != second.histo.GetNbinsX(): raise WrongInputError( "chi2 needs histos with same number of bins." ) if not x_max: x_max = int(first.histo.GetNbinsX() - 1) def get_weight_for_bin(i): val = (first.histo.GetBinContent(i+1) - second.histo.GetBinContent(i+1))**2 err1 = first.histo.GetBinError(i+1) err2 = second.histo.GetBinError(i+1) if err1 and err2: return val / (err1**2 + err2**2) else: return 0. chi2_val = __builtin__.sum( get_weight_for_bin(i) for i in xrange(x_min, x_max) ) info = second.all_info() info.update(first.all_info()) return wrappers.FloatWrapper( chi2_val, **info )
def lumi(wrp): """ Requires ``lumi`` to be defined on wrp. Returns FloatWrapper. >>> from ROOT import TH1I >>> h1 = TH1I("h1", "", 2, .5, 2.5) >>> h1.Fill(1) 1 >>> w1 = wrappers.HistoWrapper(h1, lumi=2.) >>> w2 = lumi(w1) >>> w2.float 2.0 """ if not hasattr(wrp, 'lumi'): raise WrongInputError( "lumi needs ``lumi`` to be defined on wrp. wrp: " + str(wrp) ) info = wrp.all_info() return wrappers.FloatWrapper(wrp.lumi, **info)
def lumi(wrp): """ Applies to HistoWrapper. Returns FloatWrapper. >>> from ROOT import TH1I >>> h1 = TH1I("h1", "", 2, .5, 2.5) >>> h1.Fill(1) 1 >>> w1 = wrappers.HistoWrapper(h1, lumi=2.) >>> w2 = lumi(w1) >>> w2.float 2.0 """ if not isinstance(wrp, wrappers.HistoWrapper): raise WrongInputError( "lumi needs argument of type HistoWrapper. histo: " + str(wrp) ) info = wrp.all_info() return wrappers.FloatWrapper(wrp.lumi, **info)
def data_lumi_sum_wrp(): """Returns the sum of data luminosity in as a FloatWrapper.""" lumi = data_lumi_sum() return wrappers.FloatWrapper(lumi, history='DataLumiSum(%g)' % lumi)
def data_lumi_sum_wrp(): """Returns the sum of data luminosity in as a FloatWrapper.""" return wrp.FloatWrapper(data_lumi_sum(), history="DataLumiSum")