DuneToolManager.cxx
Go to the documentation of this file.
1 // DuneToolManager.cxx
2 
3 #include "DuneToolManager.h"
6 #include <iostream>
7 #include <fstream>
8 #include <sstream>
9 #include <cctype>
10 
11 using std::string;
12 using std::cout;
13 using std::endl;
14 using std::ifstream;
15 using std::ostringstream;
16 using std::isspace;
17 using Index = unsigned int;
18 using NameVector = std::vector<string>;
19 
20 //**********************************************************************
21 
22 string DuneToolManager::fclFilename(string a_fclname, int dbg) {
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 }
92 
93 //**********************************************************************
94 
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 }
103 
104 //**********************************************************************
105 
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 }
128 
129 //**********************************************************************
130 
132 : m_fclname(fclname) {
133  cet::filepath_lookup_nonabsolute policy("FHICL_FILE_PATH");
134  m_pstools =
136  get<fhicl::ParameterSet>("tools");
138 }
139 
140 //**********************************************************************
141 
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 }
148 
149 //**********************************************************************
150 
151 const std::vector<std::string>& DuneToolManager::toolNames() const {
152  return m_toolNames;
153 }
154 
155 //**********************************************************************
156 
158  cout << "Tools for " << m_fclname << ":" << endl;
159  for ( string name : toolNames() ) {
160  cout << " " << name << endl;
161  }
162 }
163 
164 //**********************************************************************
165 
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 }
180 
181 //**********************************************************************
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
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
void print() const
unsigned int Index
std::string m_fclname
std::vector< std::string > get_pset_names() const
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
intermediate_table parse_document(std::string const &filename, cet::filepath_maker &maker)
Definition: parse.cc:720
std::vector< string > NameVector
void line(double t, double *p, double &x, double &y, double &z)
DuneToolManager(std::string fclname)
int deleteShared(std::string name)
static void help()
union ptb::content::word::word word
SharedToolMap m_sharedTools
static DuneToolManager * instance(std::string fclname="", int dbg=1)
QTextStream & endl(QTextStream &s)