CMS 3D CMS Logo

exampleModule.py
Go to the documentation of this file.
1 # This is an example of a NanoAODTools Module to add one variable to nanoAODs.
2 # Note that:
3 # -the new variable will be available for use in the subsequent modules
4 # -it is possible to update the value for existing variables
5 #
6 # Example of using from command line:
7 # nano_postproc.py outDir /eos/cms/store/user/andrey/f.root -I PhysicsTools.NanoAODTools.postprocessing.examples.exampleModule exampleModuleConstr
8 #
9 # Example of running in a python script: see test/example_postproc.py
10 #
11 
12 from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Collection
14 import ROOT
15 ROOT.PyConfig.IgnoreCommandLineOptions = True
16 
17 
18 class exampleProducer(Module):
19  def __init__(self, jetSelection):
20  self.jetSel = jetSelection
21  pass
22 
23  def beginJob(self):
24  pass
25 
26  def endJob(self):
27  pass
28 
29  def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
30  self.out = wrappedOutputTree
31  self.out.branch("EventMass", "F")
32 
33  def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
34  pass
35 
36  def analyze(self, event):
37  """process event, return True (go to next module) or False (fail, go to next event)"""
38  electrons = Collection(event, "Electron")
39  muons = Collection(event, "Muon")
40  jets = Collection(event, "Jet")
41  eventSum = ROOT.TLorentzVector()
42  for lep in muons:
43  eventSum += lep.p4()
44  for lep in electrons:
45  eventSum += lep.p4()
46  for j in filter(self.jetSel, jets):
47  eventSum += j.p4()
48  self.out.fillBranch("EventMass", eventSum.M())
49  return True
50 
51 
52 # define modules using the syntax 'name = lambda : constructor' to avoid having them loaded when not needed
53 
54 exampleModuleConstr = lambda: exampleProducer(jetSelection=lambda j: j.pt > 30)
def __init__(self, jetSelection)
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree)
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree)