ATLAS Offline Software
Loading...
Searching...
No Matches
MonitoredGroup.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef AthenaMonitoringKernel_MonitoredGroup_h
6#define AthenaMonitoringKernel_MonitoredGroup_h
7
8#include <functional>
9#include <string>
10#include <vector>
11#include <stdexcept>
12
13#include "GaudiKernel/ToolHandle.h"
14
17
18namespace Monitored {
53 class Group {
54 public:
64 template <typename... T>
65 Group(const ToolHandle<GenericMonitoringTool>& tool, T&&... monitoredGroup)
66 : m_tool(tool),
67 m_autoFill(true),
68 m_monitoredGroup{monitoredGroup...}
69 { }
70
71 Group(const ToolHandle<GenericMonitoringTool>& tool, const std::vector<std::reference_wrapper<IMonitoredVariable>>& monitoredGroup)
72 : m_tool(tool),
73 m_autoFill(true),
74 m_monitoredGroup(monitoredGroup)
75 { }
76
77 Group(const ToolHandle<GenericMonitoringTool>& tool, std::vector<std::reference_wrapper<IMonitoredVariable>>&& monitoredGroup)
78 : m_tool(tool),
79 m_autoFill(true),
80 m_monitoredGroup(std::move(monitoredGroup))
81 { }
82
84 if (m_autoFill) {
85 try {
86 fill();
87 }
88 catch (std::exception &) {
89 // fill can throw due to dereferencing a Gaudi handle
90 //or boost container exception
91 std::abort();
92 }
93 }
94 }
95
106 void fill() {
107 if ( m_tool.empty() ) return;
108 setAutoFill(false);
109 m_tool->invokeFillers(m_monitoredGroup);
110 }
111
119 void setAutoFill(bool isEnabled) { m_autoFill = isEnabled; }
120
121 protected:
122 ToolHandle<GenericMonitoringTool> m_tool;
124 const std::vector<std::reference_wrapper<IMonitoredVariable>> m_monitoredGroup;
125 };
126
127 template <typename... T>
128 void fill(const ToolHandle<GenericMonitoringTool>& tool, T&&... variables) {
129 if (!tool.empty()) {
130 tool->invokeFillers({std::forward<T>(variables)...});
131 }
132 }
133
134 // Sub-namespace for internal-use functions
135 namespace detail {
137 template <typename T = int>
138 int findToolIndex( const ToolHandleArray<GenericMonitoringTool>& toolArray, const std::string& name ) {
139 auto it = std::find_if(toolArray.begin(),toolArray.end(),[&](const auto& r) {return r.name()==name;});
140 return it!=toolArray.end()
141 ? std::distance(toolArray.begin(),it)
142 : throw std::runtime_error("The tool "+name+" could not be found in the tool array.");
143 }
144 }
145
147 template<typename V,typename std::enable_if_t<std::is_integral_v<V>>* =nullptr>
148 std::vector<V> buildToolMap(const ToolHandleArray<GenericMonitoringTool> & tools, const std::string& baseName, int nHist) {
149 std::vector<int> indexArray;
150 for ( int iHist=0; iHist<nHist; iHist++ ) {
151 std::string groupName = baseName + "_" + std::to_string(iHist);
152 indexArray.push_back(detail::findToolIndex(tools,groupName));
153 }
154 return indexArray;
155 }
156
158 template<typename V,typename std::enable_if_t<!std::is_integral_v<V>>* =nullptr,typename...T>
159 std::vector<V> buildToolMap(const ToolHandleArray<GenericMonitoringTool> & tools, const std::string& baseName, int nHist, T... dimensions) {
160 std::vector<V> indexArray;
161 for ( int iHist=0; iHist<nHist; iHist++ ) {
162 std::string groupName = baseName + "_" + std::to_string(iHist);
163 indexArray.push_back(buildToolMap<typename V::value_type>(tools,groupName,dimensions...));
164 }
165 return indexArray;
166 }
167
169 template<typename V,typename std::enable_if_t<std::is_integral_v<V>>* =nullptr>
170 std::map<std::string,int> buildToolMap(const ToolHandleArray<GenericMonitoringTool> & tools, const std::string& baseName, const std::vector<std::string>& labels) {
171 std::map<std::string,int> indexMap;
172 for ( const std::string& label : labels ) {
173 std::string groupName = baseName + "_" + label;
174 indexMap[label] = detail::findToolIndex(tools,groupName);
175 }
176 return indexMap;
177 }
178
180 template<typename V,typename std::enable_if_t<!std::is_integral_v<V>>* =nullptr,typename...T>
181 std::map<std::string,V> buildToolMap(const ToolHandleArray<GenericMonitoringTool> & tools, const std::string& baseName, const std::vector<std::string>& labels, T... dimensions) {
182 std::map<std::string,V> indexMap;
183 for ( const std::string& label : labels ) {
184 std::string groupName = baseName + "_" + label;
185 indexMap[label] = buildToolMap<typename V::mapped_type>(tools,groupName,dimensions...);
186 }
187 return indexMap;
188 }
189
190} // namespace Monitored
191
192#endif /* AthenaMonitoringKernel_MonitoredGroup_h */
Group(const ToolHandle< GenericMonitoringTool > &tool, T &&... monitoredGroup)
Group of monitored variables.
ToolHandle< GenericMonitoringTool > m_tool
void setAutoFill(bool isEnabled)
enables/disables filling when Monitored::Group leaves the scope
Group(const ToolHandle< GenericMonitoringTool > &tool, std::vector< std::reference_wrapper< IMonitoredVariable > > &&monitoredGroup)
Group(const ToolHandle< GenericMonitoringTool > &tool, const std::vector< std::reference_wrapper< IMonitoredVariable > > &monitoredGroup)
const std::vector< std::reference_wrapper< IMonitoredVariable > > m_monitoredGroup
void fill()
Explicitly fill the monitoring histograms and disable autoFill.
int r
Definition globals.cxx:22
std::string label(const std::string &format, int i)
Definition label.h:19
int findToolIndex(const ToolHandleArray< GenericMonitoringTool > &toolArray, const std::string &name)
Finds the index of an element in a tool handle array by its string name.
Generic monitoring tool for athena components.
std::vector< V > buildToolMap(const ToolHandleArray< GenericMonitoringTool > &tools, const std::string &baseName, int nHist)
Builds an array of indices (base case)
void fill(const ToolHandle< GenericMonitoringTool > &tool, T &&... variables)
STL namespace.