Belle II Software development
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
22namespace 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:
60 virtual void readFromResult(std::vector<std::tuple<unsigned short, unsigned short, unsigned short, double>>&,
64 };
65
68 public:
69
70 };
71
74 public:
77 c_None = 0,
78 c_Flat = 1,
79 c_HalfShells = 2,
80 c_Full = 3
81 };
85 static bool s_enablePXD;
87 static bool s_enableSVD;
88
90 virtual void setupAlignmentHierarchy(GlobalDerivativesHierarchy& hierarchy) override final;
91 };
92
96 public:
102 virtual unsigned short getGlobalUniqueID() const = 0;
105 virtual double getGlobalParam(unsigned short, unsigned short) = 0;
107 virtual void setGlobalParam(double, unsigned short, unsigned short) = 0;
109 virtual std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() = 0;
110
112 virtual void updateGlobalParam(double correction, unsigned short element, unsigned short param) {setGlobalParam(getGlobalParam(element, param) + correction, element, param);}
115 virtual TObject* getDBObj() = 0;
117 virtual std::string getDefaultName() const = 0;
120 virtual TObject* releaseObject() = 0;
124
126 virtual void loadFromDB(EventMetaData emd) = 0;
128 virtual void loadFromDBObjPtr() = 0;
129
131 virtual void construct() = 0;
134 virtual bool isConstructed() const = 0;
135
137 virtual bool hasBeenChangedInDB(bool resetChangedState = true) final {auto tmp = m_hasBeenChangedInDB; if (resetChangedState) m_hasBeenChangedInDB = false; return tmp;}
139 virtual void callbackDB() {m_hasBeenChangedInDB = true;}
140
143 template<class DBObjType>
144 bool is() const {return DBObjType::getGlobalUniqueID() == getGlobalUniqueID();}
145
148 bool empty()
149 {
150 // We consider this to be the definition of empty
151 if (is<EmptyGlobalParamSet>())
152 return true;
153 // This is by convention (also used in hierarchy)
154 if (getGlobalUniqueID() == 0)
155 return true;
156 // This actually constructs the object using Default constructor if not
157 // previously loaded from DB (or using construct)
158 // TODO this is still not fully impelemnted by DB objects
159 if (listGlobalParams().empty())
160 return true;
161 return false;
162 }
163
167 operator bool()
168 {
169 return not empty() and isConstructed();
170 }
171
172 protected:
175 };
176
185 template<class DBObjType>
187 public:
189 // explicit GlobalParamSet(bool autoConstruct = false) {
191 {
192 //TODO: re-enable
193 //DBObjPtr<DBObjType> dbObject;
194 //dbObject.addCallback(this, &GlobalParamSet<DBObjType>::callbackDB);
195 // if (autoConstruct)
196 // construct();
197 }
200
202 virtual unsigned short getGlobalUniqueID() const override final {return DBObjType::getGlobalUniqueID();}
205 virtual double getGlobalParam(unsigned short element, unsigned short param) override final {ensureConstructed(); return m_object->getGlobalParam(element, param);}
207 virtual void setGlobalParam(double value, unsigned short element, unsigned short param) override final {ensureConstructed(); m_object->setGlobalParam(value, element, param);}
209 virtual std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() override final {ensureConstructed(); return m_object->listGlobalParams();}
211 virtual std::string getDefaultName() const override final {return DataStore::objectName<DBObjType>("");}
212
215 virtual TObject* getDBObj() override final
216 {
217 if (!m_object)
218 return nullptr;
219
220 return m_object.get();
221 }
222
225 {
227 // Make new unique ptr to a copy of the other internal object
228 if (other.m_object)
229 m_object.reset(new DBObjType(*(static_cast<DBObjType*>(other.m_object.get()))));
230 }
231
234 {
235 // check for self-assignment
236 if (&other == this)
237 return *this;
239 // Make new unique ptr to a copy of the other internal object
240 if (other.m_object)
241 m_object.reset(new DBObjType(*(static_cast<DBObjType*>(other.m_object.get()))));
242 return *this;
243 }
244
247 virtual TObject* releaseObject() override final
248 {
249 //ensureConstructed();
250 return m_object.release();
251 }
252
254 virtual GlobalParamSetAccess* clone() override final
255 {
256 return new GlobalParamSet<DBObjType>(*this);
257 }
258
262 virtual void loadFromDBObjPtr() override final
263 {
264 DBObjPtr<DBObjType> dbObject;
265 if (dbObject) {
266 m_object.reset(new DBObjType(*dbObject));
267 hasBeenChangedInDB(); // will set m_hasBeenChangedInDB to false, ignore return value;
268 }
269 }
270
273 virtual void loadFromDB(EventMetaData event) override final
274 {
275 auto info = Database::Instance().getData(event, getDefaultName());
276 DBObjType* ptr = dynamic_cast<DBObjType*>(info.first);
277 if (!ptr) {
278 B2ERROR("Could not fetch object " << getDefaultName() << " from DB.");
279 return;
280 }
281 // don't make a copy, you own the object.
282 m_object.reset(ptr);
283 hasBeenChangedInDB(); // will set m_hasBeenChangedInDB to false, ignore return value;
284 }
285
287 virtual void construct() override final {m_object.reset(new DBObjType());}
290 virtual bool isConstructed() const override final {return !!m_object;}
291
294 virtual void callbackDB() override final {GlobalParamSetAccess::callbackDB();}
295
296
297 private:
299 std::unique_ptr<DBObjType> m_object {};
302 };
303
305 template <>
309 template <>
310 double GlobalParamSet<BeamSpot>::getGlobalParam(unsigned short element, unsigned short param);
312 template <>
313 void GlobalParamSet<BeamSpot>::setGlobalParam(double value, unsigned short element, unsigned short param);
315 template <>
316 std::vector<std::pair<unsigned short, unsigned short>> GlobalParamSet<BeamSpot>::listGlobalParams();
317
318
319
320
328 public:
332
336 explicit GlobalParamVector(const std::vector<std::string>& components = {});
337
340
342 void construct();
343
346 {
347 // this += rhs
348 // TODO: needs full implementation of listing in DBObjects
349 for (auto uid_element_param : listGlobalParams()) {
350 double delta = rhs.getGlobalParam(std::get<0>(uid_element_param), std::get<1>(uid_element_param), std::get<2>(uid_element_param));
351 updateGlobalParam(delta, std::get<0>(uid_element_param), std::get<1>(uid_element_param), std::get<2>(uid_element_param));
352 }
353 return *this;
354 }
355
357 void addSubDetectorInterface(std::shared_ptr<IGlobalParamInterface> interface = {})
358 {
359 if (interface) {
361 interface) == m_subDetectorInterfacesVector.end())
362 m_subDetectorInterfacesVector.push_back(interface);
363 }
364 }
365
370 template <class DBObjType>
371 void addDBObj(std::shared_ptr<IGlobalParamInterface> interface = {})
372 {
373 if (m_components.empty()
374 or std::find(m_components.begin(), m_components.end(), DataStore::objectName<DBObjType>("")) != m_components.end()) {
375 m_vector.insert(std::make_pair(DBObjType::getGlobalUniqueID(),
377 ));
378 // NOTE: Components disabled this way also disable added interfaces (e.g. if geometry would be needed to load)
379 // NOTE: add generic interfaces only once by addSubDetectorInterface(...)
380 addSubDetectorInterface(interface);
381 }
382 }
383
387 bool hasBeenChangedInDB(const std::set<unsigned short>& subset, bool resetChangedState = true)
388 {
389 bool changed = false;
390
391 for (auto& uID_DBObj : m_vector) {
392 if (!subset.empty() and subset.find(uID_DBObj.first) != subset.end())
393 continue;
394 if (uID_DBObj.second->hasBeenChangedInDB(resetChangedState)) {
395 changed = true;
396 break;
397 }
398 }
399
400 return changed;
401 }
403 bool hasBeenChangedInDB(const std::set<unsigned short>& subset = {}) {return hasBeenChangedInDB(subset, false);}
404
410 void updateGlobalParam(double difference, unsigned short uniqueID, unsigned short element, unsigned short param);
411
417 void setGlobalParam(double value, unsigned short uniqueID, unsigned short element, unsigned short param);
418
423 double getGlobalParam(unsigned short uniqueID, unsigned short element, unsigned short param);
424
426 std::vector<std::tuple<unsigned short, unsigned short, unsigned short>> listGlobalParams();
427
433 std::vector<TObject*> releaseObjects(bool onlyConstructed = true);
434
437 void loadFromDB();
439 void loadFromDB(const EventMetaData& event);
440
442 void postReadFromResult(std::vector<std::tuple<unsigned short, unsigned short, unsigned short, double>>& result);
444 void postHierarchyChanged(GlobalDerivativesHierarchy& hierarchy);
445
448 template<class DBObjType>
449 DBObjType* getDBObj()
450 {
451 if (m_vector.find(DBObjType::getGlobalUniqueID()) == m_vector.end())
452 return nullptr;
453
454 return static_cast<DBObjType*>(m_vector[DBObjType::getGlobalUniqueID()]->getDBObj());
455 }
456
461 template<class DBObjType>
463 {
464 if (m_vector.find(DBObjType::getGlobalUniqueID()) == m_vector.end())
466
467 return static_cast<GlobalParamSet<DBObjType>&>(*m_vector[DBObjType::getGlobalUniqueID()]);
468 }
469
471 const std::map<unsigned short, std::unique_ptr<GlobalParamSetAccess>>& getGlobalParamSets() const
472 {
473 return m_vector;
474 }
475
477 const std::vector<std::shared_ptr<IGlobalParamInterface>>& getSubDetectorInterfaces() const
478 {
480 }
481
483 std::set<unsigned short> getComponentsIDs() const
484 {
485 std::set<unsigned short> result;
486 for (auto& id_obj : m_vector)
487 result.insert(id_obj.first);
488 return result;
489 }
490 private:
492 std::map<unsigned short, std::unique_ptr<GlobalParamSetAccess>> m_vector {};
494 std::vector<std::shared_ptr<IGlobalParamInterface>> m_subDetectorInterfacesVector {};
496 std::vector<std::string> m_components {};
498 std::set<unsigned short> m_componentsIDs {};
499 };
500 }
502}
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
std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams()
list stored global parameters - empty list, no parameters
Definition: GlobalParam.h:42
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
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:95
virtual void loadFromDBObjPtr()=0
Load using DBObjPtr<DBObjType> which uses current EventMetaData to load valid constants.
virtual GlobalParamSetAccess * clone()=0
Clone the object, making a copy of the internal object - has to be implemented in derived template cl...
virtual void loadFromDB(EventMetaData emd)=0
Load the content (by copying obj retrieved from DB) for a given exp/run/event.
bool empty()
Is this set empty or otherwise 'invalid' to identify null sets, end of hierarchy.
Definition: GlobalParam.h:148
virtual TObject * releaseObject()=0
Release the object from internal unique_ptr to be managed elsewhere Useful to pass it to be stored in...
virtual void callbackDB()
Function to call when object has been changed in DB since last loaded.
Definition: GlobalParam.h:139
virtual std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams()=0
implement: generate list of all global parameters in the DBObject
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:112
bool m_hasBeenChangedInDB
Flag for object changed in DB.
Definition: GlobalParam.h:174
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:100
virtual void setGlobalParam(double, unsigned short, unsigned short)=0
implemtn: set a global param value by element and param number
virtual TObject * getDBObj()=0
Get a raw pointer to the internal DB object (can be nullptr if not yet constructed) Use with caution ...
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:137
virtual void construct()=0
Construct the internal DBObject.
bool is() const
Is this set of given type?
Definition: GlobalParam.h:144
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...
Template class for generic access to DB objects.
Definition: GlobalParam.h:186
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:262
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:207
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:205
std::unique_ptr< DBObjType > m_object
The internal DB object.
Definition: GlobalParam.h:299
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:215
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:294
void ensureConstructed()
Function to construct the object if not already done.
Definition: GlobalParam.h:301
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:273
GlobalParamSet()
Constructor. Sets callback for DB object changes.
Definition: GlobalParam.h:190
virtual std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams() override final
List global parameters in this DB object.
Definition: GlobalParam.h:209
virtual void construct() override final
Construct the internal object using default constructor.
Definition: GlobalParam.h:287
virtual unsigned short getGlobalUniqueID() const override final
The DB object unique id in global calibration.
Definition: GlobalParam.h:202
virtual std::string getDefaultName() const override final
Get the DB object default name used by datastore.
Definition: GlobalParam.h:211
GlobalParamSet< DBObjType > & operator=(const GlobalParamSet< DBObjType > &other)
Assignment operator.
Definition: GlobalParam.h:233
virtual TObject * releaseObject() override final
Release the DB object from the internal unique pointer to be managed elsewhere (e....
Definition: GlobalParam.h:247
GlobalParamSet(const GlobalParamSet< DBObjType > &other)
Copy constructor.
Definition: GlobalParam.h:224
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:290
virtual GlobalParamSetAccess * clone() override final
Clone the object, making a copy of the internal object.
Definition: GlobalParam.h:254
The central user class to manipulate any global constant in any DB object Used to retrieve global par...
Definition: GlobalParam.h:327
DBObjType * getDBObj()
Get the raw pointer toa stored object WARNING: Use with caution, returns nullptr if object not regist...
Definition: GlobalParam.h:449
std::set< unsigned short > getComponentsIDs() const
Get set of unique ids of all db objects considered.
Definition: GlobalParam.h:483
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:496
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:331
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
GlobalParamSetAccess & getGlobalParamSet()
Get reference to the underlying DB object container You should check the container.
Definition: GlobalParam.h:462
bool hasBeenChangedInDB(const std::set< unsigned short > &subset={})
Const version which does not change the state.
Definition: GlobalParam.h:403
std::map< unsigned short, std::unique_ptr< GlobalParamSetAccess > > m_vector
The vector (well, actually a map) of DB objects.
Definition: GlobalParam.h:492
std::vector< std::shared_ptr< IGlobalParamInterface > > m_subDetectorInterfacesVector
Vector of interfaces to run at specific points in the workflow.
Definition: GlobalParam.h:494
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
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
void addDBObj(std::shared_ptr< IGlobalParamInterface > interface={})
Add a DB object to the vector, optionally with interface.
Definition: GlobalParam.h:371
void addSubDetectorInterface(std::shared_ptr< IGlobalParamInterface > interface={})
Add a generic interface (cannot be disabled by 'components' in constructor)
Definition: GlobalParam.h:357
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
GlobalParamVector & operator+=(GlobalParamVector &rhs)
TODO: operator += to sum two global vectors.
Definition: GlobalParam.h:345
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:387
const std::vector< std::shared_ptr< IGlobalParamInterface > > & getSubDetectorInterfaces() const
Get the vector of added interfaces to subdetectors.
Definition: GlobalParam.h:477
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:471
std::set< unsigned short > m_componentsIDs
Vector of UniqueIDs of DB objects to consider in the vector.
Definition: GlobalParam.h:498
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:63
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:60
Very tentative interface for VXD.
Definition: GlobalParam.h:73
static bool s_enablePXD
Enable PXD in hierarchy?
Definition: GlobalParam.h:85
static bool s_enableSVD
Enable SVD in hierarchy?
Definition: GlobalParam.h:87
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:76
static E_VXDHierarchyType s_hierarchyType
What type of hierarchy to use for VXD?
Definition: GlobalParam.h:83
std::pair< TObject *, IntervalOfValidity > getData(const EventMetaData &event, const std::string &name)
Request an object from the database.
Definition: Database.cc:72
static Database & Instance()
Instance of a singleton Database.
Definition: Database.cc:42
Abstract base class for different kinds of events.