Skip to content

Commit 6e8c0d5

Browse files
committed
Correctly limit the PU weights
1 parent ea792a1 commit 6e8c0d5

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/WeightCalculatorFromHistogram.cc

+21-15
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,33 @@ float WeightCalculatorFromHistogram::checkIntegral(std::vector<float> wgt1, std:
9090
float myint=0;
9191
float refint=0;
9292
for(int i=0; i<(int)wgt1.size(); ++i) {
93+
if(verbose_) {
94+
std::cout << "i = " << i << " : wgt1 = " << wgt1[i] << " ; wgt2 = " << wgt2[i] << " ; refvals = " << refvals_[i] << '\n';
95+
}
9396
myint += wgt1[i]*refvals_[i];
9497
refint += wgt2[i]*refvals_[i];
9598
}
99+
if(verbose_) {
100+
std::cout << "myint = " << myint << "\nrefint = " << refint << '\n';
101+
}
96102
return (myint-refint)/refint;
97103
}
98104

99105
void WeightCalculatorFromHistogram::fixLargeWeights(std::vector<float> &weights, float maxshift,float hardmax) {
100-
float maxw = std::min(*(std::max_element(weights.begin(),weights.end())),float(5.));
101-
std::vector<float> cropped;
102-
while (maxw > hardmax) {
103-
cropped.clear();
104-
for(int i=0; i<(int)weights.size(); ++i) cropped.push_back(std::min(maxw,weights[i]));
105-
float shift = checkIntegral(cropped,weights);
106-
if(verbose_) std::cout << "For maximum weight " << maxw << ": integral relative change: " << shift << std::endl;
107-
if(fabs(shift) > maxshift) break;
108-
maxw *= 0.95;
109-
}
110-
maxw /= 0.95;
111-
if (cropped.size()>0) {
112-
for(int i=0; i<(int)weights.size(); ++i) cropped[i] = std::min(maxw,weights[i]);
113-
float normshift = checkIntegral(cropped,weights);
114-
for(int i=0; i<(int)weights.size(); ++i) weights[i] = cropped[i]*(1-normshift);
106+
if(verbose_) {
107+
std::cout << "hardmax = " << hardmax << "\n"
108+
"maxshift = " << maxshift << '\n';
115109
}
110+
std::vector<float> cropped = weights;
111+
float sf = 1.;
112+
do {
113+
for(int i=0; i<(int)cropped.size(); ++i) cropped[i] = std::min(hardmax,cropped[i]);
114+
float shift = checkIntegral(cropped,weights);
115+
sf = 1. / (1. + shift);
116+
if(verbose_) {
117+
std::cout << "For maximum weight " << hardmax << ": integral relative change: " << shift << '\n';
118+
}
119+
for(int i=0; i<(int)cropped.size(); ++i) cropped[i] *= sf;
120+
} while ( sf > (1. + maxshift) );
121+
for(int i=0; i<(int)weights.size(); ++i) weights[i] = cropped[i];
116122
}

0 commit comments

Comments
 (0)