Belle II Software  release-05-02-19
MeasurementCreatorFactory.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <framework/logging/Logger.h>
13 
14 #include <map>
15 #include <vector>
16 #include <memory>
17 
18 namespace Belle2 {
33  template <class BaseMeasurementCreatorType>
34  class MeasurementCreatorFactory {
35  public:
37  typedef BaseMeasurementCreatorType CreatorType;
38 
40  void initialize()
41  {
42  for (const auto& creatorWithParameterDictionary : m_creatorsWithParametersDictionary) {
43  const std::string& creatorName = creatorWithParameterDictionary.first;
44  const std::map<std::string, std::string>& parameterDictionary = creatorWithParameterDictionary.second;
45 
46  B2DEBUG(100, "Creating measurement creator with name " << creatorName);
47 
48  BaseMeasurementCreatorType* creatorPointer = createMeasurementCreatorFromName(creatorName);
49  if (creatorPointer == nullptr) {
50  B2FATAL("Can not create a measurement creator with the name " << creatorName << ". Creator not known to the factory.");
51  }
52  m_measurementCreators.push_back(std::move(std::shared_ptr<BaseMeasurementCreatorType>(creatorPointer)));
53 
54  for (const auto& parameterWithValue : parameterDictionary) {
55  const std::string& parameterName = parameterWithValue.first;
56  const std::string& parameterValue = parameterWithValue.second;
57  creatorPointer->setParameter(parameterName, parameterValue);
58  }
59  }
60  }
61 
63  virtual BaseMeasurementCreatorType* createMeasurementCreatorFromName(const std::string& /*creatorName*/) const
64  {
65  return nullptr;
66  }
67 
69  const std::vector<std::shared_ptr<BaseMeasurementCreatorType>>& getCreators() const
70  {
72  }
73 
75  std::map<std::string, std::map<std::string, std::string>>& getParameters()
76  {
78  }
79 
81  void setParameters(const std::map<std::string, std::map<std::string, std::string>>& creatorsWithParametersDictionary)
82  {
83  m_creatorsWithParametersDictionary = std::move(creatorsWithParametersDictionary);
84  }
85 
86  private:
88  std::vector<std::shared_ptr<BaseMeasurementCreatorType>> m_measurementCreators;
89 
91  std::map<std::string, std::map<std::string, std::string>> m_creatorsWithParametersDictionary;
92  };
94 }
Belle2::MeasurementCreatorFactory::m_creatorsWithParametersDictionary
std::map< std::string, std::map< std::string, std::string > > m_creatorsWithParametersDictionary
The map of dictionaries of the parameters.
Definition: MeasurementCreatorFactory.h:99
Belle2::MeasurementCreatorFactory::m_measurementCreators
std::vector< std::shared_ptr< BaseMeasurementCreatorType > > m_measurementCreators
A vector with the measurement creators.
Definition: MeasurementCreatorFactory.h:96
Belle2::MeasurementCreatorFactory::createMeasurementCreatorFromName
virtual BaseMeasurementCreatorType * createMeasurementCreatorFromName(const std::string &) const
Overload this method to create the measurement creators by their name.
Definition: MeasurementCreatorFactory.h:71
Belle2::MeasurementCreatorFactory::getCreators
const std::vector< std::shared_ptr< BaseMeasurementCreatorType > > & getCreators() const
Return the creators to the module.
Definition: MeasurementCreatorFactory.h:77
Belle2::MeasurementCreatorFactory::CreatorType
BaseMeasurementCreatorType CreatorType
Typedef for convenience.
Definition: MeasurementCreatorFactory.h:45
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MeasurementCreatorFactory::setParameters
void setParameters(const std::map< std::string, std::map< std::string, std::string >> &creatorsWithParametersDictionary)
Set the parameters.
Definition: MeasurementCreatorFactory.h:89
Belle2::MeasurementCreatorFactory::getParameters
std::map< std::string, std::map< std::string, std::string > > & getParameters()
Return a reference to the parameters you can use in the module.
Definition: MeasurementCreatorFactory.h:83
Belle2::MeasurementCreatorFactory::initialize
void initialize()
Use the parameters given to the module and create the measurement creators from them.
Definition: MeasurementCreatorFactory.h:48