11 from collections
import defaultdict
12 import modularAnalysis
as ma
13 from ROOT
import Belle2
17 """Generate a list of 10 electrons which have stupid momenta just to sort
18 them later. And then add one electron where all momentum components are
22 """We need to register the mc particles"""
25 self.
mcpmcp.registerInDataStore()
28 """And then we generate particles, ensuring some overlap in the momenta."""
31 px, py, pz = random.randrange(1, 5), random.randrange(1, 5), random.randrange(1, 5)
33 p = self.
mcpmcp.appendNew()
36 p.setMomentum(px, py, pz)
38 p = self.
mcpmcp.appendNew()
41 p.setMomentum(math.nan, math.nan, math.nan)
45 """Check if the ranks are actually what we want"""
48 """Create particle list object"""
53 """And check all the ranks"""
57 einfo = defaultdict(list)
58 for particle
in self.
plistplist:
59 px.append(particle.getPx())
60 py.append(particle.getPy())
63 names = [str(n)
for n
in particle.getExtraInfoNames()]
65 einfo[n].append(particle.getExtraInfo(n))
68 print(list(einfo.keys()))
69 assert 'M_rank' in einfo.keys(),
"Default name is not as expected"
77 px_value_ranks = {v: i
for i, v
in enumerate(sorted(set(px), reverse=
True,
78 key=
lambda v: -math.inf
if math.isnan(v)
else v), 1)}
79 py_value_ranks = {v: i
for i, v
in enumerate(sorted(set(py),
80 key=
lambda v: math.inf
if math.isnan(v)
else v), 1)}
84 for v, r
in zip(px, einfo[
"px_high_multi"]):
85 print(f
"Value: {v}, rank: {r}, should be: {px_value_ranks[v]}")
86 assert r == px_value_ranks[v],
"Rank is not correct"
88 for v, r
in zip(py, einfo[
"py_low_multi"]):
89 print(f
"Value: {v}, rank: {r}, should be: {py_value_ranks[v]}")
90 assert r == py_value_ranks[v],
"Rank is not correct"
96 simple_range = list(range(len(px)))
97 px_single_ranks = list(sorted(int(r) - 1
for r
in einfo[
"px_high_single"]))
98 assert simple_range == px_single_ranks,
"sorted ranks don't form a range from 1..n"
102 py_single_ranks = list(int(r) - 1
for r
in einfo[
"py_low_single"])
103 assert simple_range == py_single_ranks,
"ranks don't form a range from 1..n"
110 path.add_module(
"EventInfoSetter", evtNumList=10)
114 ma.fillParticleListFromMC(
"e-",
"", path=path)
116 ma.rankByHighest(
"e-",
"M", path=path)
117 ma.rankByHighest(
"e-",
"px", allowMultiRank=
False, outputVariable=
"px_high_single", path=path)
118 ma.rankByHighest(
"e-",
"px", allowMultiRank=
True, outputVariable=
"px_high_multi", path=path)
119 ma.rankByLowest(
"e-",
"py", allowMultiRank=
False, outputVariable=
"py_low_single", path=path)
120 ma.rankByLowest(
"e-",
"py", allowMultiRank=
True, outputVariable=
"py_low_multi", path=path)
129 """Check if 'numBest' works correctly"""
132 """Initializing the parameters."""
140 """Checking for module parameters to distinguish between the different test cases."""
141 self.
num_bestnum_best = kwargs.pop(
'numBest')
142 self.
allow_multirankallow_multirank = kwargs.pop(
'allowMultiRank',
False)
143 super().
param(kwargs)
146 """Create particle list 'e-:numBest(MultiRank)' object, depending on parameter choice."""
155 """Check if 'e-:numBest' and 'e-:numBestMultiRank' have the expected size"""
157 size = self.
plistplist.getListSize()
159 px = [particle.getPx()
for particle
in self.
plistplist]
160 px_value_ranks = {v: i
for i, v
in enumerate(sorted(set(px), reverse=
True,
161 key=
lambda v: -math.inf
if math.isnan(v)
else v),
163 remaining_particles = [v
for v
in px
if px_value_ranks[v] <= self.
num_bestnum_best]
164 assert size <= len(remaining_particles),
"numBest test with multirank failed: " \
165 f
"there should be {len(remaining_particles)} Particles in the list " \
166 f
"instead of {size}!"
170 assert size <= self.
num_bestnum_best, f
"numBest test failed: there are too many Particles ({size}) in the list!"
174 ma.fillParticleListFromMC(
"e-:numBest",
"", path=path)
176 ma.rankByHighest(
"e-:numBest",
"p", numBest=numBest_value, path=path)
181 ma.fillParticleListFromMC(
"e-:numBestMultiRank",
"", path=path)
183 ma.rankByHighest(
"e-:numBestMultiRank",
"px", numBest=numBest_value, allowMultiRank=
True, path=path)
185 path.add_module(
NumBestChecker(), numBest=numBest_value, allowMultiRank=
True)
A (simplified) python wrapper for StoreArray.
a (simplified) python wrapper for StoreObjPtr.
plist
particle list object
num_best
Number of candidates to keep (must be given as parameter, otherwise assert will fail).
allow_multirank
MultiRank option switch.
plist
particle list object