The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
HistogramsTests.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 2021-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, 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\***********************************************************************************/
13#include <GaudiKernel/AlgTool.h>
14#include <type_traits>
15
17 enum class Category { Simple, Complex, Bad, Wrong };
18 std::ostream& operator<<( std::ostream& o, Category v ) {
19 switch ( v ) {
21 o << "Simple";
22 break;
24 o << "Complex";
25 break;
26 case Category::Bad:
27 o << "Bad";
28 break;
29 case Category::Wrong:
30 o << "Wrong";
31 break;
32 }
33 return o;
34 }
35} // namespace Gaudi::Tests::Histograms::CustomAxis
36
37namespace Gaudi::Accumulators {
38 struct CustomAxis {
41
42 CustomAxis() = default;
44 unsigned int numBins() const { return 4; }
46 std::underlying_type_t<Category> minValue = 0, maxValue = 4;
48 std::string title{ "Category" };
50 std::vector<std::string> labels{ "Simple", "Complex", "Bad", "Wrong" };
51
52 unsigned int index( Category value ) const { return static_cast<unsigned int>( value ) + 1; }
53 friend std::ostream& operator<<( std::ostream& o, CustomAxis const& axis ) {
54 return o << axis.numBins() << " " << axis.minValue << " " << axis.maxValue;
55 }
56 };
57 void to_json( nlohmann::json& j, const CustomAxis& axis ) {
58 j = nlohmann::json{ { "nBins", axis.numBins() },
59 { "minValue", axis.minValue },
60 { "maxValue", axis.maxValue },
61 { "title", axis.title } };
62 j["labels"] = axis.labels;
63 }
64} // namespace Gaudi::Accumulators
65
67 namespace Directories {
69
71 using AlgTool::AlgTool;
72
73 mutable MyHist_t m_hist0{ this, "Top", "Top title", { 1, 0, 1 } };
74 mutable MyHist_t m_hist1{ this, "Group/First", "First title", { 1, 0, 1 } };
75 mutable MyHist_t m_hist2{ this, "Group/Second", "Second title", { 1, 0, 1 } };
76 mutable MyHist_t m_hist3{ this, "Group/SubGroup/Third", "Third title", { 1, 0, 1 } };
77
78 void fillHistos() const {
79 ++m_hist0[0.5];
80 ++m_hist1[0.5];
81 ++m_hist2[0.5];
82 ++m_hist3[0.5];
83 }
84 };
85 DECLARE_COMPONENT( HistoGroupsTool )
86
87 struct HistoGroupsAlg : Gaudi::Functional::Consumer<void()> {
89 using Base::Base;
90
91 mutable MyHist_t m_hist0{ this, "Top", "Top title", { 1, 0, 1 } };
92 mutable MyHist_t m_hist1{ this, "Group/First", "First title", { 1, 0, 1 } };
93 mutable MyHist_t m_hist2{ this, "Group/Second", "Second title", { 1, 0, 1 } };
94 mutable MyHist_t m_hist3{ this, "Group/SubGroup/Third", "Third title", { 1, 0, 1 } };
95
96 ToolHandle<HistoGroupsTool> m_tool{ this, "Tool", "Gaudi::Tests::Histograms::Directories::HistoGroupsTool/Tool" };
97
98 void operator()() const override {
99 ++m_hist0[0.5];
100 ++m_hist1[0.5];
101 ++m_hist2[0.5];
102 ++m_hist3[0.5];
103 m_tool->fillHistos();
104 }
105 };
106 DECLARE_COMPONENT( HistoGroupsAlg )
107 } // namespace Directories
108 namespace AxesLabels {
112
113 using Base::Base;
114
116 this, "hist", "Histogram title", { 5, 0, 5, "axis title", { "a", "b", "c", "d", "e" } } };
117
118 void operator()() const override {
119 for ( int i : { 1, 2, 3, 4, 5 } ) m_hist[i - 1] += i;
120 }
121 };
122 DECLARE_COMPONENT( HistWithLabelsAlg )
123 } // namespace AxesLabels
124 namespace CustomAxis {
127 using Base::Base;
128
130 std::tuple<Gaudi::Accumulators::CustomAxis>>
131 m_hist{ this, "Categories", "", Gaudi::Accumulators::CustomAxis{} };
132
133 void operator()() const override {
136 m_hist[Category::Bad] += 3;
138 }
139 };
140 DECLARE_COMPONENT( EnumAxisAlg )
141 } // namespace CustomAxis
142 namespace MultiDimLayout {
143 // Simple algorithm used to check https://gitlab.cern.ch/gaudi/Gaudi/-/issues/212
146 using Base::Base;
147
148 mutable Gaudi::Accumulators::StaticHistogram<1> m_h1{ this, "h1", "", { 10, 0, 10 } };
149 mutable Gaudi::Accumulators::StaticHistogram<2> m_h2{ this, "h2", "", { 10, 0, 10 }, { 10, 0, 10 } };
151 { 10, 0, 10 }, { 10, 0, 10 }, { 10, 0, 10 } };
152
153 void operator()() const override {
154 int value = 0;
155 // fill 1, 2 and 3 dimensional histograms with different values in each bin
156 for ( double x = -0.5; x < 11; x += 1.0 ) {
157 m_h1[x] += ++value;
158 for ( double y = -0.5; y < 11; y += 1.0 ) {
159 m_h2[{ x, y }] += ++value;
160 for ( double z = -0.5; z < 11; z += 1.0 ) { m_h3[{ x, y, z }] += ++value; }
161 }
162 }
163 }
164 };
166 } // namespace MultiDimLayout
167} // namespace Gaudi::Tests::Histograms
#define DECLARE_COMPONENT(type)
AlgTool(std::string type, std::string name, const IInterface *parent)
Standard Constructor.
Definition AlgTool.cpp:95
Handle to be used in lieu of naked pointers to tools.
Definition ToolHandle.h:132
Efficient counter implementations for Gaudi.
void to_json(nlohmann::json &j, const Axis< Arithmetic > &axis)
automatic conversion of the Axis type to json
HistogramingCounterBase< ND, Atomicity, Arithmetic, naming::histogramString, HistogramingAccumulator, AxisTupleType > StaticHistogram
standard static histograming counter. See HistogramingCounterBase for details
details::Consumer< Signature, Traits_, details::isLegacy< Traits_ > > Consumer
Definition Consumer.h:69
std::ostream & operator<<(std::ostream &o, Category v)
Gaudi::Accumulators::StaticHistogram< 1 > MyHist_t
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
std::underlying_type_t< Category > minValue
min and max values on this axis
std::underlying_type_t< Category > maxValue
unsigned int index(Category value) const
friend std::ostream & operator<<(std::ostream &o, CustomAxis const &axis)
std::string title
title of this axis
std::vector< std::string > labels
labels for the bins
Gaudi::Tests::Histograms::CustomAxis::Category Category
unsigned int numBins() const
number of bins for this Axis
Gaudi::Accumulators::StaticHistogram< 1, Gaudi::Accumulators::atomicity::full, int > MyHist_t
Gaudi::Functional::Consumer< void()> Base
Gaudi::Accumulators::StaticHistogram< 1, Gaudi::Accumulators::atomicity::full, Category, std::tuple< Gaudi::Accumulators::CustomAxis > > m_hist
AlgTool(std::string type, std::string name, const IInterface *parent)
Standard Constructor.
Definition AlgTool.cpp:95
Gaudi::Accumulators::StaticHistogram< 2 > m_h2
Gaudi::Accumulators::StaticHistogram< 1 > m_h1
Gaudi::Functional::Consumer< void()> Base
Gaudi::Accumulators::StaticHistogram< 3 > m_h3
Simple algorithm useful as base class for tests.