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