ATLAS Offline Software
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 
20 namespace Trk {
21 
22  //________________________________________________________________________
23  AlignModule::AlignModule(const AlgTool* algtool,
25  const std::string& name)
26  : AthMessaging("AlignModule")
27  , m_detelements(AlignModule::NDetectorTypes,(DetElementCollection*)nullptr)
28  , m_detIdentifiers(AlignModule::NDetectorTypes,(IdentifierCollection*)nullptr)
29  , m_alignModuleToDetElementTransforms(AlignModule::NDetectorTypes, (std::vector<Amg::Transform3D>*)nullptr)
30  , m_nChamberShifts(8)
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  {
42 
43  const AthAlgTool* athAlgTool=dynamic_cast<const AthAlgTool*>(algtool);
44  if (athAlgTool)
45  this->setLevel(athAlgTool->msg().level());
46 
48  }
49 
50  //________________________________________________________________________
52  {
53  delete [] m_chi2VAlignParamMeasType;
54  }
55 
56  //________________________________________________________________________
58  {
59  Amg::Vector3D translation = transform.translation();
60  values[0]=translation.x();
61  values[1]=translation.y();
62  values[2]=translation.z();
63 
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  //________________________________________________________________________
120  {
122  }
123 
124 
125  //________________________________________________________________________
127  const TrkDetElementBase* det,
129  const Identifier id)
130  {
131  ATH_MSG_DEBUG("adding detElement "<<det->identify()<<", detType "<<detType);
132  if (!m_detelements[detType])
133  m_detelements[detType]=new DetElementCollection;
134 
135  m_detelements[detType]->push_back(det);
136 
137  if (id.is_valid()) {
138  if(!m_detIdentifiers[detType])
139  m_detIdentifiers[detType] = new IdentifierCollection;
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  //________________________________________________________________________
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  //________________________________________________________________________
177  std::string detTypeStr(AlignModule::DetectorType detType)
178  {
179  std::string typeStr;
180  switch (detType) {
182  typeStr="none"; break;
183  case AlignModule::Pixel:
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
Trk::AlignModule::TGC
@ TGC
Definition: AlignModule.h:57
TrkDetElementBase.h
Trk::AlignModule::TRT
@ TRT
Definition: AlignModule.h:57
TrackParameters.h
MeasurementBase.h
Surface.h
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
TRTCalib_Extractor.det
det
Definition: TRTCalib_Extractor.py:36
Trk::AlignModule::m_detelements
std::vector< DetElementCollection * > m_detelements
Definition: AlignModule.h:214
Trk::AlignModule::resetAlignModuleToDetElementTransforms
void resetAlignModuleToDetElementTransforms()
Reset align module to detector element transforms based on the AlignModuleToGlobal transform and the ...
Definition: AlignModule.cxx:287
Trk::AlignModule::addDetElement
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...
Definition: AlignModule.cxx:126
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
skel.it
it
Definition: skel.GENtoEVGEN.py:396
M_PI
#define M_PI
Definition: ActiveFraction.h:11
Trk::AlignModule
Definition: AlignModule.h:45
Trk::AlignModule::AlignModule
AlignModule()=delete
Trk::AlignModule::centerOfGravity
Amg::Vector3D centerOfGravity() const
Definition: AlignModule.cxx:119
Trk::TrkDetElementBase
Definition: TrkDetElementBase.h:52
Trk::decomposeTransform
void decomposeTransform(const Amg::Transform3D &transform, double *values)
Definition: AlignModule.cxx:57
Trk::AlignModule::Pixel
@ Pixel
Definition: AlignModule.h:57
TrigInDetValidation_Base.test
test
Definition: TrigInDetValidation_Base.py:147
Trk::AlignModule::NDetectorTypes
@ NDetectorTypes
Definition: AlignModule.h:57
empty
bool empty(TH1 *h)
Definition: computils.cxx:295
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:805
AlignTSOS.h
AthMessaging::setLevel
void setLevel(MSG::Level lvl)
Change the current logging level.
Definition: AthMessaging.cxx:28
Trk::AlignModule::setGlobalFrameToAlignFrameTransform
void setGlobalFrameToAlignFrameTransform(const Amg::Transform3D &t)
set global to alignment frame transforms
Definition: AlignModule.h:114
lumiFormat.i
int i
Definition: lumiFormat.py:85
Trk::AlignModule::identifyHash
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
beamspotman.n
n
Definition: beamspotman.py:731
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
vector
Definition: MultiHisto.h:13
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
xAOD::rotation
rotation
Definition: TrackSurface_v1.cxx:15
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
AthAlgTool.h
Trk::AlignModule::m_chi2VAlignParamMeasType
double *** m_chi2VAlignParamMeasType
Definition: AlignModule.h:233
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
Trk::AlignModule::unidentified
@ unidentified
Definition: AlignModule.h:57
Trk::AlignModule::calculateAlignModuleToGlobal
Amg::Transform3D calculateAlignModuleToGlobal() const
Calculates Align to Global transform based on the TrkDetElementBase in the alignmodule.
Definition: AlignModule.cxx:214
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
Trk::AlignModule::SCT
@ SCT
Definition: AlignModule.h:57
xAOD::TrackState
TrackState_v1 TrackState
Definition: TrackState.h:11
Trk::AlignModule::m_detIdentifiers
std::vector< IdentifierCollection * > m_detIdentifiers
Definition: AlignModule.h:215
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
Trk::AlignModule::IdentifierCollection
std::vector< Identifier > IdentifierCollection
Definition: AlignModule.h:61
AlignModuleList.h
Trk::AlignModule::DetElementCollection
std::vector< const TrkDetElementBase * > DetElementCollection
typedefs to contain detector element pointers and transforms
Definition: AlignModule.h:60
Trk::AlignModule::name
const std::string & name() const
Definition: AlignModule.h:89
Trk::detTypeStr
std::string detTypeStr(AlignModule::DetectorType detType)
returns the detector type
Definition: AlignModule.cxx:177
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
Amg
Definition of ATLAS Math & Geometry primitives (Amg)
Definition: AmgStringHelpers.h:19
Trk::AlignModule::CSC
@ CSC
Definition: AlignModule.h:57
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
python.subdetectors.mmg.ids
ids
Definition: mmg.py:8
Trk::AlignModule::DetectorType
DetectorType
Definition: AlignModule.h:57
AlignModule.h
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
Trk::AlignModule::~AlignModule
virtual ~AlignModule()
Definition: AlignModule.cxx:51
Trk::AlignModule::alignModuleToDetElementTransform
const Amg::Transform3D * alignModuleToDetElementTransform(AlignModule::DetectorType detType, const TrkDetElementBase *det, const Identifier id=Identifier()) const
returns AlignModule to DetElement transform for a detector element
Definition: AlignModule.cxx:151
Amg::RotationMatrix3D
Eigen::Matrix< double, 3, 3 > RotationMatrix3D
Definition: GeoPrimitives.h:49
Trk::operator<<
MsgStream & operator<<(MsgStream &sl, const AlignModule &alignModule)
overload of << operator for MsgStream for debug output
Definition: AlignModule.cxx:204
Amg::Translation3D
Eigen::Translation< double, 3 > Translation3D
Definition: GeoPrimitives.h:44
AthCommonMsg::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
Amg::AngleAxis3D
Eigen::AngleAxisd AngleAxis3D
Definition: GeoPrimitives.h:45
AthAlgTool
Definition: AthAlgTool.h:26
Trk::AlignModule::m_alignModuleToDetElementTransforms
std::vector< std::vector< Amg::Transform3D > * > m_alignModuleToDetElementTransforms
Definition: AlignModule.h:218
Trk::AlignModule::MDT
@ MDT
Definition: AlignModule.h:57
Trk::AlignModule::identify
Identifier identify() const
Definition: AlignModule.h:97
Trk::AlignModule::m_globalToAlignFrameTransform
Amg::Transform3D m_globalToAlignFrameTransform
Definition: AlignModule.h:224
Trk::AlignModule::RPC
@ RPC
Definition: AlignModule.h:57
Trk::TrackState::NumberOfMeasurementTypes
@ NumberOfMeasurementTypes
Definition: TrackStateDefs.h:43
Identifier
Definition: IdentifierFieldParser.cxx:14