CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cmstools.py
Go to the documentation of this file.
1 """Python helper tools for CMS FWLite
2 
3 benedikt.hegner@cern.ch
4 
5 """
6 import re
7 import ROOT
8 import exceptions
9 import sys
10 ### define tab completion
11 try:
12  import readline #cmscompleter
13  readline.parse_and_bind('tab: complete')
14 except:
15  print 'WARNING: Could not load tab completion'
16 
17 
18 # for adding iterators at runtime
19 import iterators
20 
21 
22 ### workaround iterator generators for ROOT classes
23 def all(container):
24 
25  # loop over ROOT::TTree and similar
26  if hasattr(container,'GetEntries'):
27  try:
28  entries = container.GetEntries()
29  for entry in xrange(entries):
30  yield entry
31  except:
32  raise cmserror("Looping of %s failed" %container)
33 
34  # loop over std::vectors and similar
35  elif hasattr(container, 'size'):
36  try:
37  entries = container.size()
38  for entry in xrange(entries):
39  yield container[entry]
40  except:
41  pass
42 
43  # loop over containers with begin and end iterators
44 def loop(begin, end):
45  """Convert a pair of C++ iterators into a python generator"""
46  while (begin != end):
47  yield begin.__deref__() #*b
48  begin.__preinc__() #++b
49 
50 ### auto branch types (Chris Jones)
51 def createBranchBuffer(branch):
52  reColons = re.compile(r'::')
53  reCloseTemplate =re.compile(r'>')
54  reOpenTemplate =re.compile(r'<')
55  branchType = ROOT.branchToClass(branch)
56  #buffer = eval ('ROOT.'+reColons.sub(".",reOpenTemplate.sub("(ROOT.",reCloseTemplate.sub(")",branchType.GetName())))+'()')
57  buffer = ROOT.MakeRootClass(branchType.GetName()) ()
58  if( branch.GetName()[-1] != '.') and (branch.GetName()!="EventAuxiliary"):
59  branch.SetAddress(buffer)
60  else:
61  branch.SetAddress(ROOT.AddressOf(buffer))
62  return buffer
63 
64 
66  def __init__(self,obj):
67  sys.stderr.write ("WARNING: This package has been deprecated and will be removed in the near future.\nPlease switch to using FWLite.Python (https://twiki.cern.ch/twiki/bin/viewauth/CMS/WorkBookFWLitePython)\n")
68  treeName = 'Events'
69  if isinstance(obj, ROOT.TTree):
70  self._tree = obj
71  elif isinstance(obj, ROOT.TFile):
72  self._tree = obj.Get(treeName)
73  elif isinstance(obj, str):
74  self._tree = ROOT.TFile.Open(obj).Get(treeName)
75  else:
76  raise cmserror("EventTree accepts only TTrees, TFiles and filenames")
78  self._index = -1
79  self._aliases = self._tree.GetListOfAliases()
80  def branch(self,name):
81  # support for aliases
82  alias = self._tree.GetAlias(name)
83  if alias != '': name = alias
84  # access the branch in ttree
85  if name in self._usedBranches:
86  return self._usedBranches[name]
87  self._usedBranches[name]=EventBranch(self,name)
88  return self._usedBranches[name]
89  def cppCode(self, name):
90  """C++ code for accessing the product inside the full framework"""
91  alias = self._tree.GetAlias(name)
92  if alias != '': name = alias
93  tmpBranch = self._tree.GetBranch(name)
94  typeString = ROOT.branchToClass(tmpBranch).GetName()
95  if "edm::Wrapper" in typeString:
96  typeString = typeString.replace("<edm::Wrapper","")
97  typeString = typeString.rstrip(">")
98  nameParts = name.split("_")
99  if nameParts[2] == "":
100  cppCode = 'edm::Handle<%s > dummy;\nevent.getByLabel("%s", dummy);'\
101  %(typeString, nameParts[1])
102  else:
103  cppCode = 'edm::Handle<%s > dummy;\nevent.getByLabel("%s", "%s", dummy);'\
104  %(typeString, nameParts[1], nameParts[2])
105  return cppCode
106  def getListOfAliases(self):
107  return self._aliases
108  def SetAlias (self, alias, fullName):
109  self.tree().SetAlias(alias, fullName)
110  def index(self):
111  return self._index
112  def tree(self):
113  return self._tree
115  for branch in self._usedBranches.itervalues():
116  branch.setIndex(self._index)
117  def __getattr__(self, name):
118  return self.branch(name)
119  def __getitem__(self,key):
120  if key <0 or key > self._tree.GetEntries():
121  raise IndexError
122  self._index = key
123  self.__setBranchIndicies()
124  self._tree.GetEntry(self._index,0)
125  return Event(self)
126  def __iter__(self):
127  # flushing/initializing the root buffers
128  entry = 0
129  self._index = entry
130  self.__setBranchIndicies()
131  self._tree.GetEntry(self._index,0)
132  # the real loop
133  for entry in xrange(self._tree.GetEntries()):
134  self._index = entry
135  self.__setBranchIndicies()
136  self._tree.GetEntry(self._index,0)
137  yield Event(self) # TODO: don't return a new object but update the old one
138 
139 
140 class Event(object):
141  def __init__(self, eventTree):
142  self._eventTree = eventTree
143 
144  def getProduct(self, name):
145  return iterators.addIterator(self._eventTree.branch(name)())
146 
147  def __getattr__(self, name):
148  return iterators.addIterator(self._eventTree.branch(name)())
149 
150 
152  def __init__(self,parent,name):
153  self._branch = parent.tree().GetBranch(name)
154  if self._branch == None:
155  raise cmserror("Unknown branch "+name)
157  self._index = parent.index()
158  self._readData = False
159  def setIndex(self,index):
160  self._index = index
161  self._readData = False
162  def __readData(self):
163  self._branch.GetEntry(self._index)
164  self._readData = True
165 
166  # replace this by __getattr__ to allow branch.attr instead of branch().attr
167  def __call__(self):
168  if not self._readData:
169  self.__readData()
170  return self._buffer
171 
172 
173 class cmserror(exceptions.StandardError):
174  def __init__(self, message):
175  length = len(message)+7 #7=len("ERROR: ")
176  print "="*length
177  print "ERROR:", message
178  print "="*length
def createBranchBuffer
auto branch types (Chris Jones)
Definition: cmstools.py:51
def all
workaround iterator generators for ROOT classes
Definition: cmstools.py:23
list object
Definition: dbtoconf.py:77
if(conf.exists("allCellsPositionCalc"))