Belle II Software  release-08-01-10
Hierarchy.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/geometry/B2Vector3.h>
12 
13 #include <genfit/StateOnPlane.h>
14 
15 #include <root/TMatrixD.h>
16 #include <root/TGeoMatrix.h>
17 
18 #include <Geant4/G4Transform3D.hh>
19 
20 #include <map>
21 #include <set>
22 #include <vector>
23 
24 namespace Belle2 {
29  namespace alignment {
30 
32  typedef std::pair<unsigned short, unsigned short> DetectorLevelElement;
34  typedef std::pair<std::vector<int>, TMatrixD> GlobalDerivativeSet;
36  typedef std::vector<std::pair<int, double>> Constraint;
38  typedef std::map<long, Constraint> Constraints;
39 
42  public:
47 
49  void buildConstraints(Constraints& constraints);
50 
52  GlobalDerivativeSet buildGlobalDerivativesHierarchy(TMatrixD matrixChain, DetectorLevelElement child);
53 
59  template<class ChildDBObjectType, class MotherDBObjectType>
60  void insert(unsigned short child, unsigned short mother, TMatrixD childToMotherParamTransform)
61  {
62  auto childUID = std::make_pair(ChildDBObjectType::getGlobalUniqueID(), child);
63  auto parentUID = std::make_pair(MotherDBObjectType::getGlobalUniqueID(), mother);
64 
65  if (m_lookup.insert(std::make_pair(childUID, std::make_pair(parentUID, childToMotherParamTransform))).second) {
66  // Just inserted an child element (so each child is inserted exactly once)
67  // Try to insert parent with empty vect (does nothing if parent already in map)
68  m_hierarchy.insert(std::make_pair(parentUID, std::vector<DetectorLevelElement>()));
69  // insert child (can happen only once, so the vect is unique)
70  m_hierarchy[parentUID].push_back(childUID);
71  m_usedUniqueIDs.insert(ChildDBObjectType::getGlobalUniqueID());
72  m_usedUniqueIDs.insert(MotherDBObjectType::getGlobalUniqueID());
73  } else {
74  // Update element transformation if inserted again
75  m_lookup[childUID].second = childToMotherParamTransform;
76  }
77  }
78 
81  static void mergeGlobals(GlobalDerivativeSet& main, GlobalDerivativeSet additional);
82 
84  void printHierarchy();
85 
87  virtual std::vector<int> getElementLabels(DetectorLevelElement element) = 0;
88 
91  const std::set<unsigned short>& getUsedDBObjUniqueIDs() {return m_usedUniqueIDs;}
92 
93  private:
95  std::pair<DetectorLevelElement, TMatrixD> getChildToMotherTransform(DetectorLevelElement child);
96 
98  std::map<DetectorLevelElement, std::pair<DetectorLevelElement, TMatrixD>> m_lookup;
100  std::map<DetectorLevelElement, std::vector<DetectorLevelElement>> m_hierarchy;
101 
104  std::set<unsigned short> m_usedUniqueIDs {};
105  };
106 
109  public:
112 
114  std::vector<int> getElementLabels(DetectorLevelElement element) final;
115 
117  template<class LowestLevelDBObject>
118  GlobalDerivativeSet getGlobalDerivatives(unsigned short sensor, const genfit::StateOnPlane* sop, B2Vector3D bField)
119  {
120  if (bField.Mag() < 1.e-10)
121  return std::make_pair(std::vector<int>(), TMatrixD());
122 
123  DetectorLevelElement elementUID = std::make_pair(LowestLevelDBObject::getGlobalUniqueID(), sensor);
124 
125  GlobalDerivativeSet sensorDerivs = std::make_pair(getElementLabels(elementUID), getLorentzShiftDerivatives(sop, bField));
126  mergeGlobals(sensorDerivs, buildGlobalDerivativesHierarchy(getLorentzShiftDerivatives(sop, bField), elementUID));
127  return sensorDerivs;
128  }
129 
131  TMatrixD getLorentzShiftDerivatives(const genfit::StateOnPlane* sop, B2Vector3D bField);
132 
134  template<class ChildDBObjectType, class MotherDBObjectType>
135  void insertRelation(unsigned short child, unsigned short mother)
136  {
137  insert<ChildDBObjectType, MotherDBObjectType>(child, mother, TMatrixD(1, 1).UnitMatrix());
138  }
139 
140  private:
141  };
142 
145 
146  public:
147 
150 
151  // Destructor
152  ~RigidBodyHierarchy() {}
153 
155  std::vector<int> getElementLabels(DetectorLevelElement element) override;
156 
158  template<class LowestLevelDBObject>
159  GlobalDerivativeSet getGlobalDerivatives(unsigned short sensor, const genfit::StateOnPlane* sop)
160  {
161  DetectorLevelElement elementUID = std::make_pair(LowestLevelDBObject::getGlobalUniqueID(), sensor);
162 
163  GlobalDerivativeSet sensorDerivs = std::make_pair(getElementLabels(elementUID), getRigidBodyDerivatives(sop));
165  return sensorDerivs;
166  }
167 
169  template<class ChildDBObjectType, class MotherDBObjectType>
170  void insertG4Transform(unsigned short child, unsigned short mother, G4Transform3D childToMother)
171  {
172  insert<ChildDBObjectType, MotherDBObjectType>(child, mother, convertG4ToRigidBodyTransformation(childToMother));
173  }
174 
176  template<class ChildDBObjectType, class MotherDBObjectType>
177  void insertTGeoTransform(unsigned short child, unsigned short mother, TGeoHMatrix childToMother)
178  {
179  insert<ChildDBObjectType, MotherDBObjectType>(child, mother, convertTGeoToRigidBodyTransformation(childToMother));
180  }
181 
183  TMatrixD getRigidBodyDerivatives(const genfit::StateOnPlane* sop);
184 
186  TMatrixD convertG4ToRigidBodyTransformation(G4Transform3D g4transform);
187 
189  TMatrixD convertTGeoToRigidBodyTransformation(TGeoHMatrix tgeo);
190  private:
191  };
192  }
194 }
DataType Mag() const
The magnitude (rho in spherical coordinate system).
Definition: B2Vector3.h:159
Class for alignment/calibration parameter hierarchy & constraints.
Definition: Hierarchy.h:41
static void mergeGlobals(GlobalDerivativeSet &main, GlobalDerivativeSet additional)
Merge additional set into main set of global labels and derivatives TODO: move to some utilities.
Definition: Hierarchy.cc:95
void buildConstraints(Constraints &constraints)
Adds constraints from current hierarchy to a constraints vector.
Definition: Hierarchy.cc:24
std::map< DetectorLevelElement, std::pair< DetectorLevelElement, TMatrixD > > m_lookup
Map with all the parameter data (child -> (mother, transform_child2mother))
Definition: Hierarchy.h:98
std::map< DetectorLevelElement, std::vector< DetectorLevelElement > > m_hierarchy
Map of hierarchy relations mother-> child.
Definition: Hierarchy.h:100
GlobalDerivativeSet buildGlobalDerivativesHierarchy(TMatrixD matrixChain, DetectorLevelElement child)
Recursive function which adds labels and derivatives until top element in hierarchy is found.
Definition: Hierarchy.cc:76
void insert(unsigned short child, unsigned short mother, TMatrixD childToMotherParamTransform)
Template function to add relation between two elements (possibly in different objects with constants)...
Definition: Hierarchy.h:60
std::set< unsigned short > m_usedUniqueIDs
The set of unique id of each DB object used for construction For more efficient updates of hierarchy ...
Definition: Hierarchy.h:104
virtual ~GlobalDerivativesHierarchy()
Destructor (virtual)
Definition: Hierarchy.h:46
virtual std::vector< int > getElementLabels(DetectorLevelElement element)=0
The only function to implement: what are the global labels for the element?
void printHierarchy()
print the lookup map
Definition: Hierarchy.cc:116
const std::set< unsigned short > & getUsedDBObjUniqueIDs()
Get the global unique ids of DB objects used to construct hierarchy Usefull to update hierarchy only ...
Definition: Hierarchy.h:91
std::pair< DetectorLevelElement, TMatrixD > getChildToMotherTransform(DetectorLevelElement child)
Find the transformation in the lookup.
Definition: Hierarchy.cc:125
1D Hierarchy for Lorentz shift correction
Definition: Hierarchy.h:108
GlobalDerivativeSet getGlobalDerivatives(unsigned short sensor, const genfit::StateOnPlane *sop, B2Vector3D bField)
Template function to get globals for given db object and its element (and the rest of hierarchy)
Definition: Hierarchy.h:118
std::vector< int > getElementLabels(DetectorLevelElement element) final
Label for lorentz shift parameter.
Definition: Hierarchy.cc:137
TMatrixD getLorentzShiftDerivatives(const genfit::StateOnPlane *sop, B2Vector3D bField)
Derivatives for Lorentz shift in sensor plane.
Definition: Hierarchy.cc:148
void insertRelation(unsigned short child, unsigned short mother)
Template function to insert hierarchy relation bewteen two DB objects and their elements.
Definition: Hierarchy.h:135
6D Hierarchy of rigid bodies
Definition: Hierarchy.h:144
GlobalDerivativeSet getGlobalDerivatives(unsigned short sensor, const genfit::StateOnPlane *sop)
Get globals for given db object (and the rest of hierarchy) and its element at StateOnPlane.
Definition: Hierarchy.h:159
TMatrixD convertTGeoToRigidBodyTransformation(TGeoHMatrix tgeo)
Conversion from G4Transform3D to 6D rigid body transformation parametrization.
Definition: Hierarchy.cc:265
void insertG4Transform(unsigned short child, unsigned short mother, G4Transform3D childToMother)
Insert hierarchy relation.
Definition: Hierarchy.h:170
TMatrixD getRigidBodyDerivatives(const genfit::StateOnPlane *sop)
2x6 matrix of rigid body derivatives
Definition: Hierarchy.cc:203
TMatrixD convertG4ToRigidBodyTransformation(G4Transform3D g4transform)
Conversion from G4Transform3D to 6D rigid body transformation parametrization.
Definition: Hierarchy.cc:235
std::vector< int > getElementLabels(DetectorLevelElement element) override
Rigid body labels.
Definition: Hierarchy.cc:172
void insertTGeoTransform(unsigned short child, unsigned short mother, TGeoHMatrix childToMother)
Insert hierarchy relation.
Definition: Hierarchy.h:177
A state with arbitrary dimension defined in a DetPlane.
Definition: StateOnPlane.h:47
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:91