Belle II Software  release-05-02-19
Hierarchy.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Tadeas Bilka *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/geometry/B2Vector3.h>
14 
15 #include <genfit/StateOnPlane.h>
16 
17 #include <root/TMatrixD.h>
18 #include <root/TGeoMatrix.h>
19 
20 #include <Geant4/G4Transform3D.hh>
21 
22 #include <map>
23 #include <set>
24 #include <vector>
25 
26 namespace Belle2 {
31  namespace alignment {
32 
34  typedef std::pair<unsigned short, unsigned short> DetectorLevelElement;
36  typedef std::pair<std::vector<int>, TMatrixD> GlobalDerivativeSet;
38  typedef std::vector<std::pair<int, double>> Constraint;
40  typedef std::map<long, Constraint> Constraints;
41 
43  class GlobalDerivativesHierarchy {
44  public:
48  virtual ~GlobalDerivativesHierarchy() {}
49 
51  void buildConstraints(Constraints& constraints);
52 
54  GlobalDerivativeSet buildGlobalDerivativesHierarchy(TMatrixD matrixChain, DetectorLevelElement child);
55 
61  template<class ChildDBObjectType, class MotherDBObjectType>
62  void insert(unsigned short child, unsigned short mother, TMatrixD childToMotherParamTransform)
63  {
64  auto childUID = std::make_pair(ChildDBObjectType::getGlobalUniqueID(), child);
65  auto parentUID = std::make_pair(MotherDBObjectType::getGlobalUniqueID(), mother);
66 
67  if (m_lookup.insert(std::make_pair(childUID, std::make_pair(parentUID, childToMotherParamTransform))).second) {
68  // Just inserted an child element (so each child is inserted exactly once)
69  // Try to insert parent with empty vect (does nothing if parent already in map)
70  m_hierarchy.insert(std::make_pair(parentUID, std::vector<DetectorLevelElement>()));
71  // insert child (can happen only once, so the vect is unique)
72  m_hierarchy[parentUID].push_back(childUID);
73  m_usedUniqueIDs.insert(ChildDBObjectType::getGlobalUniqueID());
74  m_usedUniqueIDs.insert(MotherDBObjectType::getGlobalUniqueID());
75  } else {
76  // Update element transformation if inserted again
77  m_lookup[childUID].second = childToMotherParamTransform;
78  }
79  }
80 
83  static void mergeGlobals(GlobalDerivativeSet& main, GlobalDerivativeSet additional);
84 
86  void printHierarchy();
87 
89  virtual std::vector<int> getElementLabels(DetectorLevelElement element) = 0;
90 
93  const std::set<unsigned short>& getUsedDBObjUniqueIDs() {return m_usedUniqueIDs;}
94 
95  private:
97  std::pair<DetectorLevelElement, TMatrixD> getChildToMotherTransform(DetectorLevelElement child);
98 
100  std::map<DetectorLevelElement, std::pair<DetectorLevelElement, TMatrixD>> m_lookup;
102  std::map<DetectorLevelElement, std::vector<DetectorLevelElement>> m_hierarchy;
103 
106  std::set<unsigned short> m_usedUniqueIDs {};
107  };
108 
111  public:
114 
116  std::vector<int> getElementLabels(DetectorLevelElement element) final;
117 
119  template<class LowestLevelDBObject>
120  GlobalDerivativeSet getGlobalDerivatives(unsigned short sensor, const genfit::StateOnPlane* sop, B2Vector3D bField)
121  {
122  if (bField.Mag() < 1.e-10)
123  return std::make_pair(std::vector<int>(), TMatrixD());
124 
125  DetectorLevelElement elementUID = std::make_pair(LowestLevelDBObject::getGlobalUniqueID(), sensor);
126 
127  GlobalDerivativeSet sensorDerivs = std::make_pair(getElementLabels(elementUID), getLorentzShiftDerivatives(sop, bField));
129  return sensorDerivs;
130  }
131 
133  TMatrixD getLorentzShiftDerivatives(const genfit::StateOnPlane* sop, B2Vector3D bField);
134 
136  template<class ChildDBObjectType, class MotherDBObjectType>
137  void insertRelation(unsigned short child, unsigned short mother)
138  {
139  insert<ChildDBObjectType, MotherDBObjectType>(child, mother, TMatrixD(1, 1).UnitMatrix());
140  }
141 
142  private:
143  };
144 
146  class RigidBodyHierarchy : public GlobalDerivativesHierarchy {
147 
148  public:
149 
152 
153  // Destructor
155 
157  std::vector<int> getElementLabels(DetectorLevelElement element) override;
158 
160  template<class LowestLevelDBObject>
161  GlobalDerivativeSet getGlobalDerivatives(unsigned short sensor, const genfit::StateOnPlane* sop)
162  {
163  DetectorLevelElement elementUID = std::make_pair(LowestLevelDBObject::getGlobalUniqueID(), sensor);
164 
165  GlobalDerivativeSet sensorDerivs = std::make_pair(getElementLabels(elementUID), getRigidBodyDerivatives(sop));
167  return sensorDerivs;
168  }
169 
171  template<class ChildDBObjectType, class MotherDBObjectType>
172  void insertG4Transform(unsigned short child, unsigned short mother, G4Transform3D childToMother)
173  {
174  insert<ChildDBObjectType, MotherDBObjectType>(child, mother, convertG4ToRigidBodyTransformation(childToMother));
175  }
176 
178  template<class ChildDBObjectType, class MotherDBObjectType>
179  void insertTGeoTransform(unsigned short child, unsigned short mother, TGeoHMatrix childToMother)
180  {
181  insert<ChildDBObjectType, MotherDBObjectType>(child, mother, convertTGeoToRigidBodyTransformation(childToMother));
182  }
183 
185  TMatrixD getRigidBodyDerivatives(const genfit::StateOnPlane* sop);
186 
188  TMatrixD convertG4ToRigidBodyTransformation(G4Transform3D g4transform);
189 
191  TMatrixD convertTGeoToRigidBodyTransformation(TGeoHMatrix tgeo);
192  private:
193  };
194  }
196 }
Belle2::alignment::LorentShiftHierarchy::getLorentzShiftDerivatives
TMatrixD getLorentzShiftDerivatives(const genfit::StateOnPlane *sop, B2Vector3D bField)
Derivatives for Lorentz shift in sensor plane.
Definition: Hierarchy.cc:158
Belle2::alignment::RigidBodyHierarchy::RigidBodyHierarchy
RigidBodyHierarchy()
Constructor.
Definition: Hierarchy.h:159
Belle2::alignment::GlobalDerivativesHierarchy::getChildToMotherTransform
std::pair< DetectorLevelElement, TMatrixD > getChildToMotherTransform(DetectorLevelElement child)
Find the transformation in the lookup.
Definition: Hierarchy.cc:135
Belle2::alignment::GlobalDerivativesHierarchy::m_lookup
std::map< DetectorLevelElement, std::pair< DetectorLevelElement, TMatrixD > > m_lookup
Map with all the parameter data (child -> (mother, transform_child2mother))
Definition: Hierarchy.h:108
Belle2::alignment::GlobalDerivativesHierarchy::insert
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:70
Belle2::alignment::GlobalDerivativesHierarchy
Class for alignment/calibration parameter hierarchy & constraints.
Definition: Hierarchy.h:51
Belle2::alignment::GlobalDerivativesHierarchy::getUsedDBObjUniqueIDs
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:101
Belle2::alignment::RigidBodyHierarchy::convertG4ToRigidBodyTransformation
TMatrixD convertG4ToRigidBodyTransformation(G4Transform3D g4transform)
Conversion from G4Transform3D to 6D rigid body transformation parametrization.
Definition: Hierarchy.cc:245
Belle2::alignment::GlobalDerivativesHierarchy::m_hierarchy
std::map< DetectorLevelElement, std::vector< DetectorLevelElement > > m_hierarchy
Map of hierarchy relations mother-> child.
Definition: Hierarchy.h:110
Belle2::alignment::GlobalDerivativesHierarchy::getElementLabels
virtual std::vector< int > getElementLabels(DetectorLevelElement element)=0
The only function to implement: what are the global labels for the element?
genfit::StateOnPlane
A state with arbitrary dimension defined in a DetPlane.
Definition: StateOnPlane.h:47
Belle2::alignment::LorentShiftHierarchy::getGlobalDerivatives
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:128
Belle2::alignment::RigidBodyHierarchy::getElementLabels
std::vector< int > getElementLabels(DetectorLevelElement element) override
Rigid body labels.
Definition: Hierarchy.cc:182
Belle2::B2Vector3< double >
Belle2::alignment::RigidBodyHierarchy::convertTGeoToRigidBodyTransformation
TMatrixD convertTGeoToRigidBodyTransformation(TGeoHMatrix tgeo)
Conversion from G4Transform3D to 6D rigid body transformation parametrization.
Definition: Hierarchy.cc:275
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2::alignment::RigidBodyHierarchy::getGlobalDerivatives
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:169
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::alignment::GlobalDerivativesHierarchy::mergeGlobals
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:105
Belle2::alignment::LorentShiftHierarchy::insertRelation
void insertRelation(unsigned short child, unsigned short mother)
Template function to insert hierarchy relation bewteen two DB objects and their elements.
Definition: Hierarchy.h:145
Belle2::alignment::GlobalDerivativesHierarchy::m_usedUniqueIDs
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:114
Belle2::alignment::GlobalDerivativesHierarchy::buildConstraints
void buildConstraints(Constraints &constraints)
Adds constraints from current hierarchy to a constraints vector.
Definition: Hierarchy.cc:34
Belle2::alignment::GlobalDerivativesHierarchy::buildGlobalDerivativesHierarchy
GlobalDerivativeSet buildGlobalDerivativesHierarchy(TMatrixD matrixChain, DetectorLevelElement child)
Recursive function which adds labels and derivatives until top element in hierarchy is found.
Definition: Hierarchy.cc:86
Belle2::alignment::LorentShiftHierarchy::getElementLabels
std::vector< int > getElementLabels(DetectorLevelElement element) final
Label for lorentz shift parameter.
Definition: Hierarchy.cc:147
Belle2::alignment::RigidBodyHierarchy::insertG4Transform
void insertG4Transform(unsigned short child, unsigned short mother, G4Transform3D childToMother)
Insert hierarchy relation.
Definition: Hierarchy.h:180
Belle2::alignment::LorentShiftHierarchy::LorentShiftHierarchy
LorentShiftHierarchy()
Constructor.
Definition: Hierarchy.h:121
Belle2::alignment::RigidBodyHierarchy
6D Hierarchy of rigid bodies
Definition: Hierarchy.h:154
Belle2::alignment::GlobalDerivativesHierarchy::GlobalDerivativesHierarchy
GlobalDerivativesHierarchy()
Constructor.
Definition: Hierarchy.h:54
Belle2::alignment::LorentShiftHierarchy
1D Hierarchy for Lorentz shift correction
Definition: Hierarchy.h:118
Belle2::alignment::RigidBodyHierarchy::insertTGeoTransform
void insertTGeoTransform(unsigned short child, unsigned short mother, TGeoHMatrix childToMother)
Insert hierarchy relation.
Definition: Hierarchy.h:187
Belle2::alignment::GlobalDerivativesHierarchy::~GlobalDerivativesHierarchy
virtual ~GlobalDerivativesHierarchy()
Destructor (virtual)
Definition: Hierarchy.h:56
Belle2::alignment::RigidBodyHierarchy::getRigidBodyDerivatives
TMatrixD getRigidBodyDerivatives(const genfit::StateOnPlane *sop)
2x6 matrix of rigid body derivatives
Definition: Hierarchy.cc:213
Belle2::B2Vector3::Mag
DataType Mag() const
The magnitude (rho in spherical coordinate system).
Definition: B2Vector3.h:158
Belle2::alignment::GlobalDerivativesHierarchy::printHierarchy
void printHierarchy()
print the lookup map
Definition: Hierarchy.cc:126