Skip to content

Commit 422513a

Browse files
authored
Merge pull request #164 from zhaozhiwen/fix/103-run-weight-normalization
Normalize run weights before stochastic event distribution
2 parents c445ab6 + 8f9c836 commit 422513a

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

gemc/eventDispenser/eventDispenser.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,19 @@ void EventDispenser::distributeEvents(int nevents_to_process) {
104104
mt19937 generator(randomDevice());
105105
uniform_real_distribution<> randomDistribution(0, 1);
106106

107+
// Weights in the run-weights file are relative, not required to sum to 1. Compute the total
108+
// once and scale each draw by it, so non-normalized weights distribute events correctly.
109+
double totalWeight = 0;
110+
for (const auto& weight : runWeights) { totalWeight += weight.second; }
111+
if (totalWeight <= 0) {
112+
log->error(ERR_EVENTDISTRIBUTIONFILENOTFOUND,
113+
"Run weights sum to ", totalWeight, " (must be > 0). Check your run weights file.");
114+
return;
115+
}
116+
107117
// For each event, select a run by comparing a random draw to the cumulative weight intervals.
108-
// This assumes runWeights values represent fractions or relative weights normalized to sum to 1.
109118
for (int i = 0; i < nevents_to_process; i++) {
110-
double randomNumber = randomDistribution(generator);
119+
double randomNumber = randomDistribution(generator) * totalWeight;
111120

112121
double cumulativeWeight = 0;
113122
for (const auto& weight : runWeights) {

0 commit comments

Comments
 (0)