Belle II Software development
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 <framework/logging/Logger.h>
13#include <vxd/geometry/GeoCache.h>
14
15#include <Math/Vector3D.h>
16
17#include <map>
18#include <string>
19#include <vector>
20
21namespace Belle2 {
26 namespace alignment {
27 //std::tuple<BeamSpot, VXDAlignment, CDCAlignment, CDCLayerAlignment, CDCTimeWalks, CDCTimeZeros, CDCXtRelations, BKLMAlignment, EKLMAlignment> dbvector = {};
28
30 template <>
31 unsigned short GlobalParamSet<BeamSpot>::getGlobalUniqueID() const { return 1; }
34 template <>
35 double GlobalParamSet<BeamSpot>::getGlobalParam(unsigned short element, unsigned short param)
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 }
56
57 template <>
58 void GlobalParamSet<BeamSpot>::setGlobalParam(double value, unsigned short element, unsigned short param)
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 }
76
77 template <>
78 std::vector<std::pair<unsigned short, unsigned short>> GlobalParamSet<BeamSpot>::listGlobalParams()
79 {
80 return {{0, 1}, {0, 2}, {0, 3}};
81 }
82
83
84 GlobalParamVector::GlobalParamVector(const std::vector< std::string >& components) : m_components(components) {}
85
86 void GlobalParamVector::updateGlobalParam(double difference, short unsigned int uniqueID, short unsigned int element,
87 short unsigned int param)
88 {
89 // TODO: niw can be replaced by other fcn updateGlobalParam of GlobalParamSetAccess
90 auto prev = getGlobalParam(uniqueID, element, param);
91 setGlobalParam(prev + difference, uniqueID, element, param);
92 }
93 void GlobalParamVector::setGlobalParam(double value, short unsigned int uniqueID, short unsigned int element,
94 short unsigned int param)
95 {
96 auto dbObj = m_vector.find(uniqueID);
97 if (dbObj != m_vector.end()) {
98 dbObj->second->setGlobalParam(value, element, param);
99 } else {
100 B2WARNING("Did not found DB object with unique id " << uniqueID << " in global vector. Cannot set value for element " << element <<
101 " and parameter " << param);
102 }
103 }
104 double GlobalParamVector::getGlobalParam(short unsigned int uniqueID, short unsigned int element, short unsigned int param)
105 {
106 auto dbObj = m_vector.find(uniqueID);
107 if (dbObj != m_vector.end()) {
108 return dbObj->second->getGlobalParam(element, param);
109 } else {
110 B2WARNING("Did not found DB object with unique id " << uniqueID << " in global vector. Cannot get value for element " << element <<
111 " and parameter " << param << ". Returning 0.");
112 return 0.;
113 }
114 }
115 std::vector< std::tuple< short unsigned int, short unsigned int, short unsigned int > > GlobalParamVector::listGlobalParams()
116 {
117 std::vector<std::tuple<unsigned short, unsigned short, unsigned short>> params;
118 for (auto& uID_DBObj : m_vector) {
119 for (auto element_param : uID_DBObj.second->listGlobalParams()) {
120 params.push_back({uID_DBObj.first, element_param.first, element_param.second});
121 }
122 }
123 return params;
124 }
125 std::vector< TObject* > GlobalParamVector::releaseObjects(bool onlyConstructed)
126 {
127 std::vector<TObject*> result;
128 for (auto& uID_DBObj : m_vector) {
129 if (onlyConstructed and not uID_DBObj.second->isConstructed())
130 continue;
131
132 result.push_back({uID_DBObj.second->releaseObject()});
133 }
134 return result;
135 }
137 {
138 for (auto& uID_DBObj : m_vector) {
139 uID_DBObj.second->loadFromDBObjPtr();
140 }
141 }
143 {
144 for (auto& uID_DBObj : m_vector) {
145 uID_DBObj.second->loadFromDB(event);
146 }
147 }
149 {
150 for (auto interface : m_subDetectorInterfacesVector) {
151 interface->setupAlignmentHierarchy(hierarchy);
152 }
153
154 }
156 {
157 for (auto& uID_DBObj : m_vector) {
158 if (not uID_DBObj.second->isConstructed()) uID_DBObj.second->construct();
159 }
160 }
162 std::vector< std::tuple< short unsigned int, short unsigned int, short unsigned int, double > >& results)
163 {
164 for (auto interface : m_subDetectorInterfacesVector) {
165 interface->readFromResult(results, *this);
166 }
167 }
168
170 VXDGlobalParamInterface::E_VXDHierarchyType::c_Full;
173
175 {
176 try {
177 auto& rigidBodyHierarchy = dynamic_cast<RigidBodyHierarchy&>(hierarchy);
178 auto& geo = VXD::GeoCache::getInstance();
179 // Set-up hierarchy
180 DBObjPtr<VXDAlignment> vxdAlignments;
181 if (s_hierarchyType == c_None) {
182 // Nothing to be done
183 return;
184
185 } else if (s_hierarchyType == c_Flat) {
186 for (auto& sensor : geo.getListOfSensors()) {
187 //TODO: Don't we have better way to distinguish PXD and SVD?
188 // For PXD
189 if (sensor.getLayerNumber() <= 2 and !s_enablePXD)
190 continue;
191 // SVD layer > 2
192 if (sensor.getLayerNumber() > 2 and !s_enableSVD)
193 continue;
194
195 rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(sensor, VxdID(0, 0, 0, 1),
196 geo.getSensorInfo(sensor).getTransformation(true));
197 }
198 } else if (s_hierarchyType == c_HalfShells) {
207
208 for (auto& halfShellPlacement : geo.getHalfShellPlacements()) {
209 // For PXD
210 if (halfShellPlacement.first.getLayerNumber() <= 2 and !s_enablePXD)
211 continue;
212 // for SVD - layer > 2
213 if (halfShellPlacement.first.getLayerNumber() > 2 and !s_enableSVD)
214 continue;
215
216 TGeoHMatrix trafoHalfShell = halfShellPlacement.second;
217 trafoHalfShell *= geo.getTGeoFromRigidBodyParams(
218 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dU),
219 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dV),
220 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dW),
221 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dAlpha),
222 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dBeta),
223 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dGamma)
224 );
225 rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, alignment::EmptyGlobalParamSet>(halfShellPlacement.first, 0, trafoHalfShell);
226
227 for (auto& ladderPlacement : geo.getLadderPlacements(halfShellPlacement.first)) {
228 // Updated trafo
229 TGeoHMatrix trafoLadder = ladderPlacement.second;
230 trafoLadder *= geo.getTGeoFromRigidBodyParams(
231 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dU),
232 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dV),
233 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dW),
234 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dAlpha),
235 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dBeta),
236 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dGamma)
237 );
238 // Do not insert ladder, but insert sensor directly into half-shells below, with the additional transformation from ladder
239 //rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(ladderPlacement.first, halfShellPlacement.first, trafoLadder);
240
241 for (auto& sensorPlacement : geo.getSensorPlacements(ladderPlacement.first)) {
242 // Updated trafo
243 TGeoHMatrix trafoSensor = sensorPlacement.second;
244 trafoSensor *= geo.getTGeoFromRigidBodyParams(
245 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dU),
246 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dV),
247 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dW),
248 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dAlpha),
249 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dBeta),
250 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dGamma)
251 );
252 rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(sensorPlacement.first, halfShellPlacement.first,
253 trafoLadder * trafoSensor);
254
255
256 }
257 }
258 }
259 } else if (s_hierarchyType == c_Full) {
270
271 for (auto& halfShellPlacement : geo.getHalfShellPlacements()) {
272 // For PXD
273 if (halfShellPlacement.first.getLayerNumber() <= 2 and !s_enablePXD)
274 continue;
275 // for SVD - layer > 2
276 if (halfShellPlacement.first.getLayerNumber() > 2 and !s_enableSVD)
277 continue;
278
279 TGeoHMatrix trafoHalfShell = halfShellPlacement.second;
280 trafoHalfShell *= geo.getTGeoFromRigidBodyParams(
281 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dU),
282 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dV),
283 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dW),
284 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dAlpha),
285 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dBeta),
286 vxdAlignments->get(halfShellPlacement.first, VXDAlignment::dGamma)
287 );
288 rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, alignment::EmptyGlobalParamSet>(halfShellPlacement.first, 0, trafoHalfShell);
289
290 for (auto& ladderPlacement : geo.getLadderPlacements(halfShellPlacement.first)) {
291 // Updated trafo
292 TGeoHMatrix trafoLadder = ladderPlacement.second;
293 trafoLadder *= geo.getTGeoFromRigidBodyParams(
294 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dU),
295 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dV),
296 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dW),
297 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dAlpha),
298 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dBeta),
299 vxdAlignments->get(ladderPlacement.first, VXDAlignment::dGamma)
300 );
301 rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(ladderPlacement.first, halfShellPlacement.first, trafoLadder);
302
303 for (auto& sensorPlacement : geo.getSensorPlacements(ladderPlacement.first)) {
304 // Updated trafo
305 TGeoHMatrix trafoSensor = sensorPlacement.second;
306 trafoSensor *= geo.getTGeoFromRigidBodyParams(
307 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dU),
308 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dV),
309 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dW),
310 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dAlpha),
311 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dBeta),
312 vxdAlignments->get(sensorPlacement.first, VXDAlignment::dGamma)
313 );
314 rigidBodyHierarchy.insertTGeoTransform<VXDAlignment, VXDAlignment>(sensorPlacement.first, ladderPlacement.first, trafoSensor);
315
316
317 }
318 }
319 }
320 }
321 } catch (const std::bad_cast& e) {
322 // Bad cast
323 }
324 }
325
326 }
328}
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.
VXD alignment (and maybe some calibration) parameters.
static const baseType dAlpha
Sensor/layer/ladder alignment in local alpha.
static const baseType dGamma
Sensor/layer/ladder alignment in local gamma.
static const baseType dW
Sensor/layer/ladder alignment in local w.
static const baseType dBeta
Sensor/layer/ladder alignment in local beta.
static const baseType dU
Sensor/layer/ladder alignment in local u.
static const baseType dV
Sensor/layer/ladder alignment in local v.
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.
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,...
virtual std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams() override final
List global parameters in this DB object.
virtual unsigned short getGlobalUniqueID() const override final
The DB object unique id in global calibration.
std::vector< std::string > m_components
Vector of names of DB objects to consider in the vector - if not here and non-empy,...
std::vector< std::tuple< unsigned short, unsigned short, unsigned short > > listGlobalParams()
Create vector of all global parameters maintained by GlobalParamVector.
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...
GlobalParamVector(const std::vector< std::string > &components={})
Constructor.
std::map< unsigned short, std::unique_ptr< GlobalParamSetAccess > > m_vector
The vector (well, actually a map) of DB objects.
std::vector< std::shared_ptr< IGlobalParamInterface > > m_subDetectorInterfacesVector
Vector of interfaces to run at specific points in the workflow.
void setGlobalParam(double value, unsigned short uniqueID, unsigned short element, unsigned short param)
Set param value.
double getGlobalParam(unsigned short uniqueID, unsigned short element, unsigned short param)
Get parameter value.
void construct()
Construct all DB objects using default constructors (should be filled with zeros)
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...
void postHierarchyChanged(GlobalDerivativesHierarchy &hierarchy)
Function calling interfaces to build alignment hierarchy after constants changed in DB.
void loadFromDB()
Load content of all objects in vector with values valid at current EventMetaData Uses DBObjPtr<DBObjT...
void updateGlobalParam(double difference, unsigned short uniqueID, unsigned short element, unsigned short param)
Add 'difference' to param value.
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 tentative function: not yet used.
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.