-
Notifications
You must be signed in to change notification settings - Fork 1
/
extend_tree.py
329 lines (281 loc) · 10.8 KB
/
extend_tree.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import sys, os
from array import array
import argparse
import numpy as np
import uproot
import pandas as pd
import modules.load_data as load_data
import modules.get_ids as get_ids
from root_pandas import to_root
from tqdm import tqdm
import gc
import functools
print = functools.partial(print, flush=True)
import psutil
process = psutil.Process(os.getpid())
print(str(float(process.memory_info().rss) / 1000000)) # in bytes
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", dest="file", default=None, help="file")
parser.add_argument("-y", "--year", dest="year", default=2017, help="year")
parser.add_argument(
"-e", "--extra", dest="extra", default=None, help="extraCalib file "
)
parser.add_argument("-d", "--debug", dest="debug", default=False, action="store_true")
parser.add_argument("-n", "--noise", dest="noise", default=False, action="store_true")
parser.add_argument("-L", "--lumi", dest="lumi", default=False, action="store_true")
args = parser.parse_args()
branches = [
#'noiseSeedSC',
"eventNumber",
"runNumber",
]
branches_extra = [
"eventNumber",
"rawIdRecHitSCEle1",
"rawIdRecHitSCEle2",
]
branches_todrop = []
print("@@@ Loading files...")
file = args.file
file_extra = args.extra
if args.debug:
file = "/afs/cern.ch/work/c/camendol/ecalelf/ntuples/13TeV/ALCARERECO/PromptReco2017_103X_EtaScaleupdatedSCregressionV3/DoubleEG-Run2017B-ZSkim-Prompt-v1/297046-297723/294927-306462_Prompt_v1/pedNoise/DoubleEG-Run2017B-ZSkim-Prompt-v1-297046-297723.root"
file_extra = "/afs/cern.ch/work/c/camendol/ecalelf/ntuples/13TeV/ALCARERECO/PromptReco2017_103X_EtaScaleupdatedSCregressionV3/DoubleEG-Run2017B-ZSkim-Prompt-v1/297046-297723/294927-306462_Prompt_v1/pedNoise/extraCalibTree-DoubleEG-Run2017B-ZSkim-Prompt-v1-297046-297723.root"
if not os.path.isfile(file):
print("Could not find file:", file)
sys.exit()
if not os.path.isfile(file_extra):
print("Could not find file:", file_extra)
sys.exit()
df = load_data.load_file(file, "selected", branches)
print(df)
initial_size = df.shape[0]
print(str(float(process.memory_info().rss) / 1000000)) #
extra_chunk_list = []
extratime_chunk_list = []
event_list = df.reset_index().eventNumber.drop_duplicates().sort_values().tolist()
for df_extra_chunk in tqdm(
uproot.pandas.iterate(
file_extra, "extraCalibTree", branches_extra, entrysteps=100000, flatten=False
)
):
df_extra_chunk = df_extra_chunk[
df_extra_chunk["eventNumber"].isin(event_list)
] # .sort_values(by=["eventNumber"]).reset_index()
df_extratime_chunk = df_extra_chunk["eventNumber"].astype("uint32")
df_extra_chunk = df_extra_chunk.drop(columns=["eventNumber"])
df_extra_seed_chunk = df_extra_chunk.stack().str[0].unstack().dropna().astype("int")
df_extra_secondtoseed_chunk = (
df_extra_chunk.stack().str[1].unstack().dropna().astype("int")
)
df_extra_chunk = pd.concat(
[df_extra_seed_chunk, df_extra_secondtoseed_chunk], axis=1
)
del df_extra_seed_chunk, df_extra_secondtoseed_chunk
df_extra_chunk.columns = [
"rawIDSeedSC1",
"rawIDSeedSC2",
"rawIDSecondToSeedSC1",
"rawIDSecondToSeedSC2",
]
gc.collect()
extra_chunk_list.append(df_extra_chunk)
extratime_chunk_list.append(df_extratime_chunk)
print(str(float(process.memory_info().rss) / 1000000)) #
df_extra = pd.concat(extra_chunk_list)
df_extratime = pd.concat(extratime_chunk_list)
del extra_chunk_list, extratime_chunk_list
df_extra = (
pd.concat([df_extra, df_extratime], axis=1).reset_index().drop(columns="index")
)
del df_extratime
df = df.set_index("eventNumber")
df = df[~df.index.duplicated(keep="first")]
df_extra = df_extra.set_index("eventNumber")
df_extra = df_extra[~df_extra.index.duplicated(keep="first")]
df = pd.concat([df, df_extra], axis=1).reset_index()
types = {
"rawIDSeedSC1": "int",
"rawIDSeedSC2": "int",
"rawIDSecondToSeedSC1": "int",
"rawIDSecondToSeedSC2": "int",
}
df = df.dropna().astype(types)
del df_extra
gc.collect()
df_extra = pd.DataFrame()
df_extratime = pd.DataFrame()
print(str(float(process.memory_info().rss) / 1000000)) #
# -------------------
if args.lumi:
print("@@@ Loading fill lumi table...")
lumi_file = "/afs/cern.ch/work/c/camendol/fill_lumi/lumi_Run1_Run2_unixTime.dat"
df_lumi_chunks = pd.read_csv(
lumi_file, sep="\s+", usecols=[0, 1, 3, 6], comment="#", chunksize=5000
)
lumi_chunk_list = []
for df_lumi_chunk in tqdm(df_lumi_chunks):
df_lumi_chunk[["Run", "Fill"]] = df_lumi_chunk["Run:Fill"].str.split(
":", n=1, expand=True
)
df_lumi_chunk["LS"] = df_lumi_chunk["LS"].str.split(":", n=1, expand=True)[0]
df_lumi_chunk = df_lumi_chunk[
(df_lumi_chunk["Beam_Status"] == "STABLE_BEAMS")
].drop(columns=["Beam_Status", "Run:Fill"])
lumi_chunk_list.append(df_lumi_chunk)
# concat the list into dataframe
df_lumi = pd.concat(lumi_chunk_list)
del lumi_chunk_list
group = (
df_lumi.groupby("Run")
.agg({"Recorded(/ub)": "sum", "LS": "count", "Fill": "first"})
.reset_index()
)
df["Fill"] = df["runNumber"].map(group.astype("int").set_index("Run")["Fill"])
df["LumiSections"] = df["runNumber"].map(group.astype("int").set_index("Run")["LS"])
df["RecordedLumi"] = df["runNumber"].map(
group.astype({"Run": "int"}).set_index("Run")["Recorded(/ub)"]
)
df["LumiInst"] = df["RecordedLumi"] / 23000
print(str(float(process.memory_info().rss) / 1000000)) #
del df_lumi, group
gc.collect()
df_lumi = pd.DataFrame()
group = pd.DataFrame()
# ---------------------
print("before appending:", str(process.memory_info().rss / 1000000)) #
print("@@@ Appending DetIDs and geometric/electronic elements...")
df = get_ids.appendIdxs(df, "1").dropna()
df = get_ids.appendIdxs(df, "2").dropna()
df = get_ids.appendIdxs(df, "1", "SecondToSeed").dropna()
df = get_ids.appendIdxs(df, "2", "SecondToSeed").dropna()
print("after appending:", str(float(process.memory_info().rss) / 1000000)) #
if args.noise:
print("@@@ Loading IOVs: EcalPedestals (Run 2 UL)")
era = file.split("Run" + str(args.year))[1][0]
print(process.memory_info().rss) #
print("@@@ Run" + str(args.year) + str(era))
dump_file = (
"/afs/cern.ch/work/c/camendol/ECALconditions_dumps/EcalPedestalsRun"
+ str(args.year)
+ str(era)
+ ".dat"
)
dump_chunk_list = [] # append each chunk df here
df_dump_chunks = pd.read_csv(
dump_file,
chunksize=10000,
sep="\s+",
header=None,
usecols=[4, 5, 7],
names=["noise", "begin", "DetID"],
)
for df_dump_chunk in tqdm(df_dump_chunks):
df_dump_chunk["begin"] = df_dump_chunk["begin"].values >> 32
df_dump_chunk = df_dump_chunk.astype(
{"noise": "float", "DetID": "int32", "begin": "int32"}
)
dump_chunk_list.append(df_dump_chunk)
# concat the list into dataframe
df_dump = pd.concat(dump_chunk_list)
del dump_chunk_list
df_dump["end"] = df_dump["begin"].shift(-1)
df_dump.loc[max(df_dump.index), ["end"]] = 1591695307
df_dump.astype({"end": "int32"})
df_red = df_dump[["begin", "end"]].drop_duplicates()
gc.collect()
print(str(float(process.memory_info().rss) / 1000000)) #
# ---------------------
print("@@@ Matching IOVs...")
print(str(float(process.memory_info().rss) / 1000000))
chunk_list = []
for g, df_chunk in df.groupby(np.arange(len(df)) // 50000):
i, j = np.where(
(df_chunk.eventTime.values[:, None] >= df_red.begin.values)
& (df_chunk.eventTime.values[:, None] < df_red.end.values)
)
df_chunk = pd.DataFrame(
np.column_stack([df_chunk.values[i], df_red.values[j]]),
columns=df_chunk.columns.append(df_red.columns),
)
chunk_list.append(df_chunk)
del i, j
gc.collect()
df = pd.concat(chunk_list)
del chunk_list
print("added begin end", str(float(process.memory_info().rss) / 1000000))
print(df.shape[0])
del df_red
gc.collect()
df_red = pd.DataFrame()
print(process.memory_info().rss) #
print("@@@ Mapping Ecal pedestals by time and crystal...")
chunk_list = []
for g, df_chunk in df.groupby(np.arange(len(df)) // (initial_size / 20)):
print(
"@@@ Going over chunks...",
str(float(process.memory_info().rss) / 1000000),
)
print(df_chunk.shape[0])
df_chunk["noiseSeedSC1_GT"] = (
df_chunk.set_index(["EcalDetIDSeedSC1", "begin"])
.index.map(
(
df_dump[["DetID", "noise", "begin"]].set_index(
["DetID", "begin"]
)["noise"]
)
)
.astype("float")
)
df_chunk["noiseSeedSC2_GT"] = (
df_chunk.set_index(["EcalDetIDSeedSC2", "begin"])
.index.map(
(
df_dump[["DetID", "noise", "begin"]].set_index(
["DetID", "begin"]
)["noise"]
)
)
.astype("float")
)
df_chunk["noiseSecondToSeedSC1_GT"] = (
df_chunk.set_index(["EcalDetIDSecondToSeedSC1", "begin"])
.index.map(
(
df_dump[["DetID", "noise", "begin"]].set_index(
["DetID", "begin"]
)["noise"]
)
)
.astype("float")
)
df_chunk["noiseSecondToSeedSC2_GT"] = (
df_chunk.set_index(["EcalDetIDSecondToSeedSC2", "begin"])
.index.map(
(
df_dump[["DetID", "noise", "begin"]].set_index(
["DetID", "begin"]
)["noise"]
)
)
.astype("float")
)
chunk_list.append(df_chunk)
df = pd.concat(chunk_list)
del chunk_list
print(df.shape[0])
del df_dump
gc.collect()
df_dump = pd.DataFrame()
# don't save the quantities that exist already in the original tree
df = df.drop(columns=branches_todrop)
gc.collect()
outfile_name = file.replace(".root", "_extra.root")
print("@@@ Saving output file ", outfile_name)
df.to_root(outfile_name, key="extended", mode="w") # recreate mode
final_size = df.shape[0]
if initial_size > final_size:
print("HEADS UP! final size is smaller than initial size")
print("initial ", str(initial_size))
print("final ", str(final_size))