Skip to content

Commit b730d14

Browse files
jackal1-66alcaliva
authored andcommitted
Fraction based randomisation for Hybrid Gen (#13745)
(cherry picked from commit 5f1e50c)
1 parent 9180208 commit b730d14

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

Generators/include/Generators/GeneratorHybrid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class GeneratorHybrid : public Generator
7979

8080
bool mRandomize = false;
8181
std::vector<int> mFractions;
82+
std::vector<float> mRngFractions;
8283
int mseqCounter = 0;
8384
int mCurrentFraction = 0;
8485
int mIndex = 0;

Generators/src/GeneratorHybrid.cxx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,55 @@ Bool_t GeneratorHybrid::Init()
127127
addSubGenerator(count, gen);
128128
count++;
129129
}
130+
if (mRandomize) {
131+
if (std::all_of(mFractions.begin(), mFractions.end(), [](int i) { return i == 1; })) {
132+
LOG(info) << "Full randomisation of generators order";
133+
} else {
134+
LOG(info) << "Randomisation based on fractions";
135+
int allfracs = 0;
136+
for (auto& f : mFractions) {
137+
allfracs += f;
138+
}
139+
// Assign new rng fractions
140+
float sum = 0;
141+
float chance = 0;
142+
for (int k = 0; k < mFractions.size(); k++) {
143+
if (mFractions[k] == 0) {
144+
// Generator will not be used if fraction is 0
145+
mRngFractions.push_back(-1);
146+
LOG(info) << "Generator " << mGens[k] << " will not be used";
147+
} else {
148+
chance = static_cast<float>(mFractions[k]) / allfracs;
149+
sum += chance;
150+
mRngFractions.push_back(sum);
151+
LOG(info) << "Generator " << (mConfigs[k] == "" ? mGens[k] : mConfigs[k]) << " has a " << chance * 100 << "% chance of being used";
152+
}
153+
}
154+
}
155+
} else {
156+
LOG(info) << "Generators will be used in sequence, following provided fractions";
157+
}
130158
return Generator::Init();
131159
}
132160

133161
Bool_t GeneratorHybrid::generateEvent()
134162
{
135163
// Order randomisation or sequence of generators
136-
// following provided fractions, if not generators are used in proper sequence
164+
// following provided fractions. If not available generators will be used sequentially
137165
if (mRandomize) {
138-
mIndex = gRandom->Integer(mGens.size());
166+
if (mRngFractions.size() != 0) {
167+
// Generate number between 0 and 1
168+
float rnum = gRandom->Rndm();
169+
// Find generator index
170+
for (int k = 0; k < mRngFractions.size(); k++) {
171+
if (rnum <= mRngFractions[k]) {
172+
mIndex = k;
173+
break;
174+
}
175+
}
176+
} else {
177+
mIndex = gRandom->Integer(mGens.size());
178+
}
139179
} else {
140180
while (mFractions[mCurrentFraction] == 0 || mseqCounter == mFractions[mCurrentFraction]) {
141181
if (mFractions[mCurrentFraction] != 0) {

0 commit comments

Comments
 (0)