data_prepare.py
Go to the documentation of this file.
1 """
2 Prepare training data in numpy format from TH2s.
3 """
4 
5 import os, argparse
6 import uproot
7 import numpy as np
8 from matplotlib import pyplot as plt
9 
10 
11 def root_to_numpy(filepath, induct_saveloc, collect_saveloc):
12  file = uproot.open(filepath)
13 
14  for idx, key in enumerate(file['infilldata'].keys()):
15  z, _ = file['infilldata'][key].numpy()
16 
17  # print(z.dtype)
18  # plt.imshow(z.T, aspect='auto', vmin=-20, vmax=20, cmap='coolwarm')
19  # plt.show()
20 
21  if ('ROP0' in key) or ('ROP1' in key):
22  with open(os.path.join(induct_saveloc, "{}.npy".format(key[:-2])), "w") as f:
23  np.save(f, z)
24 
25  elif ('ROP2' in key) or ('ROP3' in key):
26  with open(os.path.join(collect_saveloc, "{}.npy".format(key[:-2])), "w") as f:
27  np.save(f, z)
28 
29  if (idx + 1) % 50 == 0:
30  print("{}/{}".format(idx + 1, len(file["dec"].keys())))
31 
32 
33 def main(input_file, output_dir):
34 
35  induct_saveloc = os.path.join(output_dir, "induction")
36  if not os.path.exists(induct_saveloc):
37  os.makedirs(induct_saveloc)
38 
39  collect_saveloc = os.path.join(output_dir, "collection")
40  if not os.path.exists(collect_saveloc):
41  os.makedirs(collect_saveloc)
42 
43  root_to_numpy(input_file, induct_saveloc, collect_saveloc)
44 
45 
47 
48  parser = argparse.ArgumentParser()
49 
50  parser.add_argument("input_file")
51  parser.add_argument("output_dir")
52 
53  args = parser.parse_args()
54 
55  return (args.input_file, args.output_dir)
56 
57 
58 if __name__ == "__main__":
59  arguments = parse_arguments()
60  main(*arguments)
int open(const char *, int)
Opens a file descriptor.
static bool format(QChar::Decomposition tag, QString &str, int index, int len)
Definition: qstring.cpp:11496
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def parse_arguments()
Definition: data_prepare.py:46
def root_to_numpy(filepath, induct_saveloc, collect_saveloc)
Definition: data_prepare.py:11
def main(input_file, output_dir)
Definition: data_prepare.py:33