Skip to content

Commit f9f9e7f

Browse files
wuctlbymfaggin
andauthored
Update MC/config/PWGHF/external/generator/generator_pythia8_embed_hf.C
Co-authored-by: Mattia Faggin <mattia.faggin@cern.ch>
1 parent 5f0a79c commit f9f9e7f

1 file changed

Lines changed: 52 additions & 25 deletions

File tree

MC/config/PWGHF/external/generator/generator_pythia8_embed_hf.C

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -398,31 +398,58 @@ private:
398398
// Integer b/c: taken directly; fractional b/c: reduced to nearest nB:nC with nC+nB <= 20
399399
static std::vector<int> BuildQuarkListFromBOverC(float bOverCRatio)
400400
{
401-
if (bOverCRatio <= 0.f) {
402-
LOG(fatal) << "bOverCRatio (b/c) must be > 0";
403-
}
404-
if (bOverCRatio > 19.f) {
405-
bOverCRatio = 19.f;
406-
LOG(warn) << "bOverCRatio (b/c) too large, using 19:1";
407-
}else if (bOverCRatio < 1.f/19.f) {
408-
bOverCRatio = 1.f/19.f;
409-
LOG(warn) << "bOverCRatio (b/c) too small, using 1:19";
410-
}
411-
412-
int nC = 1, nB = 1;
413-
float bestErr = 1e9f;
414-
for (int c = 1; c <= 19; ++c) {
415-
int b = (int)std::lround(bOverCRatio * c);
416-
if (b < 1 || c + b > 20) {
417-
continue;
418-
}
419-
float err = std::fabs((float)b / c - bOverCRatio);
420-
if (err < bestErr) {
421-
bestErr = err;
422-
nC = c;
423-
nB = b;
424-
}
425-
}
401+
const int iterNMax = 19;
402+
if (bOverCRatio <= 0.f) {
403+
LOG(fatal) << "bOverCRatio (b/c) must be > 0";
404+
}
405+
if (bOverCRatio > iterNMax*1.f) {
406+
bOverCRatio = iterNMax*1.f;
407+
LOG(warn) << "bOverCRatio (b/c) too large, using 19:1";
408+
}else if (bOverCRatio < 1.f/iterNMax) {
409+
bOverCRatio = 1.f/iterNMax;
410+
LOG(warn) << "bOverCRatio (b/c) too small, using 1:19";
411+
}
412+
413+
int nC = 1, nB = 1;
414+
float bestErr = 1e9f;
415+
for (int c = 1; c <= iterNMax; ++c) {
416+
int b = static_cast<int>(std::lround(bOverCRatio * c));
417+
if (b < 1 || c + b > 20) {
418+
continue;
419+
}
420+
float err = std::fabs(static_cast<float>(b) / c - bOverCRatio);
421+
if (err < bestErr) {
422+
/// This check here is needed in case bOverCRatio*c is not integer (it can happen with e.g. bOverCRatio=4./9.)
423+
/// In this case, b is its truncation and the desired ratio is not obtained
424+
/// It means that one needs to continue iterating until bOverCRatio*c is integer, i.e. b not truncated
425+
///
426+
/// Possible cases:
427+
/// 1. we want nB = nC*R, with R integer
428+
/// -> we enter here in the first loop iteration
429+
/// 2. we want more charm than beauty by an integer amount, i.e. nB = nC*R with R=1./N, with N integer
430+
/// -> we enter here after N iterations, when c=N
431+
/// 3. we want either more charm or beauty, but with a factor R that is not integer, as well as its inverse (e.g. bOverCRatio=4./9.)
432+
/// -> In this case, bOverCRatio*c is not integer, namely b is its truncation and the desired ratio is not obtained
433+
/// The code iterates at most until c becomes equal to the denominator of the fraction
434+
/// 4. we want one of the previous cases, but we assign to bOverCRatio a value that is not rational
435+
/// or such as we do not enter here within 19 iterations
436+
/// -> nC and nB, are not touched, therefore we do not have the desired fraction. We'll need to throw a fatal (*)
437+
///
438+
bestErr = err;
439+
nC = c;
440+
nB = b;
441+
442+
/// If we are at this point, we reached already the desired ratio between b and c.
443+
/// Let's break the loop
444+
break;
445+
}
446+
}
447+
448+
// (*) check if we have the desired fraction
449+
bool isRatioUnity = std::fabs(bOverCRatio-1) < 1e-05;
450+
if (!isRatioUnity && nC==1 && nB==1) {
451+
LOG(fatal) << "nC=" << nC << ", nB=" << nB << " but you ask bOverCRatio to be " << bOverCRatio <<", which is different from nB/nC. It means either that bOverCRatio is not rational, or that you need more than " << iterNMax << " iterations. Change it!";
452+
}
426453

427454
std::vector<int> list;
428455
// Bresenham interleaving

0 commit comments

Comments
 (0)