Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions DataFormats/simulation/src/InteractionSampler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,11 @@ bool NonUniformMuInteractionSampler::setBCIntensityScales(const std::vector<floa
{
// Sets the intensity scales per bunch crossing index
// The length of this vector needs to be compatible with the bunch filling chosen
mBCIntensityScales = scales_from_vector;

if (scales_from_vector.size() != mInteractingBCs.size()) {
LOG(error) << "Scaling factors and bunch filling scheme are not compatible. Not doing anything";
return false;
}
mBCIntensityScales = scales_from_vector;

float sum = 0.;
for (auto v : mBCIntensityScales) {
Expand All @@ -190,7 +189,7 @@ bool NonUniformMuInteractionSampler::setBCIntensityScales(const std::vector<floa
v = std::abs(v) * norm;
}
}
return false;
return true;
}

// ________________________________________________
Expand Down
129 changes: 129 additions & 0 deletions DataFormats/simulation/test/testInteractionSampler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,140 @@
#include "DataFormatsParameters/GRPLHCIFData.h"
#include "TFile.h"
#include "TGrid.h"
#include "TRandom.h"
#include <TH1F.h>
#include <cmath>
#include <cstdint>
#include <map>
#include <vector>

namespace o2
{

namespace
{
// Per-crossing counts of a sample of collisions, split into classes of colliding BCs
struct CrossingStats {
std::vector<double> slots; // colliding BCs passed over, per class
std::vector<double> occupied; // BCs with at least one collision, per class
std::vector<double> multi; // BCs with at least two collisions, per class
std::vector<double> colls; // collisions, per class
};

// Counts the collisions of a sampler per crossing; classOf maps the index of a colliding BC to its class
template <typename Sampler, typename ClassOf>
CrossingStats countCrossings(Sampler& sampler, int nColl, int nClasses, ClassOf classOf)
{
std::vector<o2::InteractionTimeRecord> samples;
samples.reserve(nColl);
sampler.generateCollisionTimes(samples);

const auto& filling = sampler.getBunchFilling();
std::vector<int> indexOfBC(o2::constants::lhc::LHCMaxBunches, -1);
int nColliding = 0;
for (int bc = 0; bc < o2::constants::lhc::LHCMaxBunches; ++bc) {
if (filling.testBC(bc)) {
indexOfBC[bc] = nColliding++;
}
}

std::map<int64_t, int> perCrossing; // key: global index of the colliding BC
int64_t prevKey = -1;
for (const auto& ir : samples) {
BOOST_REQUIRE(ir.bc < o2::constants::lhc::LHCMaxBunches && indexOfBC[ir.bc] >= 0); // only colliding BCs may be sampled
const int64_t key = int64_t(ir.orbit) * nColliding + indexOfBC[ir.bc];
BOOST_REQUIRE(key >= prevKey); // records come in time order
prevKey = key;
perCrossing[key]++;
}

CrossingStats st{std::vector<double>(nClasses), std::vector<double>(nClasses), std::vector<double>(nClasses), std::vector<double>(nClasses)};
const auto first = perCrossing.begin()->first;
const auto last = perCrossing.rbegin()->first;
for (auto key = first; key <= last; ++key) {
st.slots[classOf(int(key % nColliding))] += 1;
}
for (const auto& [key, n] : perCrossing) {
const int c = classOf(int(key % nColliding));
st.occupied[c] += 1;
st.multi[c] += (n > 1);
st.colls[c] += n;
}
return st;
}

// Poisson probability of at least two collisions in a crossing, given at least one
double poissonMultiGivenOccupied(double mu)
{
const double p0 = std::exp(-mu);
return (1. - p0 - mu * p0) / (1. - p0);
}
} // namespace

// Non-uniform sampler with uniform scales must reproduce Poisson statistics
BOOST_AUTO_TEST_CASE(NonUniformSamplerUniformScalesIsPoisson)
{
const int nColl = 500000;
auto allOneClass = [](int) { return 0; };

gRandom->SetSeed(12345);
o2::steer::NonUniformMuInteractionSampler sampler;
sampler.setMuPerBC(0.2);
sampler.init();
// the constructor already sets unit scales; setting them again also exercises the normalisation
BOOST_REQUIRE(sampler.setBCIntensityScales(std::vector<float>(sampler.getBunchFilling().getNBunches(), 1.f)));
auto st = countCrossings(sampler, nColl, 1, allOneClass);

const double mu = sampler.getMuPerBC();
const double expMulti = poissonMultiGivenOccupied(mu);
const double expOccupied = 1. - std::exp(-mu);
// relative statistical precision of the multi fraction is about 0.5% here, of the others well below that
BOOST_CHECK_CLOSE(st.multi[0] / st.occupied[0], expMulti, 5.);
BOOST_CHECK_CLOSE(st.occupied[0] / st.slots[0], expOccupied, 2.);
BOOST_CHECK_CLOSE(st.colls[0] / st.slots[0], mu, 2.);
}

// Non-uniform sampler must give each colliding BC the Poisson statistics of its own mu
BOOST_AUTO_TEST_CASE(NonUniformSamplerPerBCPoisson)
{
const int nColl = 500000;
const std::vector<double> scale{0.5, 1.5}; // even and odd colliding BCs
auto evenOdd = [](int idx) { return idx % 2; };

gRandom->SetSeed(54321);
o2::steer::NonUniformMuInteractionSampler sampler;
sampler.setMuPerBC(0.2);
sampler.init();
std::vector<float> scales(sampler.getBunchFilling().getNBunches());
for (size_t i = 0; i < scales.size(); ++i) {
scales[i] = scale[evenOdd(i)];
}
BOOST_REQUIRE(sampler.setBCIntensityScales(scales));
auto st = countCrossings(sampler, nColl, 2, evenOdd);

for (int c = 0; c < 2; ++c) {
const double muC = sampler.getMuPerBC() * sampler.getBCIntensityScales()[c];
BOOST_TEST_MESSAGE("class " << c << " mu " << muC << " multi/occupied " << st.multi[c] / st.occupied[c]
<< " expected " << poissonMultiGivenOccupied(muC));
BOOST_CHECK_CLOSE(st.multi[c] / st.occupied[c], poissonMultiGivenOccupied(muC), 7.);
BOOST_CHECK_CLOSE(st.occupied[c] / st.slots[c], 1. - std::exp(-muC), 2.);
BOOST_CHECK_CLOSE(st.colls[c] / st.slots[c], muC, 2.);
}
}

// Scales of the wrong length must be rejected and leave the previous scales untouched
BOOST_AUTO_TEST_CASE(NonUniformSamplerRejectsWrongScaleLength)
{
o2::steer::NonUniformMuInteractionSampler sampler;
sampler.setMuPerBC(0.2);
sampler.init();
const auto nBC = sampler.getBunchFilling().getNBunches();
BOOST_REQUIRE(sampler.setBCIntensityScales(std::vector<float>(nBC, 1.f)));
const auto before = sampler.getBCIntensityScales();
BOOST_CHECK(!sampler.setBCIntensityScales(std::vector<float>(nBC + 1, 2.f)));
BOOST_CHECK(sampler.getBCIntensityScales() == before);
}

BOOST_AUTO_TEST_CASE(NonUniformSampler)
{
auto run_number = 559827;
Expand Down