From 54db571cb763e3989ecfb287df0b4f827f496e10 Mon Sep 17 00:00:00 2001 From: Anton Alkin Date: Mon, 10 Feb 2025 11:07:44 +0100 Subject: [PATCH 1/2] DPL Analysis: avoid leaking analysis task abstractions --- Generators/include/Generators/AODToHepMC.h | 120 ++++----------------- run/o2aod_mc_to_hepmc.cxx | 28 ++++- 2 files changed, 48 insertions(+), 100 deletions(-) diff --git a/Generators/include/Generators/AODToHepMC.h b/Generators/include/Generators/AODToHepMC.h index 5c9fd69408050..426f9c223410b 100644 --- a/Generators/include/Generators/AODToHepMC.h +++ b/Generators/include/Generators/AODToHepMC.h @@ -257,25 +257,20 @@ struct AODToHepMC { * framework::OptionManager that propagates the options * to the program. */ - struct : framework::ConfigurableGroup { + struct { /** Option for dumping HepMC event structures to disk. Takes one * argument - the name of the file to write to. */ - framework::Configurable dump{"hepmc-dump", "", - "Dump HepMC event to output"}; + std::string dump{""}; /** Option for only storing particles from the event generator. * Note, if a particle is stored down, then its mothers will also * be stored. */ - framework::Configurable onlyGen{"hepmc-only-generated", false, - "Only export generated"}; + bool onlyGen{false}; /** Use HepMC's tree parsing for building event structure */ - framework::Configurable useTree{"hepmc-use-tree", false, - "Export as tree"}; + bool useTree{false}; /** Floating point precision used when writing to disk */ - framework::Configurable precision{"hepmc-precision", 8, - "Export precision in dump"}; + int precision{8}; /** Recenter event at IP=(0,0,0,0). */ - framework::Configurable recenter{"hepmc-recenter", false, - "Recenter the events at (0,0,0,0)"}; + bool recenter{false}; } configs; /** * @{ @@ -585,99 +580,26 @@ struct AODToHepMC { namespace framework { -/** - * This specialisation of o2::framework::OutputManager ensures that - * we can call the post-processing routine of o2::eventgen::AODToHepMC - * and thus ensure that the possible HepMC is written to disk. - * - * The O2 framework (via o2::framework::adoptAnalysisTask) inspects - * the members of the passed class (@c T) and creates - * o2::framework::OutputManager callbacks for every member. The - * default template for this does nothing. - * - * Thus, to delegate a call to a member of the analysis task (of class - * @c T), we can specialise the @c o2::framework::OutputManager - * template on the @e member type. We will then effectively have - * call-backs for - * - * - @c appendOutput - when the task is constructed - * - @c prepare - when a new set of data is recieved - * - @c finalize - when a set of data has been processed - * - @c postRun - when the run is over - * - * Concretely, we use the @c postRun to flush the HepMC data file - * to disk. - * - * For this to work, the AODToHepMC object must be a member of the - * "Task" class, e.g., - * - * @code - * struct Task { - * o2::eventgen::AODToHepMC mConverter; - * ... - * }; - * - * WorkflowSpec defineDataProcessing(ConfigContext const& cfg) { - * return WorkflowSpec{adaptAnalysisTask(cfg)}; - * } - * @endcode - */ -template <> -struct OutputManager { - /** Type of the target */ - using Target = eventgen::AODToHepMC; - /** Called when task is constructed */ - static bool appendOutput(std::vector&, Target&, uint32_t) { return true; } - /** Called when new data is received */ - static bool prepare(ProcessingContext&, Target&) { return true; } - /** Called when all data has been received */ - static bool postRun(EndOfStreamContext&, Target& t) { return t.postRun(); } - /** Called when the job finishes */ - static bool finalize(ProcessingContext&, Target& t) { return true; } -}; - -/** - * Spacialisation to pull in configurables from the converter. - * - * Ideally, the converter should simply derive from ConfigurableGroup - * and all should flow automatically, but that doesn't work for some - * reason. - * - * For this to work, the AODToHepMC object must be a member of the - * "Task" class, e.g., - * - * @code - * struct Task { - * o2::eventgen::AODToHepMC mConverter; - * ... - * }; - * - * WorkflowSpec defineDataProcessing(ConfigContext const& cfg) { - * return WorkflowSpec{adaptAnalysisTask(cfg)}; - * } - * @endcode - */ -template <> -struct OptionManager { - /** type of the target */ - using Target = eventgen::AODToHepMC; - /** Called when the task is constructed */ - static bool - appendOption(std::vector& options, - Target& target) +struct AODToHepMCPostRun { + static AODToHepMCPostRun& instance() { - OptionManager::appendOption(options, target.configs); - return true; + static AODToHepMCPostRun inst{}; + return inst; } - /** Called when options are processed */ - static bool - prepare(o2::framework::InitContext& ic, Target& target) + + AODToHepMCPostRun(eventgen::AODToHepMC* ptr_ = nullptr) + : ptr{ptr_} { - OptionManager::prepare(ic, target.configs); - return true; } -}; + void endOfStream() { + if (ptr != nullptr) { + ptr->postRun(); + } + } + + eventgen::AODToHepMC* ptr = nullptr; +}; } // namespace framework } // namespace o2 diff --git a/run/o2aod_mc_to_hepmc.cxx b/run/o2aod_mc_to_hepmc.cxx index 8827a38c7ff72..73bf5b6475a22 100644 --- a/run/o2aod_mc_to_hepmc.cxx +++ b/run/o2aod_mc_to_hepmc.cxx @@ -49,9 +49,33 @@ struct AodToHepmc { /** Alias the converter type */ using Converter = o2::eventgen::AODToHepMC; + struct : o2::framework::ConfigurableGroup { + /** Option for dumping HepMC event structures to disk. Takes one + * argument - the name of the file to write to. */ + o2::framework::Configurable dump{"hepmc-dump", "", + "Dump HepMC event to output"}; + /** Option for only storing particles from the event generator. + * Note, if a particle is stored down, then its mothers will also + * be stored. */ + o2::framework::Configurable onlyGen{"hepmc-only-generated", false, + "Only export generated"}; + /** Use HepMC's tree parsing for building event structure */ + o2::framework::Configurable useTree{"hepmc-use-tree", false, + "Export as tree"}; + /** Floating point precision used when writing to disk */ + o2::framework::Configurable precision{"hepmc-precision", 8, + "Export precision in dump"}; + /** Recenter event at IP=(0,0,0,0). */ + o2::framework::Configurable recenter{"hepmc-recenter", false, + "Recenter the events at (0,0,0,0)"}; + } configs; + /** Our converter */ Converter mConverter; + /** Post-run trigger service **/ + o2::framework::Service trigger; + /** @{ * @name Container types */ /** Alias converter header table type */ @@ -75,9 +99,11 @@ struct AodToHepmc { /** @} */ /** Initialize the job */ - void init(o2::framework::InitContext& ic) + void init(o2::framework::InitContext&) { + mConverter.configs = {(std::string)configs.dump, (bool)configs.onlyGen, (bool)configs.useTree, (int)configs.precision, (bool)configs.recenter}; mConverter.init(); + trigger->ptr = &mConverter; } /** Processing of event to extract extra HepMC information * From e1dda44eba08723485997b095f29e2a4943b1bfd Mon Sep 17 00:00:00 2001 From: ALICE Action Bot Date: Mon, 10 Feb 2025 10:41:03 +0000 Subject: [PATCH 2/2] Please consider the following formatting changes --- Generators/include/Generators/AODToHepMC.h | 3 ++- run/o2aod_mc_to_hepmc.cxx | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Generators/include/Generators/AODToHepMC.h b/Generators/include/Generators/AODToHepMC.h index 426f9c223410b..aef738455d9ad 100644 --- a/Generators/include/Generators/AODToHepMC.h +++ b/Generators/include/Generators/AODToHepMC.h @@ -592,7 +592,8 @@ struct AODToHepMCPostRun { { } - void endOfStream() { + void endOfStream() + { if (ptr != nullptr) { ptr->postRun(); } diff --git a/run/o2aod_mc_to_hepmc.cxx b/run/o2aod_mc_to_hepmc.cxx index 73bf5b6475a22..230e3247821cf 100644 --- a/run/o2aod_mc_to_hepmc.cxx +++ b/run/o2aod_mc_to_hepmc.cxx @@ -53,21 +53,21 @@ struct AodToHepmc { /** Option for dumping HepMC event structures to disk. Takes one * argument - the name of the file to write to. */ o2::framework::Configurable dump{"hepmc-dump", "", - "Dump HepMC event to output"}; + "Dump HepMC event to output"}; /** Option for only storing particles from the event generator. * Note, if a particle is stored down, then its mothers will also * be stored. */ o2::framework::Configurable onlyGen{"hepmc-only-generated", false, - "Only export generated"}; + "Only export generated"}; /** Use HepMC's tree parsing for building event structure */ o2::framework::Configurable useTree{"hepmc-use-tree", false, - "Export as tree"}; + "Export as tree"}; /** Floating point precision used when writing to disk */ o2::framework::Configurable precision{"hepmc-precision", 8, - "Export precision in dump"}; + "Export precision in dump"}; /** Recenter event at IP=(0,0,0,0). */ o2::framework::Configurable recenter{"hepmc-recenter", false, - "Recenter the events at (0,0,0,0)"}; + "Recenter the events at (0,0,0,0)"}; } configs; /** Our converter */