forked from cms-nanoAOD/nanoAOD-tools
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mhtjuProducerCpp.py
71 lines (58 loc) · 2.69 KB
/
mhtjuProducerCpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
import ROOT
import os
ROOT.PyConfig.IgnoreCommandLineOptions = True
# MHT producer, unclean jets only (no lepton overlap cleaning, no jet selection)
class mhtjuProducerCpp(Module):
def __init__(self):
if "/mhtjuProducerCppWorker_cc.so" not in ROOT.gSystem.GetLibraries():
print("Load C++ mhtjuProducerCppWorker worker module")
base = os.getenv("NANOAODTOOLS_BASE")
if base:
ROOT.gROOT.ProcessLine(
".L %s/src/mhtjuProducerCppWorker.cc+O" % base)
else:
base = "%s/src/PhysicsTools/NanoAODTools" % os.getenv(
"CMSSW_BASE")
ROOT.gSystem.Load("libPhysicsToolsNanoAODTools.so")
ROOT.gROOT.ProcessLine(
".L %s/interface/mhtjuProducerCppWorker.h" % base)
self.worker = ROOT.mhtjuProducerCppWorker()
pass
def beginJob(self):
pass
def endJob(self):
pass
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
self.initReaders(inputTree) # initReaders must be called in beginFile
self.out = wrappedOutputTree
self.out.branch("MHTju_pt", "F")
self.out.branch("MHTju_phi", "F")
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
pass
# this function gets the pointers to Value and ArrayReaders and sets
# them in the C++ worker class
def initReaders(self, tree):
self.nJet = tree.valueReader("nJet")
self.Jet_pt = tree.arrayReader("Jet_pt")
self.Jet_phi = tree.arrayReader("Jet_phi")
self.worker.setJets(self.nJet, self.Jet_pt, self.Jet_phi)
# self._ttreereaderversion must be set AFTER all calls to
# tree.valueReader or tree.arrayReader
self._ttreereaderversion = tree._ttreereaderversion
def analyze(self, event):
"""process event, return True (go to next module) or False (fail,
go to next event)"""
# do this check at every event, as other modules might have read
# further branches
if event._tree._ttreereaderversion > self._ttreereaderversion:
self.initReaders(event._tree)
# do NOT access other branches in python between the check/call to
# initReaders and the call to C++ worker code
output = self.worker.getHT()
self.out.fillBranch("MHTju_pt", output[0])
self.out.fillBranch("MHTju_phi", -output[1]) # note the minus
return True
# define modules using the syntax 'name = lambda : constructor' to avoid
# having them loaded when not needed
mhtju = lambda: mhtjuProducerCpp()