ATLAS Offline Software
Loading...
Searching...
No Matches
AlignModule.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6
10
14#include "TrkSurfaces/Surface.h"
15
16#define _USE_MATH_DEFINES
17#include <cmath>
18
19
20namespace Trk {
21
22 //________________________________________________________________________
23 AlignModule::AlignModule(const AlgTool* algtool,
24 const Amg::Transform3D& transform,
25 const std::string& name)
26 : AthMessaging("AlignModule")
31 , m_chi2VAlignParam(nullptr)
32 , m_chi2VAlignParamX(nullptr)
33 , m_chi2VAlignParamMeasType (new double**[TrackState::NumberOfMeasurementTypes])
34 , m_name(name)
35 , m_ntracks(0)
36 , m_nhits(0)
37 , m_trackchi2(0.)
38 , m_nDoF(0)
39 {
40 for (int i=0;i<(int)TrackState::NumberOfMeasurementTypes;i++)
42
43 const AthAlgTool* athAlgTool=dynamic_cast<const AthAlgTool*>(algtool);
44 if (athAlgTool)
45 this->setLevel(athAlgTool->msg().level());
46
48 }
49
50 //________________________________________________________________________
55
56 //________________________________________________________________________
57 void decomposeTransform(const Amg::Transform3D& transform, double* values)
58 {
59 Amg::Vector3D translation = transform.translation();
60 values[0]=translation.x();
61 values[1]=translation.y();
62 values[2]=translation.z();
63
64 Amg::RotationMatrix3D rotation = transform.rotation();
65
66 double sinbeta = rotation(2,0);
67 if(fabs(sinbeta) < 0.99999 ){
68
69// double invcosbeta = 1./std::sqrt(1.-sinbeta*sinbeta);
70// double sinalpha = rotation(2,1)*invcosbeta;
71// double singamma = rotation(1,0)*invcosbeta;
72// std::cout << "Alpha " << -std::asin(sinalpha) << " " << -std::atan2( -rotation(2,1), rotation(2,2) )<< std::endl;
73// std::cout << "Beta " << -std::asin(sinbeta) << " " << std::acos(1./invcosbeta) << std::endl;
74// std::cout << "Gamma " << -std::asin(singamma) << " " << -std::atan2( -rotation(1,0), rotation(0,0 )) << std::endl;
75
76 values[3]=-std::atan2( -rotation(2,1), rotation(2,2) );
77 values[4]=-std::asin(sinbeta);
78 values[5]=-std::atan2( -rotation(1,0), rotation(0,0) ) ;
79 if (values[3] == 0) values[3] = 0; // convert -0 to 0
80 if (values[5] == 0) values[5] = 0; // convert -0 to 0
81
82 } else {
83 double alphaPlusGamma = std::asin( rotation(0,1) );
84 values[3] = alphaPlusGamma;
85 values[5] = 0;
86 if( sinbeta > 0 )
87 values[4] = M_PI * 0.5;
88 else
89 values[4] = -M_PI * 0.5;
90 }
91
92 }
93
94 //________________________________________________________________________
95 Amg::Vector3D AlignModule::centerOfGravity(const std::vector<DetElementCollection*>& detelementsVec)
96 {
97 // returns the center of gravity in the global frame
98 // calculate the 'center' of this module
99 double sumx(0),sumy(0),sumz(0);
100 size_t n(0);
101 for (auto *detelements : detelementsVec) {
102 if (!detelements)
103 continue;
104
105 for(const auto *detelement : *detelements) {
106 sumx += detelement->surface().center().x();
107 sumy += detelement->surface().center().y();
108 sumz += detelement->surface().center().z();
109 }
110 n += detelements->size();
111 }
112
113 if (n!=0) return Amg::Vector3D( sumx/n, sumy/n, sumz/n);
114 else return Amg::Vector3D(0.0,0.0,0.0);
115
116 }
117
118 //________________________________________________________________________
123
124
125 //________________________________________________________________________
127 const TrkDetElementBase* det,
128 const Amg::Transform3D& transform,
129 const Identifier id)
130 {
131 ATH_MSG_DEBUG("adding detElement "<<det->identify()<<", detType "<<detType);
132 if (!m_detelements[detType])
134
135 m_detelements[detType]->push_back(det);
136
137 if (id.is_valid()) {
138 if(!m_detIdentifiers[detType])
140 m_detIdentifiers[detType]->push_back(id);
141 }
142
144 m_alignModuleToDetElementTransforms[detType]=new std::vector<Amg::Transform3D>;
145
146 m_alignModuleToDetElementTransforms[detType]->push_back(transform);
147 }
148
149 //________________________________________________________________________
150 const Amg::Transform3D* AlignModule
151 ::alignModuleToDetElementTransform(AlignModule::DetectorType detType,
152 const TrkDetElementBase* det,
153 const Identifier id) const
154 {
155 std::vector<Amg::Transform3D>* alignModToDetElemTransform=m_alignModuleToDetElementTransforms[detType];
156
157 if(m_detIdentifiers[detType] && !m_detIdentifiers[detType]->empty()) {
158 if(id.is_valid()) {
159 IdentifierCollection * ids = m_detIdentifiers[detType];
160 for (int i=0;i<(int)alignModToDetElemTransform->size();i++)
161 if ((*ids)[i]==id)
162 return &(*alignModToDetElemTransform)[i];
163 }
164 return nullptr;
165 }
166
167 DetElementCollection* detElements=m_detelements[detType];
168 for (int i=0;i<(int)alignModToDetElemTransform->size();i++)
169 if ((*detElements)[i]==det)
170 return &(*alignModToDetElemTransform)[i];
171
172
173 return nullptr;
174 }
175
176 //________________________________________________________________________
178 {
179 std::string typeStr;
180 switch (detType) {
182 typeStr="none"; break;
184 typeStr="Pixel"; break;
185 case AlignModule::SCT:
186 typeStr="SCT"; break;
187 case AlignModule::TRT:
188 typeStr="TRT"; break;
189 case AlignModule::MDT:
190 typeStr="MDT"; break;
191 case AlignModule::CSC:
192 typeStr="CSC"; break;
193 case AlignModule::RPC:
194 typeStr="RPC"; break;
195 case AlignModule::TGC:
196 typeStr="TGC"; break;
198 typeStr="NDetectorTypes"; break;
199 }
200 return typeStr;
201 }
202
203 //________________________________________________________________________
204 MsgStream& operator << (MsgStream& sl, const AlignModule& alignModule)
205 {
206 sl << "AlignModule \'"<<alignModule.name()<<"\' ID: "<<alignModule.identify()
207 <<" IDHash: "<<alignModule.identifyHash()<< endmsg;
208 return sl;
209 }
210
211
212
215 {
216 // returns the center of gravity in the global frame
217 // calculate the 'center' of this module
218 double sumx(0),sumy(0),sumz(0);
219 double sumRx(0),sumRy(0),sumRz(0);
220 size_t n(0);
221 for (auto *detelements : m_detelements) {
222 if (!detelements)
223 continue;
224
225 double trans[6];
226 for(const auto *detelement : *detelements) {
227 decomposeTransform( detelement->transform(), trans);
228 sumx += trans[0];
229 sumy += trans[1];
230 sumz += trans[2];
231 sumRx += trans[3];
232 sumRy += trans[4];
233 sumRz += trans[5];
234 ATH_MSG_DEBUG(n << " " << trans[0] << " "<< trans[1] << " " << trans[2]
235 << " "<< trans[3] << " " << trans[4] << " "<< trans[5]);
236 ++n;
237
238// Amg::Translation3D surfaceCentre( trans[0],trans[1],trans[2] );
239// Amg::Transform3D newtrans = surfaceCentre * Amg::RotationMatrix3D::Identity();
240// newtrans *= Amg::AngleAxis3D(trans[5], Amg::Vector3D(0.,0.,1.));
241// newtrans *= Amg::AngleAxis3D(trans[4], Amg::Vector3D(0.,1.,0.));
242// newtrans *= Amg::AngleAxis3D(trans[3], Amg::Vector3D(1.,0.,0.));
243//
244// decomposeTransform( newtrans, trans);
245//
246// std::cout << " Check " << trans[0] << " "<< trans[1] << " " << trans[2] << " "<< trans[3] << " " << trans[4] << " "<< trans[5] << " "<< std::endl;
247//
248//
249// Amg::Translation3D surfaceCentre2( trans[0],trans[1],trans[2] );
250// Amg::Transform3D newtrans2 = surfaceCentre * Amg::RotationMatrix3D::Identity();
251// newtrans2 *= Amg::AngleAxis3D(trans[5], Amg::Vector3D(0.,0.,1.));
252// newtrans2 *= Amg::AngleAxis3D(trans[4], Amg::Vector3D(0.,1.,0.));
253// newtrans2 *= Amg::AngleAxis3D(trans[3], Amg::Vector3D(1.,0.,0.));
254//
255// decomposeTransform( newtrans2, trans);
256//
257// std::cout << " Check2 " << trans[0] << " "<< trans[1] << " " << trans[2] << " "<< trans[3] << " " << trans[4] << " "<< trans[5] << " "<< std::endl;
258
259
260 }
261 }
262
263 if (n==0)
264 return Amg::Transform3D::Identity();
265
266 double oneOnN = 1./(double)n;
267
268 ATH_MSG_DEBUG(" SUM " << oneOnN << " " << sumx*oneOnN << " "<< sumy*oneOnN
269 << " " << sumz*oneOnN << " "<< sumRx*oneOnN << " " << sumRy*oneOnN << " "<< sumRz*oneOnN);
270
271
272 Amg::Translation3D surfaceCentre( sumx*oneOnN, sumy*oneOnN, sumz*oneOnN );
273 Amg::Transform3D newtrans = surfaceCentre * Amg::RotationMatrix3D::Identity();
274 newtrans *= Amg::AngleAxis3D(sumRz*oneOnN, Amg::Vector3D(0.,0.,1.));
275 newtrans *= Amg::AngleAxis3D(sumRy*oneOnN, Amg::Vector3D(0.,1.,0.));
276 newtrans *= Amg::AngleAxis3D(sumRx*oneOnN, Amg::Vector3D(1.,0.,0.));
277
278
279 Amg::Translation3D newtranslation(0,0,0);
280 Amg::Transform3D test = newtranslation * Amg::RotationMatrix3D::Identity();
281 test *= Amg::AngleAxis3D(M_PI * 0.25, Amg::Vector3D(0.,0.,1.));
282
283 return newtrans;
284 }
285
288 {
289
290
291 for (int i=0;i<(int)m_detelements.size();i++) {
292 DetElementCollection* detelements=m_detelements[i];
293 if (!detelements)
294 continue;
295
296 int j(0);
297 for(DetElementCollection::const_iterator it = detelements->begin() ;
298 it != detelements->end(); ++it, ++j ) {
299 Amg::Transform3D localToGlobal = (*it)->transform();
300 (*m_alignModuleToDetElementTransforms[i])[j] = (localToGlobal * m_globalToAlignFrameTransform).inverse();
301
302 double trans[6];
304 ATH_MSG_DEBUG(j << " " << trans[0] << " "<< trans[1] << " " << trans[2]
305 << " "<< trans[3] << " " << trans[4] << " "<< trans[5]);
306 }
307 }
308 }
309
310} // end namespace
#define M_PI
#define endmsg
#define ATH_MSG_DEBUG(x)
Eigen::Affine3d Transform3D
static const Attributes_t empty
MsgStream & msg() const
void setLevel(MSG::Level lvl)
Change the current logging level.
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
AlignModule(const AlgTool *algtool, const Amg::Transform3D &globalToAlignXform=Amg::Transform3D::Identity(), const std::string &name="")
constructor creates MsgStream with output level of parent tool and AlignModule for name.
virtual ~AlignModule()
double ** m_chi2VAlignParamX
std::vector< const TrkDetElementBase * > DetElementCollection
typedefs to contain detector element pointers and transforms
Definition AlignModule.h:60
Identifier identify() const
Definition AlignModule.h:97
void setGlobalFrameToAlignFrameTransform(const Amg::Transform3D &t)
set global to alignment frame transforms
const std::string & name() const
Definition AlignModule.h:89
double ** m_chi2VAlignParam
Amg::Transform3D m_globalToAlignFrameTransform
std::string m_name
void resetAlignModuleToDetElementTransforms()
Reset align module to detector element transforms based on the AlignModuleToGlobal transform and the ...
std::vector< IdentifierCollection * > m_detIdentifiers
Amg::Transform3D calculateAlignModuleToGlobal() const
Calculates Align to Global transform based on the TrkDetElementBase in the alignmodule.
void addDetElement(AlignModule::DetectorType detType, const TrkDetElementBase *det, const Amg::Transform3D &transform, Identifier id=Identifier())
used to add a detector element to the align module with a align frame to detector element local frame...
std::vector< Identifier > IdentifierCollection
Definition AlignModule.h:61
IdentifierHash identifyHash() const
Set and return index of module, used by alignment classes to keep track of order of align module.
Definition AlignModule.h:92
std::vector< std::vector< Amg::Transform3D > * > m_alignModuleToDetElementTransforms
std::vector< DetElementCollection * > m_detelements
Amg::Vector3D centerOfGravity() const
double *** m_chi2VAlignParamMeasType
AlignModule()=delete
This is the base class for all tracking detector elements with read-out relevant information.
Definition of ATLAS Math & Geometry primitives (Amg)
Eigen::AngleAxisd AngleAxis3D
Eigen::Matrix< double, 3, 3 > RotationMatrix3D
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D
Eigen::Translation< double, 3 > Translation3D
Ensure that the ATLAS eigen extensions are properly loaded.
std::string detTypeStr(AlignModule::DetectorType detType)
returns the detector type
MsgStream & operator<<(MsgStream &sl, const AlignModule &alignModule)
overload of << operator for MsgStream for debug output
void decomposeTransform(const Amg::Transform3D &transform, double *values)
STL namespace.