All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | List of all members
opdet::factory::AlgorithmFactory< Base > Class Template Reference

An algorithm factory class. More...

#include <OpRecoFactoryStuff.h>

Classes

struct  AlgoMaker
 
struct  AlgoMakerFor
 Standard algorithm maker class. More...
 
struct  Decl
 Derivative of opdet::factory::Decl with added type check. More...
 

Public Types

using Algo_t = Base
 The algorithm interface this factory produces. More...
 
using Factory_t = AlgorithmFactory< Algo_t >
 This type. More...
 

Public Member Functions

template<typename Algo >
auto declare (std::string name) -> Factory_t &
 
template<typename FirstAlgo , typename... OtherAlgos>
auto declare (opdet::factory::Decl< FirstAlgo > first, opdet::factory::Decl< OtherAlgos >...others) -> Factory_t &
 
Registration of algorithms
template<typename... Algos>
 AlgorithmFactory (opdet::factory::Decl< Algos >...algorithms)
 Constructor: register the specified algorithms. More...
 
template<typename... Algos>
 AlgorithmFactory (std::string algoKey, opdet::factory::Decl< Algos >...algorithms)
 Constructor: register the specified algorithms. More...
 
Factory_tsetAlgorithmConfigurationKey (std::string key)
 Sets the name of the configuration key with the algorithm name. More...
 
template<typename Algo >
Factory_tdeclare (std::string name)
 Registers an algorithm of type Algo associated with a name. More...
 
template<typename FirstAlgo , typename... OtherAlgos>
Factory_tdeclare (opdet::factory::Decl< FirstAlgo > first, opdet::factory::Decl< OtherAlgos >...others)
 Register a sequence of algorithms. More...
 

Queries and algorithm creation

std::string fAlgoNameKey
 Name of the configuration key with algorithm name (empty if not provided). More...
 
std::vector< std::unique_ptr
< AlgoMaker > > 
fMakers
 Registry of all maker classes. More...
 
std::unique_ptr< Algo_tcreate (std::string const &name, fhicl::ParameterSet const &pset) const
 Returns an instance of algorithm name constructed with pset. More...
 
std::unique_ptr< Algo_tcreate (fhicl::ParameterSet const &pset) const
 Creates an instance of the algorithm constructed with pset. More...
 
std::vector< std::string > names () const
 Returns a list with the names of supported algorithms. More...
 
std::string names (std::string const &sep) const
 Returns a string with the names of supported algorithms, joint. More...
 
AlgoMaker const * getMaker (std::string const &name) const
 Returns the maker with the specified name, nullptr if none. More...
 
Factory_tregisterMaker (std::unique_ptr< AlgoMaker > &&maker)
 Adds the maker to the list of registered maker classes. More...
 

Detailed Description

template<typename Base>
class opdet::factory::AlgorithmFactory< Base >

An algorithm factory class.

Template Parameters
Basethe base algorithm class

This class merges the utilities above into a coherent interface, minimizing the maintenance cost at the expense of flexibility.

Example of usage:

HitAlgoFactory {
"Name"
};
// ...
std::unique_ptr<pmtana::PMTPulseRecoBase> algo = HitAlgoFactory.create(pset);

where we assume pset is a configuration for the algorithm, and that it contains the name of the algorithm (one of those declared strings) in the key Name.

Definition at line 79 of file OpRecoFactoryStuff.h.

Member Typedef Documentation

template<typename Base >
using opdet::factory::AlgorithmFactory< Base >::Algo_t = Base

The algorithm interface this factory produces.

Definition at line 143 of file OpRecoFactoryStuff.h.

template<typename Base >
using opdet::factory::AlgorithmFactory< Base >::Factory_t = AlgorithmFactory<Algo_t>

This type.

Definition at line 144 of file OpRecoFactoryStuff.h.

Constructor & Destructor Documentation

template<typename Base >
template<typename... Algos>
opdet::factory::AlgorithmFactory< Base >::AlgorithmFactory ( opdet::factory::Decl< Algos >...  algorithms)
inline

Constructor: register the specified algorithms.

Template Parameters
Algosthe types of algorithms to register
Parameters
algorithmsthe declarators of the algorithms to be registered
See Also
declare()

This constructor declares the specified algorithms like with declare(). In addition, it may be used for example to create a "constant" instance of a factory. The example in declare() becomes:

using PedAlgoFactory_t
PedAlgoFactory_t PedAlgoFactory;
PedAlgoFactory.declare(
);

Definition at line 191 of file OpRecoFactoryStuff.h.

192  : AlgorithmFactory{ "", std::move(algorithms)... } {}
AlgorithmFactory(opdet::factory::Decl< Algos >...algorithms)
Constructor: register the specified algorithms.
template<typename Base >
template<typename... Algos>
opdet::factory::AlgorithmFactory< Base >::AlgorithmFactory ( std::string  algoKey,
opdet::factory::Decl< Algos >...  algorithms 
)

Constructor: register the specified algorithms.

Template Parameters
Algosthe types of algorithms to register
Parameters
algoKeyname of the configuration key whith the algorithm name
algorithmsthe declarators of the algorithms to be registered
See Also
declare(), setAlgorithmConfigurationKey()

In addition to the declaration of algorithms (see declare() or the other constructor), this constructor also sets the configuration key where the algorithm name is expected to be found (

See Also
setAlgorithmConfigurationKey()).

Definition at line 481 of file OpRecoFactoryStuff.h.

482  : fAlgoNameKey{ std::move(algoKey) }
483 {
484  if constexpr(sizeof...(Algos) > 0) declare(std::move(algorithms)...);
485 }
std::string fAlgoNameKey
Name of the configuration key with algorithm name (empty if not provided).
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
Factory_t & declare(std::string name)
Registers an algorithm of type Algo associated with a name.

Member Function Documentation

template<typename Base >
auto opdet::factory::AlgorithmFactory< Base >::create ( std::string const &  name,
fhicl::ParameterSet const &  pset 
) const

Returns an instance of algorithm name constructed with pset.

Parameters
namedeclared name of the algorithm
psetthe configuration of the algorithm
Returns
the newly created algorithm object

The algorithm is constructed with pset as only constructor argument.

Definition at line 515 of file OpRecoFactoryStuff.h.

517 {
518  AlgoMaker const* maker = getMaker(name);
519  if (maker) return maker->makeAlgo(pset);
520 
521  throw cet::exception{ "AlgorithmFactory" }
522  << "Unknown algorithm: '" << name << "'.\nSupported algorithms:\n - '"
523  << names("'\n - '") << "'\n";
524 
525 } // opdet::factory::AlgorithmFactory::create()
std::vector< std::string > names() const
Returns a list with the names of supported algorithms.
AlgoMaker const * getMaker(std::string const &name) const
Returns the maker with the specified name, nullptr if none.
then echo fcl name
template<typename Base >
auto opdet::factory::AlgorithmFactory< Base >::create ( fhicl::ParameterSet const &  pset) const

Creates an instance of the algorithm constructed with pset.

Parameters
psetthe configuration of the algorithm
Returns
the newly created algorithm object

The type of algorithm is discovered from pset itself with the mechanism documented in setAlgorithmConfigurationKey().

For example:

factory.setAlgorithmConfigurationKey("Name");
auto algo = factory.create(pset);

is equivalent to:

auto algo = factory.create(pset.get<std::string>("Name"), pset);

Definition at line 531 of file OpRecoFactoryStuff.h.

532 {
533  return create(pset.get<std::string>(fAlgoNameKey), pset);
534 } // opdet::factory::AlgorithmFactory::create()
std::string fAlgoNameKey
Name of the configuration key with algorithm name (empty if not provided).
std::unique_ptr< Algo_t > create(std::string const &name, fhicl::ParameterSet const &pset) const
Returns an instance of algorithm name constructed with pset.
template<typename Base >
template<typename Algo >
Factory_t& opdet::factory::AlgorithmFactory< Base >::declare ( std::string  name)

Registers an algorithm of type Algo associated with a name.

template<typename Base >
template<typename FirstAlgo , typename... OtherAlgos>
Factory_t& opdet::factory::AlgorithmFactory< Base >::declare ( opdet::factory::Decl< FirstAlgo >  first,
opdet::factory::Decl< OtherAlgos >...  others 
)

Register a sequence of algorithms.

Template Parameters
FirstAlgothe type of the first algorithm to register (mandatory)
OtherAlgosthe types of more algorithms to register
Parameters
firstthe declarator of the first algorithm to be registered
othersthe declarators of more algorithms to be registered
Returns
this factory object

This method allows the declaration of many algorithms at once, one for each declarator in the arguments. For example:

using PedAlgoFactory_t
using PedAlgoDecl = PedAlgoFactory_t::Decl;
PedAlgoFactory_t PedAlgoFactory;
PedAlgoFactory.declare(
PedAlgoDecl<pmtana::PedAlgoEdges>{ "Edges" },
PedAlgoDecl<pmtana::PedAlgoUB >{ "UB" }
);

A constructor is likewise available to register algorithms.

template<typename Base >
template<typename Algo >
auto opdet::factory::AlgorithmFactory< Base >::declare ( std::string  name) -> Factory_t&

Definition at line 491 of file OpRecoFactoryStuff.h.

493 {
494  return registerMaker(std::make_unique<AlgoMakerFor<Algo>>(std::move(name)));
495 } // opdet::factory::AlgorithmFactory::declare(string)
Factory_t & registerMaker(std::unique_ptr< AlgoMaker > &&maker)
Adds the maker to the list of registered maker classes.
then echo fcl name
template<typename Base >
template<typename FirstAlgo , typename... OtherAlgos>
auto opdet::factory::AlgorithmFactory< Base >::declare ( opdet::factory::Decl< FirstAlgo >  first,
opdet::factory::Decl< OtherAlgos >...  others 
) -> Factory_t&

Definition at line 501 of file OpRecoFactoryStuff.h.

505 {
506  declare<FirstAlgo>(std::move(first.name));
507  if constexpr(sizeof...(OtherAlgos) > 0) declare(std::move(others)...);
508  return *this;
509 } // opdet::factory::AlgorithmFactory::declare(Decl)
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
Factory_t & declare(std::string name)
Registers an algorithm of type Algo associated with a name.
std::string name
Name of the algorithm.
createEngine this
template<typename Base >
auto opdet::factory::AlgorithmFactory< Base >::getMaker ( std::string const &  name) const
private

Returns the maker with the specified name, nullptr if none.

Definition at line 549 of file OpRecoFactoryStuff.h.

550 {
551  // we could keep the makers ordered and do a binary search;
552  // not really worth though
553  for (std::unique_ptr<AlgoMaker> const& maker: fMakers)
554  if (maker->name == name) return maker.get();
555  return nullptr;
556 } // opdet::factory::AlgorithmFactory::getMaker()
std::vector< std::unique_ptr< AlgoMaker > > fMakers
Registry of all maker classes.
then echo fcl name
template<typename Base >
std::vector< std::string > opdet::factory::AlgorithmFactory< Base >::names ( ) const

Returns a list with the names of supported algorithms.

Definition at line 539 of file OpRecoFactoryStuff.h.

539  {
540  std::vector<std::string> v;
541  for (auto const& maker: fMakers) v.push_back(maker->name);
542  return v;
543 } // opdet::factory::AlgorithmFactory::names()
std::vector< std::unique_ptr< AlgoMaker > > fMakers
Registry of all maker classes.
template<typename Base >
std::string opdet::factory::AlgorithmFactory< Base >::names ( std::string const &  sep) const
inline

Returns a string with the names of supported algorithms, joint.

Definition at line 319 of file OpRecoFactoryStuff.h.

320  { return join(sep, names()); }
std::vector< std::string > names() const
Returns a list with the names of supported algorithms.
S join(S const &sep, Coll const &s)
Returns a concatenation of strings in s separated by sep.
template<typename Base >
auto opdet::factory::AlgorithmFactory< Base >::registerMaker ( std::unique_ptr< AlgoMaker > &&  maker)
private

Adds the maker to the list of registered maker classes.

Definition at line 562 of file OpRecoFactoryStuff.h.

563 {
564  fMakers.push_back(std::move(maker));
565  return *this;
566 } // opdet::factory::AlgorithmFactory::registerMaker()
std::vector< std::unique_ptr< AlgoMaker > > fMakers
Registry of all maker classes.
template<typename Base >
Factory_t& opdet::factory::AlgorithmFactory< Base >::setAlgorithmConfigurationKey ( std::string  key)
inline

Sets the name of the configuration key with the algorithm name.

Parameters
keyname of the configuration key with the algorithm name
Returns
this factory
See Also
create()

When an algorithm instance is created with create(), it is always possible to specify the name of the algorithm explicitly, using create(std::string const&, fhicl::ParameterSet const&). In addition, if it is known that the algorithm name is in a configuration key of that parameter set, the path of that configuration key can be specified here, and the other version of create(), create(fhicl::ParameterSet const&), can be used which will discover the required algorithm name from the configuration in argument (which is still the same used to construct the algorithm itself).

Example:

factory.setAlgorithmConfigurationKey("Name");
auto algo = factory.create(pset);

is equivalent to:

auto algo = factory.create(pset.get<std::string>("Name"), pset);

Definition at line 238 of file OpRecoFactoryStuff.h.

239  { fAlgoNameKey = std::move(key); return *this; }
std::string fAlgoNameKey
Name of the configuration key with algorithm name (empty if not provided).

Member Data Documentation

template<typename Base >
std::string opdet::factory::AlgorithmFactory< Base >::fAlgoNameKey
private

Name of the configuration key with algorithm name (empty if not provided).

Definition at line 340 of file OpRecoFactoryStuff.h.

template<typename Base >
std::vector<std::unique_ptr<AlgoMaker> > opdet::factory::AlgorithmFactory< Base >::fMakers
private

Registry of all maker classes.

Definition at line 346 of file OpRecoFactoryStuff.h.


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