ATLAS Offline Software
Loading...
Searching...
No Matches
GenericMonitoringTool Class Reference

#include <GenericMonitoringTool.h>

Inheritance diagram for GenericMonitoringTool:
Collaboration diagram for GenericMonitoringTool:

Public Member Functions

virtual ~GenericMonitoringTool () override
virtual StatusCode initialize () override
virtual StatusCode start () override
virtual StatusCode stop () override
void handle (const Incident &) override
void invokeFillers (const std::vector< std::reference_wrapper< Monitored::IMonitoredVariable > > &monitoredVariables) const
 feed the fillers
StatusCode book ()
 Book histograms.
void setPath (const std::string &newPath)
 Overrride configured booking path.
const std::string & getPath () const
virtual const ServiceHandle< ITHistSvc > & histogramService () const
virtual uint32_t runNumber ()
virtual uint32_t lumiBlock ()

Public Attributes

 log = logging.getLogger(__name__)

Private Types

using fillerCache_t = CxxUtils::ConcurrentStrToValMap<std::vector<std::shared_ptr<Monitored::HistogramFiller>>, CxxUtils::SimpleUpdater>

Private Attributes

ServiceHandle< ITHistSvc > m_histSvc { this, "THistSvc", "THistSvc", "Histogramming svc" }
 THistSvc (do NOT fix the service type (only the name) to allow for a different implementation online.
Gaudi::Property< std::string > m_histoPath { this, "HistPath", {}, "Directory for histograms [name of parent if not set]" }
Gaudi::Property< std::vector< std::string > > m_histograms { this, "Histograms", {}, "Definitions of histograms", "OrderedSet<std::string>"}
Gaudi::Property< bool > m_explicitBooking { this, "ExplicitBooking", false, "Do not create histograms automatically in initialize but wait until the method book is called." }
Gaudi::Property< bool > m_failOnEmpty { this, "FailOnEmpty", true, "Fail in initialize() if no histograms defined" }
BooleanProperty m_useCache { this, "UseCache", true, "Cache filler lookups" }
BooleanProperty m_registerHandler { this, "RegisterHandler", true, "Use incident handler to make 'always book' plots (else only check once)" }
std::vector< std::shared_ptr< Monitored::HistogramFiller > > m_fillers
 plain list of fillers
std::vector< std::shared_ptr< Monitored::HistogramFiller > > m_alwaysCreateFillers
 fillers that need touching, usually empty
fillerCache_t m_fillerCacheMap ATLAS_THREAD_SAFE {fillerCache_t::Updater_t()}
 lookup map to speed up filler searches

Detailed Description

Definition at line 51 of file GenericMonitoringTool.h.

Member Typedef Documentation

◆ fillerCache_t

using GenericMonitoringTool::fillerCache_t = CxxUtils::ConcurrentStrToValMap<std::vector<std::shared_ptr<Monitored::HistogramFiller>>, CxxUtils::SimpleUpdater>
private

Definition at line 86 of file GenericMonitoringTool.h.

Constructor & Destructor Documentation

◆ ~GenericMonitoringTool()

GenericMonitoringTool::~GenericMonitoringTool ( )
overridevirtual

Definition at line 22 of file GenericMonitoringTool.cxx.

22{ }

Member Function Documentation

◆ book()

StatusCode GenericMonitoringTool::book ( )

Book histograms.

Definition at line 55 of file GenericMonitoringTool.cxx.

55 {
56
57 // If no histogram path given use parent or our own name
58 if (m_histoPath.empty()) {
59 auto named = dynamic_cast<const INamedInterface*>(parent());
60 m_histoPath = named ? named->name() : name();
61 }
62
63 // Replace dot (e.g. MyAlg.MyTool) with slash to create sub-directory
64 std::replace( m_histoPath.begin(), m_histoPath.end(), '.', '/' );
65
66 ATH_MSG_DEBUG("Booking histograms in path: " << m_histoPath.value());
67
68 HistogramFillerFactory factory(this, m_histoPath);
69
70 for (const std::string& item : m_histograms) {
71 if (item.empty()) {
72 ATH_MSG_DEBUG( "Skipping empty histogram definition" );
73 continue;
74 }
75 ATH_MSG_DEBUG( "Configuring monitoring for: " << item );
76 HistogramDef def = HistogramDef::parse(item);
77
78 if (def.ok) {
79 std::shared_ptr<HistogramFiller> filler(factory.create(def));
80
81 if (filler) {
82 if (def.kAlwaysCreate) {
83 if (m_registerHandler) {
84 m_alwaysCreateFillers.push_back(filler); // prepare list of fillers for handler
85 } else {
86 filler->touch(); // create now and be done with it
87 }
88 }
89 m_fillers.push_back(std::move(filler));
90 } else {
91 ATH_MSG_WARNING( "The histogram filler cannot be instantiated for: " << def.name );
92 }
93 } else {
94 ATH_MSG_ERROR( "Unparsable histogram definition: " << item );
95 return StatusCode::FAILURE;
96 }
97 ATH_MSG_DEBUG( "Monitoring for variable " << def.name << " prepared" );
98 }
99
100 if ( m_fillers.empty() && m_failOnEmpty ) {
101 std::string hists;
102 for (const auto &h : m_histograms) hists += (h+",");
103 ATH_MSG_ERROR("No monitored variables created based on histogram definition: [" << hists <<
104 "] Remove this monitoring tool or check its configuration.");
105 return StatusCode::FAILURE;
106 }
107
108 // are there some histograms that should always be made?
109 // then register to be notified on every event
110 if (! m_alwaysCreateFillers.empty() && m_registerHandler) {
111 ATH_MSG_DEBUG("Registering incident handler");
112 SmartIF<IIncidentSvc> incSvc{service("IncidentSvc")};
113 ATH_CHECK(incSvc.isValid());
114 incSvc->addListener(this, IncidentType::BeginEvent);
115 }
116
117 return StatusCode::SUCCESS;
118}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::vector< std::shared_ptr< Monitored::HistogramFiller > > m_alwaysCreateFillers
fillers that need touching, usually empty
std::vector< std::shared_ptr< Monitored::HistogramFiller > > m_fillers
plain list of fillers
Factory of the histogram fillers.
bool kAlwaysCreate
always create this histogram, even if never filled
static const HistogramDef parse(const std::string &histogramDefinition)
Parses histogram defintion from json data.
bool ok
good declaration: parsing or copying successful
std::vector< std::string > name
names of monitored variables

◆ getPath()

const std::string & GenericMonitoringTool::getPath ( ) const
inline

Definition at line 68 of file GenericMonitoringTool.h.

68{ return m_histoPath; }

◆ handle()

void GenericMonitoringTool::handle ( const Incident & )
override

Definition at line 49 of file GenericMonitoringTool.cxx.

49 {
50 for (const auto& filler : m_alwaysCreateFillers) {
51 filler->touch();
52 }
53}

◆ histogramService()

virtual const ServiceHandle< ITHistSvc > & GenericMonitoringTool::histogramService ( ) const
inlinevirtual

Definition at line 70 of file GenericMonitoringTool.h.

70{ return m_histSvc; }

◆ initialize()

StatusCode GenericMonitoringTool::initialize ( )
overridevirtual

Definition at line 24 of file GenericMonitoringTool.cxx.

24 {
25 ATH_CHECK(m_histSvc.retrieve());
26 return StatusCode::SUCCESS;
27}

◆ invokeFillers()

void GenericMonitoringTool::invokeFillers ( const std::vector< std::reference_wrapper< Monitored::IMonitoredVariable > > & monitoredVariables) const

feed the fillers

Definition at line 165 of file GenericMonitoringTool.cxx.

165 {
166 // This is the list of fillers to consider in the invocation.
167 // If we are using the cache then this may be a proper subset of m_fillers; otherwise will just be m_fillers
168 const std::vector<std::shared_ptr<Monitored::HistogramFiller>>* fillerList{&m_fillers};
169 // pointer to list of matched fillers, if we need to update the cache (default doesn't create the vector)
170 std::optional<std::vector<std::shared_ptr<Monitored::HistogramFiller>> > matchedFillerList;
171 if (m_useCache) {
172 const auto match = m_fillerCacheMap.find(fillerKey(monitoredVariables));
173 if (match != m_fillerCacheMap.end()) {
174 fillerList = &match->second;
175 } else {
176 // make new cache entry
177 matchedFillerList.emplace();
178 }
179 }
180
181 for ( auto filler: *fillerList ) {
182 const int fillerCardinality = filler->histogramVariablesNames().size() + (filler->histogramWeightName().empty() ? 0: 1) + (filler->histogramCutMaskName().empty() ? 0 : 1);
183
184 if ( fillerCardinality == 1 ) { // simplest case, optimising this to be super fast
185 for ( auto& var: monitoredVariables ) {
186 if ( var.get().name().compare( filler->histogramVariablesNames()[0] ) == 0 ) {
187 {
188 auto guard{filler->getLock()};
189 filler->fill({&var.get()});
190 }
191 if (matchedFillerList) {
192 matchedFillerList->push_back(std::move(filler));
193 }
194 break;
195 }
196 }
197 } else { // a more complicated case, and cuts or weights
198 int matchesCount = 0;
199 Monitored::HistogramFiller::VariablesPack vars;
200 for ( const auto& var: monitoredVariables ) {
201 bool matched = false;
202 for ( unsigned fillerVarIndex = 0; fillerVarIndex < filler->histogramVariablesNames().size(); ++fillerVarIndex ) {
203 if ( var.get().name().compare( filler->histogramVariablesNames()[fillerVarIndex] ) == 0 ) {
204 vars.set(fillerVarIndex, &var.get());
205 matched = true;
206 matchesCount++;
207 break;
208 }
209 }
210 if ( matchesCount == fillerCardinality ) break;
211 if ( not matched ) { // may be a weight or cut variable still
212 if ( var.get().name().compare( filler->histogramWeightName() ) == 0 ) {
213 vars.weight = &var.get();
214 matchesCount ++;
215 } else if ( var.get().name().compare( filler->histogramCutMaskName() ) == 0 ) {
216 vars.cut = &var.get();
217 matchesCount++;
218 }
219 }
220 if ( matchesCount == fillerCardinality ) break;
221 }
222 if ( matchesCount == fillerCardinality ) {
223 {
224 auto guard{filler->getLock()};
225 filler->fill( vars );
226 }
227 if (matchedFillerList) {
228 matchedFillerList->push_back(std::move(filler));
229 }
230 } else if ( ATH_UNLIKELY( msgLvl(MSG::DEBUG) && matchesCount != 0 ) ) { // something has matched, but not all, worth informing user
231 invokeFillersDebug(msg(), vars, filler, monitoredVariables);
232 }
233 }
234 }
235
236 if (matchedFillerList) {
237 // We may hit this multiple times. If another thread has updated the cache in the meanwhile,
238 // nothing will be done here.
239 m_fillerCacheMap.emplace(fillerKey(monitoredVariables), std::move(*matchedFillerList));
240 }
241}
#define ATH_UNLIKELY(x)
static const Attributes_t empty
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:357
const Monitored::IMonitoredVariable * cut
pointer to cut mask variable, typically absent
void set(unsigned index, const IMonitoredVariable *ptr)
sets monitored variable at the index (may need to resize vector of variables)
const Monitored::IMonitoredVariable * weight
pointer to weight variable, typically absent
MsgStream & msg
Definition testRead.cxx:32

◆ lumiBlock()

uint32_t GenericMonitoringTool::lumiBlock ( )
virtual

Definition at line 247 of file GenericMonitoringTool.cxx.

247 {
248 return Gaudi::Hive::currentContext().eventID().lumi_block();
249}

◆ runNumber()

uint32_t GenericMonitoringTool::runNumber ( )
virtual

Definition at line 243 of file GenericMonitoringTool.cxx.

243 {
244 return Gaudi::Hive::currentContext().eventID().run_number();
245}

◆ setPath()

void GenericMonitoringTool::setPath ( const std::string & newPath)
inline

Overrride configured booking path.

Definition at line 67 of file GenericMonitoringTool.h.

67{ m_histoPath = newPath; }

◆ start()

StatusCode GenericMonitoringTool::start ( )
overridevirtual

Definition at line 29 of file GenericMonitoringTool.cxx.

29 {
30 if ( not m_explicitBooking ) {
31 ATH_MSG_DEBUG("Proceeding to histogram booking");
32 return book();
33 }
34 return StatusCode::SUCCESS;
35}

◆ stop()

StatusCode GenericMonitoringTool::stop ( )
overridevirtual

Definition at line 37 of file GenericMonitoringTool.cxx.

37 {
38 m_alwaysCreateFillers.clear();
39 m_fillers.clear();
40 if (m_registerHandler) {
41 ATH_MSG_DEBUG("Deregistering incident handler");
42 SmartIF<IIncidentSvc> incSvc{service("IncidentSvc")};
43 ATH_CHECK(incSvc.isValid());
44 incSvc->removeListener(this, IncidentType::BeginEvent);
45 }
46 return StatusCode::SUCCESS;
47}

Member Data Documentation

◆ ATLAS_THREAD_SAFE

fillerCache_t m_fillerCacheMap GenericMonitoringTool::ATLAS_THREAD_SAFE {fillerCache_t::Updater_t()}
mutableprivate

lookup map to speed up filler searches

Definition at line 87 of file GenericMonitoringTool.h.

87{fillerCache_t::Updater_t()};

◆ log

GenericMonitoringTool.log = logging.getLogger(__name__)

Definition at line 9 of file GenericMonitoringTool.py.

◆ m_alwaysCreateFillers

std::vector<std::shared_ptr<Monitored::HistogramFiller> > GenericMonitoringTool::m_alwaysCreateFillers
private

fillers that need touching, usually empty

Definition at line 85 of file GenericMonitoringTool.h.

◆ m_explicitBooking

Gaudi::Property<bool> GenericMonitoringTool::m_explicitBooking { this, "ExplicitBooking", false, "Do not create histograms automatically in initialize but wait until the method book is called." }
private

Definition at line 79 of file GenericMonitoringTool.h.

79{ this, "ExplicitBooking", false, "Do not create histograms automatically in initialize but wait until the method book is called." };

◆ m_failOnEmpty

Gaudi::Property<bool> GenericMonitoringTool::m_failOnEmpty { this, "FailOnEmpty", true, "Fail in initialize() if no histograms defined" }
private

Definition at line 80 of file GenericMonitoringTool.h.

80{ this, "FailOnEmpty", true, "Fail in initialize() if no histograms defined" };

◆ m_fillers

std::vector<std::shared_ptr<Monitored::HistogramFiller> > GenericMonitoringTool::m_fillers
private

plain list of fillers

Definition at line 84 of file GenericMonitoringTool.h.

◆ m_histograms

Gaudi::Property<std::vector<std::string> > GenericMonitoringTool::m_histograms { this, "Histograms", {}, "Definitions of histograms", "OrderedSet<std::string>"}
private

Definition at line 78 of file GenericMonitoringTool.h.

78{ this, "Histograms", {}, "Definitions of histograms", "OrderedSet<std::string>"};

◆ m_histoPath

Gaudi::Property<std::string> GenericMonitoringTool::m_histoPath { this, "HistPath", {}, "Directory for histograms [name of parent if not set]" }
private

Definition at line 77 of file GenericMonitoringTool.h.

77{ this, "HistPath", {}, "Directory for histograms [name of parent if not set]" };

◆ m_histSvc

ServiceHandle<ITHistSvc> GenericMonitoringTool::m_histSvc { this, "THistSvc", "THistSvc", "Histogramming svc" }
private

THistSvc (do NOT fix the service type (only the name) to allow for a different implementation online.

Definition at line 76 of file GenericMonitoringTool.h.

76{ this, "THistSvc", "THistSvc", "Histogramming svc" };

◆ m_registerHandler

BooleanProperty GenericMonitoringTool::m_registerHandler { this, "RegisterHandler", true, "Use incident handler to make 'always book' plots (else only check once)" }
private

Definition at line 82 of file GenericMonitoringTool.h.

82{ this, "RegisterHandler", true, "Use incident handler to make 'always book' plots (else only check once)" };

◆ m_useCache

BooleanProperty GenericMonitoringTool::m_useCache { this, "UseCache", true, "Cache filler lookups" }
private

Definition at line 81 of file GenericMonitoringTool.h.

81{ this, "UseCache", true, "Cache filler lookups" };

The documentation for this class was generated from the following files: