Skip to content

Commit aaf8bd3

Browse files
authored
DPL Analysis: tutorial task demonstrating configurable-wrapped class (#4537)
1 parent 7954810 commit aaf8bd3

8 files changed

Lines changed: 240 additions & 12 deletions

File tree

Analysis/Tutorials/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ o2_add_dpl_workflow(histogram-registry
135135
PUBLIC_LINK_LIBRARIES O2::Framework O2::AnalysisCore O2::AnalysisDataModel
136136
COMPONENT_NAME AnalysisTutorial)
137137

138+
o2_add_library(ConfigurableCut
139+
SOURCES src/configurableCut.cxx
140+
PUBLIC_LINK_LIBRARIES O2::AnalysisCore)
141+
142+
o2_target_root_dictionary(ConfigurableCut
143+
HEADERS include/Analysis/configurableCut.h
144+
LINKDEF src/ConfigurableCutLinkDef.h)
145+
146+
o2_add_dpl_workflow(configurable-objects
147+
SOURCES src/configurableObjects.cxx
148+
PUBLIC_LINK_LIBRARIES O2::Framework O2::ConfigurableCut
149+
COMPONENT_NAME AnalysisTutorial)
150+
138151
o2_add_dpl_workflow(muon-iteration
139152
SOURCES src/muonIteration.cxx
140153
PUBLIC_LINK_LIBRARIES O2::Framework O2::AnalysisCore O2::AnalysisDataModel
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#ifndef CONFIGURABLECUT_H
12+
#define CONFIGURABLECUT_H
13+
14+
#include <iosfwd>
15+
#include <TMath.h>
16+
17+
class configurableCut
18+
{
19+
public:
20+
configurableCut(float cut_ = 2., int state_ = 1)
21+
: cut{cut_}, state{state_}
22+
{
23+
}
24+
25+
bool method(float arg) const;
26+
27+
void setCut(float cut_);
28+
float getCut() const;
29+
30+
void setState(int state_);
31+
int getState() const;
32+
33+
private:
34+
float cut;
35+
int state;
36+
37+
ClassDef(configurableCut, 2);
38+
};
39+
40+
std::ostream& operator<<(std::ostream& os, configurableCut const& c);
41+
42+
#endif // CONFIGURABLECUT_H
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#pragma link off all globals;
12+
#pragma link off all classes;
13+
#pragma link off all functions;
14+
15+
#pragma link C++ class configurableCut + ;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#include "Analysis/configurableCut.h"
12+
#include <iostream>
13+
14+
bool configurableCut::method(float arg) const
15+
{
16+
return arg > cut;
17+
}
18+
19+
void configurableCut::setCut(float cut_)
20+
{
21+
cut = cut_;
22+
}
23+
24+
float configurableCut::getCut() const
25+
{
26+
return cut;
27+
}
28+
29+
std::ostream& operator<<(std::ostream& os, configurableCut const& c)
30+
{
31+
os << "Cut value: " << c.getCut() << "; state: " << c.getState();
32+
return os;
33+
}
34+
35+
void configurableCut::setState(int state_)
36+
{
37+
state = state_;
38+
}
39+
40+
int configurableCut::getState() const
41+
{
42+
return state;
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"internal-dpl-clock": "",
3+
"internal-dpl-aod-reader": {
4+
"aod-file": "aod.root",
5+
"time-limit": "0",
6+
"start-value-enumeration": "0",
7+
"end-value-enumeration": "-1",
8+
"step-value-enumeration": "1"
9+
},
10+
"internal-dpl-aod-spawner": "",
11+
"configurable-demo": {
12+
"cut": {
13+
"cut": "0.1",
14+
"state": "1"
15+
},
16+
"mutable_cut": {
17+
"cut": "0.2",
18+
"state": "-1"
19+
}
20+
}
21+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
#include "Framework/runDataProcessing.h"
11+
#include "Framework/AnalysisTask.h"
12+
#include "Framework/AnalysisDataModel.h"
13+
#include "Analysis/configurableCut.h"
14+
15+
using namespace o2;
16+
using namespace o2::framework;
17+
using namespace o2::framework::expressions;
18+
19+
/// This task demonstrates how to use configurable to wrap classes
20+
/// use it with supplied configuration: "configurableObject.json"
21+
22+
struct ConfigurableObjectDemo {
23+
Configurable<configurableCut> cut{"cut", {0.5, 1}, "generic cut"};
24+
MutableConfigurable<configurableCut> mutable_cut{"mutable_cut", {1., 2}, "generic cut"};
25+
void init(InitContext const&){};
26+
void process(aod::Collision const&, aod::Tracks const& tracks)
27+
{
28+
LOGF(INFO, "Cut1: %.3f; Cut2: %.3f", cut, mutable_cut);
29+
for (auto& track : tracks) {
30+
if (track.globalIndex() % 500 == 0) {
31+
std::string decision1;
32+
std::string decision2;
33+
if (cut->method(std::abs(track.eta()))) {
34+
decision1 = "true";
35+
} else {
36+
decision1 = "false";
37+
}
38+
if (mutable_cut->method(std::abs(track.eta()))) {
39+
decision2 = "true";
40+
} else {
41+
decision2 = "false";
42+
}
43+
LOGF(INFO, "Cut1: %s; Cut2: %s", decision1, decision2);
44+
if (decision2 == "false") {
45+
mutable_cut->setState(-1);
46+
} else {
47+
mutable_cut->setState(1);
48+
}
49+
}
50+
}
51+
}
52+
};
53+
54+
WorkflowSpec defineDataProcessing(ConfigContext const&)
55+
{
56+
return WorkflowSpec{
57+
adaptAnalysisTask<ConfigurableObjectDemo>("configurable-demo")};
58+
}

Framework/Core/include/Framework/Configurable.h

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,65 @@
1212
#include <string>
1313
namespace o2::framework
1414
{
15-
/// This helper allows you to create a configurable option associated to a task.
16-
/// Internally it will be bound to a ConfigParamSpec.
1715
template <typename T>
18-
struct Configurable {
19-
Configurable(std::string const& name, T defaultValue, std::string const& help)
20-
: name(name), value(defaultValue), help(help)
16+
struct ConfigurableBase {
17+
ConfigurableBase(std::string const& name, T&& defaultValue, std::string const& help)
18+
: name(name), value{std::move(defaultValue)}, help(help)
2119
{
2220
}
2321
using type = T;
2422
std::string name;
2523
T value;
2624
std::string help;
25+
};
26+
27+
template <typename T>
28+
struct ConfigurablePolicyConst : ConfigurableBase<T> {
29+
ConfigurablePolicyConst(std::string const& name, T&& defaultValue, std::string const& help)
30+
: ConfigurableBase<T>{name, std::forward<T>(defaultValue), help}
31+
{
32+
}
2733
operator T()
2834
{
29-
return value;
35+
return this->value;
3036
}
3137
T const* operator->() const
3238
{
33-
return &value;
39+
return &this->value;
40+
}
41+
};
42+
43+
template <typename T>
44+
struct ConfigurablePolicyMutable : ConfigurableBase<T> {
45+
ConfigurablePolicyMutable(std::string const& name, T&& defaultValue, std::string const& help)
46+
: ConfigurableBase<T>{name, std::forward<T>(defaultValue), help}
47+
{
48+
}
49+
operator T()
50+
{
51+
return this->value;
52+
}
53+
T* operator->()
54+
{
55+
return &this->value;
56+
}
57+
};
58+
59+
/// This helper allows you to create a configurable option associated to a task.
60+
/// Internally it will be bound to a ConfigParamSpec.
61+
template <typename T, typename IP = ConfigurablePolicyConst<T>>
62+
struct Configurable : IP {
63+
Configurable(std::string const& name, T&& defaultValue, std::string const& help)
64+
: IP{name, std::forward<T>(defaultValue), help}
65+
{
3466
}
3567
};
68+
3669
template <typename T>
37-
std::ostream& operator<<(std::ostream& os, Configurable<T> const& c)
70+
using MutableConfigurable = Configurable<T, ConfigurablePolicyMutable<T>>;
71+
72+
template <typename T, typename IP>
73+
std::ostream& operator<<(std::ostream& os, Configurable<T, IP> const& c)
3874
{
3975
os << c.value;
4076
return os;

Framework/Core/src/AnalysisManagers.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ struct OptionManager {
355355
}
356356
};
357357

358-
template <typename T>
359-
struct OptionManager<Configurable<T>> {
360-
static bool appendOption(std::vector<ConfigParamSpec>& options, Configurable<T>& what)
358+
template <typename T, typename IP>
359+
struct OptionManager<Configurable<T, IP>> {
360+
static bool appendOption(std::vector<ConfigParamSpec>& options, Configurable<T, IP>& what)
361361
{
362362
if constexpr (variant_trait_v<typename std::decay<T>::type> != VariantType::Unknown) {
363363
options.emplace_back(ConfigParamSpec{what.name, variant_trait_v<typename std::decay<T>::type>, what.value, {what.help}});
@@ -368,7 +368,7 @@ struct OptionManager<Configurable<T>> {
368368
return true;
369369
}
370370

371-
static bool prepare(InitContext& context, Configurable<T>& what)
371+
static bool prepare(InitContext& context, Configurable<T, IP>& what)
372372
{
373373
if constexpr (variant_trait_v<typename std::decay<T>::type> != VariantType::Unknown) {
374374
what.value = context.options().get<T>(what.name.c_str());

0 commit comments

Comments
 (0)