From 8ea0f6c514c7d16f0946a665cf47a5f19aa5dfa7 Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Sat, 21 Nov 2026 12:47:30 +0100 Subject: [PATCH 1/2] Add Poisson unit tests for NonUniformMuInteractionSampler This adds two unit tests for the non-uniform interaction sampler that need no GRID access. - With uniform scales, the fraction of occupied crossings with two or more collisions must match Poisson. - With alternating scales, each colliding BC must follow Poisson at its own mu. - Both tests fail without the BC jump fix from https://github.com/AliceO2Group/AliceO2/pull/16158, with 26 % instead of 9.7 % multi-collision crossings at mu = 0.2. https://its.cern.ch/jira/browse/O2-7330 Co-Authored-By: Claude Opus 5.5 --- .../test/testInteractionSampler.cxx | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/DataFormats/simulation/test/testInteractionSampler.cxx b/DataFormats/simulation/test/testInteractionSampler.cxx index b1b3691884ccf..11928c58edb5c 100644 --- a/DataFormats/simulation/test/testInteractionSampler.cxx +++ b/DataFormats/simulation/test/testInteractionSampler.cxx @@ -20,11 +20,127 @@ #include "DataFormatsParameters/GRPLHCIFData.h" #include "TFile.h" #include "TGrid.h" +#include "TRandom.h" #include +#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 + 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)]; + } + 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.); + } +} + BOOST_AUTO_TEST_CASE(NonUniformSampler) { auto run_number = 559827; From da78b6a02179c4308bdbffe58a97753f71f88517 Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Sat, 21 Nov 2026 13:08:00 +0100 Subject: [PATCH 2/2] Fix return value and size check of setBCIntensityScales This fixes NonUniformMuInteractionSampler::setBCIntensityScales and adds a unit test. - It returned false also on success. - On a size mismatch it overwrote the scales before reporting that it did nothing. - It now checks the size first and returns true on success. Co-Authored-By: Claude Opus 5.5 --- .../simulation/src/InteractionSampler.cxx | 5 ++--- .../simulation/test/testInteractionSampler.cxx | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) 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(sampler.getBunchFilling().getNBunches(), 1.f)); + BOOST_REQUIRE(sampler.setBCIntensityScales(std::vector(sampler.getBunchFilling().getNBunches(), 1.f))); auto st = countCrossings(sampler, nColl, 1, allOneClass); const double mu = sampler.getMuPerBC(); @@ -128,7 +128,7 @@ BOOST_AUTO_TEST_CASE(NonUniformSamplerPerBCPoisson) for (size_t i = 0; i < scales.size(); ++i) { scales[i] = scale[evenOdd(i)]; } - sampler.setBCIntensityScales(scales); + BOOST_REQUIRE(sampler.setBCIntensityScales(scales)); auto st = countCrossings(sampler, nColl, 2, evenOdd); for (int c = 0; c < 2; ++c) { @@ -141,6 +141,19 @@ BOOST_AUTO_TEST_CASE(NonUniformSamplerPerBCPoisson) } } +// 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;