Belle II Software development
LorentShiftHierarchy Class Reference

1D Hierarchy for Lorentz shift correction More...

#include <Hierarchy.h>

Inheritance diagram for LorentShiftHierarchy:
GlobalDerivativesHierarchy

Public Member Functions

 LorentShiftHierarchy ()
 Constructor.
 
std::vector< int > getElementLabels (DetectorLevelElement element) final
 Label for lorentz shift parameter.
 
template<class LowestLevelDBObject >
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)
 
TMatrixD getLorentzShiftDerivatives (const genfit::StateOnPlane *sop, B2Vector3D bField)
 Derivatives for Lorentz shift in sensor plane.
 
template<class ChildDBObjectType , class MotherDBObjectType >
void insertRelation (unsigned short child, unsigned short mother)
 Template function to insert hierarchy relation bewteen two DB objects and their elements.
 
void buildConstraints (Constraints &constraints)
 Adds constraints from current hierarchy to a constraints vector.
 
GlobalDerivativeSet buildGlobalDerivativesHierarchy (TMatrixD matrixChain, DetectorLevelElement child)
 Recursive function which adds labels and derivatives until top element in hierarchy is found.
 
template<class ChildDBObjectType , class MotherDBObjectType >
void insert (unsigned short child, unsigned short mother, TMatrixD childToMotherParamTransform)
 Template function to add relation between two elements (possibly in different objects with constants) First object is the child object, second its hierarchy parent.
 
void printHierarchy ()
 print the lookup map
 
const std::set< unsigned short > & getUsedDBObjUniqueIDs ()
 Get the global unique ids of DB objects used to construct hierarchy Usefull to update hierarchy only when those changed.
 

Static Public Member Functions

static void mergeGlobals (GlobalDerivativeSet &main, GlobalDerivativeSet additional)
 Merge additional set into main set of global labels and derivatives TODO: move to some utilities.
 

Private Member Functions

std::pair< DetectorLevelElement, TMatrixD > getChildToMotherTransform (DetectorLevelElement child)
 Find the transformation in the lookup.
 

Private Attributes

std::map< DetectorLevelElement, std::pair< DetectorLevelElement, TMatrixD > > m_lookup
 Map with all the parameter data (child -> (mother, transform_child2mother))
 
std::map< DetectorLevelElement, std::vector< DetectorLevelElement > > m_hierarchy
 Map of hierarchy relations mother-> child.
 
std::set< unsigned short > m_usedUniqueIDs {}
 The set of unique id of each DB object used for construction For more efficient updates of hierarchy only when used objects change.
 

Detailed Description

1D Hierarchy for Lorentz shift correction

Definition at line 108 of file Hierarchy.h.

Constructor & Destructor Documentation

◆ LorentShiftHierarchy()

Constructor.

Definition at line 111 of file Hierarchy.h.

Member Function Documentation

◆ buildConstraints()

void buildConstraints ( Constraints &  constraints)
inherited

Adds constraints from current hierarchy to a constraints vector.

Definition at line 24 of file Hierarchy.cc.

25 {
26 for (auto& parent_childs : m_hierarchy) {
27 auto parent = parent_childs.first;
28 auto childs = parent_childs.second;
29
30 // We need to check if such constraint entry already exists.
31 // For timedep parameters, some to all labels (and coefficients optionally, too) can change and
32 // in such case, we need a new constraint entry.
33 // Note the checksum for a constraint does not depend on the parent element. If parent object changes,
34 // the relative transofrmation to its children are unchanged. While if (some of) the childs change,
35 // for each time interval they are constant, there must a new constraint entry as this is an independent
36 // linear combination of parameters (because there are other parameters).
37 // Continued bellow...
38 boost::crc_32_type crc32;
39
40 auto parentLabels = getElementLabels(parent);
41 for (unsigned int iCon = 0; iCon < parentLabels.size(); iCon++) {
42 auto constraint = Constraint();
43 //auto & constraint = constraints[parentLabels[iCon]];
44
45 // No constraints if parent is global reference frame
46 // All subdetectors are actually nominally placed at constant position and rotation
47 // and rotating the detectors could only happen due cohherent movements of sub-structures (CDC layers, VXD half-shells)
48 if (parentLabels[iCon] == 0) continue;
49
50 for (unsigned int j = 0; j < childs.size(); j++) {
51 auto child = childs[j];
52 auto childLabels = getElementLabels(child);
53
54 for (unsigned int iPar = 0; iPar < childLabels.size(); iPar++) {
55 double coefficient = getChildToMotherTransform(child).second(iCon, iPar);
56 if (fabs(coefficient) > 1.0e-14) {
57 auto childLabel = childLabels[iPar];
58 constraint.push_back(std::make_pair(childLabel, coefficient));
59 // On the other hand, if the labels do not change, the checksum should not change.
60 // In future I should add second checksum and warn user if this happens. It means user has ignored the fact,
61 // that the objects already change in the input global tag. The point here is, it still might be reasonable
62 // to use the constraint coefficients we already have, as the alignment changes are usually small and do not change
63 // the constraint coefficients in first order for most use-cases. The warning should be enough -> TODO
64 crc32.process_bytes(&childLabel, sizeof(childLabel));
65 }
66 }
67 }
68
69 //constraints.insert(std::make_pair(parentLabels[iCon], constraint));
70 constraints.insert(std::make_pair(crc32.checksum(), constraint));
71
72 }
73 }
74 }
std::map< DetectorLevelElement, std::vector< DetectorLevelElement > > m_hierarchy
Map of hierarchy relations mother-> child.
Definition: Hierarchy.h:100
virtual std::vector< int > getElementLabels(DetectorLevelElement element)=0
The only function to implement: what are the global labels for the element?
std::pair< DetectorLevelElement, TMatrixD > getChildToMotherTransform(DetectorLevelElement child)
Find the transformation in the lookup.
Definition: Hierarchy.cc:125

◆ buildGlobalDerivativesHierarchy()

Belle2::alignment::GlobalDerivativeSet buildGlobalDerivativesHierarchy ( TMatrixD  matrixChain,
DetectorLevelElement  child 
)
inherited

Recursive function which adds labels and derivatives until top element in hierarchy is found.

Definition at line 76 of file Hierarchy.cc.

78 {
79 auto loc2glo = getChildToMotherTransform(child);
80
81 if (loc2glo.first == std::make_pair((unsigned short)0, (unsigned short)0))
82 return std::make_pair(std::vector<int>(), TMatrixD());
83
84
85 TMatrixD glo2loc(loc2glo.second.Invert());
86 TMatrixD drdparent = matrixChain * glo2loc;
87
88
89 GlobalDerivativeSet retVal = std::make_pair(getElementLabels(loc2glo.first), drdparent);
90 mergeGlobals(retVal, buildGlobalDerivativesHierarchy(drdparent, loc2glo.first));
91
92 return retVal;
93 }
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
GlobalDerivativeSet buildGlobalDerivativesHierarchy(TMatrixD matrixChain, DetectorLevelElement child)
Recursive function which adds labels and derivatives until top element in hierarchy is found.
Definition: Hierarchy.cc:76

◆ getChildToMotherTransform()

std::pair< Belle2::alignment::DetectorLevelElement, TMatrixD > getChildToMotherTransform ( DetectorLevelElement  child)
privateinherited

Find the transformation in the lookup.

Definition at line 125 of file Hierarchy.cc.

127 {
128 auto entry = m_lookup.find(child);
129 if (entry == m_lookup.end())
130 return std::make_pair(std::make_pair(0, 0), TMatrixD());
131
132 return entry->second;
133 }
std::map< DetectorLevelElement, std::pair< DetectorLevelElement, TMatrixD > > m_lookup
Map with all the parameter data (child -> (mother, transform_child2mother))
Definition: Hierarchy.h:98

◆ getElementLabels()

std::vector< int > getElementLabels ( DetectorLevelElement  element)
finalvirtual

Label for lorentz shift parameter.

Implements GlobalDerivativesHierarchy.

Definition at line 137 of file Hierarchy.cc.

138 {
139 std::vector<int> labels;
140 GlobalLabel label;
141 label.construct(element.first, element.second, 0);
142 // TODO: constants instead of numbers
143 labels.push_back(label.setParameterId(20));
144
145 return labels;
146 }

◆ getGlobalDerivatives()

GlobalDerivativeSet getGlobalDerivatives ( unsigned short  sensor,
const genfit::StateOnPlane *  sop,
B2Vector3D  bField 
)
inline

Template function to get globals for given db object and its element (and the rest of hierarchy)

Definition at line 118 of file Hierarchy.h.

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 }
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

◆ getLorentzShiftDerivatives()

TMatrixD getLorentzShiftDerivatives ( const genfit::StateOnPlane *  sop,
B2Vector3D  bField 
)

Derivatives for Lorentz shift in sensor plane.

Definition at line 148 of file Hierarchy.cc.

149 {
150 // values for global derivatives
151 //TMatrixD derGlobal(2, 6);
152 TMatrixD derGlobal(2, 1);
153 derGlobal.Zero();
154
155 // electrons in device go in local w-direction to P-side
156 B2Vector3D v = sop->getPlane()->getNormal();
157 // Lorentz force (without E-field) direction
158 B2Vector3D F_dir = v.Cross(bField);
159 // ... projected to sensor coordinates:
160 genfit::StateOnPlane localForce = *sop;
161 localForce.setPosMom(sop->getPos(), F_dir);
162 B2Vector3D lorentzLocal(localForce.getState()[3], localForce.getState()[4], 0); // or 0,1?
163 // Lorentz shift = parameter(layer) * B_local
164 derGlobal(0, 0) = lorentzLocal(0);
165 derGlobal(1, 0) = lorentzLocal(1);
166
167 return derGlobal;
168 }
B2Vector3< DataType > Cross(const B2Vector3< DataType > &p) const
Cross product.
Definition: B2Vector3.h:296
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition: B2Vector3.h:516

◆ getUsedDBObjUniqueIDs()

const std::set< unsigned short > & getUsedDBObjUniqueIDs ( )
inlineinherited

Get the global unique ids of DB objects used to construct hierarchy Usefull to update hierarchy only when those changed.

Definition at line 91 of file Hierarchy.h.

91{return 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:104

◆ insert()

void insert ( unsigned short  child,
unsigned short  mother,
TMatrixD  childToMotherParamTransform 
)
inlineinherited

Template function to add relation between two elements (possibly in different objects with constants) First object is the child object, second its hierarchy parent.

Parameters
childis the lement numeric id in child obj
motheris the parent object
childToMotherParamTransformis the transformation matrix (placement from geometry of the detector)

Definition at line 60 of file Hierarchy.h.

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 }

◆ insertRelation()

void insertRelation ( unsigned short  child,
unsigned short  mother 
)
inline

Template function to insert hierarchy relation bewteen two DB objects and their elements.

Definition at line 135 of file Hierarchy.h.

136 {
137 insert<ChildDBObjectType, MotherDBObjectType>(child, mother, TMatrixD(1, 1).UnitMatrix());
138 }

◆ mergeGlobals()

void mergeGlobals ( GlobalDerivativeSet &  main,
GlobalDerivativeSet  additional 
)
staticinherited

Merge additional set into main set of global labels and derivatives TODO: move to some utilities.

Definition at line 95 of file Hierarchy.cc.

97 {
98 if (additional.first.empty())
99 return;
100
101 // Create composed matrix of derivatives
102 //TODO: check main and additional matrix has the same number of rows
103 TMatrixD allDerivatives(main.second.GetNrows(), main.second.GetNcols() + additional.second.GetNcols());
104 allDerivatives.Zero();
105 allDerivatives.SetSub(0, 0, main.second);
106 allDerivatives.SetSub(0, main.second.GetNcols(), additional.second);
107
108 // Merge labels
109 main.first.insert(main.first.end(), additional.first.begin(), additional.first.end());
110 // Update matrix
111 main.second.ResizeTo(allDerivatives);
112 main.second = allDerivatives;
113
114 }

◆ printHierarchy()

void printHierarchy ( )
inherited

print the lookup map

Definition at line 116 of file Hierarchy.cc.

117 {
118 for (auto& entry : m_lookup) {
119 std::cout << "Child : " << entry.first.second << std::endl;
120 std::cout << "Mother :" << entry.second.first.second << std::endl;
121 entry.second.second.Print();
122 }
123 }

Member Data Documentation

◆ m_hierarchy

std::map<DetectorLevelElement, std::vector<DetectorLevelElement> > m_hierarchy
privateinherited

Map of hierarchy relations mother-> child.

Definition at line 100 of file Hierarchy.h.

◆ m_lookup

std::map<DetectorLevelElement, std::pair<DetectorLevelElement, TMatrixD> > m_lookup
privateinherited

Map with all the parameter data (child -> (mother, transform_child2mother))

Definition at line 98 of file Hierarchy.h.

◆ m_usedUniqueIDs

std::set<unsigned short> m_usedUniqueIDs {}
privateinherited

The set of unique id of each DB object used for construction For more efficient updates of hierarchy only when used objects change.

Definition at line 104 of file Hierarchy.h.


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