make_fwd_distortions.py
Go to the documentation of this file.
1 import ROOT as RT
2 from argparse import ArgumentParser as ap
3 import numpy as np
4 from scipy.spatial import Delaunay
5 import matplotlib.pyplot as plt
6 
7 parser = ap()
8 parser.add_argument("-i", type=str, help='Input')
9 parser.add_argument("-o", type=str, help='Output string')
10 args = parser.parse_args()
11 
12 def get_corr_points(hx, hy, hz):
13  results = []
14  for i in range(1, hx.GetNbinsX()+1):
15  x = hx.GetXaxis().GetBinCenter(i)
16  for j in range(1, hx.GetNbinsY()+1):
17  y = hx.GetYaxis().GetBinCenter(j)
18  for k in range(1, hx.GetNbinsZ()+1):
19  z = hx.GetZaxis().GetBinCenter(k)
20  results.append(
21  [x + hx.GetBinContent(i, j, k),
22  y + hy.GetBinContent(i, j, k),
23  z + hz.GetBinContent(i, j, k)])
24  return results
25 
27  results = []
28  for i in range(1, h.GetNbinsX()+1):
29  x = h.GetXaxis().GetBinCenter(i)
30  for j in range(1, h.GetNbinsY()+1):
31  y = h.GetYaxis().GetBinCenter(j)
32  for k in range(1, h.GetNbinsZ()+1):
33  z = h.GetZaxis().GetBinCenter(k)
34  results.append([x, y, z])
35  return results
36 
37 def set_distortions(hx, hy, hz, pts):
38  a = 0
39  for i in range(1, hx.GetNbinsX()+1):
40  x = hx.GetXaxis().GetBinCenter(i)
41  for j in range(1, hx.GetNbinsY()+1):
42  y = hx.GetYaxis().GetBinCenter(j)
43  for k in range(1, hx.GetNbinsZ()+1):
44  z = hx.GetZaxis().GetBinCenter(k)
45  hx.SetBinContent(i, j, k, pts[a][0] - x)
46  hy.SetBinContent(i, j, k, pts[a][1] - y)
47  hz.SetBinContent(i, j, k, pts[a][2] - z)
48  a += 1
49 def get_distorted_pts(grid_pts, tri_in):
50  results = []
51  for pt in grid_pts:
52  i = tri_in.find_simplex(pt)
53  s = tri_in.simplices[i]
54  r = tri_in.transform[i,3]
55  b = tri_in.transform[i,:3].dot(np.transpose(pt - r))
56  b = np.append(b, 1. - b.sum())
57 
58  new_point_x = 0.
59  new_point_y = 0.
60  new_point_z = 0.
61  for j, bi in zip(s,b):
62  new_point_x += grid_pts[j][0]*bi
63  new_point_y += grid_pts[j][1]*bi
64  new_point_z += grid_pts[j][2]*bi
65 
66  results.append([new_point_x, new_point_y, new_point_z])
67  return results
68 
69 
70 fIn = RT.TFile(args.i, "OPEN")
71 bkwd_z_pos = fIn.Get("RecoBkwd_Displacement_Z_Pos")
72 bkwd_z_neg = fIn.Get("RecoBkwd_Displacement_Z_Neg")
73 bkwd_x_pos = fIn.Get("RecoBkwd_Displacement_X_Pos")
74 bkwd_x_neg = fIn.Get("RecoBkwd_Displacement_X_Neg")
75 bkwd_y_pos = fIn.Get("RecoBkwd_Displacement_Y_Pos")
76 bkwd_y_neg = fIn.Get("RecoBkwd_Displacement_Y_Neg")
77 
78 fwd_z_pos = bkwd_z_pos.Clone("RecoFwd_Displacement_Z_Pos")
79 fwd_z_neg = bkwd_z_neg.Clone("RecoFwd_Displacement_Z_Neg")
80 fwd_x_pos = bkwd_x_pos.Clone("RecoFwd_Displacement_X_Pos")
81 fwd_x_neg = bkwd_x_neg.Clone("RecoFwd_Displacement_X_Neg")
82 fwd_y_pos = bkwd_y_pos.Clone("RecoFwd_Displacement_Y_Pos")
83 fwd_y_neg = bkwd_y_neg.Clone("RecoFwd_Displacement_Y_Neg")
84 
85 bkwd_pos_points = get_bin_points(bkwd_z_pos)
86 bkwd_neg_points = get_bin_points(bkwd_z_neg)
87 
88 bkwd_pos_corrected = get_corr_points(bkwd_x_pos, bkwd_y_pos, bkwd_z_pos)
89 bkwd_neg_corrected = get_corr_points(bkwd_x_neg, bkwd_y_neg, bkwd_z_neg)
90 
91 bkwd_pos_corrected_array = np.array(bkwd_pos_corrected)
92 bkwd_neg_corrected_array = np.array(bkwd_neg_corrected)
93 
94 tri_pos_corr = Delaunay(bkwd_pos_corrected_array)
95 tri_neg_corr = Delaunay(bkwd_neg_corrected_array)
96 
97 distorted_pos = get_distorted_pts(bkwd_pos_points, tri_pos_corr)
98 set_distortions(fwd_x_pos, fwd_y_pos, fwd_z_pos, distorted_pos)
99 
100 distorted_neg = get_distorted_pts(bkwd_neg_points, tri_neg_corr)
101 set_distortions(fwd_x_neg, fwd_y_neg, fwd_z_neg, distorted_neg)
102 
103 fOut = RT.TFile(args.o, "RECREATE")
104 fwd_x_pos.Write()
105 fwd_y_pos.Write()
106 fwd_z_pos.Write()
107 fwd_x_neg.Write()
108 fwd_y_neg.Write()
109 fwd_z_neg.Write()
110 
111 bkwd_x_pos.Write("RecoBkwd_Displacement_X_Pos")
112 bkwd_y_pos.Write("RecoBkwd_Displacement_Y_Neg")
113 bkwd_z_pos.Write("RecoBkwd_Displacement_Z_Pos")
114 bkwd_x_neg.Write("RecoBkwd_Displacement_X_Neg")
115 bkwd_y_neg.Write("RecoBkwd_Displacement_Y_Pos")
116 bkwd_z_neg.Write("RecoBkwd_Displacement_Z_Neg")
117 
118 ex_pos = fIn.Get("Reco_ElecField_X_Pos")
119 ey_pos = fIn.Get("Reco_ElecField_Y_Pos")
120 ez_pos = fIn.Get("Reco_ElecField_Z_Pos")
121 ex_neg = fIn.Get("Reco_ElecField_X_Neg")
122 ey_neg = fIn.Get("Reco_ElecField_Y_Neg")
123 ez_neg = fIn.Get("Reco_ElecField_Z_Neg")
124 
125 ex_pos.Write()
126 ey_pos.Write()
127 ez_pos.Write()
128 ex_neg.Write()
129 ey_neg.Write()
130 ez_neg.Write()
131 
132 fOut.Close()
133 
134 fIn.Close()
135 
136 
137 '''
138 points = np.array([[0, 0], [0, 1.0], [1, 0], [1.0, 1.0]])
139 corr_points = np.array([[.15, .15], [.2, 1.3], [1.3, .3], [1.3, 1.4]])
140 
141 tri = Delaunay(points)
142 tri_corr = Delaunay(corr_points)
143 plt.triplot(points[:,0], points[:,1], tri_corr.simplices)
144 plt.plot(points[:,0], points[:,1], 'o')
145 plt.grid()
146 
147 #plt.triplot(corr_points[:,0], corr_points[:,1], tri_disp.simplices)
148 plt.triplot(corr_points[:,0], corr_points[:,1], tri_corr.simplices)
149 plt.plot(corr_points[:,0], corr_points[:,1], 'o')
150 
151 #true_pts = []
152 #for i in range(0, 12):
153 # for j in range(0, 12):
154 # true_pts.append([i*.1, j*.1])
155 #true_grid = np.array(true_pts)
156 true_grid = np.array([[.25, .5], [.75, .5]])
157 plt.plot(true_grid[:,0], true_grid[:,1], 'o')
158 
159 print(points)
160 print(tri_corr.simplices)
161 
162 distorted_grid = []
163 
164 for pt in true_grid:
165  i = tri_corr.find_simplex(pt)
166  print(i)
167  s = tri_corr.simplices[i]
168  print(pt, i)
169  print(s)
170  r = tri_corr.transform[i,2]
171  b = tri_corr.transform[i,:2].dot(np.transpose(pt - r))
172  b = np.append(b, 1. - b.sum())
173  print(b)
174 
175  new_point_x = 0.
176  new_point_y = 0.
177  for j, bi in zip(s,b):
178  new_point_x += points[j][0]*bi
179  new_point_y += points[j][1]*bi
180  print([new_point_x, new_point_y])
181  distorted_grid.append([new_point_x, new_point_y])
182 
183 distorted_array = np.array(distorted_grid)
184 
185 deltas = [ [d[0] - t[0], d[1] - t[1]] for d, t in zip(distorted_grid, true_grid)]
186 #plt.plot(distorted_array[:,0], distorted_array[:,1], 'o')
187 delta_array = np.array(deltas)
188 for pt, d in zip(true_grid, deltas):
189  plt.arrow(pt[0], pt[1], d[0], d[1], head_width=.01)
190 plt.show()
191 '''
def set_distortions(hx, hy, hz, pts)
def get_corr_points(hx, hy, hz)
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
def get_distorted_pts(grid_pts, tri_in)