ATLAS Offline Software
Loading...
Searching...
No Matches
CfAthAlgTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5//============================================================================
6// CfAthAlgTool.h
7//============================================================================
8//
9// Author : Wolfgang Walkowiak <Wolfgang.Walkowiak@cern.ch.>
10// Changes:
11//
12// Wrapper around AthAlgTool to provide easy access to CutFlowSvc
13// and some utility methods for it.
14// Methods for accessing the CutFlowSvc are modelled after
15// AthFilterAlgorithm's implementation.
16//
17// This class inherits from AthAlgTool. It should be inherited from.
18//
19//-----------------------------------------------------------------------------
20//
21// Usage information
22//
23// Example:
24//
25// // Bmumu_reco_mumu.h:
26// class Bmumu_reco_mumu : public extends<CfAthAlgTool, IAugmentationTool> {
27// public:
28// Bmumu_reco_mumu(const std::string& t, const std::string& n,
29// const IInterface* p);
30// ...
31//
32// // Bmumu_reco_mumu.cxx:
33// Bmumu_reco_mumu::Bmumu_reco_mumu(const std::string& t,
34// const std::string& n,
35// const IInterface* p) :
36// base_class(t,n,p),
37// ...
38//
39// // inside a method like Bmumu_reco_mumu::addBranches(const EventContext& ctx):
40// ...
41// // add counter for number of events seen
42// addEvent("dimuEvents");
43// // add counter for the number of events with >= 1 reco'd vertices
44// if ( vtxContainer->size() > 0 ) {
45// addEvent("dimuWithVertexCand");
46// }
47// // add counter for the number of vertices
48// addToCounter("dimuNumVertices", vtxContainer->size());
49// ...
50//
51// Please note that a line for
52// addEvent(nameString, weight=1.);
53// or
54// addToCounter(nameString, counts=1, weight=1.);
55// is sufficient.
56// In a case a counter with that name does not exist yet, it will be
57// initialized automatically.
58//
59//============================================================================
60//
61#include "CfAthAlgTool.h"
62
63namespace DerivationFramework {
64
65 //--------------------------------------------------------------------------
66 // Constructor
67 CfAthAlgTool::CfAthAlgTool(const std::string& t,
68 const std::string& n,
69 const IInterface* p) :
70 AthAlgTool(t,n,p),
71 m_cutFlowSvc("CutFlowSvc/CutFlowSvc", n),
72 m_ctbasename(n),
73 m_bid(0), m_bidisset(false) {
74
75 ATH_MSG_DEBUG("Calling constructor with parameters");
76
77 // Declare counters base name
78 declareProperty("CountersBaseName", m_ctbasename);
79
80 // clean-up counter base name
81 std::string fstr("ToolSvc.");
82 std::string::size_type ind = m_ctbasename.find(fstr);
83 if (ind != std::string::npos) m_ctbasename.erase(ind, fstr.length());
84
85 }
86 //--------------------------------------------------------------------------
87 // Destructor
89
90 ATH_MSG_DEBUG("Calling destructor");
91 }
92 //--------------------------------------------------------------------------
93 // Initialization method invoked by the framework.
95
96 // retrieve CutFlowSvc instance
97 CHECK( m_cutFlowSvc.retrieve() );
98
99 // re-direct to base class...
101 }
102 //--------------------------------------------------------------------------
103 // add one event to a named counter -- returns true on success
104 bool CfAthAlgTool::addEvent(const std::string &name, double weight) const {
105
106 CutIdentifier id = getCounter(name);
107 if ( id > 0 ) {
108 m_cutFlowSvc->addEvent(id, weight);
109 }
110 return (id > 0);
111 }
112 //--------------------------------------------------------------------------
113 // add to a named counter -- returns true on success
114 // if counts > 1 : same weight is added multiple times
115 bool CfAthAlgTool::addToCounter(const std::string &name, uint64_t counts,
116 double weight) const {
117
118 CutIdentifier id = getCounter(name);
119 if ( id > 0 ) {
120 for (uint64_t i=0; i<counts; ++i) {
121 m_cutFlowSvc->addEvent(id, weight);
122 }
123 }
124 return (id > 0);
125 }
126 //--------------------------------------------------------------------------
127 // add a counter by name -- simply returns id if counter already exists
128 CutIdentifier CfAthAlgTool::getCounter(const std::string &name) const {
129
131 if ( id < 1 ) {
132 std::string fullname = m_ctbasename + "_" + name;
133 if ( ! m_bidisset ) {
134 throw std::runtime_error("cutFlowSvc()->registerFilter is no longer supported. code an alternative here");
135 //id = cutFlowSvc()->registerFilter(fullname, "N/A");
136 m_bid = id;
137 } else {
138 throw std::runtime_error("cutFlowSvc()->registerCut is no longer supported. code an alternative here");
139 //id = cutFlowSvc()->registerCut(fullname, "N/A", m_bid);
140 }
141 m_mctn[name] = id;
142 }
143 return id;
144 }
145 //--------------------------------------------------------------------------
146 // returns counter name by id
148
149 std::string res = "__NOT_FOUND__";
150
151 for (NameIdMap_t::iterator it = m_mctn.begin(); it != m_mctn.end(); ++it) {
152 if ( it->second == id ) {
153 res = it->first;
154 break;
155 }
156 }
157 return res;
158 }
159 //--------------------------------------------------------------------------
160 // returns counter id by name
161 CutIdentifier CfAthAlgTool::getCounterIdByName(const std::string &name) const {
162
163 CutIdentifier id = 0;
164
165 NameIdMap_t::const_iterator it = m_mctn.find(name);
166 if ( it != m_mctn.end() ) {
167 id = it->second;
168 }
169 return id;
170 }
171 //--------------------------------------------------------------------------
172} // namespace
#define ATH_MSG_DEBUG(x)
#define CHECK(...)
Evaluate an expression and check for errors.
uint32_t CutIdentifier
InstanceIdentifier is a unique identifer used for every AthFilterAlgorithm instance.
Definition ICutFlowSvc.h:26
std::pair< std::vector< unsigned int >, bool > res
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
virtual StatusCode sysInitialize() override
CfAthAlgTool(const std::string &t, const std::string &n, const IInterface *p)
CutIdentifier getCounterIdByName(const std::string &name) const
virtual StatusCode sysInitialize() override
Perform system initialization for an algorithm.
std::string getCounterNameById(CutIdentifier id) const
ServiceHandle< ICutFlowSvc > m_cutFlowSvc
CutIdentifier getCounter(const std::string &name) const
virtual bool addEvent(const std::string &name, double weight=1.) const
virtual bool addToCounter(const std::string &name, uint64_t counts=1, double weight=1.) const
THE reconstruction tool.