-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
48 lines (40 loc) · 1.4 KB
/
plot.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
import numpy as np
import pandas as pd
import uproot
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
# Specify variables which shall be used as input for the neural network
variables = ["Tau_pt"]
if __name__ == "__main__":
# Read NanoAOD files
def load_tree(filename):
tree = uproot.open(filename)["Events"]
data = {}
for name in variables:
if "Tau_" in name: # Only use tau lepton leading in pT
data[name] = [x[0] if len(x) > 0 else -10 for x in tree.array(name).tolist()]
else:
data[name] = tree.array(name)
return pd.DataFrame(data)
signal = load_tree("htt_nano.root")
background = load_tree("qcd_nano.root")
# Skim dataset
def skim(df):
return df[df["Tau_pt"] > 0]
signal = skim(signal)
background = skim(background)
# Plot each variable
for name in variables:
plt.figure(figsize=(6,6))
lims = np.percentile(signal[name], [5, 95])
bins = np.linspace(lims[0], lims[1], 30)
for df, label in [[signal, "Signal"], [background, "Background"]]:
plt.hist(df[name], bins=bins, lw=3, histtype="step", label=label, normed=1)
plt.xlabel(name)
plt.legend()
plt.xlim((bins[0], bins[-1]))
plt.tight_layout()
plt.savefig(name + ".png")
print "Created "+name+".png"
plt.clf()