From c43a17491f25601c4ce1eaa9f9be75d1b9c72d8c Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Fri, 20 Sep 2024 21:51:16 +0200 Subject: [PATCH 1/6] Fix histograms for Gaudi v40 and add a configurable histogram --- k4FWCore/scripts/k4run | 13 ++++++++----- .../options/ExampleFunctionalTransformerHist.py | 8 ++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/k4FWCore/scripts/k4run b/k4FWCore/scripts/k4run index 12d24df2..1526e61f 100755 --- a/k4FWCore/scripts/k4run +++ b/k4FWCore/scripts/k4run @@ -60,11 +60,9 @@ def add_arguments(parser, app_mgr): # skip public tools and the applicationmgr itself if "ToolSvc" in conf.name() or "ApplicationMgr" in conf.name(): continue - props = ( - conf.getPropertiesWithDescription() - ) # dict propertyname: (propertyvalue, propertydescription) + # dict propertyname: (propertyvalue, propertydescription) + props = conf.getPropertiesWithDescription() for prop in props: - # only add arguments for relevant properties if ( prop in FILTER_GAUDI_PROPS or "Audit" in prop @@ -208,7 +206,12 @@ def main(): opts_dict = vars(opts) for optionName, propTuple in option_db.items(): logger.info("Option name: %s %s %s", propTuple[1], optionName, opts_dict[optionName]) - propTuple[0].setProp(propTuple[1].rsplit(".", 1)[1], opts_dict[optionName]) + if "_Axis" in optionName: + propTuple[0].setProp( + propTuple[1].rsplit(".", 1)[1], tuple(x for x in opts_dict[optionName] if x != ()) + ) + else: + propTuple[0].setProp(propTuple[1].rsplit(".", 1)[1], opts_dict[optionName]) if opts.verbose: from Gaudi.Configuration import VERBOSE diff --git a/test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py b/test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py index c81e249d..334ffab8 100644 --- a/test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py +++ b/test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py @@ -73,6 +73,14 @@ root_hist_svc = RootHistoSink("RootHistoSink") root_hist_svc.FileName = "functional_transformer_hist.root" +try: + transformer1.CustomHistogram_Title = "Custom Title" + # Bins can be defined here + transformer1.CustomHistogram_Axis0 = (10, -5.0, 10.0, "X") +# Before Gaudi v40 there isn't a way to set the bins from python +except Exception: + pass + app = ApplicationMgr( TopAlg=[producer1, producer2, transformer1, transformer2], From 94d72921673c863701db9015e5068ea9b31b36a2 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Fri, 20 Sep 2024 22:02:47 +0200 Subject: [PATCH 2/6] Add missing changes --- k4FWCore/scripts/k4run | 3 ++ .../ExampleFunctionalTransformerHist.cpp | 29 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/k4FWCore/scripts/k4run b/k4FWCore/scripts/k4run index 1526e61f..0b40d716 100755 --- a/k4FWCore/scripts/k4run +++ b/k4FWCore/scripts/k4run @@ -206,6 +206,9 @@ def main(): opts_dict = vars(opts) for optionName, propTuple in option_db.items(): logger.info("Option name: %s %s %s", propTuple[1], optionName, opts_dict[optionName]) + # After Gaudi v40 the new configurable histograms have properties that are tuples + # and by default one of the member is an empty tuple that Gaudi seems not to like + # when used in setProp - it will try to parse it as a string and fail if "_Axis" in optionName: propTuple[0].setProp( propTuple[1].rsplit(".", 1)[1], tuple(x for x in opts_dict[optionName] if x != ()) diff --git a/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp b/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp index 881640f5..68fe7044 100644 --- a/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp +++ b/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp @@ -25,8 +25,22 @@ #include +#include "GAUDI_VERSION.h" + +#if GAUDI_MAJOR_VERSION < 40 +namespace Gaudi::Accumulators { + using StaticRootHistogram = Gaudi::Accumulators::RootHistogram; +} +#endif + struct ExampleFunctionalTransformerHist final : k4FWCore::Transformer { + StatusCode initialize() override { +#if GAUDI_MAJOR_VERSION >= 40 + m_customHistogram.createHistogram(*this); +#endif + return StatusCode::SUCCESS; + } // The pairs in KeyValues can be changed from python and they correspond // to the name of the input and output collections respectively ExampleFunctionalTransformerHist(const std::string& name, ISvcLocator* svcLoc) @@ -36,14 +50,25 @@ struct ExampleFunctionalTransformerHist final // This is the function that will be called to produce the data edm4hep::MCParticleCollection operator()(const edm4hep::MCParticleCollection& input) const override { // Fill the histogram with the energy of one particle - ++m_histograms[input[0 + !m_firstParticle.value()].getEnergy()]; + ++m_histogram[input[0 + !m_firstParticle.value()].getEnergy()]; +#if GAUDI_MAJOR_VERSION >= 40 + ++m_customHistogram[input[0 + !m_firstParticle.value()].getEnergy()]; +#endif // Return an empty collection since we don't care about the collection return {}; } private: // This is the histogram that will be filled, 1 is the number of dimensions of the histogram (1D) - mutable Gaudi::Accumulators::RootHistogram<1> m_histograms{this, "Histogram Name", "Histogram Title", {100, 0, 10.}}; + mutable Gaudi::Accumulators::StaticRootHistogram<1> m_histogram{ + this, "Histogram Name", "Histogram Title", {100, 0, 10.}}; + +public: +#if GAUDI_MAJOR_VERSION >= 40 + // This is a histogram with title, name and bins that can be set from python + void registerCallBack(Gaudi::StateMachine::Transition, std::function) {} + mutable Gaudi::Accumulators::RootHistogram<1> m_customHistogram{this, "CustomHistogram"}; +#endif Gaudi::Property m_firstParticle{this, "FirstParticle", true}; }; From f7d2a3cadd6065cd9160538de527771dd8494fef Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Fri, 20 Sep 2024 22:34:26 +0200 Subject: [PATCH 3/6] Change version to 39 --- .../src/components/ExampleFunctionalTransformerHist.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp b/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp index 68fe7044..5af1d7fc 100644 --- a/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp +++ b/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp @@ -27,7 +27,7 @@ #include "GAUDI_VERSION.h" -#if GAUDI_MAJOR_VERSION < 40 +#if GAUDI_MAJOR_VERSION < 39 namespace Gaudi::Accumulators { using StaticRootHistogram = Gaudi::Accumulators::RootHistogram; } @@ -36,7 +36,7 @@ namespace Gaudi::Accumulators { struct ExampleFunctionalTransformerHist final : k4FWCore::Transformer { StatusCode initialize() override { -#if GAUDI_MAJOR_VERSION >= 40 +#if GAUDI_MAJOR_VERSION >= 39 m_customHistogram.createHistogram(*this); #endif return StatusCode::SUCCESS; @@ -51,7 +51,7 @@ struct ExampleFunctionalTransformerHist final edm4hep::MCParticleCollection operator()(const edm4hep::MCParticleCollection& input) const override { // Fill the histogram with the energy of one particle ++m_histogram[input[0 + !m_firstParticle.value()].getEnergy()]; -#if GAUDI_MAJOR_VERSION >= 40 +#if GAUDI_MAJOR_VERSION >= 39 ++m_customHistogram[input[0 + !m_firstParticle.value()].getEnergy()]; #endif // Return an empty collection since we don't care about the collection @@ -64,7 +64,7 @@ struct ExampleFunctionalTransformerHist final this, "Histogram Name", "Histogram Title", {100, 0, 10.}}; public: -#if GAUDI_MAJOR_VERSION >= 40 +#if GAUDI_MAJOR_VERSION >= 39 // This is a histogram with title, name and bins that can be set from python void registerCallBack(Gaudi::StateMachine::Transition, std::function) {} mutable Gaudi::Accumulators::RootHistogram<1> m_customHistogram{this, "CustomHistogram"}; From 92954349eea682d0cb546e5882c13767bea2ff48 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Sun, 22 Sep 2024 18:28:00 +0200 Subject: [PATCH 4/6] Change v40 to v39 --- k4FWCore/scripts/k4run | 2 +- test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/k4FWCore/scripts/k4run b/k4FWCore/scripts/k4run index 0b40d716..6ea509ad 100755 --- a/k4FWCore/scripts/k4run +++ b/k4FWCore/scripts/k4run @@ -206,7 +206,7 @@ def main(): opts_dict = vars(opts) for optionName, propTuple in option_db.items(): logger.info("Option name: %s %s %s", propTuple[1], optionName, opts_dict[optionName]) - # After Gaudi v40 the new configurable histograms have properties that are tuples + # After Gaudi v39 the new configurable histograms have properties that are tuples # and by default one of the member is an empty tuple that Gaudi seems not to like # when used in setProp - it will try to parse it as a string and fail if "_Axis" in optionName: diff --git a/test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py b/test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py index 334ffab8..5e92c9cd 100644 --- a/test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py +++ b/test/k4FWCoreTest/options/ExampleFunctionalTransformerHist.py @@ -77,7 +77,7 @@ transformer1.CustomHistogram_Title = "Custom Title" # Bins can be defined here transformer1.CustomHistogram_Axis0 = (10, -5.0, 10.0, "X") -# Before Gaudi v40 there isn't a way to set the bins from python +# Before Gaudi v39 there isn't a way to set the bins from python except Exception: pass From 088ef4acde2f004f12f4c9ad5e1710fb12cdb997 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Tue, 24 Sep 2024 11:25:11 +0200 Subject: [PATCH 5/6] Fix alias --- .../src/components/ExampleFunctionalTransformerHist.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp b/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp index 5af1d7fc..d21eeb0a 100644 --- a/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp +++ b/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp @@ -29,7 +29,9 @@ #if GAUDI_MAJOR_VERSION < 39 namespace Gaudi::Accumulators { - using StaticRootHistogram = Gaudi::Accumulators::RootHistogram; + template + using StaticRootHistogram = + Gaudi::Accumulators::RootHistogramingCounterBase; } #endif From 684050e28ffdceb50af7a9f80ecfe2d812e9550a Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Tue, 24 Sep 2024 11:26:10 +0200 Subject: [PATCH 6/6] Add a comment about the callback --- .../src/components/ExampleFunctionalTransformerHist.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp b/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp index d21eeb0a..37bd0c8a 100644 --- a/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp +++ b/test/k4FWCoreTest/src/components/ExampleFunctionalTransformerHist.cpp @@ -68,6 +68,7 @@ struct ExampleFunctionalTransformerHist final public: #if GAUDI_MAJOR_VERSION >= 39 // This is a histogram with title, name and bins that can be set from python + // The callback function is only needed for these histograms with configurable properties void registerCallBack(Gaudi::StateMachine::Transition, std::function) {} mutable Gaudi::Accumulators::RootHistogram<1> m_customHistogram{this, "CustomHistogram"}; #endif