Belle II Software  release-05-02-19
FindletModule.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/rootification/StoreWrappedObjPtr.h>
13 
14 #include <tracking/trackFindingCDC/utilities/EvalVariadic.h>
15 
16 #include <framework/core/Module.h>
17 #include <framework/core/ModuleParamList.h>
18 
19 #include <utility>
20 #include <vector>
21 #include <array>
22 
23 namespace Belle2 {
28  namespace TrackFindingCDC {
29 
31  template<class AFindlet>
32  class FindletModule : public Module {
33 
34  private:
36  using Super = Module;
37 
39  using IOTypes = typename AFindlet::IOTypes;
40 
42  template<std::size_t I>
43  using IOType = typename std::tuple_element<I, IOTypes>::type;
44 
46  template<class T>
47  using Stripped = typename std::remove_reference<typename std::remove_const<T>::type>::type;
48 
50  template<std::size_t I>
52 
54  template <std::size_t I>
56 
61  template<std::size_t I>
62  static constexpr bool isInputStoreVector()
63  {
64  return std::is_const<IOType<I>>::value or std::is_reference<IOType<I>>::value;
65  }
66 
68  static const std::size_t c_nTypes = std::tuple_size<IOTypes>::value;
69 
71  using Indices = std::make_index_sequence<c_nTypes>;
72 
73  public:
75  explicit FindletModule(const std::array<std::string, c_nTypes>& storeVectorNames = {})
76  : m_param_storeVectorNames(storeVectorNames)
77  {
79  std::string description = "Findlet: ";
80  if (std::tuple_size<IOTypes>() == 0) {
81  // Drop Findlet prefix for full finders with no IOTypes
82  description = "";
83  }
84 
85  description += m_findlet.getDescription();
86  this->setDescription(description);
87 
89 
90  ModuleParamList moduleParamList = this->getParamList();
91 
92  const std::string prefix = "";
93  m_findlet.exposeParameters(&moduleParamList, prefix);
94 
95  this->setParamList(moduleParamList);
96  }
97 
99  virtual ~FindletModule() = default;
100 
102  virtual void initialize() override
103  {
106  m_findlet.initialize();
107  }
108 
110  virtual void beginRun() override
111  {
112  Super::beginRun();
113  m_findlet.beginRun();
114  }
115 
117  virtual void event() override
118  {
119  m_findlet.beginEvent();
120  this->createStoreVectors(Indices());
122  }
123 
125  virtual void endRun() override
126  {
127  m_findlet.endRun();
128  Super::endRun();
129  }
130 
132  virtual void terminate() override
133  {
134  m_findlet.terminate();
136  }
137 
138  private:
140  template <size_t... Is>
141  void applyFindlet(std::index_sequence<Is...>)
142  {
143  m_findlet.apply(*(getStoreVector<Is>())...);
144  }
145 
147  template <size_t... Is>
148  void createStoreVectors(std::index_sequence<Is...>)
149  {
150  evalVariadic((createStoreVector<Is>(), std::ignore)...);
151  }
152 
154  template <size_t... Is>
155  void requireOrRegisterStoreVectors(std::index_sequence<Is...>)
156  {
157  evalVariadic((requireStoreVector<Is>(), std::ignore) ...);
158  evalVariadic((registerStoreVector<Is>(), std::ignore) ...);
159  }
160 
166  template <std::size_t I>
167  void requireStoreVector()
168  {
169  if (isInputStoreVector<I>()) {
170  getStoreVector<I>().isRequired();
171  }
172  }
173 
175  template <std::size_t I>
176  void registerStoreVector()
177  {
178  if (not isInputStoreVector<I>()) {
179  getStoreVector<I>().registerInDataStore(DataStore::c_DontWriteOut | DataStore::c_ErrorIfAlreadyRegistered);
180  }
181  }
182 
184  template<std::size_t I>
185  void createStoreVector()
186  {
187  if (not getStoreVector<I>().isValid()) {
188  getStoreVector<I>().construct();
189  }
190  }
191 
193  template<std::size_t I>
195  {
197  return storeVector;
198  }
199 
201  template<size_t ... Is>
202  void addStoreVectorParameters(std::index_sequence<Is...>)
203  {
204  evalVariadic((addStoreVectorParameter<Is>(), std::ignore)...);
205  }
206 
208  template<std::size_t I>
210  {
211  std::string name = getStoreVectorParameterName<I>();
212  std::string description = getStoreVectorParameterDescription<I>();
213  if (m_param_storeVectorNames[I] == "") {
214  // Make a forced parameter
215  this->addParam(name,
217  description);
218  } else {
219  // Make an unforced parameter
220  this->addParam(name,
222  description,
224  }
225  }
226 
228  template <std::size_t I>
229  std::string getStoreVectorParameterName() const
230  {
231  bool primary = I == GetIndexInTuple<IOType<I>, IOTypes>::value;
232  int order = primary ? 1 : 2;
233  bool input = isInputStoreVector<I>();
234 
235  // Just a little bit of ADL
236  std::string classParameterName = getClassMnemomicParameterName(static_cast<StrippedIOType<I>*>(nullptr));
237  return getStoreVectorParameterName(classParameterName, order, input);
238  }
239 
241  template<std::size_t I>
242  std::string getStoreVectorParameterDescription() const
243  {
244  bool primary = I == GetIndexInTuple<IOType<I>, IOTypes>::value;
245  int order = primary ? 1 : 2;
246  bool input = isInputStoreVector<I>();
247 
248  // Just a little bit of ADL
249  std::string classParameterDescription = getClassMnemomicParameterDescription(static_cast<StrippedIOType<I>*>(nullptr));
250  return getStoreVectorParameterDescription(classParameterDescription, order, input);
251  }
252 
258  std::string
259  getStoreVectorParameterName(std::string classMnenomic, int order, bool input) const
260  {
261  std::string orderPrefix;
262  if (order == 1) {
263  orderPrefix = "";
264  } else if (order == 2) {
265  orderPrefix = "secondary";
266  } else {
267  B2ERROR("More than two inputs of the same type are not supported");
268  }
269  std::string inputPrefix = input ? "input" : "";
270 
271  if (input or order > 1) {
272  classMnenomic[0] = ::toupper(classMnenomic.at(0));
273  }
274  if (input and order > 1) {
275  inputPrefix[0] = ::toupper(inputPrefix.at(0));
276  }
277 
278  return orderPrefix + inputPrefix + classMnenomic + "s";
279  }
280 
286  std::string getStoreVectorParameterDescription(const std::string& classMnenomic, 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 + classMnenomic + " vector.";
298  }
299 
300  private:
302  std::array<std::string, c_nTypes> m_param_storeVectorNames;
303 
305  AFindlet m_findlet;
306  };
307  }
309 }
Belle2::TrackFindingCDC::FindletModule::requireOrRegisterStoreVectors
void requireOrRegisterStoreVectors(std::index_sequence< Is... >)
Require or register the vectors on the DataStore.
Definition: FindletModule.h:163
Belle2::Module::terminate
virtual void terminate()
This method is called at the end of the event processing.
Definition: Module.h:178
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::TrackFindingCDC::FindletModule::applyFindlet
void applyFindlet(std::index_sequence< Is... >)
Get the vectors from the DataStore and apply the findlet.
Definition: FindletModule.h:149
Belle2::TrackFindingCDC::FindletModule< WireHitCreator >::Stripped
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:55
Belle2::Module::c_ParallelProcessingCertified
@ 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:82
Belle2::TrackFindingCDC::FindletModule::~FindletModule
virtual ~FindletModule()=default
Make destructor of interface virtual.
Belle2::Module::c_TerminateInAllProcesses
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Definition: Module.h:85
Belle2::TrackFindingCDC::FindletModule::createStoreVectors
void createStoreVectors(std::index_sequence< Is... >)
Create the vectors on the DataStore.
Definition: FindletModule.h:156
Belle2::TrackFindingCDC::FindletModule::endRun
virtual void endRun() override
Signal the end of the run.
Definition: FindletModule.h:133
Belle2::TrackFindingCDC::FindletModule::initialize
virtual void initialize() override
Initialize the Module before event processing.
Definition: FindletModule.h:110
Belle2::TrackFindingCDC::StoreWrappedObjPtr
This class is for convenience access and registration of objects, that are stored inside the StoreWra...
Definition: StoreWrappedObjPtr.h:40
Belle2::TrackFindingCDC::FindletModule::registerStoreVector
void registerStoreVector()
Register the vector with index I to the DataStore.
Definition: FindletModule.h:184
Belle2::DataStore::c_DontWriteOut
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition: DataStore.h:73
Belle2::TrackFindingCDC::FindletModule::IOTypes
typename AFindlet::IOTypes IOTypes
Tuple of input / output types of the findlet.
Definition: FindletModule.h:47
Belle2::getClassMnemomicParameterDescription
std::string getClassMnemomicParameterDescription(const RecoTrack *dispatchTag __attribute__((unused)))
Returns a short description for class RecoTrack to be used in descriptions of parameters.
Definition: ClassMnemomics.h:33
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::TrackFindingCDC::FindletModule::Super
Module Super
Type of the base class.
Definition: FindletModule.h:44
Belle2::TrackFindingCDC::FindletModule::getStoreVectorParameterName
std::string getStoreVectorParameterName() const
Get parameter name for the StoreVector at index I.
Definition: FindletModule.h:237
Belle2::TrackFindingCDC::FindletModule::Indices
std::make_index_sequence< c_nTypes > Indices
Helper class to iterate over the individual types served to the findlet.
Definition: FindletModule.h:79
Belle2::TrackFindingCDC::FindletModule::FindletModule
FindletModule(const std::array< std::string, c_nTypes > &storeVectorNames={})
Constructor of the module.
Definition: FindletModule.h:83
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Module::endRun
virtual void endRun()
This method is called if the current run ends.
Definition: Module.h:168
Belle2::TrackFindingCDC::FindletModule::m_findlet
AFindlet m_findlet
Findlet that implements the algorithm to be executed.
Definition: FindletModule.h:313
Belle2::TrackFindingCDC::FindletModule::isInputStoreVector
static constexpr bool isInputStoreVector()
Test if the given io type designates an input store vector.
Definition: FindletModule.h:70
Belle2::TrackFindingCDC::FindletModule::getStoreVectorParameterDescription
std::string getStoreVectorParameterDescription() const
Get parameter description for the StoreVector at index I.
Definition: FindletModule.h:250
Belle2::TrackFindingCDC::FindletModule::addStoreVectorParameters
void addStoreVectorParameters(std::index_sequence< Is... >)
Expose parameters to set the names of the vectors on the DataStore.
Definition: FindletModule.h:210
Belle2::TrackFindingCDC::FindletModule::c_nTypes
static const std::size_t c_nTypes
Number of typpes served to the findlet.
Definition: FindletModule.h:76
Belle2::TrackFindingCDC::FindletModule::terminate
virtual void terminate() override
Signal to terminate the event processing.
Definition: FindletModule.h:140
Belle2::TrackFindingCDC::FindletModule::createStoreVector
void createStoreVector()
Create the vector with index I on the DataStore.
Definition: FindletModule.h:193
Belle2::TrackFindingCDC::FindletModule::event
virtual void event() override
Start processing the current event.
Definition: FindletModule.h:125
Belle2::TrackFindingCDC::FindletModule::getStoreVector
StoreVector< I > getStoreVector()
Get the vector with index I from the DataStore.
Definition: FindletModule.h:202
Belle2::TrackFindingCDC::FindletModule::IOType
typename std::tuple_element< I, IOTypes >::type IOType
Accessor for the individual input / output types of the findlet.
Definition: FindletModule.h:51
Belle2::DataStore::c_ErrorIfAlreadyRegistered
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:74
Belle2::TrackFindingCDC::FindletModule::requireStoreVector
void requireStoreVector()
Require the vector with index I to be on the DataStore.
Definition: FindletModule.h:175
Belle2::Module::beginRun
virtual void beginRun()
Called when entering a new run.
Definition: Module.h:149
Belle2::Module::Module
Module()
Constructor.
Definition: Module.cc:32
Belle2::Module::addParam
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:562
Belle2::TrackFindingCDC::GetIndexInTuple
Looks up, at which index the given Type can be found in a tuple.
Definition: EvalVariadic.h:88
Belle2::Module::getParamList
const ModuleParamList & getParamList() const
Return module param list.
Definition: Module.h:365
Belle2::TrackFindingCDC::FindletModule< WireHitCreator >::StrippedIOType
Stripped< IOType< I > > StrippedIOType
Accessor for the individual input / output types of the findlet stripped from modifiers.
Definition: FindletModule.h:59
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::Module::initialize
virtual void initialize()
Initialize the Module.
Definition: Module.h:111
Belle2::getClassMnemomicParameterName
std::string getClassMnemomicParameterName(const RecoTrack *dispatchTag __attribute__((unused)))
Returns a short name for class RecoTrack to be used in names of parameters.
Definition: ClassMnemomics.h:27
Belle2::TrackFindingCDC::FindletModule::addStoreVectorParameter
void addStoreVectorParameter()
Expose parameter to set the names of the vector with index I on the DataStore.
Definition: FindletModule.h:217
Belle2::TrackFindingCDC::FindletModule::m_param_storeVectorNames
std::array< std::string, c_nTypes > m_param_storeVectorNames
Parameters : Names of the vectors on the DataStore.
Definition: FindletModule.h:310
Belle2::TrackFindingCDC::FindletModule::beginRun
virtual void beginRun() override
Signal the beginning of a new run.
Definition: FindletModule.h:118
Belle2::Module::setParamList
void setParamList(const ModuleParamList &params)
Replace existing parameter list.
Definition: Module.h:503