Belle II Software  release-08-01-10
GlobalParam.cc
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 #include <alignment/GlobalParam.h>
10 
11 #include <alignment/Hierarchy.h>
12 #include <cdc/dbobjects/CDCLayerAlignment.h>
13 #include <framework/database/Database.h>
14 #include <framework/geometry/B2Vector3.h>
15 #include <framework/logging/Logger.h>
16 #include <vxd/geometry/GeoCache.h>
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 namespace Belle2 {
27  namespace alignment {
28  //std::tuple<BeamSpot, VXDAlignment, CDCAlignment, CDCLayerAlignment, CDCTimeWalks, CDCTimeZeros, CDCXtRelations, BKLMAlignment, EKLMAlignment> dbvector = {};
29 
31  template <>
32  unsigned short GlobalParamSet<BeamSpot>::getGlobalUniqueID() const { return 1; }
35  template <>
36  double GlobalParamSet<BeamSpot>::getGlobalParam(unsigned short element, unsigned short param)
37  {
38  this->ensureConstructed();
39  if (element != 0 or param > 3) {
40  B2ERROR("Invalid global BeamSpot parameter id");
41  return 0;
42  }
43 
44  if (auto bp = dynamic_cast<BeamSpot*>(this->getDBObj()))
45  return bp->getIPPosition()[param - 1];
46 
47  B2ERROR("Could not get value for BeamSpot");
48  return 0.;
49  }
51  template <>
52  void GlobalParamSet<BeamSpot>::setGlobalParam(double value, unsigned short element, unsigned short param)
53  {
54  this->ensureConstructed();
55  if (element != 0 or param > 3) {
56  B2ERROR("Invalid global BeamSpot id");
57  return;
58  }
59  if (auto bp = dynamic_cast<BeamSpot*>(this->getDBObj())) {
60  B2Vector3D vertex = bp->getIPPosition();
61  vertex[param - 1] = value;
62  bp->setIP(vertex, bp->getIPPositionCovMatrix());
63  } else {
64  B2ERROR("Could not set value for BeamSpot");
65  }
66 
67  }
69  template <>
70  std::vector<std::pair<unsigned short, unsigned short>> GlobalParamSet<BeamSpot>::listGlobalParams()
71  {
72  return {{0, 1}, {0, 2}, {0, 3}};
73  }
74 
75 
76  GlobalParamVector::GlobalParamVector(const std::vector< std::string >& components) : m_components(components) {}
77 
78  void GlobalParamVector::updateGlobalParam(double difference, short unsigned int uniqueID, short unsigned int element,
79  short unsigned int param)
80  {
81  // TODO: niw can be replaced by other fcn updateGlobalParam of GlobalParamSetAccess
82  auto prev = getGlobalParam(uniqueID, element, param);
83  setGlobalParam(prev + difference, uniqueID, element, param);
84  }
85  void GlobalParamVector::setGlobalParam(double value, short unsigned int uniqueID, short unsigned int element,
86  short unsigned int param)
87  {
88  auto dbObj = m_vector.find(uniqueID);
89  if (dbObj != m_vector.end()) {
90  dbObj->second->setGlobalParam(value, element, param);
91  } else {
92  B2WARNING("Did not found DB object with unique id " << uniqueID << " in global vector. Cannot set value for element " << element <<
93  " and parameter " << param);
94  }
95  }
96  double GlobalParamVector::getGlobalParam(short unsigned int uniqueID, short unsigned int element, short unsigned int param)
97  {
98  auto dbObj = m_vector.find(uniqueID);
99  if (dbObj != m_vector.end()) {
100  return dbObj->second->getGlobalParam(element, param);
101  } else {
102  B2WARNING("Did not found DB object with unique id " << uniqueID << " in global vector. Cannot get value for element " << element <<
103  " and parameter " << param << ". Returning 0.");
104  return 0.;
105  }
106  }
107  std::vector< std::tuple< short unsigned int, short unsigned int, short unsigned int > > GlobalParamVector::listGlobalParams()
108  {
109  std::vector<std::tuple<unsigned short, unsigned short, unsigned short>> params;
110  for (auto& uID_DBObj : m_vector) {
111  for (auto element_param : uID_DBObj.second->listGlobalParams()) {
112  params.push_back({uID_DBObj.first, element_param.first, element_param.second});
113  }
114  }
115  return params;
116  }
117  std::vector< TObject* > GlobalParamVector::releaseObjects(bool onlyConstructed)
118  {
119  std::vector<TObject*> result;
120  for (auto& uID_DBObj : m_vector) {
121  if (onlyConstructed and not uID_DBObj.second->isConstructed())
122  continue;
123 
124  result.push_back({uID_DBObj.second->releaseObject()});
125  }
126  return result;
127  }
129  {
130  for (auto& uID_DBObj : m_vector) {
131  uID_DBObj.second->loadFromDBObjPtr();
132  }
133  }
135  {
136  for (auto& uID_DBObj : m_vector) {
137  uID_DBObj.second->loadFromDB(event);
138  }
139  }
141  {
142  for (auto interface : m_subDetectorInterfacesVector) {
143  interface->setupAlignmentHierarchy(hierarchy);
144  }
145 
146  }
148  {
149  for (auto& uID_DBObj : m_vector) {
150  if (not uID_DBObj.second->isConstructed()) uID_DBObj.second->construct();
151  }
152  }
154  std::vector< std::tuple< short unsigned int, short unsigned int, short unsigned int, double > >& results)
155  {
156  for (auto interface : m_subDetectorInterfacesVector) {
157  interface->readFromResult(results, *this);
158  }
159  }
160 
162  VXDGlobalParamInterface::E_VXDHierarchyType::c_Full;
165 
167  {
168  try {
169  auto& rigidBodyHierarchy = dynamic_cast<RigidBodyHierarchy&>(hierarchy);
170  auto& geo = VXD::GeoCache::getInstance();
171  // Set-up hierarchy
172  DBObjPtr<VXDAlignment> vxdAlignments;
173  if (s_hierarchyType == c_None) {
174  // Nothing to be done
175  return;
176 
177  } else if (s_hierarchyType == c_Flat) {
178  for (auto& sensor : geo.getListOfSensors()) {
179  //TODO: Don't we have better way to distinguish PXD and SVD?
180  // For PXD
181  if (sensor.getLayerNumber() <= 2 and !s_enablePXD)
182  continue;
183  // SVD layer > 2
184  if (sensor.getLayerNumber() > 2 and !s_enableSVD)
185  continue;
186 
187  rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(sensor, VxdID(0, 0, 0, 1),
188  geo.get(sensor).getTransformation(true));
189  }
190  } else if (s_hierarchyType == c_HalfShells) {
200  for (auto& halfShellPlacement : geo.getHalfShellPlacements()) {
201  // For PXD
202  if (halfShellPlacement.first.getLayerNumber() <= 2 and !s_enablePXD)
203  continue;
204  // for SVD - layer > 2
205  if (halfShellPlacement.first.getLayerNumber() > 2 and !s_enableSVD)
206  continue;
207 
208  TGeoHMatrix trafoHalfShell = halfShellPlacement.second;
209  trafoHalfShell *= geo.getTGeoFromRigidBodyParams(
210  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dU),
211  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dV),
212  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dW),
213  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dAlpha),
214  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dBeta),
215  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dGamma)
216  );
217  rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, alignment::EmptyGlobalParamSet>(halfShellPlacement.first, 0, trafoHalfShell);
218 
219  for (auto& ladderPlacement : geo.getLadderPlacements(halfShellPlacement.first)) {
220  // Updated trafo
221  TGeoHMatrix trafoLadder = ladderPlacement.second;
222  trafoLadder *= geo.getTGeoFromRigidBodyParams(
223  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dU),
224  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dV),
225  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dW),
226  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dAlpha),
227  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dBeta),
228  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dGamma)
229  );
230  // Do not insert ladder, but insert sensor directly into half-shells bellow, with the additional transformation from ladder
231  //rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(ladderPlacement.first, halfShellPlacement.first, trafoLadder);
232 
233  for (auto& sensorPlacement : geo.getSensorPlacements(ladderPlacement.first)) {
234  // Updated trafo
235  TGeoHMatrix trafoSensor = sensorPlacement.second;
236  trafoSensor *= geo.getTGeoFromRigidBodyParams(
237  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dU),
238  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dV),
239  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dW),
240  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dAlpha),
241  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dBeta),
242  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dGamma)
243  );
244  rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(sensorPlacement.first, halfShellPlacement.first,
245  trafoLadder * trafoSensor);
246 
247 
248  }
249  }
250  }
251  } else if (s_hierarchyType == c_Full) {
263  for (auto& halfShellPlacement : geo.getHalfShellPlacements()) {
264  // For PXD
265  if (halfShellPlacement.first.getLayerNumber() <= 2 and !s_enablePXD)
266  continue;
267  // for SVD - layer > 2
268  if (halfShellPlacement.first.getLayerNumber() > 2 and !s_enableSVD)
269  continue;
270 
271  TGeoHMatrix trafoHalfShell = halfShellPlacement.second;
272  trafoHalfShell *= geo.getTGeoFromRigidBodyParams(
273  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dU),
274  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dV),
275  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dW),
276  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dAlpha),
277  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dBeta),
278  vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dGamma)
279  );
280  rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, alignment::EmptyGlobalParamSet>(halfShellPlacement.first, 0, trafoHalfShell);
281 
282  for (auto& ladderPlacement : geo.getLadderPlacements(halfShellPlacement.first)) {
283  // Updated trafo
284  TGeoHMatrix trafoLadder = ladderPlacement.second;
285  trafoLadder *= geo.getTGeoFromRigidBodyParams(
286  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dU),
287  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dV),
288  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dW),
289  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dAlpha),
290  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dBeta),
291  vxdAlignments->get(ladderPlacement.first, VXDAlignment::dGamma)
292  );
293  rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(ladderPlacement.first, halfShellPlacement.first, trafoLadder);
294 
295  for (auto& sensorPlacement : geo.getSensorPlacements(ladderPlacement.first)) {
296  // Updated trafo
297  TGeoHMatrix trafoSensor = sensorPlacement.second;
298  trafoSensor *= geo.getTGeoFromRigidBodyParams(
299  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dU),
300  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dV),
301  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dW),
302  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dAlpha),
303  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dBeta),
304  vxdAlignments->get(sensorPlacement.first, VXDAlignment::dGamma)
305  );
306  rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(sensorPlacement.first, ladderPlacement.first, trafoSensor);
307 
308 
309  }
310  }
311  }
312  }
313  } catch (const std::bad_cast& e) {
314  // Bad cast
315  }
316  }
317 
318  }
320 }
This class contains the beam spot position and size modeled as a gaussian distribution in space.
Definition: BeamSpot.h:22
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
Store event, run, and experiment numbers.
Definition: EventMetaData.h:33
VXD alignment (and maybe some calibration) parameters.
Definition: VXDAlignment.h:19
static const baseType dAlpha
Sensor/layer/ladder alignment in local alpha.
Definition: VXDAlignment.h:29
static const baseType dGamma
Sensor/layer/ladder alignment in local gamma.
Definition: VXDAlignment.h:33
static const baseType dW
Sensor/layer/ladder alignment in local w.
Definition: VXDAlignment.h:27
static const baseType dBeta
Sensor/layer/ladder alignment in local beta.
Definition: VXDAlignment.h:31
static const baseType dU
Sensor/layer/ladder alignment in local u.
Definition: VXDAlignment.h:23
static const baseType dV
Sensor/layer/ladder alignment in local v.
Definition: VXDAlignment.h:25
static GeoCache & getInstance()
Return a reference to the singleton instance.
Definition: GeoCache.cc:214
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:33
Object representing no parameters.
Definition: GlobalParam.h:32
Class for alignment/calibration parameter hierarchy & constraints.
Definition: Hierarchy.h:41
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
virtual unsigned short getGlobalUniqueID() const override final
The DB object unique id in global calibration.
Definition: GlobalParam.h:202
virtual std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams() override final
List global parameters in this DB object.
Definition: GlobalParam.h:209
std::vector< std::tuple< unsigned short, unsigned short, unsigned short > > listGlobalParams()
Create vector of all global parameters maintained by GlobalParamVector.
Definition: GlobalParam.cc:107
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
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 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
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
6D Hierarchy of rigid bodies
Definition: Hierarchy.h:144
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
Abstract base class for different kinds of events.