SND@LHC Software
Loading...
Searching...
No Matches
makeMuonDIS.py
Go to the documentation of this file.
1from __future__ import print_function
2import ROOT,time,os,sys
3nJob = 2
4nMult = 10 # number of events / muon
5muonIn = '$SHIPSOFT/data/muConcrete.root'
6nPerJob = 20000
7
8if len(sys.argv)>1: nJob = int(sys.argv[1])
9if len(sys.argv)>2: nMult = int(sys.argv[2])
10if len(sys.argv)>3: muonIn = sys.argv[3]
11if len(sys.argv)>4: nPerJob = int(sys.argv[4])
12
13#
14from array import array
15PDG = ROOT.TDatabasePDG.Instance()
16myPythia = ROOT.TPythia6()
17myPythia.SetMSEL(2) # msel 2 includes diffractive parts
18myPythia.SetPARP(2,2) # To get below 10 GeV, you have to change PARP(2)
19for kf in [211,321,130,310,3112,3122,3222,3312,3322,3334]:
20 kc = myPythia.Pycomp(kf)
21 myPythia.SetMDCY(kc,1,0)
22
23masssq = {}
24
25def getMasssq(pid):
26 apid = abs(int(pid))
27 if not apid in masssq:
28 masssq[apid] = PDG.GetParticle(apid).Mass()**2
29 return masssq[apid]
30
31R = int(time.time()%900000000)
32myPythia.SetMRPY(1,R)
33mutype = {-13:'gamma/mu+',13:'gamma/mu-'}
34
35# DIS event
36# incoming muon, id:px:py:pz:x:y:z:w
37# outgoing particles, id:px:py:pz
38fout = ROOT.TFile('muonDis_'+str(nJob)+'.root','recreate')
39dTree = ROOT.TTree('DIS','muon DIS')
40iMuon = ROOT.TClonesArray("TVectorD")
41iMuonBranch = dTree.Branch("InMuon",iMuon,32000,-1)
42dPart = ROOT.TClonesArray("TVectorD")
43dPartBranch = dTree.Branch("Particles",dPart,32000,-1)
44
45# read file with muons hitting concrete wall
46fin = ROOT.TFile(muonIn) # id:px:py:pz:x:y:z:w
47sTree = fin.muons
48
49def rotate(ctheta,stheta,cphi,sphi,px,py,pz):
50 #rotate around y-axis
51 px1=ctheta*px+stheta*pz
52 pzr=-stheta*px+ctheta*pz
53 #rotate around z-axis
54 pxr=cphi*px1-sphi*py
55 pyr=sphi*px1+cphi*py
56 return pxr,pyr,pzr
57
58nTOT = sTree.GetEntries()
59
60nStart = nPerJob*nJob
61nEnd = min(nTOT,nStart + nPerJob)
62if muonIn.find('Concrete')<0:
63 nStart = 0
64 nEnd = nTOT
65
66# stop pythia printout during loop
67myPythia.SetMSTU(11, 11)
68print("start production ",nStart,nEnd)
69nMade = 0
70for k in range(nStart,nEnd):
71 rc = sTree.GetEvent(k)
72 # make n events / muon
73 px,py,pz = sTree.px,sTree.py,sTree.pz
74 x,y,z = sTree.x,sTree.y,sTree.z
75 pid,w = sTree.id,sTree.w
76 p = ROOT.TMath.Sqrt(px*px+py*py+pz*pz)
77 E = ROOT.TMath.Sqrt(getMasssq(pid)+p*p)
78 # px=p*sin(theta)cos(phi),py=p*sin(theta)sin(phi),pz=p*cos(theta)
79 theta = ROOT.TMath.ACos(pz/p)
80 phi = ROOT.TMath.ATan2(py,px)
81 ctheta,stheta = ROOT.TMath.Cos(theta),ROOT.TMath.Sin(theta)
82 cphi,sphi = ROOT.TMath.Cos(phi),ROOT.TMath.Sin(phi)
83 mu = array('d',[pid,px,py,pz,E,x,y,z,w])
84 muPart = ROOT.TVectorD(9,mu)
85 myPythia.Initialize('FIXT',mutype[pid],'p+',p)
86 for n in range(nMult):
87 dPart.Clear()
88 iMuon.Clear()
89 iMuon[0] = muPart
90 myPythia.GenerateEvent()
91# remove all unnecessary stuff
92 myPythia.Pyedit(2)
93 for itrk in range(1,myPythia.GetN()+1):
94 did = myPythia.GetK(itrk,2)
95 dpx,dpy,dpz = rotate(ctheta,stheta,cphi,sphi,myPythia.GetP(itrk,1),myPythia.GetP(itrk,2),myPythia.GetP(itrk,3))
96 psq = dpx**2+dpy**2+dpz**2
97 E = ROOT.TMath.Sqrt(getMasssq(did)+psq)
98 m = array('d',[did,dpx,dpy,dpz,E])
99 part = ROOT.TVectorD(5,m)
100# copy to branch
101 nPart = dPart.GetEntries()
102 if dPart.GetSize() == nPart: dPart.Expand(nPart+10)
103 dPart[nPart] = part
104 nMade+=1
105 if nMade%10000==0: print('made so far ',nMade)
106 dTree.Fill()
107fout.cd()
108dTree.Write()
109myPythia.SetMSTU(11, 6)
110print("created nJob ",nJob,':',nStart,' - ',nEnd," events")
rotate(ctheta, stheta, cphi, sphi, px, py, pz)