Belle II Software  release-08-01-10
FindletModule.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <tracking/trackFindingCDC/rootification/StoreWrappedObjPtr.h>
11 
12 #include <tracking/trackFindingCDC/utilities/EvalVariadic.h>
13 
14 #include <framework/core/Module.h>
15 #include <framework/core/ModuleParamList.h>
16 
17 #include <utility>
18 #include <vector>
19 #include <array>
20 
21 namespace Belle2 {
26  namespace TrackFindingCDC {
27 
29  template<class AFindlet>
30  class FindletModule : public Module {
31 
32  private:
34  using Super = Module;
35 
37  using IOTypes = typename AFindlet::IOTypes;
38 
40  template<std::size_t I>
41  using IOType = typename std::tuple_element<I, IOTypes>::type;
42 
44  template<class T>
45  using Stripped = typename std::remove_reference<typename std::remove_const<T>::type>::type;
46 
48  template<std::size_t I>
50 
52  template <std::size_t I>
54 
59  template<std::size_t I>
60  static constexpr bool isInputStoreVector()
61  {
62  return std::is_const<IOType<I>>::value or std::is_reference<IOType<I>>::value;
63  }
64 
66  static const std::size_t c_nTypes = std::tuple_size<IOTypes>::value;
67 
69  using Indices = std::make_index_sequence<c_nTypes>;
70 
71  public:
73  explicit FindletModule(const std::array<std::string, c_nTypes>& storeVectorNames = {})
74  : m_param_storeVectorNames(storeVectorNames)
75  {
77  std::string description = "Findlet: ";
78  if (std::tuple_size<IOTypes>() == 0) {
79  // Drop Findlet prefix for full finders with no IOTypes
80  description = "";
81  }
82 
83  description += m_findlet.getDescription();
84  this->setDescription(description);
85 
87 
88  ModuleParamList moduleParamList = this->getParamList();
89 
90  const std::string prefix = "";
91  m_findlet.exposeParameters(&moduleParamList, prefix);
92 
93  this->setParamList(moduleParamList);
94  }
95 
97  virtual ~FindletModule() = default;
98 
100  virtual void initialize() override
101  {
104  m_findlet.initialize();
105  }
106 
108  virtual void beginRun() override
109  {
110  Super::beginRun();
111  m_findlet.beginRun();
112  }
113 
115  virtual void event() override
116  {
117  m_findlet.beginEvent();
118  this->createStoreVectors(Indices());
120  }
121 
123  virtual void endRun() override
124  {
125  m_findlet.endRun();
126  Super::endRun();
127  }
128 
130  virtual void terminate() override
131  {
132  m_findlet.terminate();
134  }
135 
136  private:
138  template <size_t... Is>
139  void applyFindlet(std::index_sequence<Is...>)
140  {
141  m_findlet.apply(*(getStoreVector<Is>())...);
142  }
143 
145  template <size_t... Is>
146  void createStoreVectors(std::index_sequence<Is...>)
147  {
148  evalVariadic((createStoreVector<Is>(), std::ignore)...);
149  }
150 
152  template <size_t... Is>
153  void requireOrRegisterStoreVectors(std::index_sequence<Is...>)
154  {
155  evalVariadic((requireStoreVector<Is>(), std::ignore) ...);
156  evalVariadic((registerStoreVector<Is>(), std::ignore) ...);
157  }
158 
164  template <std::size_t I>
166  {
167  if (isInputStoreVector<I>()) {
168  getStoreVector<I>().isRequired();
169  }
170  }
171 
173  template <std::size_t I>
175  {
176  if (not isInputStoreVector<I>()) {
177  getStoreVector<I>().registerInDataStore(DataStore::c_DontWriteOut | DataStore::c_ErrorIfAlreadyRegistered);
178  }
179  }
180 
182  template<std::size_t I>
184  {
185  if (not getStoreVector<I>().isValid()) {
186  getStoreVector<I>().construct();
187  }
188  }
189 
191  template<std::size_t I>
193  {
195  return storeVector;
196  }
197 
199  template<size_t ... Is>
200  void addStoreVectorParameters(std::index_sequence<Is...>)
201  {
202  evalVariadic((addStoreVectorParameter<Is>(), std::ignore)...);
203  }
204 
206  template<std::size_t I>
208  {
209  std::string name = getStoreVectorParameterName<I>();
210  std::string description = getStoreVectorParameterDescription<I>();
211  if (m_param_storeVectorNames[I] == "") {
212  // Make a forced parameter
213  this->addParam(name,
215  description);
216  } else {
217  // Make an unforced parameter
218  this->addParam(name,
220  description,
222  }
223  }
224 
226  template <std::size_t I>
227  std::string getStoreVectorParameterName() const
228  {
229  bool primary = I == GetIndexInTuple<IOType<I>, IOTypes>::value;
230  int order = primary ? 1 : 2;
231  bool input = isInputStoreVector<I>();
232 
233  // Just a little bit of ADL
234  std::string classParameterName = getClassMnemomicParameterName(static_cast<StrippedIOType<I>*>(nullptr));
235  return getStoreVectorParameterName(classParameterName, order, input);
236  }
237 
239  template<std::size_t I>
241  {
242  bool primary = I == GetIndexInTuple<IOType<I>, IOTypes>::value;
243  int order = primary ? 1 : 2;
244  bool input = isInputStoreVector<I>();
245 
246  // Just a little bit of ADL
247  std::string classParameterDescription = getClassMnemomicParameterDescription(static_cast<StrippedIOType<I>*>(nullptr));
248  return getStoreVectorParameterDescription(classParameterDescription, order, input);
249  }
250 
257  std::string
258  getStoreVectorParameterName(std::string classMnemonic, int order, bool input) const
259  {
260  std::string orderPrefix;
261  if (order == 1) {
262  orderPrefix = "";
263  } else if (order == 2) {
264  orderPrefix = "secondary";
265  } else {
266  B2ERROR("More than two inputs of the same type are not supported");
267  }
268  std::string inputPrefix = input ? "input" : "";
269 
270  if (input or order > 1) {
271  classMnemonic[0] = ::toupper(classMnemonic.at(0));
272  }
273  if (input and order > 1) {
274  inputPrefix[0] = ::toupper(inputPrefix.at(0));
275  }
276 
277  return orderPrefix + inputPrefix + classMnemonic + "s";
278  }
279 
286  std::string getStoreVectorParameterDescription(const std::string& classMnemonic, int order, bool input) const
287  {
288  std::string orderPrefix;
289  if (order == 1) {
290  orderPrefix = "";
291  } else if (order == 2) {
292  orderPrefix = "secondary ";
293  } else {
294  B2ERROR("More than two inputs of the same type are not supported");
295  }
296  std::string inputPrefix = input ? "input " : "";
297  return "Name of the " + orderPrefix + inputPrefix + classMnemonic + " vector.";
298  }
299 
300  private:
302  std::array<std::string, c_nTypes> m_param_storeVectorNames;
303 
305  AFindlet m_findlet;
306  };
307  }
309 }
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition: DataStore.h:71
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:72
The Module parameter list class.
Base class for Modules.
Definition: Module.h:72
const ModuleParamList & getParamList() const
Return module param list.
Definition: Module.h:363
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
Module()
Constructor.
Definition: Module.cc:30
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Definition: Module.h:83
virtual void initialize()
Initialize the Module.
Definition: Module.h:109
virtual void beginRun()
Called when entering a new run.
Definition: Module.h:147
virtual void endRun()
This method is called if the current run ends.
Definition: Module.h:166
void setParamList(const ModuleParamList &params)
Replace existing parameter list.
Definition: Module.h:501
virtual void terminate()
This method is called at the end of the event processing.
Definition: Module.h:176
Adapter of a findlet to a module that exposes the parameters of the findlet and manages the IO with t...
Definition: FindletModule.h:30
typename AFindlet::IOTypes IOTypes
Tuple of input / output types of the findlet.
Definition: FindletModule.h:37
virtual void endRun() override
Signal the end of the run.
virtual void terminate() override
Signal to terminate the event processing.
static constexpr bool isInputStoreVector()
Test if the given io type designates an input store vector.
Definition: FindletModule.h:60
Stripped< IOType< I > > StrippedIOType
Accessor for the individual input / output types of the findlet stripped from modifiers.
Definition: FindletModule.h:49
std::array< std::string, c_nTypes > m_param_storeVectorNames
Parameters : Names of the vectors on the DataStore.
FindletModule(const std::array< std::string, c_nTypes > &storeVectorNames={})
Constructor of the module.
Definition: FindletModule.h:73
std::string getStoreVectorParameterDescription() const
Get parameter description for the StoreVector at index I.
void registerStoreVector()
Register the vector with index I to the DataStore.
std::string getStoreVectorParameterDescription(const std::string &classMnemonic, int order, bool input) const
Compose a parameter description for the name of the vector on the DataStore.
typename std::tuple_element< I, IOTypes >::type IOType
Accessor for the individual input / output types of the findlet.
Definition: FindletModule.h:41
void addStoreVectorParameters(std::index_sequence< Is... >)
Expose parameters to set the names of the vectors on the DataStore.
virtual void event() override
Start processing the current event.
AFindlet m_findlet
Findlet that implements the algorithm to be executed.
StoreVector< I > getStoreVector()
Get the vector with index I from the DataStore.
static const std::size_t c_nTypes
Number of typpes served to the findlet.
Definition: FindletModule.h:66
void createStoreVector()
Create the vector with index I on the DataStore.
virtual void initialize() override
Initialize the Module before event processing.
typename std::remove_reference< typename std::remove_const< T >::type >::type Stripped
Type function to strip constant and reference qualification form a given type.
Definition: FindletModule.h:45
std::make_index_sequence< c_nTypes > Indices
Helper class to iterate over the individual types served to the findlet.
Definition: FindletModule.h:69
virtual void beginRun() override
Signal the beginning of a new run.
void requireStoreVector()
Require the vector with index I to be on the DataStore.
std::string getStoreVectorParameterName() const
Get parameter name for the StoreVector at index I.
void applyFindlet(std::index_sequence< Is... >)
Get the vectors from the DataStore and apply the findlet.
std::string getStoreVectorParameterName(std::string classMnemonic, int order, bool input) const
Compose a parameter name for the name of the vector on the DataStore.
void requireOrRegisterStoreVectors(std::index_sequence< Is... >)
Require or register the vectors on the DataStore.
virtual ~FindletModule()=default
Make destructor of interface virtual.
void createStoreVectors(std::index_sequence< Is... >)
Create the vectors on the DataStore.
void addStoreVectorParameter()
Expose parameter to set the names of the vector with index I on the DataStore.
This class is for convenience access and registration of objects, that are stored inside the StoreWra...
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
std::string getClassMnemomicParameterDescription(const RecoTrack *dispatchTag)
Returns a short description for class RecoTrack to be used in descriptions of parameters.
std::string getClassMnemomicParameterName(const RecoTrack *dispatchTag)
Returns a short name for class RecoTrack to be used in names of parameters.
Abstract base class for different kinds of events.
Looks up, at which index the given Type can be found in a tuple.
Definition: EvalVariadic.h:78