1 |
jpata |
1.1 |
import ROOT
|
2 |
|
|
from DataFormats.FWLite import Events, Handle
|
3 |
|
|
import sys
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
#Load PyTables
|
7 |
|
|
from tables import *
|
8 |
|
|
|
9 |
|
|
|
10 |
|
|
#Define the event database row
|
11 |
|
|
class Event(IsDescription):
|
12 |
|
|
eventId = Int32Col()
|
13 |
|
|
runId = Int32Col()
|
14 |
|
|
lumiId = Int32Col()
|
15 |
|
|
nTightLeptons = Int32Col()
|
16 |
|
|
leptonFlavour = Int32Col()
|
17 |
|
|
leptonEta = Float32Col()
|
18 |
|
|
jetPt0 = Float32Col()
|
19 |
|
|
|
20 |
|
|
#Create the database file
|
21 |
|
|
h5file = openFile("tutorial1.h5", mode = "w", title = "Test file")
|
22 |
|
|
|
23 |
|
|
#Create table in the database
|
24 |
|
|
table = h5file.createTable("/", "EventTable", Event)
|
25 |
|
|
|
26 |
|
|
#Get a handle to the row
|
27 |
|
|
event_row = table.row
|
28 |
|
|
|
29 |
|
|
ROOT.gROOT.SetBatch() # don't pop up canvases
|
30 |
|
|
|
31 |
|
|
#Define the input file
|
32 |
|
|
input_file = "~/singletop/sync_T_t_ntuple.root"
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
#Get the Event generator from the event file
|
36 |
|
|
events = Events(input_file)
|
37 |
|
|
|
38 |
|
|
# create handle outside of loop
|
39 |
|
|
handle = Handle ("vector<float>")
|
40 |
|
|
muEtaLabel = ("nTupleMuons", "tightMuonsEta", "SingleTop")
|
41 |
|
|
eleEtaLabel = ("nTupleElectrons", "tightElectronsEta", "SingleTop")
|
42 |
|
|
jetPtLabel = ("nTupleTopJetsPF", "topJetsPFPt", "SingleTop")
|
43 |
|
|
|
44 |
|
|
events.toBegin()
|
45 |
|
|
|
46 |
|
|
# loop over event
|
47 |
|
|
for event in events:
|
48 |
|
|
|
49 |
|
|
#Get the event contents
|
50 |
|
|
event.getByLabel (muEtaLabel, handle)
|
51 |
|
|
muEtas = handle.product()
|
52 |
|
|
event.getByLabel (eleEtaLabel, handle)
|
53 |
|
|
eleEtas = handle.product()
|
54 |
|
|
event.getByLabel (jetPtLabel, handle)
|
55 |
|
|
jetPts = handle.product()
|
56 |
|
|
|
57 |
|
|
#Convert the std::vector to python list
|
58 |
|
|
jetPts = [x for x in jetPts]
|
59 |
|
|
|
60 |
|
|
#Get the first 1 jet PT-s
|
61 |
|
|
i = 0
|
62 |
|
|
for jetPt in jetPts[0:1]:
|
63 |
|
|
event_row["jetPt%d" % i] = jetPt
|
64 |
|
|
i += 1
|
65 |
|
|
|
66 |
|
|
nMu = len(muEtas)
|
67 |
|
|
nEle = len(eleEtas)
|
68 |
|
|
nTightLeptons = nMu + nEle
|
69 |
|
|
event_row["nTightLeptons"] = nTightLeptons
|
70 |
|
|
|
71 |
|
|
if nTightLeptons != 1:
|
72 |
|
|
leptonEta = float("nan")
|
73 |
|
|
leptonFlavour = 0
|
74 |
|
|
elif nEle==1:
|
75 |
|
|
leptonEta = eleEtas[0]
|
76 |
|
|
leptonFlavour = 1
|
77 |
|
|
elif nMu==1:
|
78 |
|
|
leptonEta = muEtas[0]
|
79 |
|
|
leptonFlavour = 2
|
80 |
|
|
#for val in vals:
|
81 |
|
|
# print val
|
82 |
|
|
#for mu in muons:
|
83 |
|
|
# print mu.userFloat("pt")
|
84 |
|
|
|
85 |
|
|
eventId = event.object().id().event()
|
86 |
|
|
lumiId = event.object().id().luminosityBlock()
|
87 |
|
|
runId = event.object().id().run()
|
88 |
|
|
event_row["eventId"] = eventId
|
89 |
|
|
event_row["runId"] = runId
|
90 |
|
|
event_row["lumiId"] = lumiId
|
91 |
|
|
event_row["leptonEta"] = leptonEta
|
92 |
|
|
event_row["leptonFlavour"] = leptonFlavour
|
93 |
|
|
|
94 |
|
|
#Put the event row into the table
|
95 |
|
|
event_row.append()
|
96 |
|
|
#print "%d:%d:%d" % (runId, lumiId, eventId)
|
97 |
|
|
|
98 |
|
|
#FLush the table from memory to disk
|
99 |
|
|
table.flush()
|
100 |
|
|
|
101 |
|
|
#Print out some debugging events
|
102 |
|
|
evs = [(x["eventId"], x["leptonEta"], x["leptonFlavour"], x["jetPt0"]) for x in table.where("(eventId >= 7802) & (eventId <= 7900) & (nTightLeptons==1)")]
|
103 |
|
|
|
104 |
|
|
for ev in evs:
|
105 |
|
|
print ev
|