make_root_from_grid_output.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from __future__ import print_function
4 from array import array
5 from ROOT import *
6 
7 import glob
8 
9 def process_one_file(fn):
10  with open(fn, 'r') as inf:
11  for line in inf:
12  line = line.rstrip('\n').rstrip(' ')
13  values = line.split(' ')
14  run[0] = int(values[0])
15  subrun[0] = int(values[1])
16  cycle[0] = int(values[2])
17  evt[0] = int(values[3])
18  subevt[0] = int(values[4])
19  multivalues = values[5:]
20  ntrks[0] = len(values)/11
21  for i in range(ntrks[0]):
22  dedxll[0] = float(multivalues[0*ntrks[0]+i])
23  scatll[0] = float(multivalues[1*ntrks[0]+i])
24  dedx10[0] = float(multivalues[2*ntrks[0]+i])
25  dedx20[0] = float(multivalues[3*ntrks[0]+i])
26  dedx30[0] = float(multivalues[4*ntrks[0]+i])
27  dedx40[0] = float(multivalues[5*ntrks[0]+i])
28  # if dedx > 30 => outliers, which affect BDT results dramatically
29  # squeeze outliers to some resonable range
30  dedx10_squeezed[0] = dedx10[0]
31  if dedx10_squeezed[0] > 30.:
32  dedx10_squeezed[0] = 30
33  dedx20_squeezed[0] = dedx20[0]
34  if dedx20_squeezed[0] > 30.:
35  dedx20_squeezed[0] = 30
36  dedx30_squeezed[0] = dedx30[0]
37  if dedx30_squeezed[0] > 30.:
38  dedx30_squeezed[0] = 30
39  dedx40_squeezed[0] = dedx40[0]
40  if dedx40_squeezed[0] > 30.:
41  dedx40_squeezed[0] = 30
42  pdg[0] = int(float(multivalues[6*ntrks[0]+i]))
43  trklen[0] = float(multivalues[7*ntrks[0]+i])
44  measfrac[0] = float(multivalues[8*ntrks[0]+i])
45  remid[0] = float(multivalues[9*ntrks[0]+i])
46  remcont[0] = int(multivalues[10*ntrks[0]+i])
47  tr.Fill()
48 
49 
50 file_list = [name for name in glob.glob('grid_output/*.txt')]
51 #~ for name in glob.glob('grid_output/*.txt'):
52 #~ for name in glob.glob('grid_output/nd_rhc_numubarcc_selection_stride2_offset0.1_of_200.txt'):
53  #~ file_list.append(name)
54 
55 # make the tree
56 outf = TFile('training_sample.root', 'recreate')
57 tr = TTree('tr', 'tree of training sample')
58 
59 # tree leaves
60 run = array('i', [0])
61 subrun = array('i', [0])
62 cycle = array('i', [0])
63 evt = array('i', [0])
64 subevt = array('i', [0])
65 ntrks = array('i', [0])
66 dedxll = array('f', [0.])
67 scatll = array('f', [0.])
68 dedx10 = array('f', [0.])
69 dedx10_squeezed = array('f', [0.])
70 dedx20 = array('f', [0.])
71 dedx20_squeezed = array('f', [0.])
72 dedx30 = array('f', [0.])
73 dedx30_squeezed = array('f', [0.])
74 dedx40 = array('f', [0.])
75 dedx40_squeezed = array('f', [0.])
76 pdg = array('i', [0])
77 trklen = array('f', [0.])
78 measfrac = array('f', [0.])
79 remid = array('f', [0.])
80 remcont = array('i', [0])
81 tr.Branch('run', run, 'run/I')
82 tr.Branch('subrun', subrun, 'subrun/I')
83 tr.Branch('cycle', cycle, 'cycle/I')
84 tr.Branch('evt', evt, 'evt/I')
85 tr.Branch('subevt', subevt, 'subevt/I')
86 tr.Branch('ntrks', ntrks, 'ntrks/I')
87 tr.Branch('dedxll', dedxll, 'dedxll/F')
88 tr.Branch('scatll', scatll, 'scatll/F')
89 tr.Branch('dedx10', dedx10, 'dedx10/F')
90 tr.Branch('dedx20', dedx20, 'dedx20/F')
91 tr.Branch('dedx30', dedx30, 'dedx30/F')
92 tr.Branch('dedx40', dedx40, 'dedx40/F')
93 tr.Branch('dedx10_squeezed', dedx10_squeezed, 'dedx10_squeezed/F')
94 tr.Branch('dedx20_squeezed', dedx20_squeezed, 'dedx20_squeezed/F')
95 tr.Branch('dedx30_squeezed', dedx30_squeezed, 'dedx30_squeezed/F')
96 tr.Branch('dedx40_squeezed', dedx40_squeezed, 'dedx40_squeezed/F')
97 tr.Branch('pdg', pdg, 'pdg/I')
98 tr.Branch('trklen', trklen, 'trklen/F')
99 tr.Branch('measfrac', measfrac, 'measfrac/F')
100 tr.Branch('remid', remid, 'remid/F')
101 tr.Branch('remcont', remcont, 'remcont/I')
102 
103 for f in file_list:
105 outf.Write()
procfile open("FD_BRL_v0.txt")