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 5 of file tree.py.

Constructor & Destructor Documentation

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

Definition at line 7 of file tree.py.

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

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 32 of file tree.py.

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

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

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

Definition at line 23 of file tree.py.

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

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

Definition at line 116 of file tree.py.

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

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

Definition at line 103 of file tree.py.

References tree.Tree.defaults, geometryComparison.GeometryComparison.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, and tree.Tree.vecdefaults.

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

Definition at line 17 of file tree.py.

References tree.Tree.defaultFloatType.

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

Definition at line 20 of file tree.py.

References tree.Tree.defaultIntType.

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

Definition at line 66 of file tree.py.

References tree.Tree.branch_(), fwlite::RecordWriter::DataBuffer.branch_, edm::TypeInBranchType.branch_, fwlite::internal::Data.branch_, BareRootProductGetter::Buffer.branch_, tree.Tree.defaults, geometryComparison.GeometryComparison.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().

66  def var(self, varName,type=float, default=-99, title=None, storageType="default", filler=None ):
67  if type in [int, float]:
68  self.branch_(self.vars, varName, type, 1, title=title, storageType=storageType)
69  self.defaults[varName] = default
70  elif __builtins__['type'](type) == str:
71  # create a value, looking up the type from ROOT and calling the default constructor
72  self.vars[varName] = getattr(ROOT,type)()
73  if type in [ "TLorentzVector" ]: # custom streamer classes
74  self.tree.Branch(varName+".", type, self.vars[varName], 8000,-1)
75  else:
76  self.tree.Branch(varName+".", type, self.vars[varName])
77  if filler is None:
78  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)")
79  self.fillers[varName] = filler
80  else:
81  raise RuntimeError('Unknown type %s for branch %s: it is not int, float or a string' % (type, varName))
82  self.defaults[varName] = default
83 
def var(self, varName, type=float, default=-99, title=None, storageType="default", filler=None)
Definition: tree.py:66
def branch_(self, selfmap, varName, type, len, postfix="", storageType="default", title=None)
Definition: tree.py:32
vars
Definition: tree.py:8
defaults
Definition: tree.py:11
fillers
Definition: tree.py:15
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 84 of file tree.py.

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

84  def vector(self, varName, lenvar, maxlen=None, type=float, default=-99, title=None, storageType="default", filler=None ):
85  """either lenvar is a string, and maxlen an int (variable size array), or lenvar is an int and maxlen is not specified (fixed array)"""
86  if type in [int, float]:
87  if __builtins__['type'](lenvar) == int: # need the __builtins__ since 'type' is a variable here :-/
88  self.branch_(self.vecvars, varName, type, lenvar, postfix="[%d]" % lenvar, title=title, storageType=storageType)
89  else:
90  if maxlen == None: RuntimeError, 'You must specify a maxlen if making a dynamic array';
91  self.branch_(self.vecvars, varName, type, maxlen, postfix="[%s]" % lenvar, title=title, storageType=storageType)
92  elif __builtins__['type'](type) == str:
93  self.vecvars[varName] = ROOT.TClonesArray(type,(lenvar if __builtins__['type'](lenvar) == int else maxlen))
94  if type in [ "TLorentzVector" ]: # custom streamer classes
95  self.tree.Branch(varName+".", self.vecvars[varName], 32000, -1)
96  else:
97  self.tree.Branch(varName+".", self.vecvars[varName])
98  if filler is None:
99  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)")
100  self.fillers[varName] = filler
101  self.vecdefaults[varName] = default
102 
def vector(self, varName, lenvar, maxlen=None, type=float, default=-99, title=None, storageType="default", filler=None)
Definition: tree.py:84
vecvars
Definition: tree.py:9
def branch_(self, selfmap, varName, type, len, postfix="", storageType="default", title=None)
Definition: tree.py:32
vecdefaults
Definition: tree.py:12
fillers
Definition: tree.py:15
def tree.Tree.vfill (   self,
  varName,
  values 
)

Definition at line 122 of file tree.py.

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

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

Member Data Documentation

tree.Tree.defaultFloatType

Definition at line 13 of file tree.py.

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

tree.Tree.defaultIntType

Definition at line 14 of file tree.py.

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

tree.Tree.defaults

Definition at line 11 of file tree.py.

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

tree.Tree.fillers
tree.Tree.vars

Definition at line 8 of file tree.py.

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

tree.Tree.vecdefaults

Definition at line 12 of file tree.py.

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

tree.Tree.vecvars

Definition at line 9 of file tree.py.

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