From 091c28a931768466239afbb34843a9a283baffa2 Mon Sep 17 00:00:00 2001 From: Anton Alkin Date: Thu, 8 Oct 2020 11:26:39 +0200 Subject: [PATCH 1/4] DPL Analysis: tutorial task demonstrating configurable-wrapped class --- Analysis/Tutorials/CMakeLists.txt | 13 +++++ Analysis/Tutorials/src/configurableCut.cxx | 33 +++++++++++ Analysis/Tutorials/src/configurableCut.json | 21 +++++++ .../Tutorials/src/configurableObjects.cxx | 58 +++++++++++++++++++ .../Core/include/Framework/Configurable.h | 52 ++++++++++++++--- Framework/Core/src/AnalysisManagers.h | 8 +-- 6 files changed, 173 insertions(+), 12 deletions(-) create mode 100644 Analysis/Tutorials/src/configurableCut.cxx create mode 100644 Analysis/Tutorials/src/configurableCut.json create mode 100644 Analysis/Tutorials/src/configurableObjects.cxx diff --git a/Analysis/Tutorials/CMakeLists.txt b/Analysis/Tutorials/CMakeLists.txt index f7d61f1d8818f..85d0182bf07fd 100644 --- a/Analysis/Tutorials/CMakeLists.txt +++ b/Analysis/Tutorials/CMakeLists.txt @@ -134,3 +134,16 @@ o2_add_dpl_workflow(histogram-registry SOURCES src/histogramRegistry.cxx PUBLIC_LINK_LIBRARIES O2::Framework O2::AnalysisCore O2::AnalysisDataModel COMPONENT_NAME AnalysisTutorial) + +o2_add_library(ConfigurableCut + SOURCES src/configurableCut.cxx + PUBLIC_LINK_LIBRARIES O2::AnalysisCore) + +o2_target_root_dictionary(ConfigurableCut + HEADERS include/Analysis/configurableCut.h + LINKDEF src/ConfigurableCutLinkDef.h) + +o2_add_dpl_workflow(configurable-objects + SOURCES src/configurableObjects.cxx + PUBLIC_LINK_LIBRARIES O2::Framework O2::ConfigurableCut + COMPONENT_NAME AnalysisTutorial) diff --git a/Analysis/Tutorials/src/configurableCut.cxx b/Analysis/Tutorials/src/configurableCut.cxx new file mode 100644 index 0000000000000..8745591b6b458 --- /dev/null +++ b/Analysis/Tutorials/src/configurableCut.cxx @@ -0,0 +1,33 @@ +#include "Analysis/configurableCut.h" +#include + +bool configurableCut::method(float arg) const +{ + return arg > cut; +} + +void configurableCut::setCut(float cut_) +{ + cut = cut_; +} + +float configurableCut::getCut() const +{ + return cut; +} + +std::ostream& operator<<(std::ostream& os, configurableCut const& c) +{ + os << "Cut value: " << c.getCut() << "; state: " << c.getState(); + return os; +} + +void configurableCut::setState(int state_) +{ + state = state_; +} + +int configurableCut::getState() const +{ + return state; +} diff --git a/Analysis/Tutorials/src/configurableCut.json b/Analysis/Tutorials/src/configurableCut.json new file mode 100644 index 0000000000000..33d4294b13286 --- /dev/null +++ b/Analysis/Tutorials/src/configurableCut.json @@ -0,0 +1,21 @@ +{ + "internal-dpl-clock": "", + "internal-dpl-aod-reader": { + "aod-file": "aod.root", + "time-limit": "0", + "start-value-enumeration": "0", + "end-value-enumeration": "-1", + "step-value-enumeration": "1" + }, + "internal-dpl-aod-spawner": "", + "configurable-demo": { + "cut": { + "cut": "0.1", + "state": "1" + }, + "mutable_cut": { + "cut": "0.2", + "state": "-1" + } + } +} diff --git a/Analysis/Tutorials/src/configurableObjects.cxx b/Analysis/Tutorials/src/configurableObjects.cxx new file mode 100644 index 0000000000000..5ae293436f981 --- /dev/null +++ b/Analysis/Tutorials/src/configurableObjects.cxx @@ -0,0 +1,58 @@ +// Copyright CERN and copyright holders of ALICE O2. This software is +// distributed under the terms of the GNU General Public License v3 (GPL +// Version 3), copied verbatim in the file "COPYING". +// +// See http://alice-o2.web.cern.ch/license for full licensing information. +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. +#include "Framework/runDataProcessing.h" +#include "Framework/AnalysisTask.h" +#include "Framework/AnalysisDataModel.h" +#include "Analysis/configurableCut.h" + +using namespace o2; +using namespace o2::framework; +using namespace o2::framework::expressions; + +/// This task demonstrates how to use configurable to wrap classes +/// use it with supplied configuration: "configurableObject.json" + +struct ConfigurableObjectDemo { + Configurable cut{"cut", {0.5, 1}, "generic cut"}; + MutableConfigurable mutable_cut{"mutable_cut", {1., 2}, "generic cut"}; + void init(InitContext const&){}; + void process(aod::Collision const&, aod::Tracks const& tracks) + { + LOGF(INFO, "Cut1: %.3f; Cut2: %.3f", cut, mutable_cut); + for (auto& track : tracks) { + if (track.globalIndex() % 500 == 0) { + std::string decision1; + std::string decision2; + if (cut->method(std::abs(track.eta()))) { + decision1 = "true"; + } else { + decision1 = "false"; + } + if (mutable_cut->method(std::abs(track.eta()))) { + decision2 = "true"; + } else { + decision2 = "false"; + } + LOGF(INFO, "Cut1: %s; Cut2: %s", decision1, decision2); + if (decision2 == "false") { + mutable_cut->setState(-1); + } else { + mutable_cut->setState(1); + } + } + } + } +}; + +WorkflowSpec defineDataProcessing(ConfigContext const&) +{ + return WorkflowSpec{ + adaptAnalysisTask("configurable-demo")}; +} diff --git a/Framework/Core/include/Framework/Configurable.h b/Framework/Core/include/Framework/Configurable.h index 8e6917cc596dd..118e7afa99d93 100644 --- a/Framework/Core/include/Framework/Configurable.h +++ b/Framework/Core/include/Framework/Configurable.h @@ -12,29 +12,65 @@ #include namespace o2::framework { -/// This helper allows you to create a configurable option associated to a task. -/// Internally it will be bound to a ConfigParamSpec. template -struct Configurable { - Configurable(std::string const& name, T defaultValue, std::string const& help) - : name(name), value(defaultValue), help(help) +struct ConfigurableBase { + ConfigurableBase(std::string const& name, T&& defaultValue, std::string const& help) + : name(name), value{std::move(defaultValue)}, help(help) { } using type = T; std::string name; T value; std::string help; +}; + +template +struct ConfigurablePolicyConst : ConfigurableBase { + ConfigurablePolicyConst(std::string const& name, T&& defaultValue, std::string const& help) + : ConfigurableBase{name, std::forward(defaultValue), help} + { + } operator T() { - return value; + return this->value; } T const* operator->() const { - return &value; + return &this->value; + } +}; + +template +struct ConfigurablePolicyMutable : ConfigurableBase { + ConfigurablePolicyMutable(std::string const& name, T&& defaultValue, std::string const& help) + : ConfigurableBase{name, std::forward(defaultValue), help} + { + } + operator T() + { + return this->value; + } + T* operator->() + { + return &this->value; + } +}; + +/// This helper allows you to create a configurable option associated to a task. +/// Internally it will be bound to a ConfigParamSpec. +template > +struct Configurable : IP { + Configurable(std::string const& name, T&& defaultValue, std::string const& help) + : IP{name, std::forward(defaultValue), help} + { } }; + template -std::ostream& operator<<(std::ostream& os, Configurable const& c) +using MutableConfigurable = Configurable>; + +template +std::ostream& operator<<(std::ostream& os, Configurable const& c) { os << c.value; return os; diff --git a/Framework/Core/src/AnalysisManagers.h b/Framework/Core/src/AnalysisManagers.h index bba95b523034e..82aa5c52e7a18 100644 --- a/Framework/Core/src/AnalysisManagers.h +++ b/Framework/Core/src/AnalysisManagers.h @@ -355,9 +355,9 @@ struct OptionManager { } }; -template -struct OptionManager> { - static bool appendOption(std::vector& options, Configurable& what) +template +struct OptionManager> { + static bool appendOption(std::vector& options, Configurable& what) { if constexpr (variant_trait_v::type> != VariantType::Unknown) { options.emplace_back(ConfigParamSpec{what.name, variant_trait_v::type>, what.value, {what.help}}); @@ -368,7 +368,7 @@ struct OptionManager> { return true; } - static bool prepare(InitContext& context, Configurable& what) + static bool prepare(InitContext& context, Configurable& what) { if constexpr (variant_trait_v::type> != VariantType::Unknown) { what.value = context.options().get(what.name.c_str()); From 90da1d742d32b8a67d33c4e041595ec0e9c034c5 Mon Sep 17 00:00:00 2001 From: Anton Alkin Date: Thu, 8 Oct 2020 15:08:23 +0200 Subject: [PATCH 2/4] add missing include --- .../include/Analysis/configurableCut.h | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Analysis/Tutorials/include/Analysis/configurableCut.h diff --git a/Analysis/Tutorials/include/Analysis/configurableCut.h b/Analysis/Tutorials/include/Analysis/configurableCut.h new file mode 100644 index 0000000000000..84737a67fb2dc --- /dev/null +++ b/Analysis/Tutorials/include/Analysis/configurableCut.h @@ -0,0 +1,42 @@ +// Copyright CERN and copyright holders of ALICE O2. This software is +// distributed under the terms of the GNU General Public License v3 (GPL +// Version 3), copied verbatim in the file "COPYING". +// +// See http://alice-o2.web.cern.ch/license for full licensing information. +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#ifndef CONFIGURABLECUT_H +#define CONFIGURABLECUT_H + +#include +#include + +class configurableCut +{ + public: + configurableCut(float cut_ = 2., int state_ = 1) + : cut{cut_}, state{state_} + { + } + + bool method(float arg) const; + + void setCut(float cut_); + float getCut() const; + + void setState(int state_); + int getState() const; + + private: + float cut; + int state; + + ClassDef(configurableCut, 2); +}; + +std::ostream& operator<<(std::ostream& os, configurableCut const& c); + +#endif // CONFIGURABLECUT_H From 03ad2db878ae50746380ceec34bb1849f2891c10 Mon Sep 17 00:00:00 2001 From: Anton Alkin Date: Thu, 8 Oct 2020 17:06:16 +0200 Subject: [PATCH 3/4] add missing linkdef --- Analysis/Tutorials/src/ConfigurableCutLinkDef.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Analysis/Tutorials/src/ConfigurableCutLinkDef.h diff --git a/Analysis/Tutorials/src/ConfigurableCutLinkDef.h b/Analysis/Tutorials/src/ConfigurableCutLinkDef.h new file mode 100644 index 0000000000000..0760e732fb45f --- /dev/null +++ b/Analysis/Tutorials/src/ConfigurableCutLinkDef.h @@ -0,0 +1,15 @@ +// Copyright CERN and copyright holders of ALICE O2. This software is +// distributed under the terms of the GNU General Public License v3 (GPL +// Version 3), copied verbatim in the file "COPYING". +// +// See http://alice-o2.web.cern.ch/license for full licensing information. +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#pragma link off all globals; +#pragma link off all classes; +#pragma link off all functions; + +#pragma link C++ class configurableCut + ; From bfbdb9a99fa67400dde2a7016e80e6b030a1f2d3 Mon Sep 17 00:00:00 2001 From: Anton Alkin Date: Fri, 9 Oct 2020 15:32:09 +0200 Subject: [PATCH 4/4] added missing COPYRIGHT lines --- Analysis/Tutorials/src/configurableCut.cxx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Analysis/Tutorials/src/configurableCut.cxx b/Analysis/Tutorials/src/configurableCut.cxx index 8745591b6b458..fabca8db4b8cf 100644 --- a/Analysis/Tutorials/src/configurableCut.cxx +++ b/Analysis/Tutorials/src/configurableCut.cxx @@ -1,3 +1,13 @@ +// Copyright CERN and copyright holders of ALICE O2. This software is +// distributed under the terms of the GNU General Public License v3 (GPL +// Version 3), copied verbatim in the file "COPYING". +// +// See http://alice-o2.web.cern.ch/license for full licensing information. +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + #include "Analysis/configurableCut.h" #include