Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:26

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