Skip to content

Commit 4f0a24b

Browse files
committed
Apply formatting
1 parent 578a370 commit 4f0a24b

24 files changed

+229
-149
lines changed

tutorials/visualisation/rcanvas/df104.py

+48-20
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,35 @@
2121

2222
import ROOT
2323
import os
24-
from ROOT.Experimental import RCanvas, RText, RAttrText, RAttrFont, RAttrFill, RAttrLine, RLegend, RPadPos, RPadExtent, TObjectDrawable
24+
from ROOT.Experimental import (
25+
RCanvas,
26+
RText,
27+
RAttrText,
28+
RAttrFont,
29+
RAttrFill,
30+
RAttrLine,
31+
RLegend,
32+
RPadPos,
33+
RPadExtent,
34+
TObjectDrawable,
35+
)
2536

2637
# Enable multi-threading
2738
ROOT.ROOT.EnableImplicitMT()
2839

2940
# Create a ROOT dataframe for each dataset
3041
path = "root://eospublic.cern.ch//eos/opendata/atlas/OutreachDatasets/2020-01-22"
3142
df = {}
32-
df["data"] = ROOT.RDataFrame("mini", (os.path.join(path, "GamGam/Data/data_{}.GamGam.root".format(x)) for x in ("A", "B", "C", "D")))
43+
df["data"] = ROOT.RDataFrame(
44+
"mini", (os.path.join(path, "GamGam/Data/data_{}.GamGam.root".format(x)) for x in ("A", "B", "C", "D"))
45+
)
3346
df["ggH"] = ROOT.RDataFrame("mini", os.path.join(path, "GamGam/MC/mc_343981.ggH125_gamgam.GamGam.root"))
3447
df["VBF"] = ROOT.RDataFrame("mini", os.path.join(path, "GamGam/MC/mc_345041.VBFH125_gamgam.GamGam.root"))
3548
processes = list(df.keys())
3649

3750
# Apply scale factors and MC weight for simulated events and a weight of 1 for the data
3851
for p in ["ggH", "VBF"]:
39-
df[p] = df[p].Define("weight",
40-
"scaleFactor_PHOTON * scaleFactor_PhotonTRIGGER * scaleFactor_PILEUP * mcWeight");
52+
df[p] = df[p].Define("weight", "scaleFactor_PHOTON * scaleFactor_PhotonTRIGGER * scaleFactor_PILEUP * mcWeight")
4153
df["data"] = df["data"].Define("weight", "1.0")
4254

4355
# Select the events for the analysis
@@ -46,39 +58,55 @@
4658
df[p] = df[p].Filter("trigP")
4759

4860
# Find two good muons with tight ID, pt > 25 GeV and not in the transition region between barrel and encap
49-
df[p] = df[p].Define("goodphotons", "photon_isTightID && (photon_pt > 25000) && (abs(photon_eta) < 2.37) && ((abs(photon_eta) < 1.37) || (abs(photon_eta) > 1.52))")\
50-
.Filter("Sum(goodphotons) == 2")
61+
df[p] = (
62+
df[p]
63+
.Define(
64+
"goodphotons",
65+
"photon_isTightID && (photon_pt > 25000) && (abs(photon_eta) < 2.37) && ((abs(photon_eta) < 1.37) || (abs(photon_eta) > 1.52))",
66+
)
67+
.Filter("Sum(goodphotons) == 2")
68+
)
5169

5270
# Take only isolated photons
53-
df[p] = df[p].Filter("Sum(photon_ptcone30[goodphotons] / photon_pt[goodphotons] < 0.065) == 2")\
54-
.Filter("Sum(photon_etcone20[goodphotons] / photon_pt[goodphotons] < 0.065) == 2")
71+
df[p] = (
72+
df[p]
73+
.Filter("Sum(photon_ptcone30[goodphotons] / photon_pt[goodphotons] < 0.065) == 2")
74+
.Filter("Sum(photon_etcone20[goodphotons] / photon_pt[goodphotons] < 0.065) == 2")
75+
)
5576

5677
# Compile a function to compute the invariant mass of the diphoton system
5778
ROOT.gInterpreter.Declare(
58-
"""
79+
"""
5980
using Vec_t = const ROOT::VecOps::RVec<float>;
6081
float ComputeInvariantMass(Vec_t& pt, Vec_t& eta, Vec_t& phi, Vec_t& e) {
6182
ROOT::Math::PtEtaPhiEVector p1(pt[0], eta[0], phi[0], e[0]);
6283
ROOT::Math::PtEtaPhiEVector p2(pt[1], eta[1], phi[1], e[1]);
6384
return (p1 + p2).mass() / 1000.0;
6485
}
65-
""")
86+
"""
87+
)
6688

6789
# Define a new column with the invariant mass and perform final event selection
6890
hists = {}
6991
for p in processes:
7092
# Make four vectors and compute invariant mass
71-
df[p] = df[p].Define("m_yy", "ComputeInvariantMass(photon_pt[goodphotons], photon_eta[goodphotons], photon_phi[goodphotons], photon_E[goodphotons])")
93+
df[p] = df[p].Define(
94+
"m_yy",
95+
"ComputeInvariantMass(photon_pt[goodphotons], photon_eta[goodphotons], photon_phi[goodphotons], photon_E[goodphotons])",
96+
)
7297

7398
# Make additional kinematic cuts and select mass window
74-
df[p] = df[p].Filter("photon_pt[goodphotons][0] / 1000.0 / m_yy > 0.35")\
75-
.Filter("photon_pt[goodphotons][1] / 1000.0 / m_yy > 0.25")\
76-
.Filter("m_yy > 105 && m_yy < 160")
99+
df[p] = (
100+
df[p]
101+
.Filter("photon_pt[goodphotons][0] / 1000.0 / m_yy > 0.35")
102+
.Filter("photon_pt[goodphotons][1] / 1000.0 / m_yy > 0.25")
103+
.Filter("m_yy > 105 && m_yy < 160")
104+
)
77105

78106
# Book histogram of the invariant mass with this selection
79107
hists[p] = df[p].Histo1D(
80-
ROOT.RDF.TH1DModel(p, "Diphoton invariant mass; m_{#gamma#gamma} [GeV];Events", 30, 105, 160),
81-
"m_yy", "weight")
108+
ROOT.RDF.TH1DModel(p, "Diphoton invariant mass; m_{#gamma#gamma} [GeV];Events", 30, 105, 160), "m_yy", "weight"
109+
)
82110

83111
# Run the event loop
84112

@@ -100,8 +128,8 @@
100128
# Create canvas with pads for main plot and data/MC ratio
101129
c = RCanvas.Create("df104_HiggsToTwoPhotons")
102130

103-
lower_pad = c.AddPad(RPadPos(0,0.65), RPadExtent(1, 0.35))
104-
upper_pad = c.AddPad(RPadPos(0,0), RPadExtent(1, 0.65))
131+
lower_pad = c.AddPad(RPadPos(0, 0.65), RPadExtent(1, 0.35))
132+
upper_pad = c.AddPad(RPadPos(0, 0), RPadExtent(1, 0.65))
105133

106134
upper_frame = upper_pad.AddFrame()
107135
upper_frame.margins.bottom = 0
@@ -239,5 +267,5 @@
239267
c.Show()
240268

241269
# Save plot in PNG file
242-
if c.SaveAs("df104.png") :
243-
print("Saved figure to df104.png")
270+
if c.SaveAs("df104.png"):
271+
print("Saved figure to df104.png")

tutorials/visualisation/rcanvas/df105.py

+76-34
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,48 @@
2929

3030
# Argument parsing
3131
parser = argparse.ArgumentParser()
32-
parser.add_argument("--lumi-scale", type=float, default=0.001,
33-
help="Run only on a fraction of the total available 10 fb^-1 (only usable together with --full-dataset)")
34-
parser.add_argument("--full-dataset", action="store_true", default=False,
35-
help="Use the full dataset (use --lumi-scale to run only on a fraction of it)")
32+
parser.add_argument(
33+
"--lumi-scale",
34+
type=float,
35+
default=0.001,
36+
help="Run only on a fraction of the total available 10 fb^-1 (only usable together with --full-dataset)",
37+
)
38+
parser.add_argument(
39+
"--full-dataset",
40+
action="store_true",
41+
default=False,
42+
help="Use the full dataset (use --lumi-scale to run only on a fraction of it)",
43+
)
3644
parser.add_argument("-b", action="store_true", default=False, help="Use ROOT batch mode")
37-
parser.add_argument("-t", action="store_true", default=False, help="Use implicit multi threading (for the full dataset only possible with --lumi-scale 1.0)")
38-
if 'df105.py' in sys.argv[0]:
45+
parser.add_argument(
46+
"-t",
47+
action="store_true",
48+
default=False,
49+
help="Use implicit multi threading (for the full dataset only possible with --lumi-scale 1.0)",
50+
)
51+
if "df105.py" in sys.argv[0]:
3952
# Script
4053
args = parser.parse_args()
4154
else:
4255
# Notebook
4356
args = parser.parse_args(args=[])
4457

45-
if args.b: ROOT.gROOT.SetBatch(True)
46-
if args.t: ROOT.EnableImplicitMT()
58+
if args.b:
59+
ROOT.gROOT.SetBatch(True)
60+
if args.t:
61+
ROOT.EnableImplicitMT()
4762

48-
if not args.full_dataset: lumi_scale = 0.001 # The preskimmed dataset contains only 0.01 fb^-1
49-
else: lumi_scale = args.lumi_scale
63+
if not args.full_dataset:
64+
lumi_scale = 0.001 # The preskimmed dataset contains only 0.01 fb^-1
65+
else:
66+
lumi_scale = args.lumi_scale
5067
lumi = 10064.0
51-
print('Run on data corresponding to {:.2f} fb^-1 ...'.format(lumi * lumi_scale / 1000.0))
68+
print("Run on data corresponding to {:.2f} fb^-1 ...".format(lumi * lumi_scale / 1000.0))
5269

53-
if args.full_dataset: dataset_path = "root://eospublic.cern.ch//eos/opendata/atlas/OutreachDatasets/2020-01-22"
54-
else: dataset_path = "root://eospublic.cern.ch//eos/root-eos/reduced_atlas_opendata/w"
70+
if args.full_dataset:
71+
dataset_path = "root://eospublic.cern.ch//eos/opendata/atlas/OutreachDatasets/2020-01-22"
72+
else:
73+
dataset_path = "root://eospublic.cern.ch//eos/root-eos/reduced_atlas_opendata/w"
5574

5675
# Create a ROOT dataframe for each dataset
5776
# Note that we load the filenames from the external json file placed in the same folder than this script.
@@ -64,11 +83,11 @@
6483
for p in processes:
6584
for d in files[p]:
6685
# Construct the dataframes
67-
folder = d[0] # Folder name
68-
sample = d[1] # Sample name
69-
xsecs[sample] = d[2] # Cross-section
70-
sumws[sample] = d[3] # Sum of weights
71-
num_events = d[4] # Number of events
86+
folder = d[0] # Folder name
87+
sample = d[1] # Sample name
88+
xsecs[sample] = d[2] # Cross-section
89+
sumws[sample] = d[3] # Sum of weights
90+
num_events = d[4] # Number of events
7291
samples.append(sample)
7392
df[sample] = ROOT.RDataFrame("mini", "{}/1lep/{}/{}.1lep.root".format(dataset_path, folder, sample))
7493

@@ -98,23 +117,37 @@
98117

99118
for s in samples:
100119
# Select events with a muon or electron trigger and with a missing transverse energy larger than 30 GeV
101-
df[s] = df[s].Filter("trigE || trigM")\
102-
.Filter("met_et > 30000")
120+
df[s] = df[s].Filter("trigE || trigM").Filter("met_et > 30000")
103121

104122
# Find events with exactly one good lepton
105-
df[s] = df[s].Define("good_lep", "lep_isTightID && lep_pt > 35000 && lep_ptcone30 / lep_pt < 0.1 && lep_etcone20 / lep_pt < 0.1")\
106-
.Filter("ROOT::VecOps::Sum(good_lep) == 1")
123+
df[s] = (
124+
df[s]
125+
.Define(
126+
"good_lep", "lep_isTightID && lep_pt > 35000 && lep_ptcone30 / lep_pt < 0.1 && lep_etcone20 / lep_pt < 0.1"
127+
)
128+
.Filter("ROOT::VecOps::Sum(good_lep) == 1")
129+
)
107130

108131
# Apply additional cuts in case the lepton is an electron or muon
109-
df[s] = df[s].Define("idx", "ROOT::VecOps::ArgMax(good_lep)")\
110-
.Filter("GoodElectronOrMuon(lep_type[idx], lep_pt[idx], lep_eta[idx], lep_phi[idx], lep_E[idx], lep_trackd0pvunbiased[idx], lep_tracksigd0pvunbiased[idx], lep_z0[idx])")
132+
df[s] = (
133+
df[s]
134+
.Define("idx", "ROOT::VecOps::ArgMax(good_lep)")
135+
.Filter(
136+
"GoodElectronOrMuon(lep_type[idx], lep_pt[idx], lep_eta[idx], lep_phi[idx], lep_E[idx], lep_trackd0pvunbiased[idx], lep_tracksigd0pvunbiased[idx], lep_z0[idx])"
137+
)
138+
)
111139

112140
# Apply luminosity, scale factors and MC weights for simulated events
113141
for s in samples:
114142
if "data" in s:
115143
df[s] = df[s].Define("weight", "1.0")
116144
else:
117-
df[s] = df[s].Define("weight", "scaleFactor_ELE * scaleFactor_MUON * scaleFactor_LepTRIGGER * scaleFactor_PILEUP * mcWeight * {} / {} * {}".format(xsecs[s], sumws[s], lumi))
145+
df[s] = df[s].Define(
146+
"weight",
147+
"scaleFactor_ELE * scaleFactor_MUON * scaleFactor_LepTRIGGER * scaleFactor_PILEUP * mcWeight * {} / {} * {}".format(
148+
xsecs[s], sumws[s], lumi
149+
),
150+
)
118151

119152
# Compute transverse mass of the W boson using the lepton and the missing transverse energy and make a histogram
120153
ROOT.gInterpreter.Declare("""
@@ -128,7 +161,9 @@
128161

129162
histos = {}
130163
for s in samples:
131-
df[s] = df[s].Define("mt_w", "ComputeTransverseMass(met_et, met_phi, lep_pt[idx], lep_eta[idx], lep_phi[idx], lep_E[idx])")
164+
df[s] = df[s].Define(
165+
"mt_w", "ComputeTransverseMass(met_et, met_phi, lep_pt[idx], lep_eta[idx], lep_phi[idx], lep_E[idx])"
166+
)
132167
histos[s] = df[s].Histo1D(ROOT.RDF.TH1DModel(s, "mt_w", 24, 60, 180), "mt_w", "weight")
133168

134169
# Run the event loop and merge histograms of the respective processes
@@ -139,15 +174,19 @@
139174
# because not enough data is available.
140175
ROOT.RDF.RunGraphs([histos[s] for s in samples])
141176

177+
142178
def merge_histos(label):
143179
h = None
144180
for i, d in enumerate(files[label]):
145181
t = histos[d[1]].GetValue()
146-
if i == 0: h = t.Clone()
147-
else: h.Add(t)
182+
if i == 0:
183+
h = t.Clone()
184+
else:
185+
h.Add(t)
148186
h.SetNameTitle(label, label)
149187
return h
150188

189+
151190
data = merge_histos("data")
152191
wjets = merge_histos("wjets")
153192
zjets = merge_histos("zjets")
@@ -178,7 +217,7 @@ def merge_histos(label):
178217
frame.x.labels.size = 0.04
179218

180219
frame.y.min = 1
181-
frame.y.max = 1e10*args.lumi_scale
220+
frame.y.max = 1e10 * args.lumi_scale
182221
frame.y.log = 10
183222
frame.y.title.value = "Events"
184223
frame.y.title.size = 0.045
@@ -190,8 +229,9 @@ def merge_histos(label):
190229
# Draw stack with MC contributions
191230
stack = ROOT.THStack()
192231
for h, color in zip(
193-
[singletop, diboson, ttbar, zjets, wjets],
194-
[(0.82, 0.94, 0.76), (0.76, 0.54, 0.57), (0.61, 0.6, 0.8), (0.97, 0.81, 0.41), (0.87, 0.35, 0.42)]):
232+
[singletop, diboson, ttbar, zjets, wjets],
233+
[(0.82, 0.94, 0.76), (0.76, 0.54, 0.57), (0.61, 0.6, 0.8), (0.97, 0.81, 0.41), (0.87, 0.35, 0.42)],
234+
):
195235
h.SetLineWidth(1)
196236
h.SetLineColor("black")
197237
h.SetFillColor(ROOT.TColor.GetColor(*color))
@@ -214,7 +254,7 @@ def merge_histos(label):
214254
legend.SetBorderSize(0)
215255
legend.SetTextSize(0.04)
216256
legend.SetTextAlign(32)
217-
legend.AddEntry(data, "Data" ,"lep")
257+
legend.AddEntry(data, "Data", "lep")
218258
legend.AddEntry(wjets, "W+jets", "f")
219259
legend.AddEntry(zjets, "Z+jets", "f")
220260
legend.AddEntry(ttbar, "t#bar{t}", "f")
@@ -233,7 +273,9 @@ def merge_histos(label):
233273
lbl2.text.font = RAttrFont.kArial
234274
lbl2.text.size = 0.04
235275
lbl2.text.align = RAttrText.kLeftBottom
236-
lbl3 = c.Add[RText](RPadPos(0.05, 0.82), "#sqrt{{s}} = 13 TeV, {:.2f} fb^{{-1}}".format(lumi * args.lumi_scale / 1000.0))
276+
lbl3 = c.Add[RText](
277+
RPadPos(0.05, 0.82), "#sqrt{{s}} = 13 TeV, {:.2f} fb^{{-1}}".format(lumi * args.lumi_scale / 1000.0)
278+
)
237279
lbl3.onFrame = True
238280
lbl3.text.font = RAttrFont.kArial
239281
lbl3.text.size = 0.03
@@ -244,5 +286,5 @@ def merge_histos(label):
244286
c.Show()
245287

246288
# Save the plot
247-
if c.SaveAs("df105.png") :
289+
if c.SaveAs("df105.png"):
248290
print("Saved figure to df105.png")

tutorials/visualisation/rcanvas/raxis.cxx

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void raxis()
2626

2727
auto x1 = 0.08_normal, w1 = 0.36_normal, x2 = 0.57_normal, w2 = 0.36_normal;
2828

29-
auto draw0 = canvas->Draw<RAxisDrawable>(RPadPos(0.03_normal,0.1_normal), true, 0.8_normal);
29+
auto draw0 = canvas->Draw<RAxisDrawable>(RPadPos(0.03_normal, 0.1_normal), true, 0.8_normal);
3030
draw0->axis.ticks.SetInvert();
3131
draw0->axis.ticks.size = 0.02_normal;
3232
draw0->axis.title = "vertical";
@@ -46,8 +46,8 @@ void raxis()
4646
draw3->axis.labels.center = true;
4747

4848
auto draw4 = canvas->Draw<RAxisDrawable>(RPadPos(x1, 0.3_normal), false, w1);
49-
draw4->axis.min = TDatime(2020,11,12,9,0,0).Convert();
50-
draw4->axis.max = TDatime(2020,11,12,12,0,0).Convert();
49+
draw4->axis.min = TDatime(2020, 11, 12, 9, 0, 0).Convert();
50+
draw4->axis.max = TDatime(2020, 11, 12, 12, 0, 0).Convert();
5151
draw4->axis.SetTimeDisplay("%d/%m/%y %H:%M");
5252
draw4->axis.title = "time display";
5353
draw4->axis.labels.size = 0.01;
@@ -60,7 +60,7 @@ void raxis()
6060
draw5->axis.title = "labels, swap ticks side";
6161
draw5->axis.title.SetLeft();
6262

63-
auto draw6 = canvas->Draw<RAxisDrawable>(RPadPos(0.5_normal,0.9_normal), true, -0.8_normal);
63+
auto draw6 = canvas->Draw<RAxisDrawable>(RPadPos(0.5_normal, 0.9_normal), true, -0.8_normal);
6464
draw6->axis.min = 0;
6565
draw6->axis.max = 10;
6666
draw6->axis.ending.SetArrow();
@@ -90,7 +90,7 @@ void raxis()
9090
draw9->axis.title = "ln scale";
9191
draw9->axis.title.SetCenter();
9292

93-
auto draw10 = canvas->Draw<RAxisDrawable>(RPadPos(x2+w2, 0.3_normal), false, -w2);
93+
auto draw10 = canvas->Draw<RAxisDrawable>(RPadPos(x2 + w2, 0.3_normal), false, -w2);
9494
draw10->axis.ending.SetArrow();
9595
draw10->axis.title = "horizontal negative length";
9696
draw10->axis.title.SetCenter();

0 commit comments

Comments
 (0)