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

Functions

 rotate (px, py, pz, theta, phi)
None update_file (str filename, final_xsec)
None inspect_file (str filename)
None makeMuonDIS ()

Variables

 level
 PDG = r.TDatabasePDG.Instance()
 parser = argparse.ArgumentParser(description=__doc__)
 help
 required
 dest
 default
 type
 args = parser.parse_args()
 n_events = args.n_events
 first_mu_event = args.first_mu_event
list headers
dict Fixtarget = {1: "p+", 0: "n0"}

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

◆ inspect_file()

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

Definition at line 117 of file makeMuonDIS.py.

117def inspect_file(filename: str) -> None:
118 """Inspect the contents of muonDis file."""
119 file = r.TFile.Open(filename, "READ")
120 tree = file["DIS"]
121
122 table_rows = []
123
124 for i, event in enumerate(tree):
125 muon = event.InMuon[0]
126 isProton = int(muon[9])
127 fix_target = Fixtarget.get(isProton, "unknown")
128 cross_sec = muon[10]
129
130 nParticles = len(event.DISParticles)
131 nSoftTracks = len(event.SoftParticles)
132 nSBThits = len(event.muon_vetoPoints)
133 nUBThits = len(event.muon_UpstreamTaggerPoints)
134
135 table_rows.append([i, fix_target, nParticles, nSoftTracks, nSBThits, nUBThits, cross_sec])
136
137 file.Close()
138 logging.info("\n" + tabulate(table_rows, headers=headers, tablefmt="grid"))
139
140

◆ makeMuonDIS()

None makeMuonDIS.makeMuonDIS ( )
Generate DIS events.

Definition at line 141 of file makeMuonDIS.py.

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

◆ rotate()

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

Definition at line 63 of file makeMuonDIS.py.

63def rotate(px, py, pz, theta, phi):
64 """Rotate the daughter particle momentum to align with respect to the muon's momentum."""
65 momentum = r.TVector3(px, py, pz)
66
67 rotation = r.TRotation()
68 rotation.RotateY(theta) # Rotate around the Y-axis
69 rotation.RotateZ(phi) # Rotate around the Z-axis
70
71 # Apply the rotation to the momentum vector
72 rotated_momentum = rotation * momentum
73
74 return rotated_momentum.X(), rotated_momentum.Y(), rotated_momentum.Z()
75
76

◆ 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 77 of file makeMuonDIS.py.

77def update_file(filename: str, final_xsec) -> None:
78 """Update the DIS cross section of the muon to the converged value from Pythia."""
79 file = r.TFile.Open(filename, "read")
80
81 original_tree = file["DIS"]
82
83 temp_filename = filename + ".tmp"
84 temp_file = r.TFile.Open(temp_filename, "recreate")
85
86 updated_tree = original_tree.CloneTree(0)
87
88 for i, event in enumerate(original_tree):
89 mu = event.InMuon[0]
90 proton_xsec, neutron_xsec = final_xsec[int(first_mu_event + i / args.nDIS)]
91 # mu[9] is the isProton flag: apply the converged cross-section of the
92 # matching target rather than the last (neutron) value to every entry.
93 mu[10] = proton_xsec if int(mu[9]) == 1 else neutron_xsec
94 updated_tree.Fill()
95
96 updated_tree.Write("DIS", r.TObject.kOverwrite)
97 temp_file.Close()
98 file.Close()
99
100 os.remove(filename)
101 os.rename(temp_filename, filename)
102 logging.info("Muon DIS events successfully updated with converged cross sections.")
103
104

Variable Documentation

◆ args

makeMuonDIS.args = parser.parse_args()

Definition at line 54 of file makeMuonDIS.py.

◆ default

makeMuonDIS.default

Definition at line 33 of file makeMuonDIS.py.

◆ dest

makeMuonDIS.dest

Definition at line 30 of file makeMuonDIS.py.

◆ first_mu_event

makeMuonDIS.first_mu_event = args.first_mu_event

Definition at line 60 of file makeMuonDIS.py.

◆ Fixtarget

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

Definition at line 114 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 105 of file makeMuonDIS.py.

◆ help

makeMuonDIS.help

Definition at line 26 of file makeMuonDIS.py.

◆ level

makeMuonDIS.level

Definition at line 20 of file makeMuonDIS.py.

◆ n_events

makeMuonDIS.n_events = args.n_events

Definition at line 59 of file makeMuonDIS.py.

◆ parser

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

Definition at line 25 of file makeMuonDIS.py.

◆ PDG

makeMuonDIS.PDG = r.TDatabasePDG.Instance()

Definition at line 21 of file makeMuonDIS.py.

◆ required

makeMuonDIS.required

Definition at line 26 of file makeMuonDIS.py.

◆ type

makeMuonDIS.type

Definition at line 34 of file makeMuonDIS.py.