ATLAS Offline Software
ToolStore.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // System include(s):
6 #include <map>
7 #include <iostream>
8 
9 // Local include(s):
10 #include "AsgTools/AsgTool.h"
11 #include "AsgTools/ToolStore.h"
14 
16 typedef std::map< std::string, asg::IAsgTool* > ToolMap_t;
17 
18 namespace {
19 
21  static ToolMap_t s_tools ATLAS_THREAD_SAFE;
22  std::mutex s_toolMutex;
23 
24 } // private namespace
25 
26 namespace asg {
27 
29  using namespace msgToolStore;
30 
31  // Start with a little sanity check:
32  if( ! ptool ) {
33  ANA_MSG_ERROR( "asg::ToolStore::put: Received a null pointer" );
34  return StatusCode::FAILURE;
35  }
36 
37  // Get and check the name of the tool:
38  const std::string& name = ptool->name();
39  if( ! name.size() ) {
40  ANA_MSG_ERROR( "asg::ToolStore::put: The received tool doesn't have a name" );
41  return StatusCode::FAILURE;
42  }
43 
44  std::lock_guard<std::mutex> lock (s_toolMutex);
45  // Check whether we already have a tool with this name:
46  if( s_tools.find( name ) != s_tools.end() ) {
47  ANA_MSG_WARNING ("asg::ToolStore::put: Tool with name \""
48  << name << "\" already registered");
49  return StatusCode::FAILURE;
50  }
51 
52  // Remember the tool:
53  s_tools[ name ] = ptool;
54  return StatusCode::SUCCESS;
55  }
56 
57  IAsgTool* ToolStore::get( const std::string& name, bool silent ) {
58  using namespace msgToolStore;
59 
60  std::lock_guard<std::mutex> lock (s_toolMutex);
61  ToolMap_t::const_iterator itool = s_tools.find( name );
62  if( itool != s_tools.end() )
63  return itool->second;
64 
65  if (name.compare(0, 8, "ToolSvc.") != 0) {
66  itool = s_tools.find( "ToolSvc." + name );
67  if( itool != s_tools.end() )
68  return itool->second;
69  }
70 
71  if( ! silent ) {
72  ANA_MSG_WARNING ("Tool with name \"" << name << "\" not found");
73  }
74  return nullptr;
75  }
76 
78 
79  // Delegate the call to the other function:
80  return remove( tool->name() );
81  }
82 
83  StatusCode ToolStore::remove( const std::string& name ) {
84 
85  std::lock_guard<std::mutex> lock (s_toolMutex);
86  // Remove the tool, not checking if the call was successful or not:
87  s_tools.erase( name );
88  return StatusCode::SUCCESS;
89  }
90 
91 
92 #ifdef XAOD_STANDALONE
93  void ToolStore::dumpToolConfig () {
94  using namespace asg::msgProperty;
95 
96  // I am first putting everything into a map, so that the error
97  // messages don't interleave the property values
98  std::map<std::string,std::map<std::string,std::string> > properties;
99 
100  std::lock_guard<std::mutex> lock (s_toolMutex);
101  for (auto& tool : s_tools)
102  {
103  auto& myproperties = properties[tool.first];
104  myproperties[""] = std::string (typeid(*tool.second).name()) + "/" + tool.first;
105  AsgTool *mytool = dynamic_cast<AsgTool*>(tool.second);
106  if (mytool == nullptr)
107  {
108  ANA_MSG_ERROR ("tool " << tool.first << " not of AsgTool type");
109  myproperties[""] += " <invalid type>";
110  } else
111  {
112  for (auto& property : mytool->getPropertyMgr()->getProperties())
113  {
114  std::string asString;
115  if (property.second->getString (asString).isFailure())
116  {
117  ANA_MSG_ERROR ("on property " << property.first << " for tool " << tool.first);
118  myproperties[property.first] = "<<invalid>>";
119  } else
120  myproperties[property.first] = asString;
121  }
122  }
123  }
124  for (const auto& myproperties : properties)
125  {
126  for (auto& property : myproperties.second)
127  {
128  std::cout << myproperties.first;
129  if (!property.first.empty())
130  std::cout << "." << property.first;
131  std::cout << " = " << property.second << "\n";
132  }
133  }
134  std::cout << std::flush;
135  }
136 #endif
137 
138 } // namespace asg
asg::AsgTool
Base class for the dual-use tool implementation classes.
Definition: AsgTool.h:47
FullCPAlgorithmsTest_eljob.flush
flush
Definition: FullCPAlgorithmsTest_eljob.py:168
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
asg::IAsgTool
Base class for the dual-use tool interface classes.
Definition: IAsgTool.h:41
asg
Definition: DataHandleTestTool.h:28
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
ToolMap_t
std::map< std::string, asg::IAsgTool * > ToolMap_t
Convenience type definition.
Definition: ToolStore.cxx:16
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg)
Macro printing warning messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:292
python.JsonUtils.properties
properties
Definition: JsonUtils.py:96
asg::ToolStore::put
static StatusCode put(IAsgTool *ptool)
Store a named tool by its name.
Definition: ToolStore.cxx:28
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
test_pyathena.mytool
mytool
Definition: test_pyathena.py:19
asg::ToolStore::get
static IAsgTool * get(const std::string &name, bool silent=false)
Retrieve a tool by name.
Definition: ToolStore.cxx:57
asg::ToolStore::remove
static StatusCode remove(const IAsgTool *tool)
Remove the specified tool from the store.
Definition: ToolStore.cxx:77
python.trfDecorators.silent
def silent(func)
Redirect stdout/err to /dev/null Useful wrapper to get rid of ROOT verbosity...
Definition: trfDecorators.py:24
MsgStreamMacros.h
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
ToolStore.h
AsgTool.h
MessageCheckAsgTools.h