3
# This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
5
# MAUS is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
10
# MAUS is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with MAUS. If not, see <http://www.gnu.org/licenses/>.
21
from xboa.Bunch import Bunch
36
number_virtual_cut = 0
40
upstream_reference = 14
41
downstream_reference = 50
47
global number_virtual_e
48
global number_virtual_cut
50
if len( sys.argv ) <= 1 :
51
print "No File Supplied."
54
filename = sys.argv[1]
60
infile = ROOT.TFile( filename, "READ" )
62
data = ROOT.MAUS.Data()
63
tree = infile.Get("Spill")
64
tree.SetBranchAddress("data", data)
66
bunch_list = [ xboa.Bunch.Bunch() for i in range( number_bunches ) ]
69
for i in range( tree.GetEntries() ) :
71
spill = data.GetSpill()
73
if spill.GetDaqEventType() != "physics_event" :
76
for j in range( spill.GetMCEventSize() ) :
78
event = spill.GetAnMCEvent( j )
80
for k in range( event.GetVirtualHitsSize() ) :
81
hit = event.GetAVirtualHit( k )
83
x = hit.GetPosition()[0]
84
y = hit.GetPosition()[1]
85
z = hit.GetPosition()[2]
86
px = hit.GetMomentum()[0]
87
py = hit.GetMomentum()[1]
88
pz = hit.GetMomentum()[2]
89
pid = hit.GetParticleId()
90
PT = math.sqrt(px**2 + py**2)
92
# Have to cut electrons later on - so that we contain decaying muons
96
if ( math.sqrt( x*x + y*y ) >= aperture_cut ) :
97
number_virtual_cut += 1
101
# number_virtual_cut += 1
105
# number_virtual_cut += 1
109
number_virtual_cut += 1
113
# number_virtual_cut += 1
116
# if math.sqrt( px*px + py*py ) > pz :
117
# print "VIRTUAL - WTF", px, py, pz
118
# number_virtual_cut += 1
125
for k in range( event.GetVirtualHitsSize() ) :
126
hit = event.GetAVirtualHit( k )
128
bunch = bunch_list[ hit.GetStationId() - 1 ]
130
x = hit.GetPosition()[0]
131
y = hit.GetPosition()[1]
132
z = hit.GetPosition()[2]
133
px = hit.GetMomentum()[0]
134
py = hit.GetMomentum()[1]
135
pz = hit.GetMomentum()[2]
136
pid = hit.GetParticleId()
137
bx = hit.GetBField()[0]
138
by = hit.GetBField()[1]
139
bz = hit.GetBField()[2]
144
hit = xboa.Hit.Hit.new_from_dict( { 'x':x, 'y':y, 'z':z, 'px':px, 'py':py, 'pz':pz, 'pid':pid, 'bx':bx, 'by':by, 'bz':bz, 'mass':xboa.Common.pdg_pid_to_mass[ abs( pid ) ] }, 'energy' )
148
print "Read", len(bunch_list), "Bunches"
150
for bun in bunch_list :
151
if bun.bunch_weight() > 1.0e-9 and len( bun ) > 1 :
152
ana_list.append( bun )
154
print "Found", len(ana_list), "Non Empty Bunches"
155
print "Cut", number_virtual_cut, "Hits and ", number_virtual_e, "Electrons"
157
outfile = ROOT.TFile( "emittance_from_virtualPlanes.root", "RECREATE" )
159
canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'bunch_weight', [''], 'm')
161
canvas.Print( "BunchWeight.eps", "eps")
162
canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'emittance', ['x', 'y'], 'm', 'm')
164
canvas.Print( "Emittance.eps", "eps")
165
canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'beta', ['x', 'y'], 'm', 'm')
167
canvas.Print( "BetaFunc.eps", "eps")
168
canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'mean', ['energy'], 'm', 'MeV')
170
canvas.Print( "Energy.eps", "eps")
171
canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'mean', ['bz'], 'm', 'T')
173
canvas.Print( "MagneticField.eps", "eps")
174
canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'mean', ['pid'], 'm')
176
canvas.Print( "PID.eps", "eps")
179
bunch_up = ana_list[ upstream_reference ]
180
bunch_down = ana_list[ downstream_reference ]
182
up_means = bunch_up.mean( ['x','y','z','px','py','pz','pt','energy','bz'] )
183
down_means = bunch_down.mean( ['x','y','z','px','py','pz','pt','energy','bz'] )
186
print " === Upstream Results === "
188
print " Collective Properties"
189
print "Emittance : ", bunch_up.get_emittance( ['x','y'] )
190
print "Beta : ", bunch_up.get_beta( ['x','y'] )
191
print "Weight : ", bunch_up.bunch_weight()
194
print "X Position : ", up_means['x']
195
print "Y Position : ", up_means['y']
196
print "Z Position : ", up_means['z']
197
print "X Momentum : ", up_means['px']
198
print "Y Momentum : ", up_means['py']
199
print "Z Momentum : ", up_means['pz']
200
print "Transverese Momentum : ", up_means['pt']
201
print "Energy : ", up_means['energy']
202
print "Z Magnetic Field : ", up_means['bz']
205
print " === Downstream Results === "
207
print " Collective Properties"
208
print "Emittance : ", bunch_down.get_emittance( ['x','y'] )
209
print "Beta : ", bunch_down.get_beta( ['x','y'] )
210
print "Weight : ", bunch_down.bunch_weight()
213
print "X Position : ", down_means['x']
214
print "Y Position : ", down_means['y']
215
print "Z Position : ", down_means['z']
216
print "X Momentum : ", down_means['px']
217
print "Y Momentum : ", down_means['py']
218
print "Z Momentum : ", down_means['pz']
219
print "Transverese Momentum : ", down_means['pt']
220
print "Energy : ", down_means['energy']
221
print "Z Magnetic Field : ", down_means['bz']
224
if inspection_plane > 0 :
226
print "Saving Inspection Plane"
229
with open( 'inspection_plane.txt', 'w' ) as outfile :
230
outfile.write( '# X Y Z Px Py Pz\n' )
231
for hit in ana_list[ inspection_plane ] :
232
outfile.write( str( hit.get( 'x' ) ) + ' ' +
233
str( hit.get( 'y' ) ) + ' ' +
234
str( hit.get( 'z' ) ) + ' ' +
235
str( hit.get( 'px' ) ) + ' ' +
236
str( hit.get( 'py' ) ) + ' ' +
237
str( hit.get( 'pz' ) ) + ' ' +
238
str( hit.get( 'pid' ) ) + ' ' +
239
str( hit.get_weight() ) + '\n' )
244
if __name__ == "__main__":
246
print "Press return to close"
267
# if len( sys.argv ) <= 1 :
268
# print "No File Supplied."
271
# filename = sys.argv[1]
273
# print "Loading File"
277
# primary_bunch = Bunch.new_list_from_read_builtin('maus_root_primary', filename)
278
# bunch_list = Bunch.new_list_from_read_builtin('maus_root_virtual_hit', filename)
281
# print "Read", len(bunch_list), "Bunches"
283
# for bun in bunch_list :
284
# if bun.bunch_weight() <= 1.0e-9 :
286
# bun.cut( { 'amplitude x y' : cut_distance}, operator.ge )
287
# if bun.bunch_weight() <= 1.0e-9 :
289
# bun.cut( {'pid':11}, operator.eq )
290
# bun.cut( {'pid':-11}, operator.eq )
292
# for bun in bunch_list :
293
# if bun.bunch_weight() > 1.0e-9 and len( bun ) > 1 :
294
# ana_list.append( bun )
296
# print "Found", len(ana_list), "Non Empty Bunches"
298
# canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'bunch_weight', [''], 'm')
299
# canvas.Print( "BunchWeight.eps", "eps")
300
# canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'emittance', ['x', 'y'], 'm', 'm')
301
# canvas.Print( "Emittance.eps", "eps")
302
# canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'beta', ['x', 'y'], 'm', 'm')
303
# canvas.Print( "BetaFunc.eps", "eps")
304
# canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'mean', ['energy'], 'm', 'MeV')
305
# canvas.Print( "Energy.eps", "eps")
306
# canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'mean', ['bz'], 'm', 'T')
307
# canvas.Print( "MagneticField.eps", "eps")
308
# canvas, hist, graph = Bunch.root_graph(ana_list, 'mean', ['z'], 'mean', ['pid'], 'm')
309
# canvas.Print( "PID.eps", "eps")
310
## canvas, hist, graph = Bunch.root_graph(bunch_list, 'mean', ['z'], 'mean', ['p'], 'm', 'MeV/c')
311
## canvas.Print( "Momentum.eps", "eps")
313
#if __name__ == "__main__":
315
# print "Press return to close"