Skip to content

Commit e4d778e

Browse files
committed
[Common] Group mutable matching settings in shared state
1 parent 9418094 commit e4d778e

1 file changed

Lines changed: 24 additions & 28 deletions

File tree

‎Common/Core/MftMchMatching.h‎

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,15 +1601,16 @@ struct MftMchJointTransport {
16011601

16021602
// Bernoulli thinning is applied to final published training pairs, AFTER MC
16031603
// eligibility and donor-group normalization. Both matching planes share a draw.
1604-
inline double& mixedPairSaveFraction()
1605-
{
1606-
static double value = 1.;
1607-
return value;
1608-
}
1609-
inline uint64_t& mixedPairSamplingSeed()
1604+
struct MatchingProcessSettings {
1605+
double mixedPairSaveFraction = 1.;
1606+
uint64_t mixedPairSamplingSeed = 0;
1607+
// Public audit tables remain empty in sparse mode; internal transport stays active.
1608+
bool sparseAuditStorage = false;
1609+
};
1610+
inline MatchingProcessSettings& matchingProcessSettings()
16101611
{
1611-
static uint64_t value = 0;
1612-
return value;
1612+
static MatchingProcessSettings settings{};
1613+
return settings;
16131614
}
16141615
struct MixedPairSampler {
16151616
using Key = std::tuple<uint64_t, uint64_t, uint64_t, int64_t, int64_t, int64_t, int>;
@@ -1619,7 +1620,7 @@ struct MixedPairSampler {
16191620
MixedPairSampler()
16201621
{
16211622
std::random_device entropy;
1622-
const auto seed = mixedPairSamplingSeed() != 0 ? mixedPairSamplingSeed() : (static_cast<uint64_t>(entropy()) << 32) ^ static_cast<uint64_t>(entropy());
1623+
const auto seed = matchingProcessSettings().mixedPairSamplingSeed != 0 ? matchingProcessSettings().mixedPairSamplingSeed : (static_cast<uint64_t>(entropy()) << 32) ^ static_cast<uint64_t>(entropy());
16231624
random.seed(seed);
16241625
}
16251626
template <typename Row>
@@ -1630,7 +1631,7 @@ struct MixedPairSampler {
16301631
if (found != decisions.end()) {
16311632
return found->second;
16321633
}
1633-
const bool selected = mixedPairSaveFraction() >= 1. || (mixedPairSaveFraction() > 0. && std::bernoulli_distribution(mixedPairSaveFraction())(random));
1634+
const bool selected = matchingProcessSettings().mixedPairSaveFraction >= 1. || (matchingProcessSettings().mixedPairSaveFraction > 0. && std::bernoulli_distribution(matchingProcessSettings().mixedPairSaveFraction)(random));
16341635
decisions.emplace(key, selected);
16351636
++attempted;
16361637
saved += selected;
@@ -1639,7 +1640,7 @@ struct MixedPairSampler {
16391640
[[nodiscard]] std::string metadata() const
16401641
{
16411642
std::ostringstream text;
1642-
text << "{\"save_fraction\":" << mixedPairSaveFraction() << ",\"sampling_seed\":" << mixedPairSamplingSeed()
1643+
text << "{\"save_fraction\":" << matchingProcessSettings().mixedPairSaveFraction << ",\"sampling_seed\":" << matchingProcessSettings().mixedPairSamplingSeed
16431644
<< ",\"attempted_pairs\":" << attempted << ",\"saved_pairs\":" << saved
16441645
<< ",\"mixing_weight\":\"original donor normalization; divide by save_fraction for unbiased total counts\"}";
16451646
return text.str();
@@ -1668,11 +1669,6 @@ using MatchingOutcomes = std::map<int64_t, std::array<MatchingOutcome, 2>>;
16681669

16691670
// Public audit cursors stay empty in sparse mode. Internal transport carries
16701671
// the same rows to MC/sparse devices, independently of a pipeline's AOD writer.
1671-
inline bool& sparseAuditStorage()
1672-
{
1673-
static bool value = false;
1674-
return value;
1675-
}
16761672
struct MatchingTreeStorage {
16771673
bool mcTruth = true;
16781674
bool candidateTruth = true;
@@ -1694,15 +1690,15 @@ inline bool useInternalAuditStorage()
16941690
// Mixed labels are training rows, retained in every output mode by default.
16951691
return !(matchingTreeStorage().mcTruth && matchingTreeStorage().mixedTruth);
16961692
} else if constexpr (std::is_same_v<T, o2::aod::MatchingMchPopulation>) {
1697-
return sparseAuditStorage() || !matchingTreeStorage().mchPopulation;
1693+
return matchingProcessSettings().sparseAuditStorage || !matchingTreeStorage().mchPopulation;
16981694
} else if constexpr (std::is_same_v<T, o2::aod::MatchingCandidateTruths>) {
1699-
return sparseAuditStorage() || !(matchingTreeStorage().mcTruth && matchingTreeStorage().candidateTruth);
1695+
return matchingProcessSettings().sparseAuditStorage || !(matchingTreeStorage().mcTruth && matchingTreeStorage().candidateTruth);
17001696
} else if constexpr (std::is_same_v<T, o2::aod::MatchingCandidateRelations>) {
1701-
return sparseAuditStorage() || !(matchingTreeStorage().mcTruth && matchingTreeStorage().candidateRelations);
1697+
return matchingProcessSettings().sparseAuditStorage || !(matchingTreeStorage().mcTruth && matchingTreeStorage().candidateRelations);
17021698
} else if constexpr (std::is_same_v<T, o2::aod::MatchingMchTruths>) {
1703-
return sparseAuditStorage() || !(matchingTreeStorage().mcTruth && matchingTreeStorage().mchTruth);
1699+
return matchingProcessSettings().sparseAuditStorage || !(matchingTreeStorage().mcTruth && matchingTreeStorage().mchTruth);
17041700
}
1705-
return sparseAuditStorage();
1701+
return matchingProcessSettings().sparseAuditStorage;
17061702
}
17071703
template <typename T>
17081704
struct MatchingInternalTable;
@@ -5928,8 +5924,8 @@ inline std::string matchingOutputMode(o2::framework::ConfigContext const& contex
59285924
{
59295925
auto mode = context.options().get<std::string>("matching-output-mode");
59305926
std::string configuredMode = "tree";
5931-
mixedPairSaveFraction() = 1.;
5932-
mixedPairSamplingSeed() = 0;
5927+
matchingProcessSettings().mixedPairSaveFraction = 1.;
5928+
matchingProcessSettings().mixedPairSamplingSeed = 0;
59335929
matchingTreeStorage() = {};
59345930
for (int i = 1; i < context.argc(); ++i) {
59355931
std::string arg = context.argv()[i], source;
@@ -5942,8 +5938,8 @@ inline std::string matchingOutputMode(o2::framework::ConfigContext const& contex
59425938
boost::property_tree::ptree config;
59435939
boost::property_tree::read_json(source.substr(7), config);
59445940
configuredMode = config.get<std::string>("matching-output.cfgOutputMode", "tree");
5945-
mixedPairSaveFraction() = config.get<double>("matching-output.cfgMixedPairSaveFraction", 1.);
5946-
mixedPairSamplingSeed() = config.get<uint64_t>("matching-output.cfgMixedPairSamplingSeed", 0);
5941+
matchingProcessSettings().mixedPairSaveFraction = config.get<double>("matching-output.cfgMixedPairSaveFraction", 1.);
5942+
matchingProcessSettings().mixedPairSamplingSeed = config.get<uint64_t>("matching-output.cfgMixedPairSamplingSeed", 0);
59475943
matchingTreeStorage().mcTruth = config.get<bool>("matching-output.cfgSaveMcTruthTables", true);
59485944
matchingTreeStorage().candidateTruth = config.get<bool>("matching-output.cfgSaveCandidateTruthTable", true);
59495945
matchingTreeStorage().candidateRelations = config.get<bool>("matching-output.cfgSaveCandidateRelationsTable", true);
@@ -5960,7 +5956,7 @@ inline std::string matchingOutputMode(o2::framework::ConfigContext const& contex
59605956
if (mode != "tree" && mode != "sparse" && mode != "both") {
59615957
LOGF(fatal, "Matching output mode must be tree, sparse or both");
59625958
}
5963-
if (!std::isfinite(mixedPairSaveFraction()) || mixedPairSaveFraction() < 0. || mixedPairSaveFraction() > 1.) {
5959+
if (!std::isfinite(matchingProcessSettings().mixedPairSaveFraction) || matchingProcessSettings().mixedPairSaveFraction < 0. || matchingProcessSettings().mixedPairSaveFraction > 1.) {
59645960
LOGF(fatal, "cfgMixedPairSaveFraction must be finite and between 0 and 1");
59655961
}
59665962
return mode;
@@ -6265,12 +6261,12 @@ template <bool Mc = false>
62656261
inline o2::framework::WorkflowSpec makeMatchingWorkflow(o2::framework::ConfigContext const& cfgc, bool modernDefaults = false)
62666262
{
62676263
const auto mode = matchingOutputMode(cfgc);
6268-
sparseAuditStorage() = mode == "sparse";
6264+
matchingProcessSettings().sparseAuditStorage = mode == "sparse";
62696265
o2::framework::WorkflowSpec workflow{o2::framework::adaptAnalysisTask<MftMchMatchingTask>(cfgc, o2::framework::TaskName{"match_mft_mch_data_mc"},
62706266
modernDefaults ? o2::framework::SetDefaultProcesses{{{"processLegacy", false}, {"processMatchingPlane", true}}} : o2::framework::SetDefaultProcesses{}),
62716267
o2::framework::adaptAnalysisTask<MchDimuonMassAtPv>(cfgc, o2::framework::TaskName{"mch-dimuon-mass-at-pv"}),
62726268
o2::framework::adaptAnalysisTask<MlMatchedDimuonMassAtPv>(cfgc, o2::framework::TaskName{"ml-matched-dimuon-mass-at-pv"}),
6273-
(sparseAuditStorage() ? o2::framework::adaptAnalysisTask<MchJpsiMatchFlags<true>>(cfgc, o2::framework::TaskName{"mch-jpsi-match-flags"}) : o2::framework::adaptAnalysisTask<MchJpsiMatchFlags<false>>(cfgc, o2::framework::TaskName{"mch-jpsi-match-flags"}))};
6269+
(matchingProcessSettings().sparseAuditStorage ? o2::framework::adaptAnalysisTask<MchJpsiMatchFlags<true>>(cfgc, o2::framework::TaskName{"mch-jpsi-match-flags"}) : o2::framework::adaptAnalysisTask<MchJpsiMatchFlags<false>>(cfgc, o2::framework::TaskName{"mch-jpsi-match-flags"}))};
62746270
if (!modernDefaults) {
62756271
workflow.push_back(o2::framework::adaptAnalysisTask<MixedMatchPlanePublisher>(cfgc, o2::framework::TaskName{"mixed-match-plane-publisher"}));
62766272
}

0 commit comments

Comments
 (0)