Belle II Software development
GlobalParamSet< DBObjType > Class Template Reference

Template class for generic access to DB objects. More...

#include <GlobalParam.h>

Inheritance diagram for GlobalParamSet< DBObjType >:
GlobalParamSetAccess

Public Member Functions

 GlobalParamSet ()
 Constructor. Sets callback for DB object changes.
 
 ~GlobalParamSet ()
 Destructor.
 
virtual unsigned short getGlobalUniqueID () const override final
 The DB object unique id in global calibration.
 
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, it might need to construct the object.
 
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.
 
virtual std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams () override final
 List global parameters in this DB object.
 
virtual std::string getDefaultName () const override final
 Get the DB object default name used by datastore.
 
virtual TObject * getDBObj () override final
 Get the raw pointer to the stored object WARNING: Use with caution if you really need to access the internal object.
 
 GlobalParamSet (const GlobalParamSet< DBObjType > &other)
 Copy constructor.
 
GlobalParamSet< DBObjType > & operator= (const GlobalParamSet< DBObjType > &other)
 Assignment operator.
 
virtual TObject * releaseObject () override final
 Release the DB object from the internal unique pointer to be managed elsewhere (e.g.
 
virtual GlobalParamSetAccessclone () override final
 Clone the object, making a copy of the internal object.
 
virtual void loadFromDBObjPtr () override final
 Load content of the object using DBObjPtr<DBObjType> which will try to load object valid for current EventMetaData Also resets if the object has been changed.
 
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.
 
virtual void construct () override final
 Construct the internal object using default constructor.
 
virtual bool isConstructed () const override final
 Is the internal object already constructed?
 
virtual void callbackDB () override final
 Function to call when object has been changed in DB since last loaded TODO Hide and find the class which calls this to make it friend.
 
unsigned short getGlobalUniqueID () const
 The DB object unique id in global calibration.
 
double getGlobalParam (unsigned short element, unsigned short param)
 Get global parameter of the DB object by its element and parameter number Note this is not const, it might need to construct the object.
 
void setGlobalParam (double value, unsigned short element, unsigned short param)
 Set global parameter of the DB object by its element and parameter number.
 
std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams ()
 List global parameters in this DB object.
 
unsigned short getGlobalUniqueID () const
 The DB object unique id in global calibration.
 
double getGlobalParam (unsigned short element, unsigned short param)
 Get global parameter of the DB object by its element and parameter number Note this is not const, it might need to construct the object.
 
void setGlobalParam (double value, unsigned short element, unsigned short param)
 Set global parameter of the DB object by its element and parameter number.
 
std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams ()
 List global parameters in this DB object.
 
virtual void updateGlobalParam (double correction, unsigned short element, unsigned short param)
 Sum value of global element param with a correction.
 
virtual bool hasBeenChangedInDB (bool resetChangedState=true) final
 Has the object been updated in DB since GlobalParamSet<...> has been constructed? Since last call to this function with resetChangedState=true.
 
template<class DBObjType>
bool is () const
 Is this set of given type?
 
bool empty ()
 Is this set empty or otherwise 'invalid' to identify null sets, end of hierarchy.
 
 operator bool ()
 Tentative operator to quickly check the set Returns true when the set is not empty() and the DB object is constructed and can be accessed.
 

Protected Attributes

bool m_hasBeenChangedInDB {false}
 Flag for object changed in DB.
 

Private Member Functions

void ensureConstructed ()
 Function to construct the object if not already done.
 

Private Attributes

std::unique_ptr< DBObjType > m_object {}
 The internal DB object.
 

Detailed Description

template<class DBObjType>
class Belle2::alignment::GlobalParamSet< DBObjType >

Template class for generic access to DB objects.

Use to access any global calibration DB object, e.g.

GlobalParamSet<BeamSpot> params; params.setGlobalParam(0.0012, 0, 1);

will set X-postion of BeamSpot vertex to 0.0012

Definition at line 186 of file GlobalParam.h.

Constructor & Destructor Documentation

◆ GlobalParamSet() [1/2]

template<class DBObjType>
GlobalParamSet ( )
inline

Constructor. Sets callback for DB object changes.

Definition at line 190 of file GlobalParam.h.

191 {
192 //TODO: re-enable
193 //DBObjPtr<DBObjType> dbObject;
194 //dbObject.addCallback(this, &GlobalParamSet<DBObjType>::callbackDB);
195 // if (autoConstruct)
196 // construct();
197 }

◆ ~GlobalParamSet()

template<class DBObjType>
~GlobalParamSet ( )
inline

Destructor.

Definition at line 199 of file GlobalParam.h.

199{m_object.reset();}

◆ GlobalParamSet() [2/2]

template<class DBObjType>
GlobalParamSet ( const GlobalParamSet< DBObjType > & other)
inline

Copy constructor.

Definition at line 224 of file GlobalParam.h.

225 {
226 m_hasBeenChangedInDB = other.m_hasBeenChangedInDB;
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 }

Member Function Documentation

◆ callbackDB()

template<class DBObjType>
virtual void callbackDB ( )
inlinefinaloverridevirtual

Function to call when object has been changed in DB since last loaded TODO Hide and find the class which calls this to make it friend.

Reimplemented from GlobalParamSetAccess.

Definition at line 294 of file GlobalParam.h.

294{GlobalParamSetAccess::callbackDB();}

◆ clone()

template<class DBObjType>
virtual GlobalParamSetAccess * clone ( )
inlinefinaloverridevirtual

Clone the object, making a copy of the internal object.

Implements GlobalParamSetAccess.

Definition at line 254 of file GlobalParam.h.

255 {
256 return new GlobalParamSet<DBObjType>(*this);
257 }

◆ construct()

template<class DBObjType>
virtual void construct ( )
inlinefinaloverridevirtual

Construct the internal object using default constructor.

Implements GlobalParamSetAccess.

Definition at line 287 of file GlobalParam.h.

287{m_object.reset(new DBObjType());}

◆ empty()

bool empty ( )
inlineinherited

Is this set empty or otherwise 'invalid' to identify null sets, end of hierarchy.

Definition at line 148 of file GlobalParam.h.

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 }

◆ ensureConstructed()

template<class DBObjType>
void ensureConstructed ( )
inlineprivate

Function to construct the object if not already done.

Definition at line 301 of file GlobalParam.h.

301{if (!m_object) construct();}

◆ getDBObj()

template<class DBObjType>
virtual TObject * getDBObj ( )
inlinefinaloverridevirtual

Get the raw pointer to the stored object WARNING: Use with caution if you really need to access the internal object.

Implements GlobalParamSetAccess.

Definition at line 215 of file GlobalParam.h.

216 {
217 if (!m_object)
218 return nullptr;
219
220 return m_object.get();
221 }

◆ getDefaultName()

template<class DBObjType>
virtual std::string getDefaultName ( ) const
inlinefinaloverridevirtual

Get the DB object default name used by datastore.

Implements GlobalParamSetAccess.

Definition at line 211 of file GlobalParam.h.

211{return DataStore::objectName<DBObjType>("");}

◆ getGlobalParam() [1/3]

double getGlobalParam ( unsigned short element,
unsigned short param )
virtual

Get global parameter of the DB object by its element and parameter number Note this is not const, it might need to construct the object.

Implements GlobalParamSetAccess.

◆ getGlobalParam() [2/3]

double getGlobalParam ( unsigned short element,
unsigned short param )
virtual

Get global parameter of the DB object by its element and parameter number Note this is not const, it might need to construct the object.

Implements GlobalParamSetAccess.

Definition at line 35 of file GlobalParam.cc.

36 {
37 this->ensureConstructed();
38 if (element != 0 or param > 3) {
39 B2ERROR("Invalid global BeamSpot parameter id");
40 return 0;
41 }
42
43 if (auto bp = dynamic_cast<BeamSpot*>(this->getDBObj())) {
44 if (param == 1) {
45 return bp->getIPPosition().X();
46 } else if (param == 2) {
47 return bp->getIPPosition().Y();
48 } else if (param == 3) {
49 return bp->getIPPosition().Z();
50 }
51 }
52
53 B2ERROR("Could not get value for BeamSpot");
54 return 0.;
55 }

◆ getGlobalParam() [3/3]

template<class DBObjType>
virtual double getGlobalParam ( unsigned short element,
unsigned short param )
inlinefinaloverridevirtual

Get global parameter of the DB object by its element and parameter number Note this is not const, it might need to construct the object.

Implements GlobalParamSetAccess.

Definition at line 205 of file GlobalParam.h.

205{ensureConstructed(); return m_object->getGlobalParam(element, param);}

◆ getGlobalUniqueID() [1/3]

unsigned short getGlobalUniqueID ( ) const
virtual

The DB object unique id in global calibration.

Implements GlobalParamSetAccess.

◆ getGlobalUniqueID() [2/3]

unsigned short getGlobalUniqueID ( ) const
virtual

The DB object unique id in global calibration.

Implements GlobalParamSetAccess.

Definition at line 31 of file GlobalParam.cc.

31{ return 1; }

◆ getGlobalUniqueID() [3/3]

template<class DBObjType>
virtual unsigned short getGlobalUniqueID ( ) const
inlinefinaloverridevirtual

The DB object unique id in global calibration.

Implements GlobalParamSetAccess.

Definition at line 202 of file GlobalParam.h.

202{return DBObjType::getGlobalUniqueID();}

◆ hasBeenChangedInDB()

virtual bool hasBeenChangedInDB ( bool resetChangedState = true)
inlinefinalvirtualinherited

Has the object been updated in DB since GlobalParamSet<...> has been constructed? Since last call to this function with resetChangedState=true.

Definition at line 137 of file GlobalParam.h.

137{auto tmp = m_hasBeenChangedInDB; if (resetChangedState) m_hasBeenChangedInDB = false; return tmp;}

◆ is()

template<class DBObjType>
bool is ( ) const
inlineinherited

Is this set of given type?

Returns
true if stored set is of given type

Definition at line 144 of file GlobalParam.h.

144{return DBObjType::getGlobalUniqueID() == getGlobalUniqueID();}

◆ isConstructed()

template<class DBObjType>
virtual bool isConstructed ( ) const
inlinefinaloverridevirtual

Is the internal object already constructed?

we construct the object on first access to the stored values

Implements GlobalParamSetAccess.

Definition at line 290 of file GlobalParam.h.

290{return !!m_object;}

◆ listGlobalParams() [1/3]

std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams ( )
virtual

List global parameters in this DB object.

Implements GlobalParamSetAccess.

◆ listGlobalParams() [2/3]

std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams ( )
virtual

List global parameters in this DB object.

Implements GlobalParamSetAccess.

Definition at line 78 of file GlobalParam.cc.

79 {
80 return {{0, 1}, {0, 2}, {0, 3}};
81 }

◆ listGlobalParams() [3/3]

template<class DBObjType>
virtual std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams ( )
inlinefinaloverridevirtual

List global parameters in this DB object.

Implements GlobalParamSetAccess.

Definition at line 209 of file GlobalParam.h.

209{ensureConstructed(); return m_object->listGlobalParams();}

◆ loadFromDB()

template<class DBObjType>
virtual void loadFromDB ( EventMetaData event)
inlinefinaloverridevirtual

Load content of the object valid at given event from DB Also resets if the object has been changed.

Implements GlobalParamSetAccess.

Definition at line 273 of file GlobalParam.h.

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 }

◆ loadFromDBObjPtr()

template<class DBObjType>
virtual void loadFromDBObjPtr ( )
inlinefinaloverridevirtual

Load content of the object using DBObjPtr<DBObjType> which will try to load object valid for current EventMetaData Also resets if the object has been changed.

Implements GlobalParamSetAccess.

Definition at line 262 of file GlobalParam.h.

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 }

◆ operator bool()

operator bool ( )
inlineinherited

Tentative operator to quickly check the set Returns true when the set is not empty() and the DB object is constructed and can be accessed.

Definition at line 167 of file GlobalParam.h.

168 {
169 return not empty() and isConstructed();
170 }

◆ operator=()

template<class DBObjType>
GlobalParamSet< DBObjType > & operator= ( const GlobalParamSet< DBObjType > & other)
inline

Assignment operator.

Definition at line 233 of file GlobalParam.h.

234 {
235 // check for self-assignment
236 if (&other == this)
237 return *this;
238 m_hasBeenChangedInDB = other.m_hasBeenChangedInDB;
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 }

◆ releaseObject()

template<class DBObjType>
virtual TObject * releaseObject ( )
inlinefinaloverridevirtual

Release the DB object from the internal unique pointer to be managed elsewhere (e.g.

stored to the DB and deleted by it)

Implements GlobalParamSetAccess.

Definition at line 247 of file GlobalParam.h.

248 {
249 //ensureConstructed();
250 return m_object.release();
251 }

◆ setGlobalParam() [1/3]

void setGlobalParam ( double value,
unsigned short element,
unsigned short param )
virtual

Set global parameter of the DB object by its element and parameter number.

Implements GlobalParamSetAccess.

◆ setGlobalParam() [2/3]

void setGlobalParam ( double value,
unsigned short element,
unsigned short param )
virtual

Set global parameter of the DB object by its element and parameter number.

Implements GlobalParamSetAccess.

Definition at line 58 of file GlobalParam.cc.

59 {
60 this->ensureConstructed();
61 if (element != 0 or param > 3) {
62 B2ERROR("Invalid global BeamSpot id");
63 return;
64 }
65 if (auto bp = dynamic_cast<BeamSpot*>(this->getDBObj())) {
66 ROOT::Math::XYZVector vertex = bp->getIPPosition();
67 if (param == 1) vertex.SetX(value);
68 else if (param == 2) vertex.SetY(value);
69 else if (param == 3) vertex.SetZ(value);
70 bp->setIP(vertex, bp->getIPPositionCovMatrix());
71 } else {
72 B2ERROR("Could not set value for BeamSpot");
73 }
74
75 }

◆ setGlobalParam() [3/3]

template<class DBObjType>
virtual void setGlobalParam ( double value,
unsigned short element,
unsigned short param )
inlinefinaloverridevirtual

Set global parameter of the DB object by its element and parameter number.

Implements GlobalParamSetAccess.

Definition at line 207 of file GlobalParam.h.

207{ensureConstructed(); m_object->setGlobalParam(value, element, param);}

◆ updateGlobalParam()

virtual void updateGlobalParam ( double correction,
unsigned short element,
unsigned short param )
inlinevirtualinherited

Sum value of global element param with a correction.

Definition at line 112 of file GlobalParam.h.

112{setGlobalParam(getGlobalParam(element, param) + correction, element, param);}

Member Data Documentation

◆ m_hasBeenChangedInDB

bool m_hasBeenChangedInDB {false}
protectedinherited

Flag for object changed in DB.

Definition at line 174 of file GlobalParam.h.

174{false};

◆ m_object

template<class DBObjType>
std::unique_ptr<DBObjType> m_object {}
private

The internal DB object.

Definition at line 299 of file GlobalParam.h.

299{};

The documentation for this class was generated from the following file: