CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes
tree.Tree Class Reference
Inheritance diagram for tree.Tree:

Public Member Functions

def __init__ (self, name, title, defaultFloatType="D", defaultIntType="I")
 
def branch_ (self, selfmap, varName, type, len, postfix="", storageType="default", title=None)
 
def copyStructure (self, tree)
 
def fill (self, varName, value)
 
def reset (self)
 
def setDefaultFloatType (self, defaultFloatType)
 
def setDefaultIntType (self, defaultIntType)
 
def var (self, varName, type=float, default=-99, title=None, storageType="default", filler=None)
 
def vector (self, varName, lenvar, maxlen=None, type=float, default=-99, title=None, storageType="default", filler=None)
 
def vfill (self, varName, values)
 

Public Attributes

 defaultFloatType
 
 defaultIntType
 
 defaults
 
 fillers
 
 tree
 
 vars
 
 vecdefaults
 
 vecvars
 

Detailed Description

Definition at line 6 of file tree.py.

Constructor & Destructor Documentation

def tree.Tree.__init__ (   self,
  name,
  title,
  defaultFloatType = "D",
  defaultIntType = "I" 
)

Definition at line 8 of file tree.py.

8  def __init__(self, name, title, defaultFloatType="D", defaultIntType="I"):
9  self.vars = {}
10  self.vecvars = {}
11  self.tree = TTree(name, title)
12  self.defaults = {}
13  self.vecdefaults = {}
14  self.defaultFloatType = defaultFloatType
15  self.defaultIntType = defaultIntType
16  self.fillers = {}
17 
vecvars
Definition: tree.py:10
def __init__(self, name, title, defaultFloatType="D", defaultIntType="I")
Definition: tree.py:8
vars
Definition: tree.py:9
tree
Definition: tree.py:11
vecdefaults
Definition: tree.py:13
defaults
Definition: tree.py:12
defaultIntType
Definition: tree.py:15
fillers
Definition: tree.py:16
defaultFloatType
Definition: tree.py:14

Member Function Documentation

def tree.Tree.branch_ (   self,
  selfmap,
  varName,
  type,
  len,
  postfix = "",
  storageType = "default",
  title = None 
)
Backend function used to create scalar and vector branches. 
   Users should call "var" and "vector", not this function directly.

Definition at line 33 of file tree.py.

References tree.Tree.defaultFloatType, and tree.Tree.defaultIntType.

Referenced by tree.Tree.var(), and tree.Tree.vector().

33  def branch_(self, selfmap, varName, type, len, postfix="", storageType="default", title=None):
34  """Backend function used to create scalar and vector branches.
35  Users should call "var" and "vector", not this function directly."""
36  if storageType == "default":
37  storageType = self.defaultIntType if type is int else self.defaultFloatType
38  if type is float :
39  if storageType == "F":
40  selfmap[varName]=numpy.zeros(len,numpy.float32)
41  self.tree.Branch(varName,selfmap[varName],varName+postfix+'/F')
42  elif storageType == "D":
43  selfmap[varName]=numpy.zeros(len,numpy.float64)
44  self.tree.Branch(varName,selfmap[varName],varName+postfix+'/D')
45  else:
46  raise RuntimeError('Unknown storage type %s for branch %s' % (storageType, varName))
47  elif type is int:
48  dtypes = {
49  "i" : numpy.uint32,
50  "s" : numpy.uint16,
51  "b" : numpy.uint8,
52  "l" : numpy.uint64,
53  "I" : numpy.int32,
54  "S" : numpy.int16,
55  "B" : numpy.int8,
56  "L" : numpy.int64,
57  }
58  if storageType not in dtypes:
59  raise RuntimeError('Unknown storage type %s for branch %s' % (storageType, varName))
60  selfmap[varName]=numpy.zeros(len,dtypes[storageType])
61  self.tree.Branch(varName,selfmap[varName],varName+postfix+'/'+storageType)
62  else:
63  raise RuntimeError('Unknown type %s for branch %s' % (type, varName))
64  if title:
65  self.tree.GetBranch(varName).SetTitle(title)
66 
def branch_(self, selfmap, varName, type, len, postfix="", storageType="default", title=None)
Definition: tree.py:33
defaultIntType
Definition: tree.py:15
defaultFloatType
Definition: tree.py:14
def tree.Tree.copyStructure (   self,
  tree 
)

Definition at line 24 of file tree.py.

References CentralityTable::BinValues.var, TaggingVariablePlotter::VariableConfig.var, PhysicsTools::SourceVariableSet::PosVar.var, tree.Tree.var(), reco::parser::Grammar::definition< ScannerT >.var, and PhysicsTools::MVAComputer::InputVar.var.

24  def copyStructure(self, tree):
25  for branch in tree.GetListOfBranches():
26  name = branch.GetName()
27  typeName = branch.GetListOfLeaves()[0].GetTypeName()
28  type = float
29  if typeName == 'Int_t':
30  type = int
31  self.var(name, type)
32 
def var(self, varName, type=float, default=-99, title=None, storageType="default", filler=None)
Definition: tree.py:67
def copyStructure(self, tree)
Definition: tree.py:24
def tree.Tree.fill (   self,
  varName,
  value 
)

Definition at line 117 of file tree.py.

References tree.Tree.fillers, tree.Tree.vars, PlotAlignmentValidation::DMRPlotInfo.vars, and JsonOutputProducer.vars.

117  def fill(self, varName, value ):
118  if isinstance(self.vars[varName], numpy.ndarray):
119  self.vars[varName][0]=value
120  else:
121  self.fillers[varName](self.vars[varName],value)
122 
vars
Definition: tree.py:9
def fill(self, varName, value)
Definition: tree.py:117
fillers
Definition: tree.py:16
def tree.Tree.reset (   self)

Definition at line 104 of file tree.py.

References tree.Tree.defaults, geometryComparison.GeometryComparison.defaults, primaryVertexValidation.PrimaryVertexValidation.defaults, offlineValidation.OfflineValidation.defaults, zMuMuValidation.ZMuMuValidation.defaults, preexistingValidation.PreexistingValidation.defaults, plottingOptions.BasePlottingOptions.defaults, genericValidation.GenericValidation.defaults, preexistingValidation.PreexistingOfflineValidation.defaults, plottingOptions.PlottingOptionsTrackSplitting.defaults, plottingOptions.PlottingOptionsZMuMu.defaults, plottingOptions.PlottingOptionsOffline.defaults, plottingOptions.PlottingOptionsPrimaryVertex.defaults, genericValidation.GenericValidationData.defaults, genericValidation.GenericValidationData_CTSR.defaults, svgfig.Path.defaults, svgfig.Curve.defaults, svgfig.Poly.defaults, svgfig.Text.defaults, svgfig.TextGlobal.defaults, svgfig.Dots.defaults, svgfig.Line.defaults, svgfig.LineGlobal.defaults, svgfig.VLine.defaults, svgfig.HLine.defaults, svgfig.Rect.defaults, svgfig.Ellipse.defaults, svgfig.Ticks.defaults, svgfig.CurveAxis.defaults, svgfig.LineAxis.defaults, svgfig.XAxis.defaults, svgfig.YAxis.defaults, svgfig.Axes.defaults, svgfig.HGrid.defaults, svgfig.VGrid.defaults, svgfig.Grid.defaults, svgfig.XErrorBars.defaults, svgfig.YErrorBars.defaults, tree.Tree.fillers, tree.Tree.vars, PlotAlignmentValidation::DMRPlotInfo.vars, JsonOutputProducer.vars, tree.Tree.vecdefaults, and tree.Tree.vecvars.

104  def reset(self):
105  for name,value in six.iteritems(self.vars):
106  if name in self.fillers:
107  self.fillers[name](value, self.defaults[name])
108  else:
109  value[0]=self.defaults[name]
110  for name,value in six.iteritems(self.vecvars):
111  if isinstance(value, numpy.ndarray):
112  value.fill(self.vecdefaults[name])
113  else:
114  if isinstance(value, ROOT.TObject) and value.ClassName() == "TClonesArray":
115  value.ExpandCreateFast(0)
116 
vecvars
Definition: tree.py:10
vars
Definition: tree.py:9
def reset(self)
Definition: tree.py:104
vecdefaults
Definition: tree.py:13
defaults
Definition: tree.py:12
fillers
Definition: tree.py:16
def tree.Tree.setDefaultFloatType (   self,
  defaultFloatType 
)

Definition at line 18 of file tree.py.

References tree.Tree.defaultFloatType.

18  def setDefaultFloatType(self, defaultFloatType):
19  self.defaultFloatType = defaultFloatType
20 
def setDefaultFloatType(self, defaultFloatType)
Definition: tree.py:18
defaultFloatType
Definition: tree.py:14
def tree.Tree.setDefaultIntType (   self,
  defaultIntType 
)

Definition at line 21 of file tree.py.

References tree.Tree.defaultIntType.

21  def setDefaultIntType(self, defaultIntType):
22  self.defaultIntType = defaultIntType
23 
def setDefaultIntType(self, defaultIntType)
Definition: tree.py:21
defaultIntType
Definition: tree.py:15
def tree.Tree.var (   self,
  varName,
  type = float,
  default = -99,
  title = None,
  storageType = "default",
  filler = None 
)

Definition at line 67 of file tree.py.

References tree.Tree.branch_(), fwlite::RecordWriter::DataBuffer.branch_, edm::TypeInBranchType.branch_, fwlite::internal::Data.branch_, BareRootProductGetter::Buffer.branch_, edm::PoolOutputModule::SpecialSplitLevelForBranch.branch_, tree.Tree.defaults, geometryComparison.GeometryComparison.defaults, primaryVertexValidation.PrimaryVertexValidation.defaults, offlineValidation.OfflineValidation.defaults, zMuMuValidation.ZMuMuValidation.defaults, preexistingValidation.PreexistingValidation.defaults, plottingOptions.BasePlottingOptions.defaults, genericValidation.GenericValidation.defaults, preexistingValidation.PreexistingOfflineValidation.defaults, plottingOptions.PlottingOptionsTrackSplitting.defaults, plottingOptions.PlottingOptionsZMuMu.defaults, plottingOptions.PlottingOptionsOffline.defaults, plottingOptions.PlottingOptionsPrimaryVertex.defaults, genericValidation.GenericValidationData.defaults, genericValidation.GenericValidationData_CTSR.defaults, svgfig.Path.defaults, svgfig.Curve.defaults, svgfig.Poly.defaults, svgfig.Text.defaults, svgfig.TextGlobal.defaults, svgfig.Dots.defaults, svgfig.Line.defaults, svgfig.LineGlobal.defaults, svgfig.VLine.defaults, svgfig.HLine.defaults, svgfig.Rect.defaults, svgfig.Ellipse.defaults, svgfig.Ticks.defaults, svgfig.CurveAxis.defaults, svgfig.LineAxis.defaults, svgfig.XAxis.defaults, svgfig.YAxis.defaults, svgfig.Axes.defaults, svgfig.HGrid.defaults, svgfig.VGrid.defaults, svgfig.Grid.defaults, svgfig.XErrorBars.defaults, svgfig.YErrorBars.defaults, tree.Tree.fillers, tree.Tree.vars, PlotAlignmentValidation::DMRPlotInfo.vars, and JsonOutputProducer.vars.

Referenced by tree.Tree.copyStructure().

67  def var(self, varName,type=float, default=-99, title=None, storageType="default", filler=None ):
68  if type in [int, float]:
69  self.branch_(self.vars, varName, type, 1, title=title, storageType=storageType)
70  self.defaults[varName] = default
71  elif __builtins__['type'](type) == str:
72  # create a value, looking up the type from ROOT and calling the default constructor
73  self.vars[varName] = getattr(ROOT,type)()
74  if type in [ "TLorentzVector" ]: # custom streamer classes
75  self.tree.Branch(varName+".", type, self.vars[varName], 8000,-1)
76  else:
77  self.tree.Branch(varName+".", type, self.vars[varName])
78  if filler is None:
79  raise RuntimeError("Error: when brancing with an object, filler should be set to a function that takes as argument an object instance and a value, and set the instance to the value (as otherwise python assignment of objects changes the address as well)")
80  self.fillers[varName] = filler
81  else:
82  raise RuntimeError('Unknown type %s for branch %s: it is not int, float or a string' % (type, varName))
83  self.defaults[varName] = default
84 
def var(self, varName, type=float, default=-99, title=None, storageType="default", filler=None)
Definition: tree.py:67
def branch_(self, selfmap, varName, type, len, postfix="", storageType="default", title=None)
Definition: tree.py:33
vars
Definition: tree.py:9
defaults
Definition: tree.py:12
fillers
Definition: tree.py:16
def tree.Tree.vector (   self,
  varName,
  lenvar,
  maxlen = None,
  type = float,
  default = -99,
  title = None,
  storageType = "default",
  filler = None 
)
either lenvar is a string, and maxlen an int (variable size array), or lenvar is an int and maxlen is not specified (fixed array)

Definition at line 85 of file tree.py.

References tree.Tree.branch_(), fwlite::RecordWriter::DataBuffer.branch_, edm::TypeInBranchType.branch_, fwlite::internal::Data.branch_, BareRootProductGetter::Buffer.branch_, edm::PoolOutputModule::SpecialSplitLevelForBranch.branch_, tree.Tree.fillers, tree.Tree.vecdefaults, and tree.Tree.vecvars.

85  def vector(self, varName, lenvar, maxlen=None, type=float, default=-99, title=None, storageType="default", filler=None ):
86  """either lenvar is a string, and maxlen an int (variable size array), or lenvar is an int and maxlen is not specified (fixed array)"""
87  if type in [int, float]:
88  if __builtins__['type'](lenvar) == int: # need the __builtins__ since 'type' is a variable here :-/
89  self.branch_(self.vecvars, varName, type, lenvar, postfix="[%d]" % lenvar, title=title, storageType=storageType)
90  else:
91  if maxlen == None: RuntimeError, 'You must specify a maxlen if making a dynamic array';
92  self.branch_(self.vecvars, varName, type, maxlen, postfix="[%s]" % lenvar, title=title, storageType=storageType)
93  elif __builtins__['type'](type) == str:
94  self.vecvars[varName] = ROOT.TClonesArray(type,(lenvar if __builtins__['type'](lenvar) == int else maxlen))
95  if type in [ "TLorentzVector" ]: # custom streamer classes
96  self.tree.Branch(varName+".", self.vecvars[varName], 32000, -1)
97  else:
98  self.tree.Branch(varName+".", self.vecvars[varName])
99  if filler is None:
100  raise RuntimeError("Error: when brancing with an object, filler should be set to a function that takes as argument an object instance and a value, and set the instance to the value (as otherwise python assignment of objects changes the address as well)")
101  self.fillers[varName] = filler
102  self.vecdefaults[varName] = default
103 
def vector(self, varName, lenvar, maxlen=None, type=float, default=-99, title=None, storageType="default", filler=None)
Definition: tree.py:85
vecvars
Definition: tree.py:10
def branch_(self, selfmap, varName, type, len, postfix="", storageType="default", title=None)
Definition: tree.py:33
vecdefaults
Definition: tree.py:13
fillers
Definition: tree.py:16
def tree.Tree.vfill (   self,
  varName,
  values 
)

Definition at line 123 of file tree.py.

References tree.Tree.fillers, and tree.Tree.vecvars.

123  def vfill(self, varName, values ):
124  a = self.vecvars[varName]
125  if isinstance(a, numpy.ndarray):
126  for (i,v) in enumerate(values):
127  a[i]=v
128  else:
129  if isinstance(a, ROOT.TObject) and a.ClassName() == "TClonesArray":
130  a.ExpandCreateFast(len(values))
131  fillit = self.fillers[varName]
132  for (i,v) in enumerate(values):
133  fillit(a[i],v)
134 
vecvars
Definition: tree.py:10
def vfill(self, varName, values)
Definition: tree.py:123
fillers
Definition: tree.py:16

Member Data Documentation

tree.Tree.defaultFloatType

Definition at line 14 of file tree.py.

Referenced by tree.Tree.branch_(), and tree.Tree.setDefaultFloatType().

tree.Tree.defaultIntType

Definition at line 15 of file tree.py.

Referenced by tree.Tree.branch_(), and tree.Tree.setDefaultIntType().

tree.Tree.defaults

Definition at line 12 of file tree.py.

Referenced by tree.Tree.reset(), and tree.Tree.var().

tree.Tree.fillers
tree.Tree.vars

Definition at line 9 of file tree.py.

Referenced by tree.Tree.fill(), tree.Tree.reset(), and tree.Tree.var().

tree.Tree.vecdefaults

Definition at line 13 of file tree.py.

Referenced by tree.Tree.reset(), and tree.Tree.vector().

tree.Tree.vecvars

Definition at line 10 of file tree.py.

Referenced by tree.Tree.reset(), tree.Tree.vector(), and tree.Tree.vfill().