The Gaudi Framework  master (37c0b60a)
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 };
19  switch ( v ) {
20  case Category::Simple:
21  o << "Simple";
22  break;
23  case Category::Complex:
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 
37 namespace 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 
66 namespace Gaudi::Tests::Histograms {
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 
115  mutable MyHist_t m_hist{
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 
131  m_hist{ this, "Categories", "", Gaudi::Accumulators::CustomAxis{} };
132 
133  void operator()() const override {
136  m_hist[Category::Bad] += 3;
137  m_hist[Category::Wrong] += 4;
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  };
165  DECLARE_COMPONENT( TestAlg )
166  } // namespace MultiDimLayout
167 } // namespace Gaudi::Tests::Histograms
Gaudi::Tests::Histograms::Directories::HistoGroupsTool
Definition: HistogramsTests.cpp:70
Gaudi::Functional::Consumer
details::Consumer< Signature, Traits_, details::isLegacy< Traits_ > > Consumer
Definition: Consumer.h:69
std::string
STL class.
Gaudi::Accumulators::CustomAxis::numBins
unsigned int numBins() const
number of bins for this Axis
Definition: HistogramsTests.cpp:44
Gaudi::Accumulators::CustomAxis::minValue
std::underlying_type_t< Category > minValue
min and max values on this axis
Definition: HistogramsTests.cpp:46
Gaudi::Tests::Histograms::AxesLabels::HistWithLabelsAlg::m_hist
MyHist_t m_hist
Definition: HistogramsTests.cpp:115
Gaudi::Accumulators::CustomAxis::operator<<
friend std::ostream & operator<<(std::ostream &o, CustomAxis const &axis)
Definition: HistogramsTests.cpp:53
Gaudi::Tests::Histograms::Directories::HistoGroupsTool::fillHistos
void fillHistos() const
Definition: HistogramsTests.cpp:78
Gaudi::Accumulators::atomicity::full
@ full
std::vector< std::string >
Gaudi::Accumulators::HistogramingCounterBase
A base counter dealing with Histograms.
Definition: StaticHistogram.h:653
Gaudi::Tests::Histograms::MultiDimLayout::TestAlg::m_h1
Gaudi::Accumulators::StaticHistogram< 1 > m_h1
Definition: HistogramsTests.cpp:148
Gaudi::Tests::Histograms::AxesLabels::HistWithLabelsAlg
Definition: HistogramsTests.cpp:109
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:86
AlgTool::AlgTool
AlgTool(std::string type, std::string name, const IInterface *parent)
Standard Constructor.
Definition: AlgTool.cpp:116
Gaudi::Tests::Histograms
Definition: HistogramsTests.cpp:16
Gaudi::Accumulators::CustomAxis::title
std::string title
title of this axis
Definition: HistogramsTests.cpp:48
std::tuple
Gaudi::Tests::Histograms::CustomAxis::EnumAxisAlg::m_hist
Gaudi::Accumulators::StaticHistogram< 1, Gaudi::Accumulators::atomicity::full, Category, std::tuple< Gaudi::Accumulators::CustomAxis > > m_hist
Definition: HistogramsTests.cpp:131
Gaudi::Tests::Histograms::CustomAxis::Category::Bad
@ Bad
Gaudi::Tests::Histograms::MultiDimLayout::TestAlg::m_h3
Gaudi::Accumulators::StaticHistogram< 3 > m_h3
Definition: HistogramsTests.cpp:150
Gaudi::Tests::Histograms::Directories::HistoGroupsAlg
Definition: HistogramsTests.cpp:87
Gaudi::Tests::Histograms::Directories::HistoGroupsTool::m_hist1
MyHist_t m_hist1
Definition: HistogramsTests.cpp:74
Gaudi::Tests::Histograms::MultiDimLayout::TestAlg::m_h2
Gaudi::Accumulators::StaticHistogram< 2 > m_h2
Definition: HistogramsTests.cpp:149
Gaudi::Functional::details::Consumer
Definition: Consumer.h:24
ToolHandle
Definition: ToolHandle.h:132
Gaudi::Tests::Histograms::MultiDimLayout::TestAlg::operator()
void operator()() const override
Definition: HistogramsTests.cpp:153
Gaudi::Accumulators::CustomAxis::CustomAxis
CustomAxis()=default
Consumer.h
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::ostream
STL class.
Gaudi::Tests::Histograms::AxesLabels::HistWithLabelsAlg::operator()
void operator()() const override
Definition: HistogramsTests.cpp:118
Gaudi::Tests::Histograms::CustomAxis::Category
Category
Definition: HistogramsTests.cpp:17
Gaudi::Tests::Histograms::Directories::HistoGroupsAlg::operator()
void operator()() const override
Definition: HistogramsTests.cpp:98
Gaudi::Tests::Histograms::Directories::HistoGroupsTool::m_hist0
MyHist_t m_hist0
Definition: HistogramsTests.cpp:73
Gaudi::Accumulators::CustomAxis::labels
std::vector< std::string > labels
labels for the bins
Definition: HistogramsTests.cpp:50
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Accumulators::to_json
void to_json(nlohmann::json &j, const Axis< Arithmetic > &axis)
automatic conversion of the Axis type to json
Definition: StaticHistogram.h:318
Gaudi::Tests::Histograms::Directories::HistoGroupsTool::m_hist3
MyHist_t m_hist3
Definition: HistogramsTests.cpp:76
Gaudi::Tests::Histograms::CustomAxis::EnumAxisAlg::operator()
void operator()() const override
Definition: HistogramsTests.cpp:133
Gaudi::Accumulators::CustomAxis
Definition: HistogramsTests.cpp:38
Gaudi::Tests::Histograms::CustomAxis::Category::Wrong
@ Wrong
Gaudi::Tests::Histograms::CustomAxis::Category::Complex
@ Complex
Gaudi::Accumulators
Definition: CounterArray.h:18
Gaudi::Accumulators::CustomAxis::maxValue
std::underlying_type_t< Category > maxValue
Definition: HistogramsTests.cpp:46
Gaudi::Tests::Histograms::CustomAxis::EnumAxisAlg
Definition: HistogramsTests.cpp:125
AlgTool
Definition: AlgTool.h:62
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Properties.v
v
Definition: Properties.py:122
AlgTool.h
Gaudi::Tests::Histograms::MultiDimLayout::TestAlg
Definition: HistogramsTests.cpp:144
Gaudi::Tests::Histograms::CustomAxis
Definition: HistogramsTests.cpp:16
Gaudi::Tests::Histograms::CustomAxis::operator<<
std::ostream & operator<<(std::ostream &o, Category v)
Definition: HistogramsTests.cpp:18
Gaudi::Tests::Histograms::Directories::HistoGroupsTool::m_hist2
MyHist_t m_hist2
Definition: HistogramsTests.cpp:75
Gaudi::Tests::Histograms::CustomAxis::Category::Simple
@ Simple
Gaudi::Accumulators::CustomAxis::Category
Gaudi::Tests::Histograms::CustomAxis::Category Category
Definition: HistogramsTests.cpp:39
StaticHistogram.h
Gaudi::Accumulators::CustomAxis::index
unsigned int index(Category value) const
Definition: HistogramsTests.cpp:52