ATLAS Offline Software
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 CfAthAlgTool, public 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 // CfAthAlgTool(t,n,p),
37 // ...
38 //
39 // // inside a method like Bmumu_reco_mumu::addBranches():
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 //
62 
63 namespace 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...
100  return AthAlgTool::sysInitialize();
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 
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 
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
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
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
DerivationFramework::CfAthAlgTool::getCounter
CutIdentifier getCounter(const std::string &name) const
Definition: CfAthAlgTool.cxx:128
DerivationFramework::CfAthAlgTool::m_mctn
NameIdMap_t m_mctn
Definition: CfAthAlgTool.h:73
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
DerivationFramework::CfAthAlgTool::addToCounter
virtual bool addToCounter(const std::string &name, uint64_t counts=1, double weight=1.) const
Definition: CfAthAlgTool.cxx:115
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
DerivationFramework::CfAthAlgTool::~CfAthAlgTool
virtual ~CfAthAlgTool()
Definition: CfAthAlgTool.cxx:88
skel.it
it
Definition: skel.GENtoEVGEN.py:423
DerivationFramework::CfAthAlgTool::addEvent
virtual bool addEvent(const std::string &name, double weight=1.) const
Definition: CfAthAlgTool.cxx:104
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
DerivationFramework::CfAthAlgTool::m_cutFlowSvc
ServiceHandle< ICutFlowSvc > m_cutFlowSvc
Definition: CfAthAlgTool.h:66
lumiFormat.i
int i
Definition: lumiFormat.py:92
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:14
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
CutIdentifier
uint32_t CutIdentifier
InstanceIdentifier is a unique identifer used for every AthFilterAlgorithm instance.
Definition: ICutFlowSvc.h:22
DerivationFramework::CfAthAlgTool::m_bidisset
bool m_bidisset
Definition: CfAthAlgTool.h:77
DerivationFramework::CfAthAlgTool::getCounterIdByName
CutIdentifier getCounterIdByName(const std::string &name) const
Definition: CfAthAlgTool.cxx:161
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:191
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
DerivationFramework::CfAthAlgTool::m_ctbasename
std::string m_ctbasename
Definition: CfAthAlgTool.h:69
AthCommonDataStore< AthCommonMsg< AlgTool > >::sysInitialize
virtual StatusCode sysInitialize() override
Perform system initialization for an algorithm.
CfAthAlgTool.h
DerivationFramework::CfAthAlgTool::sysInitialize
virtual StatusCode sysInitialize() override
Perform system initialization for an algorithm.
Definition: CfAthAlgTool.cxx:94
DerivationFramework::CfAthAlgTool::getCounterNameById
std::string getCounterNameById(CutIdentifier id) const
Definition: CfAthAlgTool.cxx:147
AthAlgTool
Definition: AthAlgTool.h:26
DerivationFramework::CfAthAlgTool::m_bid
CutIdentifier m_bid
Definition: CfAthAlgTool.h:76
checkFileSG.ind
list ind
Definition: checkFileSG.py:118
DerivationFramework::CfAthAlgTool::CfAthAlgTool
CfAthAlgTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: CfAthAlgTool.cxx:67