5 if __name__ ==
"__main__":
6 import argparse, itertools, sys, unittest
8 parser = argparse.ArgumentParser()
9 parser.add_argument(
'--lhefile-hwithdecay')
10 parser.add_argument(
'--lhefile-jhugenvbfvh')
11 parser.add_argument(
'--lhefile-jhugentth')
12 parser.add_argument(
'unittest_args', nargs=
'*')
13 args = parser.parse_args()
17 from mela
import Mela, SimpleParticle_t, SimpleParticleCollection_t
19 InputEvent = collections.namedtuple(
"InputEvent",
"daughters associated mothers isgen")
22 __metaclass__ = abc.ABCMeta
24 lines = event.split(
"\n")
28 if "<wgt" not in line:
continue
29 match = re.match(
"<wgt id='(.*)'>([0-9+Ee.-]*)</wgt>", line)
30 if match: self.
weights[match.group(1)] = float(match.group(2))
32 lines = [line
for line
in lines
if not (
"<" in line
or ">" in line
or not line.split(
"#")[0].strip())]
33 nparticles, _, weight, _, _, _ = lines[0].split()
35 nparticles =
int(nparticles)
37 if nparticles != len(lines)-1:
38 raise ValueError(
"Wrong number of particles! Should be {}, have {}".format(nparticles, len(lines)-1))
41 if not list(mothers): mothers =
None
45 def extracteventparticles(cls, lines, isgen):
"has to be a classmethod that returns daughters, associated, mothers"
53 daughters, mothers, associated = [], [], []
58 id, status, mother1, mother2 = (
int(_)
for _
in line.split()[0:4])
60 if (1 <= abs(id) <= 6
or abs(id) == 21)
and not isgen:
61 line = line.replace(str(id),
"0", 1)
62 mother1s.append(mother1)
63 mother2s.append(mother2)
66 elif status == 1
and (1 <= abs(id) <= 6
or 11 <= abs(id) <= 16
or abs(id)
in (21, 22)):
68 if mother1 != mother2
or mother1
is None:
69 associated.append(line)
71 if ids[mother1]
in (25, 39):
72 daughters.append(line)
74 mother2 = mother2s[mother1]
75 mother1 = mother1s[mother1]
77 if not isgen: mothers =
None
78 return daughters, associated, mothers
83 daughters, mothers, associated = [], [], []
85 id, status, mother1, mother2 = (
int(_)
for _
in line.split()[0:4])
86 if (1 <= abs(id) <= 6
or abs(id) == 21)
and not isgen:
87 line = line.replace(str(id),
"0", 1)
92 raise ValueError(
"Higgs has status {}, expected it to be 1\n\n".format(status) +
"\n".join(lines))
93 daughters.append(line)
94 if abs(id)
in (0, 1, 2, 3, 4, 5, 11, 12, 13, 14, 15, 16, 21)
and status == 1:
95 associated.append(line)
97 if len(daughters) != 1:
98 raise ValueError(
"More than one H in the event??\n\n"+
"\n".join(lines))
100 raise ValueError(
"Wrong number of associated particles (expected {}, found {})\n\n".format(cls.
nassociatedparticles, len(associated))+
"\n".join(lines))
101 if len(mothers) != 2:
102 raise ValueError(
"{} mothers in the event??\n\n".format(len(mothers))+
"\n".join(lines))
104 if not isgen: mothers =
None
105 return daughters, associated, mothers
107 nassociatedparticles =
None
110 nassociatedparticles = 2
113 nassociatedparticles = 6
118 daughters, mothers, associated = [], [], []
120 id, status, mother1, mother2 = (
int(_)
for _
in line.split()[0:4])
121 if (1 <= abs(id) <= 6
or abs(id) == 21)
and not isgen:
122 line = line.replace(str(id),
"0", 1)
125 if abs(id)
in (11, 12, 13, 14, 15, 16)
and status == 1:
126 daughters.append(line)
127 if abs(id)
in (0, 1, 2, 3, 4, 5, 21)
and status == 1:
128 associated.append(line)
130 if len(daughters) != 4:
131 raise ValueError(
"Wrong number of daughters (expected {}, found {})\n\n".format(4, len(daughters))+
"\n".join(lines))
133 raise ValueError(
"Wrong number of associated particles (expected {}, found {})\n\n".format(cls.
nassociatedparticles, len(associated))+
"\n".join(lines))
134 if len(mothers) != 2:
135 raise ValueError(
"{} mothers in the event??\n\n".format(len(mothers))+
"\n".join(lines))
137 if not isgen: mothers =
None
138 return daughters, associated, mothers
140 nassociatedparticles =
None
144 Simple class to iterate through an LHE file and calculate probabilities for each event
146 h1 = ROOT.TH1F("costheta1", "costheta1", 100, -1, 1)
147 h2 = ROOT.TH1F("D_0minus", "D_0minus", 100, 0, 1)
148 with LHEFile("filename.lhe") as f:
149 for event in f: #event becomes the mela object
150 h1.Fill(event.computeDecayAngles().costheta1)
152 p0plus = event.computeP()
154 p0minus = event.computeP()
155 h2.Fill(p0plus / (p0plus + p0minus))
157 __metaclass__ = abc.ABCMeta
162 self.
isgen = kwargs.pop(
"isgen",
True)
163 reusemela = kwargs.pop(
"reusemela",
False)
164 gzip = kwargs.pop(
"gzip",
False)
165 if kwargs:
raise ValueError(
"Unknown kwargs: " +
", ".join(kwargs))
167 if reusemela
and melaargs
in self.
__melas:
173 if gzip:
from gzip
import GzipFile
as openfunction
183 for linenumber, line
in enumerate(self.
f, start=1):
184 if "<event>" not in line
and not event:
187 if "</event>" in line:
192 except GeneratorExit:
195 print(
"On line", linenumber)
199 self.
mela.resetInputEvent()
204 lheevent = self.lheeventclass(event, self.
isgen)
210 self.setInputEvent(*lheevent)
214 return "filename",
"f",
"mela",
"isgen",
"daughters",
"mothers",
"associated",
"weight",
"weights"
217 if attr ==
"mela":
raise RuntimeError(
"Something is wrong, trying to access mela before it's created")
218 return getattr(self.
mela, attr)
223 setattr(self.
mela, attr, value)
226 lheeventclass = LHEEvent_Hwithdecay
228 lheeventclass = LHEEvent_StableHiggs
230 lheeventclass = LHEEvent_JHUGenVBFVH
232 lheeventclass = LHEEvent_JHUGenttH
234 lheeventclass = LHEEvent_Offshell4l
236 if __name__ ==
'__main__':
238 @unittest.skipUnless(args.lhefile_hwithdecay,
"needs --lhefile-hwithdecay argument")
241 for event, i
in itertools.izip(f, range(10)):
243 event.setProcess(TVar.SelfDefine_spin0, TVar.JHUGen, TVar.ZZINDEPENDENT)
244 prob = event.computeP()
245 self.assertNotEqual(prob, 0)
246 print(prob, event.computeDecayAngles())
248 @unittest.skipUnless(args.lhefile_jhugenvbfvh,
"needs --lhefile-jhugenvbfvh argument")
251 for event, i
in itertools.izip(f, range(10)):
253 if any(11 <= abs(p.first) <= 16
for p
in event.associated):
254 if sum(p.first
for p
in event.associated) == 0:
255 VHprocess = TVar.Lep_ZH
256 event.setProcess(TVar.SelfDefine_spin0, TVar.JHUGen, TVar.Lep_ZH)
258 VHprocess = TVar.Lep_WH
259 event.setProcess(TVar.SelfDefine_spin0, TVar.JHUGen, TVar.Lep_WH)
261 VHprocess = TVar.Had_ZH
262 event.setProcess(TVar.SelfDefine_spin0, TVar.JHUGen, TVar.JJVBF)
263 prob = event.computeProdP()
264 self.assertNotEqual(prob, 0)
265 print(prob, event.computeVBFAngles(), event.computeVHAngles(VHprocess))
267 @unittest.skipUnless(args.lhefile_jhugentth,
"needs --lhefile-jhugentth argument")
270 for event, i
in itertools.izip(f, range(10)):
273 unittest.main(argv=[sys.argv[0]]+args.unittest_args)