DuneToolManager.h
Go to the documentation of this file.
1 // DuneToolManager.h
2 //
3 // David Adams
4 // April 2017
5 //
6 // This class provides access to DUNE tools. It is a quick and dirty
7 // implementation of a tool manager intended for use until we get something
8 // better from larsoft or art.
9 //
10 // Tools are typically obtained from the primary instance of this class which
11 // is accessed via DuneToolManager::instance(). The first call to this method
12 // locates the top-level fcl file and loads all the tool definitions. Tools
13 // are constructed when getPrivate or getPublic is called.
14 //
15 // This first call typically occurs during job initialization. The fcl file
16 // name may be provided or, if not, it is obtained from the command line
17 // (option -c). If that is also absent, the default tools_dune.fcl is used.
18 //
19 // It is also possible to construct additional tool managers from fcl file names.
20 // Tool instances are not shared between different tool managers. These
21 // managers might be assigned to different threads or used to locate tools
22 // that are not defined in the primary fcl file.
23 
24 #ifndef DuneToolManager_H
25 #define DuneToolManager_H
26 
27 #include <string>
28 #include <vector>
29 #include <memory>
30 #include <iostream>
31 #include "fhiclcpp/ParameterSet.h"
33 
35 
36 public: // Type aliases and subclasses.
37 
38  using Name = std::string;
39  using NameVector = std::vector<Name>;
40 
42  public:
43  virtual ~SharedToolEntry() =default;
44  };
45 
46  template<class T>
48  public:
49  using ToolPtr = std::unique_ptr<T>;
50  TSharedToolEntry(ToolPtr& a_ptr) : m_ptr(std::move(a_ptr)) { }
51  ~TSharedToolEntry() =default;
52  T* get() { return m_ptr.get(); }
53  private:
55  };
56 
57  using SharedToolPtr = std::unique_ptr<SharedToolEntry>;
58  using SharedToolMap = std::map<Name, SharedToolPtr>;
59 
60 public:
61 
62  // Set/retrieve the primary fcl filename.
63  // The name is assigned from setName the first time this method is called.
64  // If setName is blank and the tool manager is not yet set, an attempt
65  // is made to find the fcl name on the command line following "-c".
66  // If a different name is provided in later calls, it is ignored and a
67  // warning message is broadcast.
68  static std::string fclFilename(std::string setName ="", int dbg =1);
69 
70  // Return the primary instance of this class.
71  // The fcl file returned by fclFilename is read the first time this is called.
72  static DuneToolManager* instance(std::string fclname ="", int dbg =1);
73 
74  // Display a help message.
75  static void help();
76 
77  // Ctor from FCL file name.
78  explicit DuneToolManager(std::string fclname);
79 
80  // Return a private (not shared) copy of a tool.
81  template<class T>
82  std::unique_ptr<T> getPrivate(std::string name) {
83  std::string myname = "DuneToolManager::getPrivate: ";
84  fhicl::ParameterSet psTool;
85  if ( name.size() == 0 ) {
86  std::cout << myname << "Tool name is blank" << std::endl;
87  return nullptr;
88  } else if ( name[0] == '{' ) { // Name is a tool cfg string
89  makeParameterSet(name, psTool);
90  } else if ( std::find(m_toolNames.begin(), m_toolNames.end(), name) != m_toolNames.end() ) {
92  } else {
93  std::cout << myname << "No such tool name: " << name << std::endl;
94  return nullptr;
95  }
96  return art::make_tool<T>(psTool);
97  }
98 
99  // Return a shared tool.
100  template<class T>
102  std::string myname = "DuneToolManager::getShared: ";
103  SharedToolMap::iterator itoo = m_sharedTools.find(name);
104  if ( itoo != m_sharedTools.end() ) {
105  SharedToolPtr& pent = itoo->second;
106  if ( pent ) {
107  TSharedToolEntry<T>* ptent = dynamic_cast<TSharedToolEntry<T>*>(pent.get());
108  return ptent->get();
109  } else {
110  std::cout << myname << "ERROR: Null tool pointer for " << name << "." << std::endl;
111  }
112  }
113  if ( std::find(m_toolNames.begin(), m_toolNames.end(), name) != m_toolNames.end() ) {
115  std::unique_ptr<T> ptoo = art::make_tool<T>(psTool);
116  TSharedToolEntry<T>* ptent(new TSharedToolEntry<T>(ptoo));
117  SharedToolEntry* pent = ptent;
118  //m_sharedTools[name] = pent;
119  m_sharedTools.emplace(name, pent);
120  return ptent->get();
121  } else {
122  std::cout << myname << "No such tool name: " << name << std::endl;
123  return nullptr;
124  }
125  }
126 
127  // Delete a shared tool.
128  // Returns 0 if tool is deleted.
129  // These tools are otherwise deleted when the tool manager is deleted. This method
130  // provides the opportunity to ensure this destruction occurs before general C++
131  // closeout, e.g. before Root objects begin to dissappear.
133 
134  // Return the list of available tool names.
135  const std::vector<std::string>& toolNames() const;
136 
137  // Display the list of available tools.
138  void print() const;
139 
140 private:
141 
146 
147  // Convert a string into a paramter set.
149 
150 };
151 
152 #endif
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
int makeParameterSet(std::string scfg, fhicl::ParameterSet &ps)
std::vector< Name > NameVector
bool dbg
const std::vector< std::string > & toolNames() const
std::string string
Definition: nybbler.cc:12
STL namespace.
virtual ~SharedToolEntry()=default
void print() const
std::string Name
std::string m_fclname
std::unique_ptr< SharedToolEntry > SharedToolPtr
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
static std::string fclFilename(std::string setName="", int dbg=1)
static constexpr double ps
Definition: Units.h:99
NameVector m_toolNames
fhicl::ParameterSet m_pstools
std::unique_ptr< T > getPrivate(std::string name)
DuneToolManager(std::string fclname)
int deleteShared(std::string name)
static void help()
std::map< Name, SharedToolPtr > SharedToolMap
SharedToolMap m_sharedTools
static DuneToolManager * instance(std::string fclname="", int dbg=1)
T * getShared(std::string name)
QTextStream & endl(QTextStream &s)