EvtTimeShiftFactory.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file EvtTimeShiftFactory.cxx
3 /// \brief factory for generating evgb::EvtTimeShiftI class objects
4 ///
5 /// \version
6 /// \author Robert Hatcher <rhatcher \at fnal.gov>
7 /// Fermi National Accelerator Laboratory
8 ///
9 /// \update 2015-06-22 initial version
10 ////////////////////////////////////////////////////////////////////////
11 
12 #include "EvtTimeShiftFactory.h"
13 #include <iostream>
14 #include <iomanip>
15 #include <sstream>
16 
18 #include "cetlib_except/exception.h"
19 
20 namespace evgb {
21 
22 // Define static variable which holds the one-and-only instance
23 EvtTimeShiftFactory* EvtTimeShiftFactory::fgTheInstance;
24 
26 {
27  fgTheInstance = this; // record created self in static pointer
28 }
29 
31 {
32  fgTheInstance = 0;
33 }
34 
36 {
37  // Cleaner dtor calls EvtTimeShiftFactory dtor at job end
38  static Cleaner cleaner;
39 
40  if ( ! fgTheInstance ) {
41  // need to create one
42  cleaner.UseMe(); // dummy call to quiet compiler warnings
44  }
45 
46  return *fgTheInstance;
47 }
48 
51  const std::string& config) const
52 {
54 
55  mf::LogDebug("EvtTime")
56  << "EvtTimeShiftFactory::GetEvtTimeShift rwh name --->"
57  << name << "<--- \n config -->" << config << "<---" << std::endl;
58 
59  // trim any leading whitespace
60  std::string nameLocal = name;
61  std::string configLocal = "";
62  if( nameLocal.find_first_not_of(" \t\n") != 0 )
63  nameLocal.erase( 0, nameLocal.find_first_not_of(" \t\n") );
64 
65  // in case "name" actually includes the config string
66  size_t iws = nameLocal.find_first_of(" \t\n");
67  if ( iws != std::string::npos ) {
68  configLocal = nameLocal.substr(iws,std::string::npos);
69  configLocal += " ";
70  nameLocal.erase(iws,std::string::npos);
71  }
72  configLocal += config; // append any addition config string
73 
74  mf::LogDebug("EvtTime")
75  << "EvtTimeShiftFactory::GetEvtTimeShift rwh name --->"
76  << nameLocal << "<--- \n config -->" << configLocal << "<---" << std::endl;
77 
78  // we don't want map creating an entry if it doesn't exist
79  // so use map::find() not map::operator[]
81  = fFunctionMap.find(nameLocal);
82  if ( fFunctionMap.end() != itr ) {
83  // found an appropriate entry in the list
84  EvtTimeShiftICtorFuncPtr_t foo = itr->second; // this is the function
85  p = (*foo)(configLocal); // use function to create the EvtTimeShiftI
86  }
87  if ( ! p ) {
88  mf::LogInfo("EvtTime")
89  << "### EvtTimeShiftFactory WARNING: "
90  << "EvtTimeShiftI class \"" << nameLocal << "\" is not known" << std::endl;
91  Print();
92  throw cet::exception("NoEvtTimeShiftClass")
93  << "EvtTimeShiftI class \"" << nameLocal << "\" is not known" << std::endl;
94  }
95  return p;
96 }
97 
99 {
100  // check if we know the name
101  bool res = false;
103  = fFunctionMap.find(name);
104  if ( fFunctionMap.end() != itr ) res = true;
105  return res;
106 }
107 
108 const std::vector<std::string>&
110 {
111  // list of names might be out of date due to new registrations
112  // rescan the std::map on each call (which won't be frequent)
113  listnames.clear();
114 
115  // scan map for registered names
117  for ( itr = fFunctionMap.begin(); itr != fFunctionMap.end(); ++itr )
118  listnames.push_back(itr->first);
119 
120  return listnames;
121 }
122 
124 {
125  std::ostringstream msg;
126  msg << "EvtTimeShiftFactory list of known EvtTimeShiftI classes: \n";
127 
128  const std::vector<std::string>& known = AvailableEvtTimeShift();
129  for (size_t i=0; i < known.size(); ++i) {
130  msg << " [" << std::setw(2) << i << "] " << known[i] << std::endl;
131  }
132  mf::LogInfo("EvtTime") << msg.str();
133 }
134 
137  bool* boolptr)
138 {
139  // record new functions for creating processes
140  fFunctionMap[name] = foo;
141  fBoolPtrMap[name] = boolptr;
142  return true;
143 }
144 
145 } // namespace evgb
static QCString name
Definition: declinfo.cpp:673
intermediate_table::iterator iterator
void msg(const char *fmt,...)
Definition: message.cpp:107
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
intermediate_table::const_iterator const_iterator
static EvtTimeShiftFactory * fgTheInstance
bool IsKnownEvtTimeShift(const std::string &)
const std::vector< std::string > & AvailableEvtTimeShift() const
interface for event time distribution
Definition: EvtTimeShiftI.h:29
std::map< std::string, EvtTimeShiftICtorFuncPtr_t > fFunctionMap
static Config * config
Definition: config.cpp:1054
bool RegisterCreator(std::string name, EvtTimeShiftICtorFuncPtr_t ctorptr, bool *ptr)
std::map< std::string, bool * > fBoolPtrMap
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
p
Definition: test.py:223
std::vector< std::string > listnames
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
A class for generating concrete EvtTimeShiftI derived classes based on the factory pattern...
static EvtTimeShiftFactory & Instance()
evgb::EvtTimeShiftI *(* EvtTimeShiftICtorFuncPtr_t)(const std::string &)
Physics generators for neutrinos, cosmic rays, and others.
Definition: CRYHelper.cxx:33
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
evgb::EvtTimeShiftI * GetEvtTimeShift(const std::string &name, const std::string &config="") const