diff --git a/DataFormats/simulation/src/InteractionSampler.cxx b/DataFormats/simulation/src/InteractionSampler.cxx index 809a97f4c48a6..b690c63052647 100644 --- a/DataFormats/simulation/src/InteractionSampler.cxx +++ b/DataFormats/simulation/src/InteractionSampler.cxx @@ -168,12 +168,11 @@ bool NonUniformMuInteractionSampler::setBCIntensityScales(const std::vector +#include +#include +#include +#include namespace o2 { +namespace +{ +// Per-crossing counts of a sample of collisions, split into classes of colliding BCs +struct CrossingStats { + std::vector slots; // colliding BCs passed over, per class + std::vector occupied; // BCs with at least one collision, per class + std::vector multi; // BCs with at least two collisions, per class + std::vector colls; // collisions, per class +}; + +// Counts the collisions of a sampler per crossing; classOf maps the index of a colliding BC to its class +template +CrossingStats countCrossings(Sampler& sampler, int nColl, int nClasses, ClassOf classOf) +{ + std::vector samples; + samples.reserve(nColl); + sampler.generateCollisionTimes(samples); + + const auto& filling = sampler.getBunchFilling(); + std::vector 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 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(nClasses), std::vector(nClasses), std::vector(nClasses), std::vector(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(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 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 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(nBC, 1.f))); + const auto before = sampler.getBCIntensityScales(); + BOOST_CHECK(!sampler.setBCIntensityScales(std::vector(nBC + 1, 2.f))); + BOOST_CHECK(sampler.getBCIntensityScales() == before); +} + BOOST_AUTO_TEST_CASE(NonUniformSampler) { auto run_number = 559827;