File indexing completed on 2024-11-25 02:29:49
0001 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
0002 from PhysicsTools.HeppyCore.statistics.tree import Tree as Tree
0003 from ROOT import TFile
0004
0005 class TreeAnalyzerNumpy( Analyzer ):
0006 """Base TreeAnalyzerNumpy, to create flat TTrees.
0007
0008 Check out TestTreeAnalyzer for a concrete example.
0009 IMPORTANT: FOR NOW, CANNOT RUN SEVERAL TreeAnalyzers AT THE SAME TIME!
0010 Anyway, you want only one TTree, don't you?"""
0011
0012 def __init__(self, cfg_ana, cfg_comp, looperName):
0013 super(TreeAnalyzerNumpy,self).__init__(cfg_ana, cfg_comp, looperName)
0014 self.outservicename = getattr(cfg_ana,"outservicename","outputfile")
0015 self.treename = getattr(cfg_ana,"treename","tree")
0016
0017
0018 def beginLoop(self, setup) :
0019 super(TreeAnalyzerNumpy, self).beginLoop(setup)
0020 if self.outservicename in setup.services:
0021 print("Using outputfile given in", self.outservicename)
0022 self.file = setup.services[self.outservicename].file
0023 else :
0024 fileName = '/'.join([self.dirName,
0025 'tree.root'])
0026 isCompressed = self.cfg_ana.isCompressed if hasattr(self.cfg_ana,'isCompressed') else 1
0027 print('Compression', isCompressed)
0028 self.file = TFile( fileName, 'recreate', '', isCompressed )
0029 self.file.cd()
0030 if self.file.Get(self.treename) :
0031 raise RuntimeError("You are booking two Trees with the same name in the same file")
0032 self.tree = Tree(self.treename, self.name)
0033 self.tree.setDefaultFloatType(getattr(self.cfg_ana, 'defaultFloatType','D'));
0034 self.declareVariables(setup)
0035
0036 def declareVariables(self,setup):
0037 print('TreeAnalyzerNumpy.declareVariables : overload this function.')
0038 pass
0039
0040 def write(self, setup):
0041 super(TreeAnalyzerNumpy, self).write(setup)
0042 if self.outservicename not in setup.services:
0043 self.file.Write()
0044