CMS 3D CMS Logo

TreeAnalyzerNumpy.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
3 from PhysicsTools.HeppyCore.statistics.tree import Tree as Tree
4 from ROOT import TFile
5 
7  """Base TreeAnalyzerNumpy, to create flat TTrees.
8 
9  Check out TestTreeAnalyzer for a concrete example.
10  IMPORTANT: FOR NOW, CANNOT RUN SEVERAL TreeAnalyzers AT THE SAME TIME!
11  Anyway, you want only one TTree, don't you?"""
12 
13  def __init__(self, cfg_ana, cfg_comp, looperName):
14  super(TreeAnalyzerNumpy,self).__init__(cfg_ana, cfg_comp, looperName)
15  self.outservicename = getattr(cfg_ana,"outservicename","outputfile")
16  self.treename = getattr(cfg_ana,"treename","tree")
17 
18 
19  def beginLoop(self, setup) :
20  super(TreeAnalyzerNumpy, self).beginLoop(setup)
21  if self.outservicename in setup.services:
22  print("Using outputfile given in", self.outservicename)
23  self.file = setup.services[self.outservicename].file
24  else :
25  fileName = '/'.join([self.dirName,
26  'tree.root'])
27  isCompressed = self.cfg_ana.isCompressed if hasattr(self.cfg_ana,'isCompressed') else 1
28  print('Compression', isCompressed)
29  self.file = TFile( fileName, 'recreate', '', isCompressed )
30  self.file.cd()
31  if self.file.Get(self.treename) :
32  raise RuntimeError("You are booking two Trees with the same name in the same file")
33  self.tree = Tree(self.treename, self.name)
34  self.tree.setDefaultFloatType(getattr(self.cfg_ana, 'defaultFloatType','D')); # or 'F'
35  self.declareVariables(setup)
36 
37  def declareVariables(self,setup):
38  print('TreeAnalyzerNumpy.declareVariables : overload this function.')
39  pass
40 
41  def write(self, setup):
42  super(TreeAnalyzerNumpy, self).write(setup)
43  if self.outservicename not in setup.services:
44  self.file.Write()
45 
def __init__(self, cfg_ana, cfg_comp, looperName)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:21
T * Get(Args... args)
Definition: Trend.h:122