Example #1
0
def create_tree(filename="test_tree.root"): 
    outfile = TFile(filename, 'recreate')
    tree = Tree('test_tree', 'A test tree')
    tree.var('var1')
    for i in range(100):
        tree.fill('var1', i)
        tree.tree.Fill()
    print('creating a tree', tree.tree.GetName(),\
        tree.tree.GetEntries(), 'entries in',\
        outfile.GetName())
    outfile.Write()
Example #2
0
class SimpleTreeProducer(Analyzer):
    def beginLoop(self):
        super(SimpleTreeProducer, self).beginLoop()
        self.rootfile = TFile('/'.join([self.dirName, 'simple_tree.root']),
                              'recreate')
        self.tree = Tree(self.cfg_ana.tree_name, self.cfg_ana.tree_title)
        self.tree.var('test_variable')

    def process(self, event):
        self.tree.fill('test_variable', event.input.var1)
        self.tree.tree.Fill()

    def write(self):
        self.rootfile.Write()
        self.rootfile.Close()
Example #3
0
 def beginLoop(self, setup):
     super(SimpleTreeProducer, self).beginLoop(setup)
     self.rootfile = TFile('/'.join([self.dirName,
                                     'simple_tree.root']),
                           'recreate')
     self.tree = Tree( self.cfg_ana.tree_name,
                       self.cfg_ana.tree_title )
     self.tree.var('test_variable')
Example #4
0
 def beginLoop(self, setup) :
     super(TreeAnalyzerNumpy, self).beginLoop(setup)
     if self.outservicename in setup.services:
         print("Using outputfile given in", self.outservicename)
         self.file = setup.services[self.outservicename].file
     else :
         fileName = '/'.join([self.dirName,
                          'tree.root'])
         isCompressed = self.cfg_ana.isCompressed if hasattr(self.cfg_ana,'isCompressed') else 1
         print('Compression', isCompressed)
         self.file = TFile( fileName, 'recreate', '', isCompressed )
     self.file.cd()
     if self.file.Get(self.treename) :
         raise RuntimeError("You are booking two Trees with the same name in the same file")
     self.tree = Tree(self.treename, self.name)
     self.tree.setDefaultFloatType(getattr(self.cfg_ana, 'defaultFloatType','D')); # or 'F'
     self.declareVariables(setup)
Example #5
0
 def beginLoop(self, setup):
     super(TreeAnalyzerNumpy, self).beginLoop(setup)
     print setup.services
     if self.outservicename in setup.services:
         print "Using outputfile given in", self.outservicename
         self.file = setup.services[self.outservicename].file
     else:
         fileName = '/'.join([self.dirName, 'tree.root'])
         isCompressed = self.cfg_ana.isCompressed if hasattr(
             self.cfg_ana, 'isCompressed') else 1
         print 'Compression', isCompressed
         self.file = TFile(fileName, 'recreate', '', isCompressed)
     self.tree = Tree('tree', self.name)
     self.tree.setDefaultFloatType(
         getattr(self.cfg_ana, 'defaultFloatType', 'D'))
     # or 'F'
     self.declareVariables(setup)
Example #6
0
class SimpleTreeProducer(Analyzer):

    def beginLoop(self, setup):
        super(SimpleTreeProducer, self).beginLoop(setup)
        self.rootfile = TFile('/'.join([self.dirName,
                                        'simple_tree.root']),
                              'recreate')
        self.tree = Tree( self.cfg_ana.tree_name,
                          self.cfg_ana.tree_title )
        self.tree.var('test_variable')

    def process(self, event):
        self.tree.fill('test_variable', event.input.var1)
        self.tree.tree.Fill()

    def write(self, setup):
        self.rootfile.Write()
        self.rootfile.Close()
Example #7
0
 def test_fill(self):
     fi = TFile('tree.root', 'RECREATE')
     tr = Tree('test_tree', 'A test tree')
     tr.var('a')
     tr.var('b')
     tr.fill('a', 3)
     tr.fill('a', 4)
     tr.fill('b', 5)
     tr.tree.Fill()
     fi.Write()
     fi.Close()
Example #8
0
    def __init__(self, cfg_ana, cfg_comp, looperName):
        super(TreeAnalyzerNumpy,self).__init__(cfg_ana, cfg_comp, looperName)
        fileName = '/'.join([self.dirName,
                             'tree.root'])

        isCompressed = self.cfg_ana.isCompressed if hasattr(cfg_ana,'isCompressed') else 1
        print 'Compression', isCompressed

        self.file = TFile( fileName, 'recreate', '', isCompressed )
        self.tree = Tree('tree', self.name)
Example #9
0
class TreeAnalyzerNumpy(Analyzer):
    """Base TreeAnalyzerNumpy, to create flat TTrees.

    Check out TestTreeAnalyzer for a concrete example.
    IMPORTANT: FOR NOW, CANNOT RUN SEVERAL TreeAnalyzers AT THE SAME TIME!
    Anyway, you want only one TTree, don't you?"""
    def __init__(self, cfg_ana, cfg_comp, looperName):
        super(TreeAnalyzerNumpy, self).__init__(cfg_ana, cfg_comp, looperName)
        self.outservicename = getattr(cfg_ana, "outservicename", "outputfile")
        self.treename = getattr(cfg_ana, "treename", "tree")

    def beginLoop(self, setup):
        super(TreeAnalyzerNumpy, self).beginLoop(setup)
        if self.outservicename in setup.services:
            print "Using outputfile given in", self.outservicename
            self.file = setup.services[self.outservicename].file
        else:
            fileName = '/'.join([self.dirName, 'tree.root'])
            isCompressed = self.cfg_ana.isCompressed if hasattr(
                self.cfg_ana, 'isCompressed') else 1
            print 'Compression', isCompressed
            self.file = TFile(fileName, 'recreate', '', isCompressed)
        self.file.cd()
        if self.file.Get(self.treename):
            raise RuntimeError, "You are booking two Trees with the same name in the same file"
        self.tree = Tree(self.treename, self.name)
        self.tree.setDefaultFloatType(
            getattr(self.cfg_ana, 'defaultFloatType', 'D'))
        # or 'F'
        self.declareVariables(setup)

    def declareVariables(self, setup):
        print 'TreeAnalyzerNumpy.declareVariables : overload this function.'
        pass

    def write(self, setup):
        super(TreeAnalyzerNumpy, self).write(setup)
        if self.outservicename not in setup.services:
            self.file.Write()
Example #10
0
 def test_fill(self):
     fi = TFile('tree.root','RECREATE')
     tr = Tree('test_tree', 'A test tree')
     tr.var('a')
     tr.var('b')
     tr.fill('a', 3)
     tr.fill('a', 4)
     tr.fill('b', 5)
     tr.tree.Fill()
     fi.Write()
     fi.Close()
Example #11
0
class TreeAnalyzerNumpy( Analyzer ):
    """Base TreeAnalyzerNumpy, to create flat TTrees.

    Check out TestTreeAnalyzer for a concrete example.
    IMPORTANT: FOR NOW, CANNOT RUN SEVERAL TreeAnalyzers AT THE SAME TIME!
    Anyway, you want only one TTree, don't you?"""

    def __init__(self, cfg_ana, cfg_comp, looperName):
        super(TreeAnalyzerNumpy,self).__init__(cfg_ana, cfg_comp, looperName)
        self.outservicename = getattr(cfg_ana,"outservicename","outputfile")
        self.treename = getattr(cfg_ana,"treename","tree")


    def beginLoop(self, setup) :
        super(TreeAnalyzerNumpy, self).beginLoop(setup)
        if self.outservicename in setup.services:
            print("Using outputfile given in", self.outservicename)
            self.file = setup.services[self.outservicename].file
        else :
            fileName = '/'.join([self.dirName,
                             'tree.root'])
            isCompressed = self.cfg_ana.isCompressed if hasattr(self.cfg_ana,'isCompressed') else 1
            print('Compression', isCompressed)
            self.file = TFile( fileName, 'recreate', '', isCompressed )
        self.file.cd()
        if self.file.Get(self.treename) :
            raise RuntimeError("You are booking two Trees with the same name in the same file")
        self.tree = Tree(self.treename, self.name)
        self.tree.setDefaultFloatType(getattr(self.cfg_ana, 'defaultFloatType','D')); # or 'F'
        self.declareVariables(setup)
        
    def declareVariables(self,setup):
        print('TreeAnalyzerNumpy.declareVariables : overload this function.')
        pass

    def write(self, setup):
        super(TreeAnalyzerNumpy, self).write(setup)
        if self.outservicename not in setup.services:
            self.file.Write() 
Example #12
0
 def beginLoop(self, setup) :
     super(TreeAnalyzerNumpy, self).beginLoop(setup)
     print setup.services
     if self.outservicename in setup.services:
         print "Using outputfile given in", self.outservicename
         self.file = setup.services[self.outservicename].file
     else :
         fileName = '/'.join([self.dirName,
                          'tree.root'])
         isCompressed = self.cfg_ana.isCompressed if hasattr(self.cfg_ana,'isCompressed') else 1
         print 'Compression', isCompressed
         self.file = TFile( fileName, 'recreate', '', isCompressed )
     self.tree = Tree('tree', self.name)
     self.tree.setDefaultFloatType(getattr(self.cfg_ana, 'defaultFloatType','D')); # or 'F'
     self.declareVariables(setup)
Example #13
0
def create_tree(filename="test_tree.root"):
    outfile = TFile(filename, 'recreate')
    tree = Tree('test_tree', 'A test tree')
    tree.var('var1')
    for i in range(100):
        tree.fill('var1', i)
        tree.tree.Fill()
    print('creating a tree', tree.tree.GetName(),\
        tree.tree.GetEntries(), 'entries in',\
        outfile.GetName())
    outfile.Write()
Example #14
0
from ROOT import TFile
from PhysicsTools.HeppyCore.statistics.tree import Tree

outfile = TFile('test_tree.root', 'recreate')

tree = Tree('test_tree', 'A test tree')
tree.var('var1')

for i in range(100):
    tree.fill('var1', i)
    tree.tree.Fill()

print 'creating a tree', tree.tree.GetName(),\
      tree.tree.GetEntries(), 'entries in',\
      outfile.GetName()

outfile.Write()
Example #15
0
 def write(self, dirName, fileName='RLTInfo.root'):
     f = TFile('/'.join([dirName, fileName]), 'RECREATE')
     t = Tree('RLTInfo', 'HLT/Run/Lumi information')
     t.var('run', int)
     t.var('lumi', int)
     t.var('counts', int)
     t.var('trigger', int)
     for rlt, count in six.iteritems(self.dict):
         t.fill('run', rlt[1])
         t.fill('lumi', rlt[2])
         t.fill('counts', count.integer)
         t.tree.Fill()
     f.Write()
     f.Close()
Example #16
0
 def write(self, dirName, fileName='RLTInfo.root'):
     f = TFile('/'.join( [dirName, fileName]), 'RECREATE')
     t = Tree('RLTInfo','HLT/Run/Lumi information')
     t.var('run', int )
     t.var('lumi', int )
     t.var('counts', int )
     t.var('trigger', int )
     for rlt, count in six.iteritems(self.dict):
         t.fill('run', rlt[1])
         t.fill('lumi', rlt[2])
         t.fill( 'counts', count.integer)
         t.tree.Fill()
     f.Write()
     f.Close()
Example #17
0
 def beginLoop(self):
     super(SimpleTreeProducer, self).beginLoop()
     self.rootfile = TFile('/'.join([self.dirName, 'simple_tree.root']),
                           'recreate')
     self.tree = Tree(self.cfg_ana.tree_name, self.cfg_ana.tree_title)
     self.tree.var('test_variable')
Example #18
0
            ret[(run,lumi,evt)] = seljets
        if i > options.maxEv: break

    print "Read %d events from %s %s, found %s events with interesting jets" % (i, role, filename, len(ret))
    return ret

events_ref = readEvents(args[0], "reference")
events_new = readEvents(args[1], "new")
common = sorted(set(events_ref.keys()).intersection(events_new.keys()))
print "%d common events. now doing jet-by-jet comparison" % len(common)


from PhysicsTools.HeppyCore.utils.deltar import matchObjectCollection3, deltaR
from PhysicsTools.HeppyCore.statistics.tree import Tree
fout = ROOT.TFile.Open(args[2], "RECREATE")
tree = Tree("t","t","F");
for X in "run", "lumi", "event":
    tree.var(X,int)
jetvars = events_ref[common[0]][0].keys() 
for Y in jetvars:
    tree.var("r_"+Y)
    tree.var(""  +Y)
tree.var("deltaR")


goodevents = set()
print "%-14s     |  %8s   %8s   %8s   %7s  |  %7s  %7s    %7s  %7s    %7s  %7s    %7s  %7s    %7s  %7s" % ( 
        "event", "new pt", "ref pt", "gen pt", "gen eta",  "c had","ref", "n had","ref", "c em","ref", "n em","ref", "mu","ref")
for key in common:
    (run,lumi,event) = key
    jets_ref = events_ref[key]