Skip to content

Commit a986dd4

Browse files
HistogramRegistry: add support for all root histogram types (#4616)
- store histograms in std::variant to support arbitrary root types - add fill() and fillWeight() functions to the registry - support filling by value and filling from filtered table with variable number of arguments depending on histogram dimensions
1 parent b7bb4e9 commit a986dd4

File tree

6 files changed

+572
-247
lines changed

6 files changed

+572
-247
lines changed

Analysis/Tutorials/src/histogramRegistry.cxx

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ struct ATask {
2727
"registry",
2828
true,
2929
{
30-
{"eta", "#eta", {HistogramType::kTH1F, {{102, -2.01, 2.01}}}}, //
31-
{"phi", "#varphi", {HistogramType::kTH1F, {{100, 0., 2. * M_PI}}}} //
32-
} //
30+
{"eta", "#eta", {HistType::kTH1F, {{102, -2.01, 2.01}}}}, //
31+
{"phi", "#varphi", {HistType::kTH1F, {{100, 0., 2. * M_PI}}}} //
32+
} //
3333
};
3434

3535
void process(aod::Tracks const& tracks)
3636
{
3737
for (auto& track : tracks) {
38-
registry.get("eta")->Fill(track.eta());
39-
registry.get("phi")->Fill(track.phi());
38+
registry.get<TH1>("eta")->Fill(track.eta());
39+
registry.get<TH1>("phi")->Fill(track.phi());
4040
}
4141
}
4242
};
@@ -47,9 +47,9 @@ struct BTask {
4747
"registry",
4848
true,
4949
{
50-
{"eta", "#eta", {HistogramType::kTH1F, {{102, -2.01, 2.01}}}}, //
51-
{"ptToPt", "#ptToPt", {HistogramType::kTH2F, {{100, -0.01, 10.01}, {100, -0.01, 10.01}}}} //
52-
} //
50+
{"eta", "#eta", {HistType::kTH1F, {{102, -2.01, 2.01}}}}, //
51+
{"ptToPt", "#ptToPt", {HistType::kTH2F, {{100, -0.01, 10.01}, {100, -0.01, 10.01}}}} //
52+
} //
5353
};
5454

5555
void process(aod::Tracks const& tracks)
@@ -59,9 +59,128 @@ struct BTask {
5959
}
6060
};
6161

62+
struct CTask {
63+
64+
HistogramRegistry registry{
65+
"registry",
66+
true,
67+
{
68+
{"1d", "test 1d", {HistType::kTH1I, {{100, -10.0f, 10.0f}}}}, //
69+
{"2d", "test 2d", {HistType::kTH2F, {{100, -10.0f, 10.01f}, {100, -10.0f, 10.01f}}}}, //
70+
{"3d", "test 3d", {HistType::kTH3D, {{100, -10.0f, 10.01f}, {100, -10.0f, 10.01f}, {100, -10.0f, 10.01f}}}}, //
71+
{"4d", "test 4d", {HistType::kTHnC, {{100, -10.0f, 10.01f}, {100, -10.0f, 10.01f}, {100, -10.0f, 10.01f}, {100, -10.0f, 10.01f}}}}, //
72+
{"5d", "test 5d", {HistType::kTHnSparseL, {{10, -10.0f, 10.01f}, {10, -10.0f, 10.01f}, {10, -10.0f, 10.01f}, {10, -10.0f, 10.01f}, {10, -10.0f, 10.01f}}}}, //
73+
} //
74+
};
75+
76+
void init(o2::framework::InitContext&)
77+
{
78+
registry.add({"7d", "test 7d", {HistType::kTHnC, {{3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}}}});
79+
80+
registry.add({"6d", "test 6d", {HistType::kTHnC, {{3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}, {3, -10.0f, 10.01f}}}});
81+
82+
registry.add({"1d-profile", "test 1d profile", {HistType::kTProfile, {{20, 0.0f, 10.01f}}}});
83+
registry.add({"2d-profile", "test 2d profile", {HistType::kTProfile2D, {{20, 0.0f, 10.01f}, {20, 0.0f, 10.01f}}}});
84+
registry.add({"3d-profile", "test 3d profile", {HistType::kTProfile3D, {{20, 0.0f, 10.01f}, {20, 0.0f, 10.01f}, {20, 0.0f, 10.01f}}}});
85+
86+
registry.add({"2d-weight", "test 2d weight", {HistType::kTH2C, {{2, -10.0f, 10.01f}, {2, -10.0f, 10.01f}}}, true});
87+
88+
registry.add({"3d-weight", "test 3d weight", {HistType::kTH3C, {{2, -10.0f, 10.01f}, {2, -10.0f, 10.01f}, {2, -10.0f, 10.01f}}}, true});
89+
90+
registry.add({"4d-weight", "test 4d weight", {HistType::kTHnC, {{2, -10.0f, 10.01f}, {2, -10.0f, 10.01f}, {2, -10.0f, 10.01f}, {100, -10.0f, 10.01f}}}, true});
91+
92+
registry.add({"1d-profile-weight", "test 1d profile weight", {HistType::kTProfile, {{2, -10.0f, 10.01f}}}, true});
93+
registry.add({"2d-profile-weight", "test 2d profile weight", {HistType::kTProfile2D, {{2, -10.0f, 10.01f}, {2, -10.0f, 10.01f}}}, true});
94+
}
95+
96+
void process(aod::Tracks const& tracks)
97+
{
98+
using namespace aod::track;
99+
// does not work with dynamic columns (e.g. Charge, NormalizedPhi)
100+
registry.fill<Eta>("1d", tracks, eta > -0.7f);
101+
registry.fill<Pt, Eta, RawPhi>("3d", tracks, eta > 0.f);
102+
registry.fill<Pt, Eta, RawPhi, P, X>("5d", tracks, pt > 0.15f);
103+
registry.fill<Pt, Eta, RawPhi, P, X, Y, Z>("7d", tracks, pt > 0.15f);
104+
registry.fill<Pt, Eta, RawPhi>("2d-profile", tracks, eta > -0.5f);
105+
106+
// fill 4d histogram with weight (column X)
107+
registry.fillWeight<Pt, Eta, RawPhi, Z, X>("4d-weight", tracks, eta > 0.f);
108+
109+
registry.fillWeight<Pt, Eta, RawPhi>("2d-weight", tracks, eta > 0.f);
110+
111+
registry.fillWeight<Pt, Eta, RawPhi>("1d-profile-weight", tracks, eta > 0.f);
112+
113+
for (auto& track : tracks) {
114+
registry.fill("2d", track.eta(), track.pt());
115+
registry.fill("4d", track.pt(), track.eta(), track.phi(), track.signed1Pt());
116+
registry.fill("6d", track.pt(), track.eta(), track.phi(), track.snp(), track.tgl(), track.alpha());
117+
registry.fill("1d-profile", track.pt(), track.eta());
118+
registry.fill("3d-profile", track.pt(), track.eta(), track.phi(), track.snp());
119+
120+
// fill 3d histogram with weight (2.)
121+
registry.fillWeight("3d-weight", track.pt(), track.eta(), track.phi(), 2.);
122+
123+
registry.fillWeight("2d-profile-weight", track.pt(), track.eta(), track.phi(), 5.);
124+
}
125+
}
126+
};
127+
128+
struct DTask {
129+
HistogramRegistry spectra{"spectra", true, {}};
130+
HistogramRegistry etaStudy{"etaStudy", true, {}};
131+
132+
void init(o2::framework::InitContext&)
133+
{
134+
std::vector<double> ptBinning = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
135+
1.1, 1.2, 1.3, 1.4, 1.5, 2.0, 5.0, 10.0, 20.0, 50.0};
136+
std::vector<double> centBinning = {0., 30., 60., 90.};
137+
138+
AxisSpec ptAxis = {ptBinning, "#it{p}_{T} (GeV/c)"};
139+
AxisSpec centAxis = {centBinning, "centrality"};
140+
AxisSpec etaAxis = {5, -0.8, 0.8, "#eta"};
141+
AxisSpec phiAxis = {4, 0., 2. * M_PI, "#phi"};
142+
const int nCuts = 5;
143+
AxisSpec cutAxis = {nCuts, -0.5, nCuts - 0.5, "cut setting"};
144+
145+
HistogramConfigSpec defaultParticleHist({HistType::kTHnF, {ptAxis, etaAxis, centAxis, cutAxis}});
146+
147+
spectra.add({"myControlHist", "a", {HistType::kTH2F, {ptAxis, etaAxis}}});
148+
spectra.get<TH2>("myControlHist")->GetYaxis()->SetTitle("my-y-axis");
149+
spectra.get<TH2>("myControlHist")->SetTitle("something meaningful");
150+
151+
spectra.add({"pions", "Pions", defaultParticleHist});
152+
spectra.add({"kaons", "Kaons", defaultParticleHist});
153+
spectra.add({"sigmas", "Sigmas", defaultParticleHist});
154+
spectra.add({"lambdas", "Lambd", defaultParticleHist});
155+
156+
spectra.get<THn>("lambdas")->SetTitle("Lambdas");
157+
158+
etaStudy.add({"positive", "A side spectra", {HistType::kTH1I, {ptAxis}}});
159+
etaStudy.add({"negative", "C side spectra", {HistType::kTH1I, {ptAxis}}});
160+
}
161+
162+
void process(aod::Tracks const& tracks)
163+
{
164+
using namespace aod::track;
165+
166+
etaStudy.fill<Pt>("positive", tracks, eta > 0.f);
167+
etaStudy.fill<Pt>("negative", tracks, eta < 0.f);
168+
169+
for (auto& track : tracks) {
170+
spectra.fill("myControlHist", track.pt(), track.eta());
171+
spectra.fill("pions", track.pt(), track.eta(), 50., 0.);
172+
spectra.fill("kaons", track.pt(), track.eta(), 50., 0.);
173+
spectra.fill("sigmas", track.pt(), track.eta(), 50., 0.);
174+
spectra.fill("lambdas", track.pt(), track.eta(), 50., 0.);
175+
}
176+
}
177+
};
178+
62179
WorkflowSpec defineDataProcessing(ConfigContext const&)
63180
{
64181
return WorkflowSpec{
65182
adaptAnalysisTask<ATask>("eta-and-phi-histograms"),
66-
adaptAnalysisTask<BTask>("filtered-histograms")};
183+
adaptAnalysisTask<BTask>("filtered-histograms"),
184+
adaptAnalysisTask<CTask>("dimension-test"),
185+
adaptAnalysisTask<DTask>("realistic-example")};
67186
}

Framework/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ o2_add_library(Framework
103103
src/WorkflowSpec.cxx
104104
src/runDataProcessing.cxx
105105
src/ExternalFairMQDeviceProxy.cxx
106+
src/HistogramRegistry.cxx
106107
test/TestClasses.cxx
107108
PRIVATE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_LIST_DIR}/src
108109
PUBLIC_LINK_LIBRARIES ${DEBUGGUI_TARGET}

0 commit comments

Comments
 (0)