Esempio n. 1
0
 def test_append(self):
     """Also tests layered=False"""
     cut = ROOT.TCut('bar<0')
     self.splice.make_splice('bar', cut)
     cut = ROOT.TCut('bar>=0')
     # this should add the rest of the entries
     tree = self.splice.make_splice('bar', cut, append=True)
     self.assertEqual(self.nentries, nplotted(tree, 'bar'))
Esempio n. 2
0
    def test_make_splice(self):
        cut = ROOT.TCut('foo>10')
        nexpected = nplotted(self.splice.reset(), 'foo', cut)
        tree = self.splice.make_splice('foo_gt_10', cut)
        self.assertEqual(nexpected, nplotted(tree, 'foo'))

        cut = ROOT.TCut('bar<0')
        nexpected = nplotted(self.splice.reset(), 'bar', cut)
        tree = self.splice.make_splice('nve_bar', cut)
        self.assertEqual(nexpected, nplotted(tree, 'bar'))
Esempio n. 3
0
 def test_elistarray_splice(self):
     # entries with 5 elements in data are guaranteed to pass, for
     # other entries, it passes when the passing element is < size,
     # which happens 3/5 times (when it is b/w 0-2), it is always
     # found for size = 5.  Therefore, it is found for at least 4/5
     # entries.
     cut = ROOT.TCut('(data[]%5)>3')
     tree = self.splice.make_splice('data_mod_5',
                                    cut,
                                    listtype='entrylistarray')
     expected = nplotted(tree, 'data')
     # print self.ndata, self.nentries, expected, 4*self.nentries/5
     self.assertLess(expected, self.ndata)
     self.assertLess(expected, self.nentries)
     self.assertGreater(expected, 4 * self.nentries / 5)
Esempio n. 4
0
 def _draw_match(fname, hists):
     for pair in hists:
         myc = None
         for obj in pair:
             if fnmatchcase(obj.GetName(), options.dump):
                 xaxis, yaxis = obj.GetXaxis(), obj.GetYaxis()
                 xaxis.SetTitle(get_label(xaxis.GetTitle()))
                 yaxis.SetTitle(get_label(yaxis.GetTitle()))
                 yaxis.SetTitleOffset(1.25)
                 if not myc:
                     myc = ROOT.TCanvas('myc', '', 800, 500)
                     myc.cd()
                     obj.Draw()
                 else:
                     obj.Draw('same')
         if myc:
             myc.Print(fname)
         del myc
Esempio n. 5
0
def setUpModule():
    rfile = ROOT.TFile.Open('/tmp/testtree.root', 'recreate')
    tree = ROOT.TTree('testtree', '')
    foo = np.array([0], dtype=np.float32)
    tree.Branch("foo", foo, "foo/F")
    bar = np.array([0], dtype=np.double)
    tree.Branch("bar", bar, "bar/D")
    baz = np.array([0], dtype=np.int32)
    tree.Branch("baz", baz, "baz/I")
    sz = np.array([0], dtype=np.int32)
    tree.Branch("sz", sz, "sz/I")
    data = np.array([1., 2., 3., 4., 5.], dtype=np.float32)
    tree.Branch("data", data, "data[sz]/F")
    for i in range(1000):
        foo[0] = np.random.lognormal(mean=np.pi)
        bar[0] = np.random.normal(loc=0)
        baz[0] = np.random.binomial(100, 0.3)
        data += i
        sz[0] = 3 + i % 3
        tree.Fill()
    tree.Write()
    rfile.Close()
Esempio n. 6
0
    def setUp(self):
        """Make ROOT files with some contents.

        Structure:

          /tmp/test_Rdir{0,1}.root:/dira/hista
          /tmp/test_Rdir{0,1}.root:/dirb/histb
          /tmp/test_Rdir{0,1}.root:/dirc/histx
          /tmp/test_Rdir{0,1}.root:/dirc/dird/histy
          /tmp/test_Rdir{0,1}.root:/dirc/dird/dire/histz
          /tmp/test_Rdir{0,1}.root:/hist{0,1,2}

        """
        self.fnames, self.rfiles = [], []
        for i in range(2):
            self.fnames.append('/tmp/test_Rdir{}.root'.format(i))
            self.rfiles.append(TFile.Open(self.fnames[-1], 'recreate'))

        for f in self.rfiles:
            rdir = f.mkdir('dira')
            rdir.WriteTObject(ROOT.TH1C('hista', '', 10, 0, 10))

            rdir = f.mkdir('dirb')
            rdir.WriteTObject(ROOT.TH1C('histb', '', 10, 0, 10))

            rdir = f.mkdir('dirc/dird/dire')
            rdir.cd()
            gDirectory.WriteTObject(ROOT.TH1C('histx', '', 10, 0, 10))
            rdir.cd('dird')
            gDirectory.WriteTObject(ROOT.TH1C('histy', '', 10, 0, 10))
            rdir.cd('dird/dire')
            gDirectory.WriteTObject(ROOT.TH1C('histz', '', 10, 0, 10))

            for i in range(3):
                f.WriteTObject(ROOT.TH1C('hist{}'.format(i), '', 10, 0, 10))

        for f in self.rfiles:
            f.Write()
            f.Close()
Esempio n. 7
0
    ihists = get_hists(['file'], rfileconf, pathtool, robj_t=ROOT.TH1)

    # triangular matrix indices for use w/ both cov matrices & scatter plots
    import numpy as np
    dims = ihists['file'][0].GetXaxis().GetNbins() - 1  # nvars - 1 in corrn matrix
    opts = np.empty(shape=(dims, dims), dtype=object)
    tril = np.tril_indices(dims)
    triu = np.triu_indices(dims)

## covariance matrices
from utils import get_label
if options.lcorrns:
    matrices = {}
    for transform in transforms:
        corrn = [
            ROOT.TH2D(transform+'_sig', 'Correlation matrix after {} transform (sig)'
                      .format(transforms[transform]), dims, 0, dims, dims, 0, dims),
            ROOT.TH2D(transform+'_bkg', 'Correlation matrix after {} transform (bkg)'
                      .format(transforms[transform]), dims, 0, dims, dims, 0, dims)
            ]

        for i, idx in enumerate(zip(*triu)):
            hist = [
                sig_hists[transform+'_corr'][i*2],
                bkg_hists[transform+'_corr'][i*2]
            ]

            if idx[0] == idx[1]:    # set bin label using diagonal
                for i in xrange(len(hist)):
                    name = hist[i].GetName()
                    if i == 0:
                        name = name[5:name.find('_Signal')]
Esempio n. 8
0
 def setUp(self):
     self.hist1 = ROOT.TH1D('hist1', '', 20, -3, 3)
     self.hist1.FillRandom('gaus', 1000)
Esempio n. 9
0
 def setUp(self):
     self.nbins = 20
     self.hist = ROOT.TH1F('hist', '', self.nbins, -3, 3)
     self.hist.FillRandom('gaus', 1000)
Esempio n. 10
0
        axes.set_ylabel('Background rejection efficiency')
        axes.xaxis.set_label_coords(0.9, -0.05)
        for key, hist in roc.iteritems():
            info = hist_info(hist)
            hist = pycopy(Hist, ROOT.TH1, hist, *info[0], **info[1])
            line = rplt.hist(hist, stacked=False)
            print type(line), line
        axes.legend(fontsize=10, numpoints=1, frameon=False, ncol=3)

    if doprint:
        pp.savefig()
        pp.close()
    elif not options.batch:
        plt.show()
else:
    canvas = ROOT.TCanvas('canvas', '', 800, 600)
    if doprint:
        canvas.Print('{}_my_ROC_curves.pdf['.format(prefix))

    cols = (ROOT.kAzure, ROOT.kRed, ROOT.kBlack)
    legend = ROOT.TLegend(0.12, 0.15, 0.8, 0.6)
    legend.SetHeader('MVA classifiers')
    legend.SetBorderSize(0)
    legend.SetFillStyle(0)
    ROOT.gStyle.SetOptStat(False)

    from utils import th1integral, distance
    for i, roc in enumerate(rocs):
        coln = 0
        for cl, hist in roc.iteritems():
            # metrics
Esempio n. 11
0
if conf.read() > 0:
    session = conf.get_session_config(options.session)
else:
    sys.exit('No usable sessions in config file')
if not session:
    sys.exit('Couldn\'t find session in config file')
print '::: Training {} MVAs: {}\n{}'.format(len(session.methods),
                                            session.methods, '='*50)
print session
print ':::'

from fixes import ROOT
ROOT.gROOT.SetBatch(True)

# files & trees
tree_s = ROOT.TChain(options.sigtree)
map(lambda f: tree_s.Add(f), session.sig_file)
tree_b = ROOT.TChain(options.bkgtree)
map(lambda f: tree_b.Add(f), session.bkg_file)

if not tree_s or not tree_b:
    sys.exit('Unable to read input trees.')

nentries_s = tree_s.GetEntries(str(session.cut_sig))
nentries_b = tree_b.GetEntries(str(session.cut_bkg))

# NOTE: normalise: create option like
# nTrain_Signal=num:nTrain_Background=num:...
if options.norm:
    size = nentries_b if nentries_s > nentries_b else nentries_s
Esempio n. 12
0
def get_estimate(mva, cut, tree=tree):
    cut = ROOT.TCut('{}>{}'.format(mva, cut))
    # NOTE: only process 200,000 entries
    tree.Draw('{0}>>h{0}'.format(mva), cut, 'goff', options.maxentries)
    hist = ROOT.gDirectory.Get('h{}'.format(mva))
    return hist.GetEntries()
Esempio n. 13
0
 def test_get_splice(self):
     cut = ROOT.TCut('sz==4')
     self.splice.make_splice('sz_eq_4', cut)
     nexpected = nplotted(self.splice.reset(), 'sz', cut)
     tree = self.splice.get_splice('sz_eq_4')
     self.assertEqual(nexpected, nplotted(tree, 'sz'))
Esempio n. 14
0
 def test_set_splice(self):
     cut = ROOT.TCut('bar>0')
     self.splice.make_splice('pve_bar', cut)
     nexpected = nplotted(self.splice.reset(), 'bar', cut)
     tree = self.splice.set_splice(self.splice.elists['pve_bar'])
     self.assertEqual(nexpected, nplotted(tree, 'bar'))
Esempio n. 15
0
 def test_reset(self):
     self.splice.make_splice('sz_gt_5', ROOT.TCut('sz>5'))  # zero
     self.assertEqual(nplotted(self.splice.reset(), 'sz'), self.nentries)
Esempio n. 16
0
 def test_get_entries(self):
     tree = self.splice.make_splice('sz_gt_3', ROOT.TCut('sz>3'))  # 2/3
     self.assertEqual(self.splice.get_entries(), nplotted(tree, 'sz'))