Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
DuneToolManager Class Reference

#include <DuneToolManager.h>

Classes

class  SharedToolEntry
 
class  TSharedToolEntry
 

Public Types

using Name = std::string
 
using NameVector = std::vector< Name >
 
using SharedToolPtr = std::unique_ptr< SharedToolEntry >
 
using SharedToolMap = std::map< Name, SharedToolPtr >
 

Public Member Functions

 DuneToolManager (std::string fclname)
 
template<class T >
std::unique_ptr< T > getPrivate (std::string name)
 
template<class T >
T * getShared (std::string name)
 
int deleteShared (std::string name)
 
const std::vector< std::string > & toolNames () const
 
void print () const
 

Static Public Member Functions

static std::string fclFilename (std::string setName="", int dbg=1)
 
static DuneToolManagerinstance (std::string fclname="", int dbg=1)
 
static void help ()
 

Private Member Functions

int makeParameterSet (std::string scfg, fhicl::ParameterSet &ps)
 

Private Attributes

std::string m_fclname
 
fhicl::ParameterSet m_pstools
 
NameVector m_toolNames
 
SharedToolMap m_sharedTools
 

Detailed Description

Definition at line 34 of file DuneToolManager.h.

Member Typedef Documentation

Definition at line 38 of file DuneToolManager.h.

Definition at line 39 of file DuneToolManager.h.

Definition at line 58 of file DuneToolManager.h.

Definition at line 57 of file DuneToolManager.h.

Constructor & Destructor Documentation

DuneToolManager::DuneToolManager ( std::string  fclname)
explicit

Definition at line 131 of file DuneToolManager.cxx.

132 : m_fclname(fclname) {
133  cet::filepath_lookup_nonabsolute policy("FHICL_FILE_PATH");
134  m_pstools =
136  get<fhicl::ParameterSet>("tools");
138 }
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
std::string m_fclname
std::vector< std::string > get_pset_names() const
NameVector m_toolNames
fhicl::ParameterSet m_pstools
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
Definition: parse.cc:720

Member Function Documentation

int DuneToolManager::deleteShared ( std::string  name)

Definition at line 142 of file DuneToolManager.cxx.

142  {
143  SharedToolMap::iterator itoo = m_sharedTools.find(tnam);
144  if ( itoo == m_sharedTools.end() ) return 1;
145  m_sharedTools.erase(tnam);
146  return 0;
147 }
intermediate_table::iterator iterator
SharedToolMap m_sharedTools
string DuneToolManager::fclFilename ( std::string  setName = "",
int  dbg = 1 
)
static

Definition at line 22 of file DuneToolManager.cxx.

22  {
23  const string myname = "DuneToolManager::fclFilename: ";
24  if ( dbg >= 2 ) cout << myname << "Called with " << a_fclname << endl;
25  static string fclname;
26  bool haveName = fclname.size();
27  bool setName = a_fclname.size();
28  if ( !haveName && !setName ) {
29  // Use ps to discover the command line.
30  Index pid = getpid();
31  ostringstream ssftmp;
32  ssftmp << "tmpproc" << pid << ".tmp";
33  string sftmp = ssftmp.str();
34  ostringstream sscom;
35  sscom << "ps -fwwp " << pid << " >" << sftmp;
36  string scom = sscom.str();
37  system(scom.c_str());
38  ifstream fin(sftmp.c_str());
39  string hdrline;
40  getline(fin, hdrline); // Skip header
41  string::size_type iposCom = hdrline.find("CMD");
42  string longline;
43  getline(fin, longline);
44  string line = longline.substr(iposCom);
45  scom = "rm " + sftmp;
46  system(scom.c_str());
47  if ( dbg >= 1 ) cout << myname << "Taking fcl name from command line: " << line << endl;
48  // Split command line into words.
49  NameVector words;
50  bool newWord = true;
51  for ( string::size_type ipos=0; ipos<line.size(); ++ipos ) {
52  char ch = line[ipos];
53  //if ( ch == '\0' || ch == 0 || isspace(ch) ) {
54  if ( isspace(ch) ) {
55  newWord = true;
56  } else {
57  if ( newWord ) {
58  newWord = false;
59  words.emplace_back();
60  }
61  words.back() += ch;
62  }
63  }
64  // Find the flag "-c" or "--config" and use following text as the fcl name.
65  for ( Index iwrd=0; iwrd<words.size(); ++iwrd ) {
66  string word = words[iwrd];
67  if ( word == "-c" || word == "--config") {
68  if ( words.size() > iwrd+1 ) fclname = words[iwrd+1];
69  } else if ( word.substr(0,2) == "-c" ) {
70  fclname = word.substr(2);
71  }
72  }
73  // If name was not found, switch to a default.
74  if ( fclname.empty() ) {
75  cout << myname << "ERROR: unable to retrieve configuration file name from command line." << endl;
76  fclname = "tools_dune.fcl";
77  }
78  } else if ( !haveName && setName ) {
79  fclname = a_fclname;
80  } else if ( haveName && setName ) {
81  if ( a_fclname != fclname ) {
82  cout << myname << "WARNING: Ignoring inconsistent configuration name: "
83  << a_fclname << " != " << fclname << endl;
84  }
85  }
86  if ( fclname.empty() ) {
87  cout << myname << "ERROR: Unexpected empty file name!" << endl;
88  fclname = "nosuchfile.fcl";
89  }
90  return fclname;
91 }
bool dbg
unsigned int Index
std::vector< string > NameVector
void line(double t, double *p, double &x, double &y, double &z)
union ptb::content::word::word word
QTextStream & endl(QTextStream &s)
template<class T >
std::unique_ptr<T> DuneToolManager::getPrivate ( std::string  name)
inline

Definition at line 82 of file DuneToolManager.h.

82  {
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  }
static QCString name
Definition: declinfo.cpp:673
int makeParameterSet(std::string scfg, fhicl::ParameterSet &ps)
std::string string
Definition: nybbler.cc:12
uint size() const
Definition: qcstring.h:201
T get(std::string const &key) const
Definition: ParameterSet.h:271
NameVector m_toolNames
fhicl::ParameterSet m_pstools
QTextStream & endl(QTextStream &s)
template<class T >
T* DuneToolManager::getShared ( std::string  name)
inline

Definition at line 101 of file DuneToolManager.h.

101  {
102  std::string myname = "DuneToolManager::getShared: ";
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  }
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
std::string string
Definition: nybbler.cc:12
std::unique_ptr< SharedToolEntry > SharedToolPtr
T get(std::string const &key) const
Definition: ParameterSet.h:271
NameVector m_toolNames
fhicl::ParameterSet m_pstools
SharedToolMap m_sharedTools
QTextStream & endl(QTextStream &s)
void DuneToolManager::help ( )
static

Definition at line 106 of file DuneToolManager.cxx.

106  {
107  cout << "DuneToolManager provides access to named tools." << endl;
108  cout << "It is typically accessed a singleton, e.g." << endl;
109  cout << " DuneToolManager* ptm = DuneToolManger::instance();" << endl;
110  cout << "The name of the fcl file defining the tools may be provided as an argument." << endl;
111  cout << endl;
112  cout << "Available tools may be listed with:" << endl;
113  cout << " ptm->print() // Displays the names of available tools." << endl;
114  cout << " ptm->toolNames() // Returns a vector of the tool names." << endl;
115  cout << endl;
116  cout << "A private instance of a tool with type MyTool may be otained with:" << endl;
117  cout << " ptm->getPrivate<MyTool>(\"tnam\")" << endl;
118  cout << "The tool configuration is taken from the fcl block tools.tnam." << endl;
119  cout << "A unique pointer is returned so the tool instance is destroyed with that pointer." << endl;
120  cout << endl;
121  cout << "A shared instance of a tool may be obtained with:" << endl;
122  cout << " ptm->getShared<MyTool>(\"tnam\")" << endl;
123  cout << "A bare pointer is returned and subsequent callers receive the same pointer." << endl;
124  cout << "The tool manager deletes all shared tools when it is destroyed." << endl;
125  cout << endl;
126  cout << "In either case, tool instances are created only when they are obtained." << endl;
127 }
QTextStream & endl(QTextStream &s)
DuneToolManager * DuneToolManager::instance ( std::string  fclname = "",
int  dbg = 1 
)
static

Definition at line 95 of file DuneToolManager.cxx.

95  {
96  const string myname = "DuneToolManager::instance: ";
97  if ( dbg >= 2 ) cout << myname << "Called with " << a_fclname << endl;
98  static std::unique_ptr<DuneToolManager> pins;
99  string fclname = fclFilename(a_fclname, dbg);
100  if ( !pins ) pins.reset(new DuneToolManager(fclname));
101  return pins.get();
102 }
bool dbg
static std::string fclFilename(std::string setName="", int dbg=1)
DuneToolManager(std::string fclname)
QTextStream & endl(QTextStream &s)
int DuneToolManager::makeParameterSet ( std::string  scfg,
fhicl::ParameterSet ps 
)
private

Definition at line 166 of file DuneToolManager.cxx.

166  {
167  // Strip surrounding braces.
168  if ( scfgin.size() < 1 ) return 1;
169  string scfg;
170  if ( scfgin[0] == '{' ) {
171  if ( scfgin[scfgin.size()-1] != '}' ) return 2;
172  scfg = scfgin.substr(1, scfgin.size()-2);
173  } else {
174  scfg = scfgin;
175  }
176  // Parse the configuration string.
177  ps = fhicl::ParameterSet::make(scfg);
178  return 0;
179 }
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
void DuneToolManager::print ( ) const

Definition at line 157 of file DuneToolManager.cxx.

157  {
158  cout << "Tools for " << m_fclname << ":" << endl;
159  for ( string name : toolNames() ) {
160  cout << " " << name << endl;
161  }
162 }
static QCString name
Definition: declinfo.cpp:673
const std::vector< std::string > & toolNames() const
std::string m_fclname
QTextStream & endl(QTextStream &s)
const std::vector< std::string > & DuneToolManager::toolNames ( ) const

Definition at line 151 of file DuneToolManager.cxx.

151  {
152  return m_toolNames;
153 }
NameVector m_toolNames

Member Data Documentation

std::string DuneToolManager::m_fclname
private

Definition at line 142 of file DuneToolManager.h.

fhicl::ParameterSet DuneToolManager::m_pstools
private

Definition at line 143 of file DuneToolManager.h.

SharedToolMap DuneToolManager::m_sharedTools
private

Definition at line 145 of file DuneToolManager.h.

NameVector DuneToolManager::m_toolNames
private

Definition at line 144 of file DuneToolManager.h.


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