53def createRootMap(inFileName, rootFileName, cmScale, storeCoords):
54 print ('Create map {0} from {1} using cmScale = {2}'.format(rootFileName,
55 inFileName, cmScale))
56 if storeCoords is True:
57 print ('We will also store the x,y,z field coordinates in {0}'.format(rootFileName))
58
59 rangeInfo = findRanges(inFileName, cmScale)
60
61
62 theFile = ROOT.TFile.Open(rootFileName, 'recreate')
63
64 rangeTree = ROOT.TTree('Range', 'Range')
65 rangeTree.SetDirectory(theFile)
66
67
68 rStruct = ROOT.rangeStruct()
69 rangeTree.Branch('xMin', ROOT.AddressOf(rStruct, 'xMin'), 'xMin/F')
70 rangeTree.Branch('xMax', ROOT.AddressOf(rStruct, 'xMax'), 'xMax/F')
71 rangeTree.Branch('dx', ROOT.AddressOf(rStruct, 'dx'), 'dx/F')
72 rangeTree.Branch('yMin', ROOT.AddressOf(rStruct, 'yMin'), 'yMin/F')
73 rangeTree.Branch('yMax', ROOT.AddressOf(rStruct, 'yMax'), 'yMax/F')
74 rangeTree.Branch('dy', ROOT.AddressOf(rStruct, 'dy'), 'dy/F')
75 rangeTree.Branch('zMin', ROOT.AddressOf(rStruct, 'zMin'), 'zMin/F')
76 rangeTree.Branch('zMax', ROOT.AddressOf(rStruct, 'zMax'), 'zMax/F')
77 rangeTree.Branch('dz', ROOT.AddressOf(rStruct, 'dz'), 'dz/F')
78
79 rStruct.xMin = rangeInfo['xMin']
80 rStruct.xMax = rangeInfo['xMax']
81 rStruct.dx = rangeInfo['dx']
82 rStruct.yMin = rangeInfo['yMin']
83 rStruct.yMax = rangeInfo['yMax']
84 rStruct.dy = rangeInfo['dy']
85 rStruct.zMin = rangeInfo['zMin']
86 rStruct.zMax = rangeInfo['zMax']
87 rStruct.dz = rangeInfo['dz']
88
89
90 x0 = 0
91 y0 = 0
92 z0 = 0.5 * (rStruct.zMin + rStruct.zMax)
93
94
95
96
97
98
99 print ('Centering field map using co-ordinate shift {0} {1} {2} cm'.format(x0, y0, z0))
100
101
102 rStruct.xMin = rStruct.xMin - x0
103 rStruct.xMax = rStruct.xMax - x0
104
105 rStruct.yMin = rStruct.yMin - y0
106 rStruct.yMax = rStruct.yMax - y0
107
108 rStruct.zMin = rStruct.zMin - z0
109 rStruct.zMax = rStruct.zMax - z0
110
111 print ('x range = {0} to {1}'.format(rStruct.xMin, rStruct.xMax))
112 print ('y range = {0} to {1}'.format(rStruct.yMin, rStruct.yMax))
113 print ('z range = {0} to {1}'.format(rStruct.zMin, rStruct.zMax))
114
115
116 rangeTree.Fill()
117
118
119 dataTree = ROOT.TTree('Data', 'Data')
120 dataTree.SetDirectory(theFile)
121
122
123
124
125
126 dStruct = ROOT.dataStruct()
127 if storeCoords is True:
128 dataTree.Branch('x', ROOT.AddressOf(dStruct, 'x'), 'x/F')
129 dataTree.Branch('y', ROOT.AddressOf(dStruct, 'y'), 'y/F')
130 dataTree.Branch('z', ROOT.AddressOf(dStruct, 'z'), 'z/F')
131
132 dataTree.Branch('Bx', ROOT.AddressOf(dStruct, 'Bx'), 'Bx/F')
133 dataTree.Branch('By', ROOT.AddressOf(dStruct, 'By'), 'By/F')
134 dataTree.Branch('Bz', ROOT.AddressOf(dStruct, 'Bz'), 'Bz/F')
135
136
137
138 inData = pd.read_csv(inFileName, delim_whitespace=True, header=None)
139 inData.columns=["x", "y", "z", "bx", "by", "bz"]
140 inData = inData.sort_values(by=["x","y","z"])
141 inData = inData.astype(float)
142
143 count = 0.
144 data_shape = float(inData.shape[0])
145 for row in inData.itertuples():
146 if row.Index / data_shape >= count:
147 print("Processed: {} %".format(count * 100))
148 count += 0.1
149
150
151 if storeCoords is True:
152 dStruct.x = row.x * cmScale - x0
153 dStruct.y = row.y * cmScale - y0
154 dStruct.z = row.z * cmScale - z0
155
156
157 dStruct.Bx = row.bx
158 dStruct.By = row.by
159 dStruct.Bz = row.bz
160
161 dataTree.Fill()
162
163 theFile.cd()
164 rangeTree.Write()
165 dataTree.Write()
166 theFile.Close()
167
168