FairShip
Loading...
Searching...
No Matches
makeMuonDIS Namespace Reference

Functions

def rotate (px, py, pz, theta, phi)
 
None update_file (str filename, final_xsec)
 
None inspect_file (str filename)
 
None makeMuonDIS ()
 
def getMasssq (pid)
 
def rotate (ctheta, stheta, cphi, sphi, px, py, pz)
 

Variables

 level
 
PDG = r.TDatabasePDG.Instance()
 
argparse parser = argparse.ArgumentParser(description=__doc__)
 
 help
 
 required
 
 dest
 
 default
 
 type
 
argparse args = parser.parse_args()
 
argparse n_events = args.n_events
 
argparse first_mu_event = args.first_mu_event
 
list headers
 
dict Fixtarget = {1: "p+", 0: "n0"}
 
int nJob = 2
 
int nMult = 10
 
str muonIn = "$SHIPSOFT/data/muConcrete.root"
 
int nPerJob = 20000
 
ROOT myPythia = ROOT.TPythia6()
 
ROOT kc = myPythia.Pycomp(kf)
 
dict masssq = {}
 
int R = int(time.time() % 900000000)
 
dict mutype = {-13: "gamma/mu+", 13: "gamma/mu-"}
 
ROOT fout = ROOT.TFile("muonDis_" + str(nJob) + ".root", "recreate")
 
ROOT dTree = ROOT.TTree("DIS", "muon DIS")
 
ROOT iMuon = ROOT.TClonesArray("TVectorD")
 
ROOT iMuonBranch = dTree.Branch("InMuon", iMuon, 32000, -1)
 
ROOT dPart = ROOT.TClonesArray("TVectorD")
 
ROOT dPartBranch = dTree.Branch("Particles", dPart, 32000, -1)
 
ROOT fin = ROOT.TFile(muonIn)
 
ROOT sTree = fin.muons
 
ROOT nTOT = sTree.GetEntries()
 
int nStart = nPerJob * nJob
 
min nEnd = min(nTOT, nStart + nPerJob)
 
int nMade = 0
 
ROOT rc = sTree.GetEvent(k)
 
 px
 
 py
 
 pz
 
 x
 
 y
 
 z
 
 pid
 
 w
 
ROOT p = ROOT.TMath.Sqrt(px * px + py * py + pz * pz)
 
ROOT E = ROOT.TMath.Sqrt(getMasssq(pid) + p * p)
 
ROOT theta = ROOT.TMath.ACos(pz / p)
 
ROOT phi = ROOT.TMath.ATan2(py, px)
 
 ctheta
 
 stheta
 
 cphi
 
 sphi
 
array mu = array("d", [pid, px, py, pz, E, x, y, z, w])
 
ROOT tca_vec = iMuon.ConstructedAt(0)
 
ROOT muPart = ROOT.TVectorD(9, mu)
 
ROOT did = myPythia.GetK(itrk, 2)
 
 dpx
 
 dpy
 
 dpz
 
dpx psq = dpx**2 + dpy**2 + dpz**2
 
array m = array("d", [did, dpx, dpy, dpz, E])
 
ROOT part = ROOT.TVectorD(5, m)
 
len nPart = len(dPart)
 

Detailed Description

Script to generate DIS events for muons in Pythia6, and save them to a ROOT file (along with the original muon's soft interactions).

Function Documentation

◆ getMasssq()

def makeMuonDIS.getMasssq (   pid)

Definition at line 40 of file makeMuonDIS.py.

40def getMasssq(pid):
41 apid = abs(int(pid))
42 if apid not in masssq:
43 masssq[apid] = PDG.GetParticle(apid).Mass() ** 2
44 return masssq[apid]
45
46

◆ inspect_file()

None makeMuonDIS.inspect_file ( str  filename)
Inspect the contents of muonDis file.

Definition at line 106 of file makeMuonDIS.py.

106def inspect_file(filename: str) -> None:
107 """Inspect the contents of muonDis file."""
108 file = r.TFile.Open(filename, "READ")
109 tree = file.DIS
110
111 table_rows = []
112
113 for i, event in enumerate(tree):
114 muon = event.InMuon[0]
115 isProton = int(muon[9])
116 fix_target = Fixtarget.get(isProton, "unknown")
117 cross_sec = muon[10]
118
119 nParticles = len(event.DISParticles)
120 nSoftTracks = len(event.SoftParticles)
121 nSBThits = len(event.muon_vetoPoints)
122 nUBThits = len(event.muon_UpstreamTaggerPoints)
123
124 table_rows.append([i, fix_target, nParticles, nSoftTracks, nSBThits, nUBThits, cross_sec])
125
126 file.Close()
127 logging.info("\n" + tabulate(table_rows, headers=headers, tablefmt="grid"))
128
129

◆ makeMuonDIS()

None makeMuonDIS.makeMuonDIS ( )
Generate DIS events.

Definition at line 130 of file makeMuonDIS.py.

130def makeMuonDIS() -> None:
131 """Generate DIS events."""
132 final_xsec = {}
133
134 logging.info(f"Opening input file: {args.inputFile}")
135 muonFile = r.TFile.Open(args.inputFile, "read")
136
137 try:
138 muon_tree = muonFile.MuonAndSoftInteractions
139 except Exception as e:
140 logging.error(e)
141 muonFile.Close()
142 exit(1)
143
144 logging.debug(f"Total entries in the tree: {muon_tree.GetEntries()}")
145 last_mu_event = min(muon_tree.GetEntries(), first_mu_event + n_events)
146
147 logging.info("Creating output file: muonDis.root")
148
149 outputFile = r.TFile.Open("muonDis.root", "recreate")
150 output_tree = r.TTree("DIS", "muon DIS")
151
152 iMuon = r.TClonesArray("TVectorD")
153 output_tree.Branch("InMuon", iMuon, 32000, -1)
154
155 dPartDIS = r.TClonesArray("TVectorD")
156 output_tree.Branch("DISParticles", dPartDIS, 32000, -1)
157
158 dPartSoft = r.TClonesArray("TVectorD")
159 output_tree.Branch("SoftParticles", dPartSoft, 32000, -1)
160
161 muon_vetoPoints = r.TClonesArray("vetoPoint")
162 output_tree.Branch("muon_vetoPoints", muon_vetoPoints, 32000, -1)
163
164 muon_UpstreamTaggerPoints = r.TClonesArray("UpstreamTaggerPoint")
165 output_tree.Branch("muon_UpstreamTaggerPoints", muon_UpstreamTaggerPoints, 32000, -1)
166
167 myPythia = r.TPythia6()
168 myPythia.SetMSEL(2)
169 myPythia.SetPARP(2, 2)
170 for kf in [211, 321, 130, 310, 3112, 3122, 3222, 3312, 3322, 3334]:
171 kc = myPythia.Pycomp(kf)
172 myPythia.SetMDCY(kc, 1, 0)
173
174 seed = int(time.time() % 900000000)
175 myPythia.SetMRPY(1, seed)
176 mutype = {-13: "gamma/mu+", 13: "gamma/mu-"}
177
178 myPythia.SetMSTU(11, 11)
179 logging.info(f"Processing muon events from {first_mu_event} to {last_mu_event - 1}...")
180
181 nMade = 0
182
183 for k in range(first_mu_event, last_mu_event):
184 DIS_table = [] # debug
185 cross_sections = []
186
187 muon_tree.GetEvent(k)
188
189 imuondata = muon_tree.imuondata
190
191 pid = imuondata[0]
192 px = imuondata[1]
193 py = imuondata[2]
194 pz = imuondata[3]
195 x = imuondata[4]
196 y = imuondata[5]
197 z = imuondata[6]
198 w = imuondata[7]
199 time_muon = imuondata[8]
200 nmuons = imuondata[9] # number of muons in the original MuBack event
201
202 p = r.TMath.Sqrt(px**2 + py**2 + pz**2)
203 mass = PDG.GetParticle(abs(int(pid))).Mass()
204 E = r.TMath.Sqrt(mass**2 + p**2)
205
206 theta = r.TMath.ACos(pz / p)
207 phi = r.TMath.ATan2(py, px)
208
209 isProton = 1
210 xsec = 0
211
212 mu = array(
213 "d",
214 [
215 pid,
216 px,
217 py,
218 pz,
219 E,
220 x,
221 y,
222 z,
223 w,
224 isProton,
225 xsec,
226 time_muon,
227 args.nDIS,
228 nmuons,
229 ],
230 )
231 muPart = r.TVectorD(14, mu)
232 myPythia.Initialize("FIXT", mutype[pid], "p+", p) # target = "p+"
233 myPythia.Pylist(1)
234
235 for a in range(args.nDIS):
236 if a == args.nDIS // 2:
237 myPythia.Initialize("FIXT", mutype[pid], "n0", p) # target = "n0"
238 isProton = 0
239 # logging.debug("Switching to neutron interaction")
240
241 dPartDIS.Clear()
242 iMuon.Clear()
243 muPart[9] = isProton
244 iMuon[0] = muPart
245 myPythia.GenerateEvent()
246 myPythia.Pyedit(1)
247
248 for itrk in range(1, myPythia.GetN() + 1):
249 xsec = myPythia.GetPARI(1)
250
251 muPart[10] = xsec
252 did = myPythia.GetK(itrk, 2)
253 dpx, dpy, dpz = rotate(
254 myPythia.GetP(itrk, 1),
255 myPythia.GetP(itrk, 2),
256 myPythia.GetP(itrk, 3),
257 theta,
258 phi,
259 )
260 psq = dpx**2 + dpy**2 + dpz**2
261 masssq = PDG.GetParticle(did).Mass() ** 2
262 E = r.TMath.Sqrt(masssq + psq)
263 m = array("d", [did, dpx, dpy, dpz, E])
264 part = r.TVectorD(5, m)
265 nPart = len(dPartDIS)
266 if dPartDIS.GetSize() == nPart:
267 dPartDIS.Expand(nPart + 10)
268 # dPartDIS.ConstructedAt(nPart).Use(part) #to be adapted later
269 dPartDIS[nPart] = part
270
271 cross_sections.append(xsec)
272
273 dPartSoft.Clear()
274
275 for softTrack in muon_tree.tracks:
276 did = softTrack.GetPdgCode()
277 dpx = softTrack.GetPx()
278 dpy = softTrack.GetPy()
279 dpz = softTrack.GetPz()
280
281 psq = dpx**2 + dpy**2 + dpz**2
282 masssq = PDG.GetParticle(did).Mass() ** 2
283 E = r.TMath.Sqrt(masssq + psq)
284
285 softx = softTrack.GetStartX()
286 softy = softTrack.GetStartY()
287 softz = softTrack.GetStartZ()
288 time_ = softTrack.GetStartT()
289
290 m = array("d", [did, dpx, dpy, dpz, E, softx, softy, softz, time_])
291
292 part = r.TVectorD(9, m)
293 nPart = len(dPartSoft)
294 if dPartSoft.GetSize() == nPart:
295 dPartSoft.Expand(nPart + 10)
296 # dPartSoft.ConstructedAt(nPart).Use(part) #to be adapted later
297 dPartSoft[nPart] = part
298
299 muon_vetoPoints.Clear()
300
301 index = 0
302 for hit in muon_tree.muon_vetoPoints:
303 if muon_vetoPoints.GetSize() == index:
304 muon_vetoPoints.Expand(index + 1)
305 hit.SetTrackID(0) # Set TrackID to match for muon ID for new simulation
306 muon_vetoPoints[index] = hit
307 index += 1
308
309 muon_UpstreamTaggerPoints.Clear()
310
311 ubt_index = 0
312 for hit in muon_tree.muon_UpstreamTaggerPoints:
313 if muon_UpstreamTaggerPoints.GetSize() == ubt_index:
314 muon_UpstreamTaggerPoints.Expand(ubt_index + 1)
315 hit.SetTrackID(0) # Set TrackID to match for muon ID for new simulation
316 muon_UpstreamTaggerPoints[ubt_index] = hit
317 ubt_index += 1
318
319 output_tree.Fill()
320 DIS_table.append(
321 [
322 a,
323 Fixtarget[isProton],
324 myPythia.GetN(),
325 len(dPartSoft),
326 len(muon_vetoPoints),
327 len(muon_UpstreamTaggerPoints),
328 xsec,
329 ]
330 )
331
332 final_xsec[k] = xsec
333
334 nMade += 1
335 logging.debug(
336 f"\nMuon index:{k} \n\tPID = {pid}, weight = {w}, converged_cross_section= {final_xsec[k]}\n\tpx = {px}, py = {py}, pz = {pz}, E = {E},\n\tx = {x}, y = {y}, z = {z}\n\n\tDIS Events Summary\n{tabulate(DIS_table, headers=headers, tablefmt='grid')}"
337 )
338 if nMade % 10 == 0:
339 logging.info(f"Muons processed: {nMade}")
340
341 outputFile.cd()
342 output_tree.Write()
343 myPythia.SetMSTU(11, 6)
344 logging.info(
345 f"DIS generated for muons (index {first_mu_event} - {last_mu_event - 1}) , output saved in muonDis.root, nDISPerMuon = {args.nDIS}"
346 )
347 outputFile.Close()
348 muonFile.Close()
349
350 update_file("muonDis.root", final_xsec)
351
352

◆ rotate() [1/2]

def makeMuonDIS.rotate (   ctheta,
  stheta,
  cphi,
  sphi,
  px,
  py,
  pz 
)

Definition at line 66 of file makeMuonDIS.py.

66def rotate(ctheta, stheta, cphi, sphi, px, py, pz):
67 # rotate around y-axis
68 px1 = ctheta * px + stheta * pz
69 pzr = -stheta * px + ctheta * pz
70 # rotate around z-axis
71 pxr = cphi * px1 - sphi * py
72 pyr = sphi * px1 + cphi * py
73 return pxr, pyr, pzr
74
75

◆ rotate() [2/2]

def makeMuonDIS.rotate (   px,
  py,
  pz,
  theta,
  phi 
)
Rotate the daughter particle momentum to align with respect to the muon's momentum.

Definition at line 55 of file makeMuonDIS.py.

55def rotate(px, py, pz, theta, phi):
56 """Rotate the daughter particle momentum to align with respect to the muon's momentum."""
57 momentum = r.TVector3(px, py, pz)
58
59 rotation = r.TRotation()
60 rotation.RotateY(theta) # Rotate around the Y-axis
61 rotation.RotateZ(phi) # Rotate around the Z-axis
62
63 # Apply the rotation to the momentum vector
64 rotated_momentum = rotation * momentum
65
66 return rotated_momentum.X(), rotated_momentum.Y(), rotated_momentum.Z()
67
68

◆ update_file()

None makeMuonDIS.update_file ( str  filename,
  final_xsec 
)
Update the DIS cross section of the muon to the converged value from Pythia.

Definition at line 69 of file makeMuonDIS.py.

69def update_file(filename: str, final_xsec) -> None:
70 """Update the DIS cross section of the muon to the converged value from Pythia."""
71 file = r.TFile.Open(filename, "read")
72
73 original_tree = file.DIS
74
75 temp_filename = filename + ".tmp"
76 temp_file = r.TFile.Open(temp_filename, "recreate")
77
78 updated_tree = original_tree.CloneTree(0)
79
80 for i, event in enumerate(original_tree):
81 mu = event.InMuon[0]
82 mu[10] = final_xsec[int(first_mu_event + i / args.nDIS)]
83 updated_tree.Fill()
84
85 updated_tree.Write("DIS", r.TObject.kOverwrite)
86 temp_file.Close()
87 file.Close()
88
89 os.remove(filename)
90 os.rename(temp_filename, filename)
91 logging.info("Muon DIS events successfully updated with converged cross sections.")
92
93

Variable Documentation

◆ args

argparse makeMuonDIS.args = parser.parse_args()

Definition at line 50 of file makeMuonDIS.py.

◆ cphi

makeMuonDIS.cphi

Definition at line 100 of file makeMuonDIS.py.

◆ ctheta

makeMuonDIS.ctheta

Definition at line 99 of file makeMuonDIS.py.

◆ default

makeMuonDIS.default

Definition at line 29 of file makeMuonDIS.py.

◆ dest

makeMuonDIS.dest

Definition at line 26 of file makeMuonDIS.py.

◆ did

ROOT makeMuonDIS.did = myPythia.GetK(itrk, 2)

Definition at line 114 of file makeMuonDIS.py.

◆ dPart

ROOT makeMuonDIS.dPart = ROOT.TClonesArray("TVectorD")

Definition at line 58 of file makeMuonDIS.py.

◆ dPartBranch

ROOT makeMuonDIS.dPartBranch = dTree.Branch("Particles", dPart, 32000, -1)

Definition at line 59 of file makeMuonDIS.py.

◆ dpx

makeMuonDIS.dpx

Definition at line 115 of file makeMuonDIS.py.

◆ dpy

makeMuonDIS.dpy

Definition at line 115 of file makeMuonDIS.py.

◆ dpz

makeMuonDIS.dpz

Definition at line 115 of file makeMuonDIS.py.

◆ dTree

ROOT makeMuonDIS.dTree = ROOT.TTree("DIS", "muon DIS")

Definition at line 55 of file makeMuonDIS.py.

◆ E

ROOT makeMuonDIS.E = ROOT.TMath.Sqrt(getMasssq(pid) + p * p)

Definition at line 95 of file makeMuonDIS.py.

◆ fin

ROOT makeMuonDIS.fin = ROOT.TFile(muonIn)

Definition at line 62 of file makeMuonDIS.py.

◆ first_mu_event

argparse makeMuonDIS.first_mu_event = args.first_mu_event

Definition at line 52 of file makeMuonDIS.py.

◆ Fixtarget

dict makeMuonDIS.Fixtarget = {1: "p+", 0: "n0"}

Definition at line 103 of file makeMuonDIS.py.

◆ fout

ROOT makeMuonDIS.fout = ROOT.TFile("muonDis_" + str(nJob) + ".root", "recreate")

Definition at line 54 of file makeMuonDIS.py.

◆ headers

list makeMuonDIS.headers
Initial value:
1= [
2 "DIS_index",
3 "Fix_Target",
4 "nParticles in event",
5 "nSoftTracks_iMuon",
6 "nSBThits_iMuon",
7 "nUBThits_iMuon",
8 "cross_sec",
9]

Definition at line 94 of file makeMuonDIS.py.

◆ help

makeMuonDIS.help

Definition at line 22 of file makeMuonDIS.py.

◆ iMuon

ROOT makeMuonDIS.iMuon = ROOT.TClonesArray("TVectorD")

Definition at line 56 of file makeMuonDIS.py.

◆ iMuonBranch

ROOT makeMuonDIS.iMuonBranch = dTree.Branch("InMuon", iMuon, 32000, -1)

Definition at line 57 of file makeMuonDIS.py.

◆ kc

ROOT makeMuonDIS.kc = myPythia.Pycomp(kf)

Definition at line 34 of file makeMuonDIS.py.

◆ level

makeMuonDIS.level

Definition at line 16 of file makeMuonDIS.py.

◆ m

array makeMuonDIS.m = array("d", [did, dpx, dpy, dpz, E])

Definition at line 120 of file makeMuonDIS.py.

◆ masssq

dict makeMuonDIS.masssq = {}

Definition at line 37 of file makeMuonDIS.py.

◆ mu

array makeMuonDIS.mu = array("d", [pid, px, py, pz, E, x, y, z, w])

Definition at line 101 of file makeMuonDIS.py.

◆ muonIn

sys makeMuonDIS.muonIn = "$SHIPSOFT/data/muConcrete.root"

Definition at line 14 of file makeMuonDIS.py.

◆ muPart

ROOT makeMuonDIS.muPart = ROOT.TVectorD(9, mu)

Definition at line 107 of file makeMuonDIS.py.

◆ mutype

dict makeMuonDIS.mutype = {-13: "gamma/mu+", 13: "gamma/mu-"}

Definition at line 49 of file makeMuonDIS.py.

◆ myPythia

ROOT makeMuonDIS.myPythia = ROOT.TPythia6()

Definition at line 30 of file makeMuonDIS.py.

◆ n_events

argparse makeMuonDIS.n_events = args.n_events

Definition at line 51 of file makeMuonDIS.py.

◆ nEnd

ROOT makeMuonDIS.nEnd = min(nTOT, nStart + nPerJob)

Definition at line 79 of file makeMuonDIS.py.

◆ nJob

int makeMuonDIS.nJob = 2

Definition at line 12 of file makeMuonDIS.py.

◆ nMade

int makeMuonDIS.nMade = 0

Definition at line 87 of file makeMuonDIS.py.

◆ nMult

int makeMuonDIS.nMult = 10

Definition at line 13 of file makeMuonDIS.py.

◆ nPart

len makeMuonDIS.nPart = len(dPart)

Definition at line 123 of file makeMuonDIS.py.

◆ nPerJob

int makeMuonDIS.nPerJob = 20000

Definition at line 15 of file makeMuonDIS.py.

◆ nStart

int makeMuonDIS.nStart = nPerJob * nJob

Definition at line 78 of file makeMuonDIS.py.

◆ nTOT

ROOT makeMuonDIS.nTOT = sTree.GetEntries()

Definition at line 76 of file makeMuonDIS.py.

◆ p

ROOT makeMuonDIS.p = ROOT.TMath.Sqrt(px * px + py * py + pz * pz)

Definition at line 94 of file makeMuonDIS.py.

◆ parser

argparse makeMuonDIS.parser = argparse.ArgumentParser(description=__doc__)

Definition at line 21 of file makeMuonDIS.py.

◆ part

ROOT makeMuonDIS.part = ROOT.TVectorD(5, m)

Definition at line 121 of file makeMuonDIS.py.

◆ PDG

ROOT makeMuonDIS.PDG = r.TDatabasePDG.Instance()

Definition at line 17 of file makeMuonDIS.py.

◆ phi

ROOT makeMuonDIS.phi = ROOT.TMath.ATan2(py, px)

Definition at line 98 of file makeMuonDIS.py.

◆ pid

makeMuonDIS.pid

Definition at line 93 of file makeMuonDIS.py.

◆ psq

dpx makeMuonDIS.psq = dpx**2 + dpy**2 + dpz**2

Definition at line 118 of file makeMuonDIS.py.

◆ px

makeMuonDIS.px

Definition at line 91 of file makeMuonDIS.py.

◆ py

makeMuonDIS.py

Definition at line 91 of file makeMuonDIS.py.

◆ pz

makeMuonDIS.pz

Definition at line 91 of file makeMuonDIS.py.

◆ R

int makeMuonDIS.R = int(time.time() % 900000000)

Definition at line 47 of file makeMuonDIS.py.

◆ rc

ROOT makeMuonDIS.rc = sTree.GetEvent(k)

Definition at line 89 of file makeMuonDIS.py.

◆ required

makeMuonDIS.required

Definition at line 22 of file makeMuonDIS.py.

◆ sphi

makeMuonDIS.sphi

Definition at line 100 of file makeMuonDIS.py.

◆ stheta

makeMuonDIS.stheta

Definition at line 99 of file makeMuonDIS.py.

◆ sTree

ROOT makeMuonDIS.sTree = fin.muons

Definition at line 63 of file makeMuonDIS.py.

◆ tca_vec

ROOT makeMuonDIS.tca_vec = iMuon.ConstructedAt(0)

Definition at line 106 of file makeMuonDIS.py.

◆ theta

ROOT makeMuonDIS.theta = ROOT.TMath.ACos(pz / p)

Definition at line 97 of file makeMuonDIS.py.

◆ type

makeMuonDIS.type

Definition at line 30 of file makeMuonDIS.py.

◆ w

makeMuonDIS.w

Definition at line 93 of file makeMuonDIS.py.

◆ x

makeMuonDIS.x

Definition at line 92 of file makeMuonDIS.py.

◆ y

makeMuonDIS.y

Definition at line 92 of file makeMuonDIS.py.

◆ z

makeMuonDIS.z

Definition at line 92 of file makeMuonDIS.py.