1 """Python helper tools for CMS FWLite
3 benedikt.hegner@cern.ch
6 from __future__
import absolute_import
14 readline.parse_and_bind(
'tab: complete')
16 print 'WARNING: Could not load tab completion'
20 from .
import iterators
27 if hasattr(container,
'GetEntries'):
29 entries = container.GetEntries()
30 for entry
in xrange(entries):
33 raise cmserror(
"Looping of %s failed" %container)
36 elif hasattr(container,
'size'):
38 entries = container.size()
39 for entry
in xrange(entries):
40 yield container[entry]
46 """Convert a pair of C++ iterators into a python generator"""
48 yield begin.__deref__()
53 reColons = re.compile(
r'::')
54 reCloseTemplate =re.compile(
r'>')
55 reOpenTemplate =re.compile(
r'<')
56 branchType = ROOT.branchToClass(branch)
58 buffer = ROOT.MakeRootClass(branchType.GetName()) ()
59 if( branch.GetName()[-1] !=
'.')
and (branch.GetName()!=
"EventAuxiliary"):
60 branch.SetAddress(buffer)
62 branch.SetAddress(ROOT.AddressOf(buffer))
68 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")
70 if isinstance(obj, ROOT.TTree):
72 elif isinstance(obj, ROOT.TFile):
73 self.
_tree = obj.Get(treeName)
74 elif isinstance(obj, str):
75 self.
_tree = ROOT.TFile.Open(obj).Get(treeName)
77 raise cmserror(
"EventTree accepts only TTrees, TFiles and filenames")
83 alias = self._tree.GetAlias(name)
84 if alias !=
'': name = alias
91 """C++ code for accessing the product inside the full framework"""
92 alias = self._tree.GetAlias(name)
93 if alias !=
'': name = alias
94 tmpBranch = self._tree.GetBranch(name)
95 typeString = ROOT.branchToClass(tmpBranch).GetName()
96 if "edm::Wrapper" in typeString:
97 typeString = typeString.replace(
"<edm::Wrapper",
"")
98 typeString = typeString.rstrip(
">")
99 nameParts = name.split(
"_")
100 if nameParts[2] ==
"":
101 cppCode =
'edm::Handle<%s > dummy;\nevent.getByLabel("%s", dummy);'\
102 %(typeString, nameParts[1])
104 cppCode =
'edm::Handle<%s > dummy;\nevent.getByLabel("%s", "%s", dummy);'\
105 %(typeString, nameParts[1], nameParts[2])
116 for branch
in self._usedBranches.itervalues():
117 branch.setIndex(self.
_index)
121 if key <0
or key > self._tree.GetEntries():
125 self._tree.GetEntry(self.
_index,0)
132 self._tree.GetEntry(self.
_index,0)
134 for entry
in xrange(self._tree.GetEntries()):
137 self._tree.GetEntry(self.
_index,0)
146 return iterators.addIterator(self._eventTree.branch(name)())
149 return iterators.addIterator(self._eventTree.branch(name)())
156 raise cmserror(
"Unknown branch "+name)
164 self._branch.GetEntry(self.
_index)
176 length = len(message)+7
178 print "ERROR:", message