CMS 3D CMS Logo

mhtjuProducerCpp.py
Go to the documentation of this file.
1 # Example of calling a C++ helper from a Module.
2 #
3 # Run with:
4 # nano_postproc.py outDir /eos/cms/store/user/andrey/f.root -I PhysicsTools.NanoAODTools.postprocessing.examples.mhtjuProducerCpp mhtju
5 
7 import ROOT
8 import os
9 ROOT.PyConfig.IgnoreCommandLineOptions = True
10 
11 
12 # MHT producer, unclean jets only (no lepton overlap cleaning, no jet selection)
13 class mhtjuProducerCpp(Module):
14  def __init__(self):
15  base = os.getenv("NANOAODTOOLS_BASE")
16  if base:
17  # Running in standalone mode: compile the C++ helper
18  if "/MhtjuProducerCppWorker_cc.so" not in ROOT.gSystem.GetLibraries():
19  print("Load C++ MhtjuProducerCppWorker worker module")
20  ROOT.gROOT.ProcessLine(
21  ".L %s/test/examples/MhtjuProducerCppWorker.cc+O" % base)
22  else:
23  # Load the helper from the CMSSW compiled. This is not required if
24  # dictionaries for the helper are generated with classes_def.xml and
25  # classes.h
26  base = "%s/src/PhysicsTools/NanoAODTools" % os.getenv("CMSSW_BASE")
27  ROOT.gSystem.Load("libPhysicsToolsNanoAODToolsTest.so")
28  ROOT.gROOT.ProcessLine(".L %s/test/examples/MhtjuProducerCppWorker.h" % base)
29  self.worker = ROOT.MhtjuProducerCppWorker()
30  pass
31 
32  def beginJob(self):
33  pass
34 
35  def endJob(self):
36  pass
37 
38  def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
39  self.initReaders(inputTree) # initReaders must be called in beginFile
40  self.out = wrappedOutputTree
41  self.out.branch("MHTju_pt", "F")
42  self.out.branch("MHTju_phi", "F")
43 
44  def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
45  pass
46 
47  # this function gets the pointers to Value and ArrayReaders and sets
48  # them in the C++ worker class
49  def initReaders(self, tree):
50  self.nJet = tree.valueReader("nJet")
51  self.Jet_pt = tree.arrayReader("Jet_pt")
52  self.Jet_phi = tree.arrayReader("Jet_phi")
53  self.worker.setJets(self.nJet, self.Jet_pt, self.Jet_phi)
54  # self._ttreereaderversion must be set AFTER all calls to
55  # tree.valueReader or tree.arrayReader
56  self._ttreereaderversion = tree._ttreereaderversion
57 
58  def analyze(self, event):
59  """process event, return True (go to next module) or False (fail,
60  go to next event)"""
61 
62  # do this check at every event, as other modules might have read
63  # further branches
64  if event._tree._ttreereaderversion > self._ttreereaderversion:
65  self.initReaders(event._tree)
66  # do NOT access other branches in python between the check/call to
67  # initReaders and the call to C++ worker code
68  output = self.worker.getHT()
69 
70  self.out.fillBranch("MHTju_pt", output[0])
71  self.out.fillBranch("MHTju_phi", -output[1]) # note the minus
72  return True
73 
74 
75 # define modules using the syntax 'name = lambda : constructor' to avoid
76 # having them loaded when not needed
77 
78 mhtju = lambda: mhtjuProducerCpp()
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree)