1 | import sys
|
---|
2 |
|
---|
3 | import urllib2
|
---|
4 | import cStringIO
|
---|
5 | import zipfile
|
---|
6 |
|
---|
7 | import ROOT
|
---|
8 |
|
---|
9 | import ProMCHeader_pb2
|
---|
10 | import ProMC_pb2
|
---|
11 |
|
---|
12 | ################################################################################
|
---|
13 |
|
---|
14 | class HttpFile(object):
|
---|
15 | def __init__(self, url):
|
---|
16 | self.url = url
|
---|
17 | self.offset = 0
|
---|
18 | self.length = -1
|
---|
19 |
|
---|
20 | def size(self):
|
---|
21 | if self.length < 0:
|
---|
22 | f = urllib2.urlopen(self.url)
|
---|
23 | self.length = int(f.headers["Content-length"])
|
---|
24 | return self.length
|
---|
25 |
|
---|
26 | def read(self, count = -1):
|
---|
27 | req = urllib2.Request(self.url)
|
---|
28 | if count < 0:
|
---|
29 | end = self.size() - 1
|
---|
30 | else:
|
---|
31 | end = self.offset + count - 1
|
---|
32 | req.headers['Range'] = "bytes=%s-%s" % (self.offset, end)
|
---|
33 | f = urllib2.urlopen(req)
|
---|
34 | data = f.read()
|
---|
35 | # FIXME: should check that we got the range expected, etc.
|
---|
36 | chunk = len(data)
|
---|
37 | if count >= 0:
|
---|
38 | assert chunk == count
|
---|
39 | self.offset += chunk
|
---|
40 | return data
|
---|
41 |
|
---|
42 | def seek(self, offset, whence = 0):
|
---|
43 | if whence == 0:
|
---|
44 | self.offset = offset
|
---|
45 | elif whence == 1:
|
---|
46 | self.offset += offset
|
---|
47 | elif whence == 2:
|
---|
48 | self.offset = self.size() + offset
|
---|
49 | else:
|
---|
50 | raise Exception("Invalid whence")
|
---|
51 |
|
---|
52 | def tell(self):
|
---|
53 | return self.offset
|
---|
54 |
|
---|
55 | ################################################################################
|
---|
56 |
|
---|
57 | def ConvertInput(name, momentumUnit, lengthUnit, branch, factory,
|
---|
58 | allParticleOutputArray, stableParticleOutputArray, partonOutputArray):
|
---|
59 |
|
---|
60 | pdg = ROOT.TDatabasePDG.Instance()
|
---|
61 |
|
---|
62 | data = ProMC_pb2.ProMCEvent()
|
---|
63 | data.ParseFromString(zip.read(name))
|
---|
64 |
|
---|
65 | event = data.event
|
---|
66 | particles = data.particles
|
---|
67 |
|
---|
68 | element = branch.NewEntry()
|
---|
69 |
|
---|
70 |
|
---|
71 | # element.Number = event.Number
|
---|
72 | # element.ProcessID = event.Process_ID
|
---|
73 | # element.MPI = event.MPI
|
---|
74 | # element.Weight = event.Weight
|
---|
75 | # element.Scale = event.Scale
|
---|
76 | # element.AlphaQED = event.Alpha_QED
|
---|
77 | # element.AlphaQCD = event.Alpha_QCD
|
---|
78 | # element.ID1 = event.ID1
|
---|
79 | # element.ID2 = event.ID2
|
---|
80 | # element.X1 = event.X1
|
---|
81 | # element.X2 = event.X2
|
---|
82 | # element.ScalePDF = event.Scale_PDF
|
---|
83 | # element.PDF1 = event.PDF1
|
---|
84 | # element.PDF2 = event.PDF2
|
---|
85 |
|
---|
86 | for i in range(len(particles.pdg_id)):
|
---|
87 | pid = particles.pdg_id[i]
|
---|
88 | status = particles.status[i]
|
---|
89 | px = particles.Px[i]
|
---|
90 | py = particles.Py[i]
|
---|
91 | pz = particles.Pz[i]
|
---|
92 | mass = particles.mass[i]
|
---|
93 |
|
---|
94 | x = particles.X[i]
|
---|
95 | y = particles.Y[i]
|
---|
96 | z = particles.Z[i]
|
---|
97 | t = particles.T[i]
|
---|
98 |
|
---|
99 | candidate = factory.NewCandidate()
|
---|
100 |
|
---|
101 | candidate.PID = pid
|
---|
102 | pdgCode = ROOT.TMath.Abs(candidate.PID)
|
---|
103 |
|
---|
104 | candidate.Status = status
|
---|
105 |
|
---|
106 | # candidate.M1 = particles.mother1[i]
|
---|
107 | # candidate.M2 = particles.mother2[i]
|
---|
108 |
|
---|
109 | # candidate.D1 = particles.daughter1[i]
|
---|
110 | # candidate.D2 = particles.daughter2[i]
|
---|
111 |
|
---|
112 | candidate.Mass = mass
|
---|
113 |
|
---|
114 | candidate.Momentum.SetXYZM(px, py, pz, mass)
|
---|
115 |
|
---|
116 | candidate.Position.SetXYZT(x, y, z, t)
|
---|
117 |
|
---|
118 | pdgParticle = pdg.GetParticle(pid)
|
---|
119 | if pdgParticle == None:
|
---|
120 | candidate.Charge = -999
|
---|
121 | continue
|
---|
122 | else:
|
---|
123 | candidate.Charge = int(pdgParticle.Charge()/3.0)
|
---|
124 |
|
---|
125 | allParticleOutputArray.Add(candidate)
|
---|
126 |
|
---|
127 | if status == 1:
|
---|
128 | stableParticleOutputArray.Add(candidate)
|
---|
129 | elif pdgCode <= 5 or pdgCode == 21 or pdgCode == 15:
|
---|
130 | partonOutputArray.Add(candidate)
|
---|
131 |
|
---|
132 | ################################################################################
|
---|
133 |
|
---|
134 | if len(sys.argv) < 2:
|
---|
135 | print " Usage: DelphesProMC.py config_file output_file input_file(s)"
|
---|
136 | sys.exit(1)
|
---|
137 |
|
---|
138 | ROOT.gSystem.Load('libDelphes')
|
---|
139 |
|
---|
140 | outputFile = ROOT.TFile(sys.argv[2], "CREATE")
|
---|
141 |
|
---|
142 | if not outputFile.IsOpen():
|
---|
143 | print "** ERROR: can't open ", sys.argv[2]
|
---|
144 | sys.exit(1)
|
---|
145 |
|
---|
146 | treeWriter = ROOT.ExRootTreeWriter(outputFile, "Delphes")
|
---|
147 |
|
---|
148 | branchEvent = treeWriter.NewBranch("Event", ROOT.HepMCEvent.Class())
|
---|
149 |
|
---|
150 | confReader = ROOT.ExRootConfReader()
|
---|
151 | confReader.ReadFile(sys.argv[1])
|
---|
152 |
|
---|
153 | modularDelphes = ROOT.Delphes("Delphes")
|
---|
154 | modularDelphes.SetConfReader(confReader)
|
---|
155 | modularDelphes.SetTreeWriter(treeWriter)
|
---|
156 |
|
---|
157 | factory = modularDelphes.GetFactory()
|
---|
158 | allParticleOutputArray = modularDelphes.ExportArray("allParticles")
|
---|
159 | stableParticleOutputArray = modularDelphes.ExportArray("stableParticles")
|
---|
160 | partonOutputArray = modularDelphes.ExportArray("partons")
|
---|
161 |
|
---|
162 | modularDelphes.InitTask()
|
---|
163 |
|
---|
164 | for fileName in sys.argv[3:]:
|
---|
165 | print "** Reading " + fileName
|
---|
166 |
|
---|
167 | file = HttpFile(fileName)
|
---|
168 | zip = zipfile.ZipFile(file)
|
---|
169 |
|
---|
170 | # retrive information from the header file
|
---|
171 | header = ProMCHeader_pb2.ProMCHeader()
|
---|
172 | header.ParseFromString(zip.read("header"))
|
---|
173 | momentumUnit = float(header.MomentumUnit)
|
---|
174 | lengthUnit = float(header.LengthUnit)
|
---|
175 |
|
---|
176 | modularDelphes.Clear()
|
---|
177 | treeWriter.Clear()
|
---|
178 | for name in zip.namelist():
|
---|
179 | if name.isdigit():
|
---|
180 | ConvertInput(name, momentumUnit, lengthUnit, branchEvent, factory,
|
---|
181 | allParticleOutputArray, stableParticleOutputArray, partonOutputArray)
|
---|
182 |
|
---|
183 | modularDelphes.ProcessTask()
|
---|
184 |
|
---|
185 | treeWriter.Fill()
|
---|
186 |
|
---|
187 | modularDelphes.Clear()
|
---|
188 | treeWriter.Clear()
|
---|
189 |
|
---|
190 | zip.close()
|
---|
191 |
|
---|
192 | modularDelphes.FinishTask()
|
---|
193 | treeWriter.Write()
|
---|
194 |
|
---|
195 | del treeWriter
|
---|