Belle II Software  release-06-01-15
GlobalParam.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 
9 #pragma once
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/database/Database.h>
13 #include <framework/database/DBObjPtr.h>
14 #include <mdst/dbobjects/BeamSpot.h>
15 
16 #include <map>
17 #include <memory>
18 #include <set>
19 #include <string>
20 #include <vector>
21 
22 namespace Belle2 {
27  namespace alignment {
28  class GlobalParamVector;
29  class GlobalDerivativesHierarchy;
30 
32  class EmptyGlobalParamSet : public TObject {
33  public:
34  // ------------- Interface to global Millepede calibration ----------------
36  static unsigned short getGlobalUniqueID() {return 0;}
38  double getGlobalParam(unsigned short, unsigned short) {return 0.;}
40  void setGlobalParam(double, unsigned short, unsigned short) {}
42  std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() {return {};}
43  // ------------------------------------------------------------------------
44  };
45 
46 
51  public:
59  virtual void readFromResult(std::vector<std::tuple<unsigned short, unsigned short, unsigned short, double>>&,
60  GlobalParamVector&) {};
63  };
64 
67  public:
68 
69  };
70 
73  public:
76  c_None = 0,
77  c_Flat = 1,
78  c_HalfShells = 2,
79  c_Full = 3
80  };
84  static bool s_enablePXD;
86  static bool s_enableSVD;
87 
89  virtual void setupAlignmentHierarchy(GlobalDerivativesHierarchy& hierarchy) override final;
90  };
91 
95  public:
99  virtual ~GlobalParamSetAccess() {}
101  virtual unsigned short getGlobalUniqueID() const = 0;
104  virtual double getGlobalParam(unsigned short, unsigned short) = 0;
106  virtual void setGlobalParam(double, unsigned short, unsigned short) = 0;
108  virtual std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() = 0;
109 
111  virtual void updateGlobalParam(double correction, unsigned short element, unsigned short param) {setGlobalParam(getGlobalParam(element, param) + correction, element, param);}
114  virtual TObject* getDBObj() = 0;
116  virtual std::string getDefaultName() const = 0;
119  virtual TObject* releaseObject() = 0;
122  virtual GlobalParamSetAccess* clone() = 0;
123 
125  virtual void loadFromDB(EventMetaData emd) = 0;
127  virtual void loadFromDBObjPtr() = 0;
128 
130  virtual void construct() = 0;
133  virtual bool isConstructed() const = 0;
134 
136  virtual bool hasBeenChangedInDB(bool resetChangedState = true) final {auto tmp = m_hasBeenChangedInDB; if (resetChangedState) m_hasBeenChangedInDB = false; return tmp;}
138  virtual void callbackDB() {m_hasBeenChangedInDB = true;}
139 
142  template<class DBObjType>
143  bool is() const {return DBObjType::getGlobalUniqueID() == getGlobalUniqueID();}
144 
147  bool empty()
148  {
149  // We consider this to be the definition of empty
150  if (is<EmptyGlobalParamSet>())
151  return true;
152  // This is by convention (also used in hierarchy)
153  if (getGlobalUniqueID() == 0)
154  return true;
155  // This actually constructs the object using Default constructor if not
156  // previously loaded from DB (or using construct)
157  // TODO this is still not fully impelemnted by DB objects
158  if (listGlobalParams().empty())
159  return true;
160  return false;
161  }
162 
166  operator bool()
167  {
168  return not empty() and isConstructed();
169  }
170 
171  protected:
173  bool m_hasBeenChangedInDB {false};
174  };
175 
184  template<class DBObjType>
186  public:
188  // explicit GlobalParamSet(bool autoConstruct = false) {
190  {
191  //TODO: re-enable
192  //DBObjPtr<DBObjType> dbObject;
193  //dbObject.addCallback(this, &GlobalParamSet<DBObjType>::callbackDB);
194  // if (autoConstruct)
195  // construct();
196  }
199 
201  virtual unsigned short getGlobalUniqueID() const override final {return DBObjType::getGlobalUniqueID();}
204  virtual double getGlobalParam(unsigned short element, unsigned short param) override final {ensureConstructed(); return m_object->getGlobalParam(element, param);}
206  virtual void setGlobalParam(double value, unsigned short element, unsigned short param) override final {ensureConstructed(); m_object->setGlobalParam(value, element, param);}
208  virtual std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() override final {ensureConstructed(); return m_object->listGlobalParams();}
210  virtual std::string getDefaultName() const override final {return DataStore::objectName<DBObjType>("");}
211 
214  virtual TObject* getDBObj() override final
215  {
216  if (!m_object)
217  return nullptr;
218 
219  return m_object.get();
220  }
221 
224  {
226  // Make new unique ptr to a copy of the other internal object
227  if (other.m_object)
228  m_object.reset(new DBObjType(*(static_cast<DBObjType*>(other.m_object.get()))));
229  }
230 
233  {
234  // check for self-assignment
235  if (&other == this)
236  return *this;
238  // Make new unique ptr to a copy of the other internal object
239  if (other.m_object)
240  m_object.reset(new DBObjType(*(static_cast<DBObjType*>(other.m_object.get()))));
241  return *this;
242  }
243 
246  virtual TObject* releaseObject() override final
247  {
248  //ensureConstructed();
249  return m_object.release();
250  }
251 
253  virtual GlobalParamSetAccess* clone() override final
254  {
255  return new GlobalParamSet<DBObjType>(*this);
256  }
257 
261  virtual void loadFromDBObjPtr() override final
262  {
263  DBObjPtr<DBObjType> dbObject;
264  if (dbObject) {
265  m_object.reset(new DBObjType(*dbObject));
266  hasBeenChangedInDB(); // will set m_hasBeenChangedInDB to false, ignore return value;
267  }
268  }
269 
272  virtual void loadFromDB(EventMetaData event) override final
273  {
274  auto info = Database::Instance().getData(event, getDefaultName());
275  DBObjType* ptr = dynamic_cast<DBObjType*>(info.first);
276  if (!ptr) {
277  B2ERROR("Could not fetch object " << getDefaultName() << " from DB.");
278  return;
279  }
280  // don't make a copy, you own the object.
281  m_object.reset(ptr);
282  hasBeenChangedInDB(); // will set m_hasBeenChangedInDB to false, ignore return value;
283  }
284 
286  virtual void construct() override final {m_object.reset(new DBObjType());}
289  virtual bool isConstructed() const override final {return !!m_object;}
290 
293  virtual void callbackDB() override final {GlobalParamSetAccess::callbackDB();}
294 
295 
296  private:
298  std::unique_ptr<DBObjType> m_object {};
301  };
302 
304  template <>
308  template <>
309  double GlobalParamSet<BeamSpot>::getGlobalParam(unsigned short element, unsigned short param);
311  template <>
312  void GlobalParamSet<BeamSpot>::setGlobalParam(double value, unsigned short element, unsigned short param);
314  template <>
315  std::vector<std::pair<unsigned short, unsigned short>> GlobalParamSet<BeamSpot>::listGlobalParams();
316 
317 
318 
319 
327  public:
331 
335  explicit GlobalParamVector(const std::vector<std::string>& components = {});
336 
339 
341  void construct();
342 
345  {
346  // this += rhs
347  // TODO: needs full implementation of listing in DBObjects
348  for (auto uid_element_param : listGlobalParams()) {
349  double delta = rhs.getGlobalParam(std::get<0>(uid_element_param), std::get<1>(uid_element_param), std::get<2>(uid_element_param));
350  updateGlobalParam(delta, std::get<0>(uid_element_param), std::get<1>(uid_element_param), std::get<2>(uid_element_param));
351  }
352  return *this;
353  }
354 
356  void addSubDetectorInterface(std::shared_ptr<IGlobalParamInterface> interface = {})
357  {
358  if (interface) {
360  interface) == m_subDetectorInterfacesVector.end())
361  m_subDetectorInterfacesVector.push_back(interface);
362  }
363  }
364 
369  template <class DBObjType>
370  void addDBObj(std::shared_ptr<IGlobalParamInterface> interface = {})
371  {
372  if (m_components.empty()
373  or std::find(m_components.begin(), m_components.end(), DataStore::objectName<DBObjType>("")) != m_components.end()) {
374  m_vector.insert(std::make_pair(DBObjType::getGlobalUniqueID(),
376  ));
377  // NOTE: Components disabled this way also disable added interfaces (e.g. if geometry would be needed to load)
378  // NOTE: add generic interfaces only once by addSubDetectorInterface(...)
379  addSubDetectorInterface(interface);
380  }
381  }
382 
386  bool hasBeenChangedInDB(const std::set<unsigned short>& subset, bool resetChangedState = true)
387  {
388  bool changed = false;
389 
390  for (auto& uID_DBObj : m_vector) {
391  if (!subset.empty() and subset.find(uID_DBObj.first) != subset.end())
392  continue;
393  if (uID_DBObj.second->hasBeenChangedInDB(resetChangedState)) {
394  changed = true;
395  break;
396  }
397  }
398 
399  return changed;
400  }
402  bool hasBeenChangedInDB(const std::set<unsigned short>& subset = {}) {return hasBeenChangedInDB(subset, false);}
403 
409  void updateGlobalParam(double difference, unsigned short uniqueID, unsigned short element, unsigned short param);
410 
416  void setGlobalParam(double value, unsigned short uniqueID, unsigned short element, unsigned short param);
417 
422  double getGlobalParam(unsigned short uniqueID, unsigned short element, unsigned short param);
423 
425  std::vector<std::tuple<unsigned short, unsigned short, unsigned short>> listGlobalParams();
426 
432  std::vector<TObject*> releaseObjects(bool onlyConstructed = true);
433 
436  void loadFromDB();
438  void loadFromDB(const EventMetaData& event);
439 
441  void postReadFromResult(std::vector<std::tuple<unsigned short, unsigned short, unsigned short, double>>& result);
443  void postHierarchyChanged(GlobalDerivativesHierarchy& hierarchy);
444 
447  template<class DBObjType>
448  DBObjType* getDBObj()
449  {
450  if (m_vector.find(DBObjType::getGlobalUniqueID()) == m_vector.end())
451  return nullptr;
452 
453  return static_cast<DBObjType*>(m_vector[DBObjType::getGlobalUniqueID()]->getDBObj());
454  }
455 
460  template<class DBObjType>
462  {
463  if (m_vector.find(DBObjType::getGlobalUniqueID()) == m_vector.end())
464  return c_emptyGlobalParamSet;
465 
466  return static_cast<GlobalParamSet<DBObjType>&>(*m_vector[DBObjType::getGlobalUniqueID()]);
467  }
468 
470  const std::map<unsigned short, std::unique_ptr<GlobalParamSetAccess>>& getGlobalParamSets() const
471  {
472  return m_vector;
473  }
474 
476  const std::vector<std::shared_ptr<IGlobalParamInterface>>& getSubDetectorInterfaces() const
477  {
479  }
480 
482  std::set<unsigned short> getComponentsIDs() const
483  {
484  std::set<unsigned short> result;
485  for (auto& id_obj : m_vector)
486  result.insert(id_obj.first);
487  return result;
488  }
489  private:
491  std::map<unsigned short, std::unique_ptr<GlobalParamSetAccess>> m_vector {};
493  std::vector<std::shared_ptr<IGlobalParamInterface>> m_subDetectorInterfacesVector {};
495  std::vector<std::string> m_components {};
497  std::set<unsigned short> m_componentsIDs {};
498  };
499  }
501 }
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
Store event, run, and experiment numbers.
Definition: EventMetaData.h:33
Object representing no parameters.
Definition: GlobalParam.h:32
double getGlobalParam(unsigned short, unsigned short)
Get global parameter - return zero.
Definition: GlobalParam.h:38
static unsigned short getGlobalUniqueID()
Get global unique id = 0 (empty/none)
Definition: GlobalParam.h:36
std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams()
list stored global parameters - empty list, no parameters
Definition: GlobalParam.h:42
void setGlobalParam(double, unsigned short, unsigned short)
Set global parameter - do nothing.
Definition: GlobalParam.h:40
Class for alignment/calibration parameter hierarchy & constraints.
Definition: Hierarchy.h:41
Base accessor class to store different DBObjects e.g.
Definition: GlobalParam.h:94
virtual void loadFromDBObjPtr()=0
Load using DBObjPtr<DBObjType> which uses current EventMetaData to load valid constants.
virtual std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams()=0
implement: generate list of all global parameters in the DBObject
virtual void loadFromDB(EventMetaData emd)=0
Load the content (by copying obj retrieved from DB) for a given exp/run/event.
virtual TObject * releaseObject()=0
Release the object from internal unique_ptr to be managed elsewhere Useful to pass it to be stored in...
bool empty()
Is this set empty or otherwise 'invalid' to identify null sets, end of hierarchy.
Definition: GlobalParam.h:147
virtual void callbackDB()
Function to call when object has been changed in DB since last loaded.
Definition: GlobalParam.h:138
virtual TObject * getDBObj()=0
Get a raw pointer to the internal DB object (can be nullptr if not yet constructed) Use with caution ...
virtual std::string getDefaultName() const =0
Get the default DBObject name used by datastore.
virtual void updateGlobalParam(double correction, unsigned short element, unsigned short param)
Sum value of global element param with a correction.
Definition: GlobalParam.h:111
bool m_hasBeenChangedInDB
Flag for object changed in DB.
Definition: GlobalParam.h:173
virtual unsigned short getGlobalUniqueID() const =0
implement: Get the global unique id of DBObject
virtual ~GlobalParamSetAccess()
Virtual destructor (base class, but with no members)
Definition: GlobalParam.h:99
virtual void setGlobalParam(double, unsigned short, unsigned short)=0
implemtn: set a global param value by element and param number
virtual bool hasBeenChangedInDB(bool resetChangedState=true) final
Has the object been updated in DB since GlobalParamSet<...> has been constructed? Since last call to ...
Definition: GlobalParam.h:136
virtual void construct()=0
Construct the internal DBObject.
bool is() const
Is this set of given type?
Definition: GlobalParam.h:143
virtual bool isConstructed() const =0
Has the internal DBObject been already constructed The object is constructed at latest on first acces...
virtual double getGlobalParam(unsigned short, unsigned short)=0
implement: get a global parameter value based on the element number in the DBObject and its parameter...
virtual GlobalParamSetAccess * clone()=0
Clone the object, making a copy of the internal object - has to be implemented in derived template cl...
Template class for generic access to DB objects.
Definition: GlobalParam.h:185
virtual void loadFromDBObjPtr() override final
Load content of the object using DBObjPtr<DBObjType> which will try to load object valid for current ...
Definition: GlobalParam.h:261
virtual void setGlobalParam(double value, unsigned short element, unsigned short param) override final
Set global parameter of the DB object by its element and parameter number.
Definition: GlobalParam.h:206
virtual double getGlobalParam(unsigned short element, unsigned short param) override final
Get global parameter of the DB object by its element and parameter number Note this is not const,...
Definition: GlobalParam.h:204
std::unique_ptr< DBObjType > m_object
The internal DB object.
Definition: GlobalParam.h:298
virtual void callbackDB() override final
Function to call when object has been changed in DB since last loaded TODO Hide and find the class wh...
Definition: GlobalParam.h:293
void ensureConstructed()
Function to construct the object if not already done.
Definition: GlobalParam.h:300
GlobalParamSet< DBObjType > & operator=(const GlobalParamSet< DBObjType > &other)
Assignment operator.
Definition: GlobalParam.h:232
virtual void loadFromDB(EventMetaData event) override final
Load content of the object valid at given event from DB Also resets if the object has been changed.
Definition: GlobalParam.h:272
GlobalParamSet()
Constructor. Sets callback for DB object changes.
Definition: GlobalParam.h:189
virtual void construct() override final
Construct the internal object using default constructor.
Definition: GlobalParam.h:286
virtual unsigned short getGlobalUniqueID() const override final
The DB object unique id in global calibration.
Definition: GlobalParam.h:201
virtual std::string getDefaultName() const override final
Get the DB object default name used by datastore.
Definition: GlobalParam.h:210
virtual TObject * getDBObj() override final
Get the raw pointer to the stored object WARNING: Use with caution if you really need to access the i...
Definition: GlobalParam.h:214
virtual std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams() override final
List global parameters in this DB object.
Definition: GlobalParam.h:208
virtual GlobalParamSetAccess * clone() override final
Clone the object, making a copy of the internal object.
Definition: GlobalParam.h:253
virtual bool isConstructed() const override final
Is the internal object already constructed? we construct the object on first access to the stored val...
Definition: GlobalParam.h:289
virtual TObject * releaseObject() override final
Release the DB object from the internal unique pointer to be managed elsewhere (e....
Definition: GlobalParam.h:246
The central user class to manipulate any global constant in any DB object Used to retrieve global par...
Definition: GlobalParam.h:326
std::vector< std::string > m_components
Vector of names of DB objects to consider in the vector - if not here and non-empy,...
Definition: GlobalParam.h:495
GlobalParamVector & operator+=(GlobalParamVector &rhs)
TODO: operator += to sum two global vectors.
Definition: GlobalParam.h:344
std::vector< std::tuple< unsigned short, unsigned short, unsigned short > > listGlobalParams()
Create vector of all global parameters maintained by GlobalParamVector.
Definition: GlobalParam.cc:107
GlobalParamSet< EmptyGlobalParamSet > c_emptyGlobalParamSet
An empty set to which reference is returned if DB object not found Also used e.g.
Definition: GlobalParam.h:330
std::vector< TObject * > releaseObjects(bool onlyConstructed=true)
Get the vector of raw pointers to DB objects Caller takes the ownership of the objects and has to del...
Definition: GlobalParam.cc:117
GlobalParamVector(const std::vector< std::string > &components={})
Constructor.
Definition: GlobalParam.cc:76
bool hasBeenChangedInDB(const std::set< unsigned short > &subset={})
Const version which does not change the state.
Definition: GlobalParam.h:402
std::map< unsigned short, std::unique_ptr< GlobalParamSetAccess > > m_vector
The vector (well, actually a map) of DB objects.
Definition: GlobalParam.h:491
std::vector< std::shared_ptr< IGlobalParamInterface > > m_subDetectorInterfacesVector
Vector of interfaces to run at specific points in the workflow.
Definition: GlobalParam.h:493
void setGlobalParam(double value, unsigned short uniqueID, unsigned short element, unsigned short param)
Set param value.
Definition: GlobalParam.cc:85
double getGlobalParam(unsigned short uniqueID, unsigned short element, unsigned short param)
Get parameter value.
Definition: GlobalParam.cc:96
void construct()
Construct all DB objects using default constructors (should be filled with zeros)
Definition: GlobalParam.cc:147
DBObjType * getDBObj()
Get the raw pointer toa stored object WARNING: Use with caution, returns nullptr if object not regist...
Definition: GlobalParam.h:448
void addDBObj(std::shared_ptr< IGlobalParamInterface > interface={})
Add a DB object to the vector, optionally with interface.
Definition: GlobalParam.h:370
const std::map< unsigned short, std::unique_ptr< GlobalParamSetAccess > > & getGlobalParamSets() const
Get map of all contained parameter sets, key = unique id of the set (DB object)
Definition: GlobalParam.h:470
GlobalParamSetAccess & getGlobalParamSet()
Get reference to the underlying DB object container You should check the container.
Definition: GlobalParam.h:461
void addSubDetectorInterface(std::shared_ptr< IGlobalParamInterface > interface={})
Add a generic interface (cannot be disabled by 'components' in constructor)
Definition: GlobalParam.h:356
void postHierarchyChanged(GlobalDerivativesHierarchy &hierarchy)
Function calling interfaces to build alignment hierarchy after constants changed in DB.
Definition: GlobalParam.cc:140
void loadFromDB()
Load content of all objects in vector with values valid at current EventMetaData Uses DBObjPtr<DBObjT...
Definition: GlobalParam.cc:128
void updateGlobalParam(double difference, unsigned short uniqueID, unsigned short element, unsigned short param)
Add 'difference' to param value.
Definition: GlobalParam.cc:78
bool hasBeenChangedInDB(const std::set< unsigned short > &subset, bool resetChangedState=true)
Has any DB object in vector changed from last call to this function.
Definition: GlobalParam.h:386
void postReadFromResult(std::vector< std::tuple< unsigned short, unsigned short, unsigned short, double >> &result)
Function calling interfaces to do post processing after reading millepede result into GlobalParamVect...
Definition: GlobalParam.cc:153
const std::vector< std::shared_ptr< IGlobalParamInterface > > & getSubDetectorInterfaces() const
Get the vector of added interfaces to subdetectors.
Definition: GlobalParam.h:476
std::set< unsigned short > m_componentsIDs
Vector of UniqueIDs of DB objects to consider in the vector.
Definition: GlobalParam.h:497
std::set< unsigned short > getComponentsIDs() const
Get set of unique ids of all db objects considered.
Definition: GlobalParam.h:482
Some tentative base class to allow to add functionality to the default behavior, like manipulate cons...
Definition: GlobalParam.h:50
virtual void setupAlignmentHierarchy(GlobalDerivativesHierarchy &)
Super-tentavive: not yet used.
Definition: GlobalParam.h:62
virtual ~IGlobalParamInterface()
Destructor.
Definition: GlobalParam.h:53
virtual void readFromResult(std::vector< std::tuple< unsigned short, unsigned short, unsigned short, double >> &, GlobalParamVector &)
Implement this to be called after Millepede finishes and all global parameters are filled in DB objec...
Definition: GlobalParam.h:59
Very tentative interface for VXD.
Definition: GlobalParam.h:72
static bool s_enablePXD
Enable PXD in hierarchy?
Definition: GlobalParam.h:84
static bool s_enableSVD
Enable SVD in hierarchy?
Definition: GlobalParam.h:86
virtual void setupAlignmentHierarchy(GlobalDerivativesHierarchy &hierarchy) override final
Very tentaive function: not yet used.
Definition: GlobalParam.cc:166
E_VXDHierarchyType
Type of VXD hierarchy.
Definition: GlobalParam.h:75
static E_VXDHierarchyType s_hierarchyType
What type of hierarchy to use for VXD?
Definition: GlobalParam.h:82
std::pair< TObject *, IntervalOfValidity > getData(const EventMetaData &event, const std::string &name)
Request an object from the database.
Definition: Database.cc:71
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:41
Abstract base class for different kinds of events.