From 703ee0d86f13a7750c79c58b96666957c8914aa0 Mon Sep 17 00:00:00 2001 From: Sandro Wenzel Date: Mon, 23 Nov 2026 13:14:46 +0100 Subject: [PATCH] Apply the -t trigger to the Hyperloop hybrid expansion This fixes a problem in the Hyperloop expansion of an external generator, where a trigger given with -t was ignored. - Triggers from -t are only attached to non-hybrid generators, and the generated hybrid JSON had none. - An external trigger is now written into every sub-generator of the generated JSON. - With a particle trigger the plain external generator is kept, with a warning. - The hybrid generator now derives an empty trigger function name from the macro file name, as the plain path does. - A hybrid trigger that returns no trigger type is now fatal instead of being skipped. - A hybrid trigger mode without any trigger macro is now fatal. - A trigger given with -t to a user-configured hybrid generator now gives a warning. - A cocktail in parallel mode is now rejected, since it produced empty events and stalled. - A non-positive GeneratorHybrid.num_workers on Hyperloop now gives a warning. Co-Authored-By: Claude Opus 5.5 --- Generators/src/GeneratorFactory.cxx | 49 +++++++++++++++++++++++++---- Generators/src/GeneratorHybrid.cxx | 20 +++++++++--- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/Generators/src/GeneratorFactory.cxx b/Generators/src/GeneratorFactory.cxx index 4e6789726cc78..ae56a947a1cf4 100644 --- a/Generators/src/GeneratorFactory.cxx +++ b/Generators/src/GeneratorFactory.cxx @@ -68,7 +68,9 @@ constexpr int MaxHyperloopParallelGenerators = 8; int getHyperloopNParallelGenerators() { const int nWorkers = GeneratorHybridParam::Instance().num_workers; - if (nWorkers > MaxHyperloopParallelGenerators) { + if (nWorkers < 1) { + LOG(warning) << "GeneratorHybrid.num_workers = " << nWorkers << " is not positive: using 1"; + } else if (nWorkers > MaxHyperloopParallelGenerators) { LOG(warning) << "GeneratorHybrid.num_workers = " << nWorkers << " exceeds the maximum of " << MaxHyperloopParallelGenerators << " parallel sub-generators: capping to " << MaxHyperloopParallelGenerators; } @@ -83,7 +85,12 @@ int getHyperloopNParallelGenerators() // Note on GeneratorHybrid.num_workers: it controls both the number of TBB workers running the // sub-generators in parallel and nGenerators (see getHyperloopNParallelGenerators). // On Hyperloop it is part of the standard train configuration for MCGen datasets -std::string buildHyperloopExternalHybridConfig(GeneratorExternalParam const& extparams, int nGenerators) +// +// An external trigger (trgparams not null) is attached to every sub-generator. Each clone gets +// its own trigger object, but the macro is loaded once: its static state is shared between +// the worker threads, so trigger macros must be stateless. +std::string buildHyperloopExternalHybridConfig(GeneratorExternalParam const& extparams, int nGenerators, + TriggerExternalParam const* trgparams) { rapidjson::Document doc; doc.SetObject(); @@ -101,6 +108,18 @@ std::string buildHyperloopExternalHybridConfig(GeneratorExternalParam const& ext rapidjson::Value generator(rapidjson::kObjectType); generator.AddMember("name", "external", alloc); generator.AddMember("config", config, alloc); + if (trgparams) { + // same OR mode the factory sets when the trigger is attached to a plain generator + rapidjson::Value spec(rapidjson::kObjectType); + spec.AddMember("macro", rapidjson::Value(trgparams->fileName.c_str(), alloc), alloc); + spec.AddMember("function", rapidjson::Value(trgparams->funcName.c_str(), alloc), alloc); + rapidjson::Value specs(rapidjson::kArrayType); + specs.PushBack(spec, alloc); + rapidjson::Value triggers(rapidjson::kObjectType); + triggers.AddMember("mode", "or", alloc); + triggers.AddMember("specs", specs, alloc); + generator.AddMember("triggers", triggers, alloc); + } generators.PushBack(generator, alloc); fractions.PushBack(1, alloc); } @@ -166,6 +185,7 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair LOG(fatal) << "Monopoles were registered in TDatabasePDG with a mass other than G4.monopoleMass = " << monopoleMass << " GeV"; } auto genconfig = conf.getGenerator(); + bool triggerInHybridConfig = false; // the -t trigger was written into the Hyperloop hybrid configuration #if defined(GENERATORS_WITH_PYTHIA8) && defined(GENERATORS_WITH_HEPMC3) std::string hyperloopExtHybridConfigFile; // set when IS_HYPERLOOP is defined if (genconfig.compare("external") == 0 || genconfig.compare("extgen") == 0) { @@ -177,14 +197,27 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair // configuration into a parallel hybrid of clones (one per worker, capped to // MaxHyperloopParallelGenerators), to increase on-the-fly MC-generation throughput. // A single clone brings no parallelism, so the plain external generator is kept. + // Only an external trigger can be carried into the hybrid configuration; with any other + // trigger the plain external generator is kept, so that the trigger is still applied. + const auto trgconfig = conf.getTrigger(); const int nGenerators = getHyperloopNParallelGenerators(); - if (nGenerators > 1) { + if (nGenerators == 1) { + LOG(info) << "IS_HYPERLOOP detected: single worker requested, keeping the external generator as is"; + } else if (trgconfig.compare("particle") == 0) { + LOG(warning) << "IS_HYPERLOOP detected: the particle trigger cannot be set in a hybrid generator, " + << "keeping the external generator as is, without parallel generation"; + } else if (!trgconfig.empty() && trgconfig.compare("external") != 0) { + // left to the "Invalid trigger" check below + } else { + if (!trgconfig.empty() && TriggerExternalParam::Instance().fileName.empty()) { + LOG(fatal) << "External trigger requested but TriggerExternal.fileName is empty"; + } LOG(info) << "IS_HYPERLOOP detected: expanding external generator into " << nGenerators << " parallel hybrid sub-generators"; - hyperloopExtHybridConfigFile = buildHyperloopExternalHybridConfig(GeneratorExternalParam::Instance(), nGenerators); + auto trgparams = trgconfig.empty() ? nullptr : &TriggerExternalParam::Instance(); + hyperloopExtHybridConfigFile = buildHyperloopExternalHybridConfig(GeneratorExternalParam::Instance(), nGenerators, trgparams); + triggerInHybridConfig = trgparams != nullptr; genconfig = "hybrid"; - } else { - LOG(info) << "IS_HYPERLOOP detected: single worker requested, keeping the external generator as is"; } } } @@ -383,6 +416,10 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair Trigger trigger = nullptr; DeepTrigger deeptrigger = nullptr; + if (genconfig.compare("hybrid") == 0 && !conf.getTrigger().empty() && !triggerInHybridConfig) { + LOG(warning) << "Trigger '" << conf.getTrigger() << "' is not applied to the hybrid generator: " + << "triggers of a hybrid generator are set in its JSON configuration"; + } if (!(genconfig.compare("hybrid") == 0)) { auto trgconfig = conf.getTrigger(); if (trgconfig.empty()) { diff --git a/Generators/src/GeneratorHybrid.cxx b/Generators/src/GeneratorHybrid.cxx index cdebde104e4f2..c19dccf5eb6ec 100644 --- a/Generators/src/GeneratorHybrid.cxx +++ b/Generators/src/GeneratorHybrid.cxx @@ -251,8 +251,10 @@ Bool_t GeneratorHybrid::Init() LOG(info) << "Setting Trigger mode of generator " << gen << " to: " << mTriggerModes[count]; o2::eventgen::Trigger trigger = nullptr; o2::eventgen::DeepTrigger deeptrigger = nullptr; + int nTriggersSet = 0; for (int trg = 0; trg < mTriggerMacros[count].size(); trg++) { - if (mTriggerMacros[count][trg].empty() || mTriggerFuncs[count][trg].empty()) { + // an empty function name is derived from the macro file name by GetFromMacro + if (mTriggerMacros[count][trg].empty()) { continue; } std::string expandedMacro = o2::utils::expandShellVarsInFileName(mTriggerMacros[count][trg]); @@ -265,9 +267,8 @@ Bool_t GeneratorHybrid::Init() deeptrigger = o2::conf::GetFromMacro(expandedMacro, mTriggerFuncs[count][trg], "o2::eventgen::DeepTrigger", "deeptrigger"); } if (!trigger && !deeptrigger) { - LOG(warn) << "Failed to retrieve \'external trigger\': problem with configuration"; - LOG(warn) << "Trigger " << trg << " of generator " << gen << " will not be included"; - continue; + LOG(fatal) << "Failed to retrieve trigger " << trg << " of generator " << gen << ": problem with configuration"; + return false; } else { LOG(info) << "Trigger " << trg << " of generator " << gen << " successfully set"; } @@ -276,6 +277,11 @@ Bool_t GeneratorHybrid::Init() } else { gens[count]->addDeepTrigger(deeptrigger); } + ++nTriggersSet; + } + if (nTriggersSet == 0) { + LOG(fatal) << "Trigger mode set for generator " << gen << " but no trigger macro given"; + return false; } } count++; @@ -769,6 +775,12 @@ Bool_t GeneratorHybrid::parseJSON(const std::string& path) } } + // parallel mode takes each event from one shared queue and never collects the members of a cocktail + if (mGenerationMode == GenMode::kParallel && mCocktailMode) { + LOG(fatal) << "Cocktails cannot be used in parallel mode: use the sequential mode"; + return false; + } + // Get fractions and put them in mFractions if (doc.HasMember("fractions")) { const auto& fractions = doc["fractions"];