示例#1
0
class _EfficiencyCurve(object):
    def __init__(self, name, bins, threshold):
        from rootpy.plotting import Hist
        self._pass = Hist(bins, name=name + '_pass')
        self._total = Hist(bins, name=name + '_total')
        self._dist = Hist(bins, name=name + '_dist')
        self._threshold = threshold
        self._efficiency = None

    def fill(self, recoValue, l1Value, weight=1.):
        """ Fills the histograms used for efficiency calculation
        :param recoValue: the reconstructed quanity
        :type recoValue: float
        :param l1Value: the L1 Trigger quantity
        :type l1Value: float
        :param weight: weight to fill the histograms with, default=1.0
        :type weight: float
        """
        self._total.fill(recoValue, weight)
        self._dist.fill(l1Value, weight)
        if l1Value > self._threshold:
            self._pass.fill(recoValue, weight)

    def calculate_efficiency(self):
        from rootpy import asrootpy
        from ROOT import TEfficiency
        self._efficiency = asrootpy(TEfficiency(self._pass, self._total))
        self._efficiency.SetName(self._total.GetName() + '_eff')

    def get_efficiency(self):
        if not self._efficiency:
            self.calculate_efficiency()
        return self._efficiency
示例#2
0
文件: tree.py 项目: rreece/ipyhep
def _project(tree,
             var,
             selection='',
             weight=1.0,
             bins=None,
             includeover=False):

    h = None
    if var.count(':') == 0:
        ## Hist (1D)
        if bins:
            if isinstance(bins, tuple):
                assert len(bins) == 3
                h = Hist(*bins)
            elif isinstance(bins, list):
                h = Hist(bins)
            else:
                assert False
        else:
            assert False
    elif var.count(':') == 1:
        ## Hist2D
        ## like rootpy, we use a convention where var=x:y, unlike ROOT
        varx, vary = var.split(':')
        var = ':'.join([vary, varx])
        if bins:
            if isinstance(bins, tuple):
                assert len(bins) == 6
                h = Hist2D(*bins)
            elif isinstance(bins, list):
                ## TODO: support variable bins for Hist2D
                h = Hist2D(*bins)
                #assert False
            else:
                assert False
        else:
            assert False
    else:
        assert False

    assert h
    #            kwargs['hist'] = h

    ## add the weight to the selection via a TCut
    weighted_selection = str(selection)
    if weight and weight != 1.0:
        weighted_selection = Cut(weighted_selection)
        weighted_selection = weighted_selection * weight
        weighted_selection = str(weighted_selection)

    tree.Draw('%s>>%s' % (var, h.GetName()), weighted_selection)

    ##    print tree.GetSelectedRows() ## debuging

    #    for event in t:
    #        x = getattr(event, var)
    #        h.fill(x)

    if h:
        h.SetDirectory(0)

        #        elist = ROOT.gDirectory.Get('elist')
        #        elist.Print('all')

        if var.count(':') == 0 and includeover:
            ## include overflow for Hist (1D)
            nbins = h.GetNbinsX()
            c1 = h.GetBinContent(nbins)
            c2 = h.GetBinContent(nbins + 1)
            e1 = h.GetBinError(nbins)
            e2 = h.GetBinError(nbins + 1)
            h.SetBinContent(nbins, c1 + c2)
            h.SetBinError(
                nbins,
                math.sqrt((c1 * e1 * e1 + c2 * e2 * e2) /
                          (c1 + c2)) if c1 + c2 != 0.0 else 0.0)
            h.SetBinContent(nbins + 1, 0.0)
            h.SetBinError(nbins + 1, 0.0)

    return h
示例#3
0
                    h = Hist(toDraw['edges'], name=histName)
                elif histDimension == 2:
                    h = Hist2D(toDraw['edges'][0],
                               toDraw['edges'][1],
                               name=histName)
                elif histDimension == 3:
                    h = Hist2D(toDraw['edges'][0],
                               toDraw['edges'][1],
                               toDraw['edges'][2],
                               name=histName)
                else:
                    raise ValueError(
                        'I dunno how to handle {0}'.format(toDraw))
            else:
                print "ERROR: problem configuring the binning of the histograms..."
                exit()
            # things look ok, so we draw to the histogram
            print "\t\tdrawing {0}\n\t\twith cut ({1})*({2})".format(
                toDraw['draw'], args.eventWeightBranch, cut['name'])
            tree.Draw(toDraw['draw'],
                      '({0:s})*({1:s})'.format(args.eventWeightBranch,
                                               cut['cut']),
                      hist=h)
            samename = h.GetName()
            hnew = h.merge_bins([(-2, -1)])  # merge upper overflow bin
            hnew.SetName(samename)
            # write to file
            print "\t\twriting to file"
            hnew.write()
    out_file.close()