7 from optparse
import OptionParser
8 import xml.etree.ElementTree
as ET
9 from array
import array
10 from math
import cos, sin
14 def loop( evt, tgeo, tout ):
16 offset = [ 0., 5.5, 411. ]
17 collarLo = [ -320., -120., 30. ]
18 collarHi = [ 320., 120., 470. ]
21 geoEff = pyGeoEff.geoEff(args.seed)
23 geoEff.setNthrows(4992)
25 geoEff.setUseFixedBeamDir(
False)
28 OffAxisPoints =
array(
'f', [-2, 0.5, 3, 5.5, 8, 10.5, 13, 15.5, 18, 20.5, 23, 25.5, 28, 30.5])
29 meanPDPZ =
array(
'f', [ 93.6072, 93.362, 90.346, 85.6266, 81.1443, 76.6664, 73.0865, 69.8348, 67.5822, 65.005, 62.4821, 60.8336, 59.1433, 57.7352])
30 gDecayZ = ROOT.TGraph(14, OffAxisPoints, meanPDPZ)
34 GNuMIFluxTree = ET.parse(os.environ[
"GNUMIXML"])
35 beamLineRotation =
float(GNuMIFluxTree.getroot()[0].findall(
'beamdir')[0].findall(
'rotation')[0].text)
36 posText = GNuMIFluxTree.getroot()[0].findall(
'beampos')[0].text
37 posText = posText.replace(
"(",
"")
38 posText = posText.replace(
")",
"")
39 posText = posText.replace(
"=",
",")
40 posText = posText.split(
",")
41 beamRefDetCoord = [
float(posText[i])
for i
in range(3)]
42 detRefBeamCoord = [
float(posText[i])
for i
in range(3,6)]
45 print(
"dumpTree.py WARNING!!! Error reading GNUMIXML file. Set GNUMIXML enviornment variable to point at GNuMI flux configuration xml file. Exitting!")
49 geoEff.setVetoSizes([30])
51 geoEff.setVetoEnergyThresholds([20, 30, 40])
53 geoEff.setActiveX(collarLo[0]-30, collarHi[0]+30)
54 geoEff.setActiveY(collarLo[1]-30, collarHi[1]+30)
55 geoEff.setActiveZ(collarLo[2]-30, collarHi[2]+30)
57 geoEff.setRangeX(-1, -1)
58 geoEff.setRandomizeX(
False)
59 geoEff.setRangeY(collarLo[1]-30, collarHi[1]+30)
60 geoEff.setRangeZ(collarLo[2]-30, collarHi[2]+30)
62 geoEff.setOffsetX(offset[0])
63 geoEff.setOffsetY(offset[1])
64 geoEff.setOffsetZ(offset[2])
66 event = ROOT.TG4Event()
67 events.SetBranchAddress(
"Event",ROOT.AddressOf(event))
69 N = events.GetEntries()
71 print "Starting loop over %d entries" % N
77 print "Event %d of %d..." % (ient,N)
80 for ivtx,vertex
in enumerate(event.Primaries):
84 t_vtx[0]=0.0; t_vtx[1]=0.0; t_vtx[2]=0.0;
85 t_p3lep[0]=0.0; t_p3lep[1]=0.0; t_p3lep[2]=0.0;
86 t_lepDeath[0]=0.0; t_lepDeath[1]=0.0; t_lepDeath[2]=0.0;
89 t_muonExitPt[0] = 0.0; t_muonExitPt[1] = 0.0; t_muonExitPt[2] = 0.0;
90 t_muonExitMom[0] = 0.0; t_muonExitMom[1] = 0.0; t_muonExitMom[2] = 0.0;
92 t_muon_endVolName.replace(0, ROOT.std.string.npos,
"")
106 reaction=vertex.Reaction
110 t_vtx[i] = vertex.Position[i] / 10. - offset[i]
117 decayZbeamCoord = gDecayZ.Eval(vertex.Position[0] / 1000 - detRefBeamCoord[0])*100
118 decayZdetCoord = (-1*detRefBeamCoord[2]*100+decayZbeamCoord)*cos(beamLineRotation) - (-1*detRefBeamCoord[1]*100*sin(beamLineRotation)) + beamRefDetCoord[2]*100
119 decayYdetCoord = (-1*detRefBeamCoord[1]*100*cos(beamLineRotation)) + (-1*detRefBeamCoord[2]*100+decayZbeamCoord)*sin(beamLineRotation) + beamRefDetCoord[1]*100
120 decayXdetCoord = -1*detRefBeamCoord[0]*100 + beamRefDetCoord[0]*100
122 geoEff.setDecayPos(decayXdetCoord, decayYdetCoord, decayZdetCoord)
123 geoEff.setVertex(vertex.Position[0] / 10., vertex.Position[1] / 10.,vertex.Position[2] / 10.)
126 if (iwritten % 100) == 0 :
127 geoEff.throwTransforms()
128 t_geoEffThrowsY.clear()
129 for i
in geoEff.getCurrentThrowTranslationsY() :
130 t_geoEffThrowsY.push_back(i)
131 t_geoEffThrowsZ.clear()
132 for i
in geoEff.getCurrentThrowTranslationsZ() :
133 t_geoEffThrowsZ.push_back(i)
134 t_geoEffThrowsPhi.clear()
135 for i
in geoEff.getCurrentThrowRotations() :
136 t_geoEffThrowsPhi.push_back(i)
137 tGeoEfficiencyThrowsOut.Fill()
144 for ipart,particle
in enumerate(vertex.Particles):
145 e = particle.Momentum[3]
146 p = (particle.Momentum[0]**2 + particle.Momentum[1]**2 + particle.Momentum[2]**2)**0.5
147 m = (e**2 - p**2)**0.5
148 t_fsPdg[nfsp] = particle.PDGCode
149 t_fsPx[nfsp] = particle.Momentum[0]
150 t_fsPy[nfsp] = particle.Momentum[1]
151 t_fsPz[nfsp] = particle.Momentum[2]
153 fsParticleIdx[particle.TrackId] = nfsp
155 pdg = particle.PDGCode
156 if abs(pdg)
in [11,12,13,14]:
157 ileptraj = particle.TrackId
160 for i
in range(3): t_p3lep[i] = particle.Momentum[i]
163 assert ileptraj != -1,
"There isn't a lepton??" 167 if abs(t_lepPdg[0]) == 13:
168 leptraj = event.Trajectories[ileptraj]
169 for p
in leptraj.Points:
171 node = tgeo.FindNode( pt.X(), pt.Y(), pt.Z() )
172 volName = node.GetName()
174 if "LAr" in volName
or "PixelPlane" in volName
or "sPlane" in volName:
175 t_muonExitPt[0] = pt.X() / 10. - offset[0]
176 t_muonExitPt[1] = pt.Y() / 10. - offset[1]
177 t_muonExitPt[2] = pt.Z() / 10. - offset[2]
178 t_muonExitMom[0] = p.Momentum.x()
179 t_muonExitMom[1] = p.Momentum.y()
180 t_muonExitMom[2] = p.Momentum.z()
182 t_muonExitPt[0] = pt.X() / 10. - offset[0]
183 t_muonExitPt[1] = pt.Y() / 10. - offset[1]
184 t_muonExitPt[2] = pt.Z() / 10. - offset[2]
187 endpt = leptraj.Points[-1].Position
189 node = tgeo.FindNode( endpt.X(), endpt.Y(), endpt.Z() )
191 t_lepDeath[0] = endpt.X()/10. - offset[0]
192 t_lepDeath[1] = endpt.Y()/10. - offset[1]
193 t_lepDeath[2] = endpt.Z()/10. - offset[2]
195 endVolName = node.GetName()
196 t_muon_endVolName.replace(0, ROOT.std.string.npos, endVolName)
197 if "LArActive" in endVolName: t_muonReco[0] = 1
198 elif "ECal" in endVolName: t_muonReco[0] = 3
199 else: t_muonReco[0] = 0
203 for key
in event.SegmentDetectors:
204 if key.first
in [
"TPC_Drift1",
"TPC_Drift2"]:
209 if hit.PrimaryId == ileptraj:
211 hStart = ROOT.TVector3( hit.Start[0]/10.-offset[0], hit.Start[1]/10.-offset[1], hit.Start[2]/10.-offset[2] )
212 hStop = ROOT.TVector3( hit.Stop[0]/10.-offset[0], hit.Stop[1]/10.-offset[1], hit.Stop[2]/10.-offset[2] )
213 tot_length += (hStop-hStart).Mag()
217 for key
in event.SegmentDetectors:
218 if key.first
in [
"BarrelECal_vol",
"EndcapECal_vol"]:
223 if hit.PrimaryId == ileptraj:
225 hStart = ROOT.TVector3( hit.Start[0]/10.-offset[0], hit.Start[1]/10.-offset[1], hit.Start[2]/10.-offset[2] )
226 hStop = ROOT.TVector3( hit.Stop[0]/10.-offset[0], hit.Stop[1]/10.-offset[1], hit.Stop[2]/10.-offset[2] )
227 etot_length += (hStop-hStart).Mag()
229 t_muGArLen[0] = tot_length
230 t_muECalLen[0] = etot_length
232 if tot_length > 50.: t_muonReco[0] = 2
236 for key
in event.SegmentDetectors:
237 if key.first ==
"ArgonCube":
245 for traj
in event.Trajectories:
248 if event.Trajectories[mom].PDGCode == 111
and event.Trajectories[tid].PDGCode == 22
and event.Trajectories[mom].ParentId == -1:
249 gamma_tids.append(tid)
252 mom = event.Trajectories[mom].ParentId
253 if mom
in gamma_tids:
254 tid_to_gamma[tid] = mom
255 traj_to_pdg[traj] = event.Trajectories[tid].PDGCode
260 geoEff_EDepPosition = []
261 geoEff_EDepEnergy = []
263 track_length = [0.
for i
in range(nfsp)]
264 dEdX = [[]
for i
in range(nfsp)]
265 this_step = [[0.,0.]
for i
in range(nfsp)]
266 end_point = [
None for i
in range(nfsp)]
267 int_energy = [0.
for i
in range(nfsp)]
268 trk_calo = [0.
for i
in range(nfsp)]
273 hStart = ROOT.TVector3( hit.Start[0]/10.-offset[0], hit.Start[1]/10.-offset[1], hit.Start[2]/10.-offset[2] )
274 hStop = ROOT.TVector3( hit.Stop[0]/10.-offset[0], hit.Stop[1]/10.-offset[1], hit.Stop[2]/10.-offset[2] )
280 traj = event.Trajectories[tid]
281 if traj.ParentId == -1:
282 idx = fsParticleIdx[hit.PrimaryId]
283 trk_calo[idx] += hit.EnergyDeposit
284 end_point[idx] = hStop
285 dx = (hStop-hStart).Mag()
286 track_length[idx] += dx
287 this_step[idx][1] += dx
288 this_step[idx][0] += hit.EnergyDeposit
289 if this_step[idx][1] > 0.5:
290 dEdX[idx].append( (this_step[idx][0], this_step[idx][1], hStart) )
291 this_step[idx] = [0., 0.]
294 if ep
is None:
continue 295 if (hStart-ep).Mag() < 10.:
296 int_energy[k] += hit.EnergyDeposit
298 if tid
in tid_to_gamma:
299 gamma_energy[tid_to_gamma[tid]] += hit.EnergyDeposit
301 if hit.PrimaryId != ileptraj:
302 hStart = ROOT.TVector3( hit.Start[0]/10.-offset[0], hit.Start[1]/10.-offset[1], hit.Start[2]/10.-offset[2] )
303 total_energy += hit.EnergyDeposit
306 if hStart.x() < collarLo[0]
or hStart.x() > collarHi[0]
or hStart.y() < collarLo[1]
or hStart.y() > collarHi[1]
or hStart.z() < collarLo[2]
or hStart.z() > collarHi[2]:
307 collar_energy += hit.EnergyDeposit
310 for dim
in range(3) :
311 geoEff_EDepPosition.append((hit.Start[dim] + hit.Stop[dim])/2./10.)
312 geoEff_EDepEnergy.append(hit.EnergyDeposit)
315 pdg = traj_to_pdg[traj]
316 if pdg
in [11, -11, 13, -13]:
continue 317 elif pdg == 2212: t_hadP[0] += hit.EnergyDeposit
318 elif pdg == 2112: t_hadN[0] += hit.EnergyDeposit
319 elif pdg == 211: t_hadPip[0] += hit.EnergyDeposit
320 elif pdg == -211: t_hadPim[0] += hit.EnergyDeposit
321 elif pdg == 111: t_hadPi0[0] += hit.EnergyDeposit
322 else: t_hadOther[0] += hit.EnergyDeposit
324 t_hadTot[0] = total_energy
325 t_hadCollar[0] = collar_energy
327 geoEff.setHitSegEdeps(geoEff_EDepEnergy)
328 geoEff.setHitSegPoss(geoEff_EDepPosition)
330 geoEffThrowResultsList = geoEff.getHadronContainmentThrows()
332 t_geoEffThrowResults.clear()
333 for i
in range(len(geoEffThrowResultsList)) :
334 iVec = ROOT.std.vector(
'vector< uint64_t >')()
335 for j
in range (len(geoEffThrowResultsList[i])) :
336 jVec = ROOT.std.vector(
'uint64_t')()
337 for k
in range(len(geoEffThrowResultsList[i][j])) :
338 jVec.push_back(geoEffThrowResultsList[i][j][k])
340 t_geoEffThrowResults.push_back(iVec)
342 for i
in range(nfsp):
343 t_fsTrkLen[i] = track_length[i]
345 t_fsTrkEnddEdX[i] = 0.
346 t_fsTrkFrontdEdX[i] = 0.
350 totlen += dEdX[i][j][1]
351 t_fsTrkEnddEdX[i] += dEdX[i][j][0]
353 if totlen > 3.:
break 354 if totlen > 0.: t_fsTrkEnddEdX[i] /= totlen
358 totlen += dEdX[i][k][1]
359 t_fsTrkFrontdEdX[i] += dEdX[i][k][0]
360 if totlen > 0.: t_fsTrkFrontdEdX[i] /= totlen
362 t_fsTrkEndpointBall[i] = int_energy[i]
363 t_fsTrkCalo[i] = trk_calo[i]
370 mom = event.Trajectories[t].ParentId
371 if t_fsGamma1[mom] == 0.: t_fsGamma1[mom] = gamma_energy[t]
372 elif t_fsGamma2[mom] == 0.: t_fsGamma2[mom] = gamma_energy[t]
374 print "Pi0 has more than two photons wtf" 382 if __name__ ==
"__main__":
384 ROOT.gROOT.SetBatch(1)
386 parser = OptionParser()
387 parser.add_option(
'--infile', help=
'Input file name', default=
"edep.root")
388 parser.add_option(
'--outfile', help=
'Output file name', default=
"out.root")
389 parser.add_option(
'--seed', help=
'Seed for geometric efficiency throws', default=0, type =
"int")
391 (args, dummy) = parser.parse_args()
394 fout = ROOT.TFile( args.outfile,
"RECREATE" )
395 tout = ROOT.TTree(
"tree",
"tree" )
397 tout.Branch(
'ievt',t_ievt,
'ievt/I')
399 tout.Branch(
'Ev',t_Ev,
'Ev/F')
401 tout.Branch(
'p3lep',t_p3lep,
'p3lep[3]/F')
403 tout.Branch(
'vtx',t_vtx,
'vtx[3]/F')
405 tout.Branch(
'lepDeath',t_lepDeath,
'lepDeath[3]/F')
407 tout.Branch(
'lepPdg',t_lepPdg,
'lepPdg/I')
409 tout.Branch(
'lepKE',t_lepKE,
'lepKE/F')
411 tout.Branch(
'muonExitPt',t_muonExitPt,
'muonExitPt[3]/F')
413 tout.Branch(
'muonExitMom',t_muonExitMom,
'muonExitMom[3]/F')
415 tout.Branch(
'muonReco',t_muonReco,
'muonReco/I')
416 t_muon_endVolName = ROOT.std.string()
417 tout.Branch(
'muon_endVolName', t_muon_endVolName)
419 tout.Branch(
'muGArLen',t_muGArLen,
'muGArLen/F')
421 tout.Branch(
'muECalLen',t_muECalLen,
'muECalLen/F')
423 tout.Branch(
'hadTot', t_hadTot,
'hadTot/F' )
425 tout.Branch(
'hadP', t_hadP,
'hadP/F' )
427 tout.Branch(
'hadN', t_hadN,
'hadN/F' )
429 tout.Branch(
'hadPip', t_hadPip,
'hadPip/F' )
431 tout.Branch(
'hadPim', t_hadPim,
'hadPim/F' )
433 tout.Branch(
'hadPi0', t_hadPi0,
'hadPi0/F' )
435 tout.Branch(
'hadOther', t_hadOther,
'hadOther/F' )
437 tout.Branch(
'hadCollar', t_hadCollar,
'hadCollar/F' )
439 tout.Branch(
'nFS',t_nFS,
'nFS/I')
441 tout.Branch(
'fsPdg',t_fsPdg,
'fsPdg[nFS]/I')
443 tout.Branch(
'fsPx',t_fsPx,
'fsPx[nFS]/F')
445 tout.Branch(
'fsPy',t_fsPy,
'fsPy[nFS]/F')
447 tout.Branch(
'fsPz',t_fsPz,
'fsPz[nFS]/F')
449 tout.Branch(
'fsE',t_fsE,
'fsE[nFS]/F')
451 tout.Branch(
'fsTrkLen',t_fsTrkLen,
'fsTrkLen[nFS]/F')
452 t_fsTrkFrontdEdX =
array(
'f',100*[0.])
453 tout.Branch(
'fsTrkFrontdEdX',t_fsTrkFrontdEdX,
'fsTrkFrontdEdX[nFS]/F')
454 t_fsTrkEnddEdX =
array(
'f',100*[0.])
455 tout.Branch(
'fsTrkEnddEdX',t_fsTrkEnddEdX,
'fsTrkEnddEdX[nFS]/F')
456 t_fsTrkEndpointBall =
array(
'f', 100*[0.])
457 tout.Branch(
'fsTrkEndpointBall',t_fsTrkEndpointBall,
'fsTrkEndpointBall[nFS]/F')
459 tout.Branch(
'fsGamma1',t_fsGamma1,
'fsGamma1[nFS]/F')
461 tout.Branch(
'fsGamma2',t_fsGamma2,
'fsGamma2[nFS]/F')
463 tout.Branch(
'fsTrkCalo',t_fsTrkCalo,
'fsTrkCalo[nFS]/F')
466 t_geoEffThrowResults = ROOT.std.vector(
'std::vector< std::vector < uint64_t > >')()
467 tout.Branch(
'geoEffThrowResults', t_geoEffThrowResults)
470 tGeoEfficiencyThrowsOut = ROOT.TTree(
"geoEffThrows",
"geoEffThrows")
471 t_geoEffThrowsY = ROOT.std.vector(
'float')()
472 tGeoEfficiencyThrowsOut.Branch(
"geoEffThrowsY", t_geoEffThrowsY)
473 t_geoEffThrowsZ = ROOT.std.vector(
'float')()
474 tGeoEfficiencyThrowsOut.Branch(
"geoEffThrowsZ", t_geoEffThrowsZ)
475 t_geoEffThrowsPhi = ROOT.std.vector(
'float')()
476 tGeoEfficiencyThrowsOut.Branch(
"geoEffThrowsPhi", t_geoEffThrowsPhi)
478 events = ROOT.TChain(
"EDepSimEvents",
"main event tree" )
481 tf = ROOT.TFile( args.infile )
482 tf.MakeProject(
"EDepSimEvents",
"*",
"RECREATE++")
484 events = tf.Get(
"EDepSimEvents" )
485 tgeo = tf.Get(
"EDepSimGeometry")
486 loop( events, tgeo, tout )
490 tGeoEfficiencyThrowsOut.Write()
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
def loop(evt, tgeo, tout)
auto array(Array const &a)
Returns a manipulator which will print the specified array.