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 = []
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]
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
224
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)
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)
254 isProton = 0
255
256
257 dPartDIS.Clear()
258 iMuon.Clear()
259 muPart[9] = isProton
260 myPythia.GenerateEvent()
261 myPythia.Pyedit(1)
262
263
264
265 xsec = myPythia.GetPARI(1)
266 muPart[10] = xsec
267
268
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)
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)
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