-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.py
133 lines (92 loc) · 5.91 KB
/
test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--nThreads", type=int, help="number of threads", default=None)
parser.add_argument("--useBoost", type=bool, help="user boost histograms", default=False)
args = parser.parse_args()
import ROOT
ROOT.gInterpreter.ProcessLine(".O3")
if args.nThreads is not None:
if args.nThreads > 1:
ROOT.ROOT.EnableImplicitMT(args.nThreads)
else:
ROOT.ROOT.EnableImplicitMT()
#ROOT.TTreeProcessorMT.SetTasksPerWorkerHint(1)
import pickle
import gzip
import narf
from datasets import datasets2016
import hist
import lz4.frame
import numba
datasets = datasets2016.allDatasets()
boost_hist_default = ROOT.boost.histogram.use_default
boost_hist_options_none = ROOT.boost.histogram.axis.option.none_t
# standard regular axes
axis_pt = hist.axis.Regular(29, 26., 55., name = "pt")
axis_eta = hist.axis.Regular(48, -2.4, 2.4, name = "eta")
# categorical axes in python bindings always have an overflow bin, so use a regular
# axis for the charge
axis_charge = hist.axis.Regular(2, -2., 2., underflow=False, overflow=False, name = "charge")
# integer axis with no overflow
axis_pdf_idx = hist.axis.Integer(0, 103, underflow=False, overflow=False, name = "pdfidx")
#@numba.njit
#def arrsq(arr):
#return arr*arr
#@ROOT.Numba.Declare(["RVec<float>"], "RVec<float>")
#def weightsq(w):
#return arrsq(w)
def build_graph(df, dataset):
results = []
if dataset.is_data:
df = df.Define("weight", "1.0")
else:
df = df.Define("weight", "std::copysign(1.0, genWeight)")
weightsum = df.SumAndCount("weight")
df = df.Define("vetoMuons", "Muon_pt > 10 && Muon_looseId && abs(Muon_eta) < 2.4 && abs(Muon_dxybs) < 0.05")
df = df.Filter("Sum(vetoMuons) == 1")
df = df.Define("goodMuons", "vetoMuons && Muon_mediumId")
df = df.Filter("Sum(goodMuons) == 1")
df = df.Define("goodMuons_Pt0", "Muon_pt[goodMuons][0]")
df = df.Define("goodMuons_Eta0", "Muon_eta[goodMuons][0]")
df = df.Define("goodMuons_Charge0", "Muon_charge[goodMuons][0]")
#df = df.Filter("nMuon > 0")
#df = df.Define("goodMuons_Pt0", "Muon_pt[0]")
#df = df.Define("goodMuons_Eta0", "Muon_eta[0]")
#df = df.Define("goodMuons_Charge0", "Muon_charge[0]")
if args.useBoost:
#hptetacharge = df.HistoBoost("hptetacharge", [axis_pt, axis_eta, axis_charge], ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "weight"])
hptetacharge = df.HistoBoost("hptetacharge", [axis_pt, axis_eta, axis_charge], ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "weight"], var_axis_names = ["pdfidx"])
#hptetacharge = df.HistoBoost("hptetacharge", [axis_pt], ["goodMuons_Pt0", "weight"])
else:
#hptetacharge = df.Histo3D(("hptetacharge", "", 29, 26., 55., 48, -2.4, 2.4, 2, -2., 2.), "goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "weight")
hptetacharge = df.Histo3DWithBoost(("hptetacharge", "", 29, 26., 55., 48, -2.4, 2.4, 2, -2., 2.), "goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "weight")
#hptetacharge = df.Histo1DWithBoost(("hptetacharge", "", 29, 26., 55.), "goodMuons_Pt0", "weight")
results.append(hptetacharge)
if not dataset.is_data:
#df = df.Define("pdfweight", "weight*LHEPdfWeight")
#df = df.DefinePerSample("pdfidx", "std::array<int, 103> res; std::iota(res.begin(), res.end(), 0); return res;")
#hptetachargepdf = df.HistoBoost("hptetachargepdf", [axis_pt, axis_eta, axis_charge, axis_pdf_idx], ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "pdfidx", "pdfweight"])
#results.append(hptetachargepdf)
#df = df.DefinePerSample("pdfidx", "std::array<int, 103> res; std::iota(res.begin(), res.end(), 0); return res;")
for i in range(10):
wname = f"pdfweight_{i}"
#df = df.Define(wname, "weight*LHEPdfWeight")
df = df.Define(wname, "Eigen::TensorFixedSize<double, Eigen::Sizes<103>> res; auto w = weight*LHEPdfWeight; std::copy(std::begin(w), std::end(w), res.data()); return res;")
if args.useBoost:
#hptetachargepdf = df.HistoBoost(f"hptetachargepdf_{i}", [axis_pt, axis_eta, axis_charge, axis_pdf_idx], ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "pdfidx", wname])
#hptetachargepdf = df.HistoBoost(f"hptetachargepdf_{i}", [axis_pt, axis_eta, axis_charge, axis_pdf_idx], ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "pdfidx", wname], storage=hist.storage.Double())
#hptetachargepdf = df.HistoBoostArr(f"hptetachargepdf_{i}", [axis_pt, axis_eta, axis_charge], ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", wname])
hptetachargepdf = df.HistoBoost(f"hptetachargepdf_{i}", [axis_pt, axis_eta, axis_charge], ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", wname])
#hptetachargepdf = df.HistoBoost(f"hptetachargepdf_{i}", [axis_pt, axis_eta, axis_charge], ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", wname], storage=hist.storage.Double())
else:
#hptetachargepdf = df.HistoND((f"hptetachargepdf_{i}", "", 4, [29, 48, 2, 103], [26., -2.4, -2., -0.5], [55., 2.4, 2., 102.5]), ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "pdfidx", f"pdfweight_{i}"])
#hptetachargepdf = df.HistoNDWithBoost((f"hptetachargepdf_{i}", "", 4, [29, 48, 2, 103], [26., -2.4, -2., -0.5], [55., 2.4, 2., 102.5]), ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", "pdfidx", wname])
hptetachargepdf = df.HistoNDWithBoost((f"hptetachargepdf_{i}", "", 3, [29, 48, 2], [26., -2.4, -2.], [55., 2.4, 2.]), ["goodMuons_Pt0", "goodMuons_Eta0", "goodMuons_Charge0", wname])
results.append(hptetachargepdf)
return results, weightsum
resultdict = narf.build_and_run(datasets, build_graph)
fname = "test.pkl.lz4"
print("writing output")
#with gzip.open(fname, "wb") as f:
with lz4.frame.open(fname, "wb") as f:
pickle.dump(resultdict, f)