ATLAS Offline Software
Loading...
Searching...
No Matches
TRTTrkAlignDBTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
7
9
12
16
18
20#include "TFile.h"
21
22#include <vector>
23#include <cmath>
24
25namespace InDet
26{
27
28//________________________________________________________________________
29TRTTrkAlignDBTool::TRTTrkAlignDBTool(const std::string & type, const std::string & name, const IInterface * parent)
30 : AthAlgTool(type,name,parent)
31 , m_trtHelper(nullptr)
32 , m_pixHelper(nullptr)
33 , m_outputAlignFile("OutputTRTAlignment.txt")
34 , m_outputStrawAlignFile("OutputTRTStrawAlignment.txt")
35 , m_oldAlignFile("OldTRTAlignment.txt")
36 , m_oldStrawAlignFile("OldTRTStrawAlignment.txt")
37 , m_writeTextFile(true)
38 , m_writeOldConstants(false)
39 , m_updateConstants(true)
40 , m_trtAlignDbSvc("TRT_AlignDbSvc",name)
41 , m_alignModuleTool("Trk::AlignModuleTool/AlignModuleTool")
42 , m_trtGeoManager("")
43 , m_trtAlignLevel(-1)
46 , m_ntuple{}
47{
48 declareInterface<ITrkAlignDBTool>(this);
49
50 declareProperty("OutputTextFile", m_outputAlignFile, "Text file to write the final alignment constants");
51 declareProperty("OutputStrawTextFile", m_outputStrawAlignFile, "Text file to write the final straw alignment constants");
52 declareProperty("WriteTextFile", m_writeTextFile, "Whether or not to write the final/new alignment constants to text file");
53 declareProperty("WriteOldConstants", m_writeOldConstants, "Whether or not to write the initial/old alignment constants to text file");
54 declareProperty("OldAlignFile", m_oldAlignFile, "Text file to write the initial/old alignment constants");
55 declareProperty("OldStrawAlignFile", m_oldStrawAlignFile, "Text file to write the initial/old straw alignment constants");
56 declareProperty("TrtAlignDbSvc", m_trtAlignDbSvc, "Service for interaction with the TRT alignment DB");
57 declareProperty("AlignModuleTool", m_alignModuleTool, "Tool for handling of align module collections");
58 declareProperty("TRTGeometryManager", m_trtGeoManager, "Tool for handling the TRT alignment geometry");
59 declareProperty("UpdateConstants", m_updateConstants, "Whether or no to update the alignment constants");
60
61 m_logStream = nullptr;
62}
63
64//________________________________________________________________________
67
68//________________________________________________________________________
70{
71 ATH_MSG_DEBUG("initialize() of TRTTrkAlignDBTool");
72
74 ATH_MSG_WARNING("Not updating TRT constants!");
75 return StatusCode::SUCCESS;
76 }
77 // get TRT db service
78 ATH_CHECK( m_trtAlignDbSvc.retrieve() );
79 // get AlignModuleTool
80 ATH_CHECK( m_alignModuleTool.retrieve());
81 // get TRT ID Helper
82 ATH_CHECK( detStore()->retrieve(m_trtHelper, "TRT_ID"));
83 // get Pixel ID Helper
84 ATH_CHECK( detStore()->retrieve(m_pixHelper) );
85
86 // get TRTGeometryManagerTool
87 if( !m_trtGeoManager.empty() ) {
88 ATH_CHECK( m_trtGeoManager.retrieve());
89 ATH_MSG_DEBUG("retrieved " << m_trtGeoManager);
90 m_trtAlignLevel = m_trtGeoManager->alignLevel();
91 m_trtAlignLevelBarrel = m_trtGeoManager->alignLevelBarrel();
92 m_trtAlignLevelEndcaps = m_trtGeoManager->alignLevelEndcaps();
93 ATH_MSG_DEBUG("Retrieved TRT Global alignment level: "<<m_trtAlignLevel);
94 ATH_MSG_DEBUG("Retrieved TRT Barrel alignment level: "<<m_trtAlignLevelBarrel);
95 ATH_MSG_DEBUG("Retrieved TRT Endcap alignment level: "<<m_trtAlignLevelEndcaps);
96 if(m_trtAlignLevelEndcaps==200) {
97 ATH_MSG_WARNING("Requested update of TRT Endcap constants for the old scheme.");
98 ATH_MSG_WARNING("This will only work with \'alignString = \"ALold\"\' option of TRT_AlignDbSvc.");
100 }
101 }
102
103 if(m_trtAlignLevel == 0)
104 ATH_MSG_INFO(" Requested update of Level "<<m_trtAlignLevel<<" alignment constants for TRT");
105 else {
106 ATH_MSG_INFO(" Requested update of Level "<<m_trtAlignLevelBarrel<<" alignment constants for TRT Barrel");
107 ATH_MSG_INFO(" Requested update of Level "<<m_trtAlignLevelEndcaps<<" alignment constants for TRT Endcaps");
108 }
109
110 // check alignment level but don't stop on error
111 // if unknown level given, set to -1
112 if(!checkAlignLevel()) {
113 ATH_MSG_FATAL( " No geometry manager available or alignment level not given. " );
114 // we don't want to stop if there's errors
115 // so we comment out the return statement
116// return StatusCode::FAILURE;
117 }
118 return StatusCode::SUCCESS;
119}
120
121//________________________________________________________________________
123{
124 ATH_MSG_DEBUG("finalize() of TRTTrkAlignDBTool");
125
126 return StatusCode::SUCCESS;
127}
128
129//________________________________________________________________________
131{
132 if(m_trtAlignLevel == 0)
133 return true;
134
135 m_trtAlignLevel = -1;
136
137 bool ok=false;
138
139 switch(m_trtAlignLevelBarrel) {
140 case 1: case 2: case 3:
141 ok = true;
142 break;
143 default:
144 ATH_MSG_ERROR(" Storing of alignment constants for level "<<m_trtAlignLevelBarrel<<" for TRT Barrel is not implemented.");
146 ok = false;
147 break;
148 }
149
150 switch(m_trtAlignLevelEndcaps) {
151 case 1: case 2: case 3:
152 ok = ok && true;
153 break;
154 default:
155 ATH_MSG_ERROR(" Storing of alignment constants for level "<<m_trtAlignLevelEndcaps<<" for TRT Endcaps is not implemented.");
157 ok = false;
158 break;
159 }
160
161 return ok;
162}
163
164//________________________________________________________________________
166{
167 ATH_MSG_DEBUG("in TRTTrkAlignDBTool::writeAlignPar()");
168
169 // do we want to update the DB?
170 if(!m_updateConstants) {
171 ATH_MSG_INFO("Update of TRT constants not requested!");
172 return;
173 }
174
175 // write initial alignment constants
177 ATH_MSG_INFO("Writing old TRT alignment constants to file "<<m_oldAlignFile);
178 if( m_trtAlignDbSvc -> writeAlignTextFile(m_oldAlignFile) != StatusCode::SUCCESS )
179 ATH_MSG_ERROR("Cannot write to file "<<m_oldAlignFile);
180 if( m_trtAlignDbSvc -> writeStrawAlignTextFile(m_oldStrawAlignFile) != StatusCode::SUCCESS )
181 ATH_MSG_ERROR("Cannot write to file "<<m_oldStrawAlignFile);
182 }
183
184 // now update the DB
185 updateDB();
186
187 // and write the final alignment constants
188 if(m_writeTextFile) {
189 if( m_trtAlignDbSvc->writeAlignTextFile(m_outputAlignFile) != StatusCode::SUCCESS )
190 ATH_MSG_ERROR("Cannot write to file "<<m_outputAlignFile);
191 else
192 ATH_MSG_INFO("Wrote final TRT alignment constants into "<<m_outputAlignFile);
193
194 if( m_trtAlignDbSvc->writeStrawAlignTextFile(m_outputStrawAlignFile) != StatusCode::SUCCESS )
195 ATH_MSG_ERROR("Cannot write to file "<<m_outputStrawAlignFile);
196 else
197 ATH_MSG_INFO("Wrote final TRT Straw alignment constants into "<<m_outputStrawAlignFile);
198 }
199
200 return;
201}
202
203//________________________________________________________________________
205{
206 ATH_MSG_DEBUG(" in TRTTrkAlignDBTool::updateDB()");
207
208 // There are 3 sets of alignment constants in the DB for the TRT
209 // L0 : whole TRT
210 // L1 : barrel + 2 endcaps
211 // L2 : barrel modules and endcap discs
212 //
213 // If we're doing alignment of the level between these, the constants
214 // of the closest higher level have to be updated.
215 // This is not allways necessary for all alignmodules since some of them
216 // can be defined to be identical with modules at lower alignment level,
217 // but we keep it like this for the moment.
218
219 // updating level 0, level 1 or level 2 constants in the DB
220 ATH_MSG_DEBUG("Global alignment level: "<<m_trtAlignLevel);
221 ATH_MSG_DEBUG("Barrel alignment level: "<<m_trtAlignLevelBarrel);
222 ATH_MSG_DEBUG("Endcap alignment level: "<<m_trtAlignLevelEndcaps);
223
225 ATH_MSG_ERROR(" No geometry manager available or alignment level not given.");
226 ATH_MSG_ERROR(" DB not updated.");
227 return;
228 }
229
230 ATH_MSG_INFO("-------------------------------------------------------");
231 if(m_trtAlignLevel>=0)
232 ATH_MSG_INFO("updating Level "<<m_trtAlignLevel<<" alignment constants for modules");
233 else {
234 ATH_MSG_INFO("updating Level "<<m_trtAlignLevelBarrel<<" alignment constants for Barrel modules");
235 ATH_MSG_INFO("updating Level "<<m_trtAlignLevelEndcaps<<" alignment constants for Endcap modules");
236 }
237
238 Amg::Transform3D dbtransform;
239
240 // loop over all AlignModules
241 std::vector<Trk::AlignModule *>::const_iterator imod = m_alignModuleTool->getAlignModuleCollection()->begin();
242 std::vector<Trk::AlignModule *>::const_iterator imod_end = m_alignModuleTool->getAlignModuleCollection()->end();
243 for( ; imod != imod_end; ++imod ) {
244
245 Trk::AlignModule * module = *imod;
246
247 ATH_MSG_DEBUG("-------------------------------------------------------");
248 ATH_MSG_DEBUG("Alignment corrections for module: "<<module->name());
249
250 // the identifier of the module for the DB
251 Identifier modID = module->identify();
252
253 // The TRT AlignModules of levels 1 and 2 use the TRT Identifier.
254 // For level 0 the Identifier can be either TRT of Pixel but we have to check
255 // that the AlignModule contains the TRT detector elements
256 // (there can also be L0 without TRT in which case we don't want to do anything)
257 if(!m_trtHelper->is_trt(modID) && (m_trtAlignLevel!=0 || !containsTRT(module)) ) {
258 ATH_MSG_DEBUG("Module with ID: "<<modID<<" is not a TRT module. Skipping.");
259 continue;
260 }
261
262 // get active alignment parameters for the module
263 DataVector<Trk::AlignPar> * alignPars = m_alignModuleTool->getAlignPars(module);
264 if(alignPars->size() == 0) {
265 ATH_MSG_INFO("Alignment for module "<<module->name()<<" not available.");
266 continue;
267 }
268
269 // get final alignment parameters
270 DataVector<Trk::AlignPar> * fullAlignPars = m_alignModuleTool->getFullAlignPars(module);
271 double apTraX = fullAlignPars->at(Trk::AlignModule::TransX)->par();
272 double apTraY = fullAlignPars->at(Trk::AlignModule::TransY)->par();
273 double apTraZ = fullAlignPars->at(Trk::AlignModule::TransZ)->par();
274 double apRotX = fullAlignPars->at(Trk::AlignModule::RotX)->par();
275 double apRotY = fullAlignPars->at(Trk::AlignModule::RotY)->par();
276 double apRotZ = fullAlignPars->at(Trk::AlignModule::RotZ)->par();
277
278 // construct the alignment transform
279 Amg::Translation3D translation( apTraX, apTraY, apTraZ );
280 Amg::Transform3D transform = translation * Amg::RotationMatrix3D::Identity();
281 transform *= Amg::AngleAxis3D(apRotZ, Amg::Vector3D(0.,0.,1.));
282 transform *= Amg::AngleAxis3D(apRotY, Amg::Vector3D(0.,1.,0.));
283 transform *= Amg::AngleAxis3D(apRotX, Amg::Vector3D(1.,0.,0.));
284
285
286
287 ATH_MSG_DEBUG(" - translation X = "<<apTraX);
288 ATH_MSG_DEBUG(" - translation Y = "<<apTraY);
289 ATH_MSG_DEBUG(" - translation Z = "<<apTraZ);
290 ATH_MSG_DEBUG(" - rotation X = "<<apRotX);
291 ATH_MSG_DEBUG(" - rotation Y = "<<apRotY);
292 ATH_MSG_DEBUG(" - rotation Z = "<<apRotZ);
293
294 // update TRT level 0 constants
295 if(m_trtAlignLevel == 0) {
296 updateL0asL1(modID,dbtransform);
297 continue;
298 }
299
300 int level;
301 if(m_trtHelper->is_barrel(modID))
302 level = m_trtAlignLevelBarrel;
303 else
305
306 // update the constants in memory
307 ATH_MSG_DEBUG("updating level "<<level<<" constants for module "<<module->name()<<" with ID "<<modID);
308 switch(level) {
309 case 1: case 2: {
310 // for levels 1 and 2 alignment the DB frame equals to the global frame but the align
311 // frame does not, so we have to apply an additional transform
312 Amg::Transform3D dbFrameToAlignFrame = module->globalFrameToAlignFrame();
313
314 ATH_MSG_DEBUG("DB to align");
315 printTransform(dbFrameToAlignFrame);
316
317 dbtransform = dbFrameToAlignFrame.inverse() * transform * dbFrameToAlignFrame;
318
319 break;
320 }
321
322 case 3:
323 // for level 3 alignment the DB frame and the align frame equals to the local frame
324 // so we don't apply any additional transform
325
326 // if we are in the Endcap we need to rotate about global Z
327 //if(abs(m_trtHelper->barrel_ec_id(modID.get_identifier32().get_compact()).get_compact()) == 2) { # Bug!
328 if( abs(m_trtHelper->barrel_ec(modID)) == 2) {
329 ATH_MSG_DEBUG("L3 module in TRT end-cap A so we apply additional rotation about global Z (" << M_PI << ")");
330 Amg::Translation3D newtranslation( 0,0,0 );
331 Amg::Transform3D newtransform = newtranslation * Amg::AngleAxis3D( M_PI, Amg::Vector3D(0.,0.,1.));
332 dbtransform = newtransform * transform;
333 }
334 else
335 // otherwise we don't do anything
336 dbtransform = transform;
337
338 break;
339
340 default:
341 // otherwise we don't know what to do
342 // so we do nothing for the module
343 ATH_MSG_WARNING("Unknown level "<<level<<". Skipping the module.");
344 continue;
345 }
346
347 ATH_MSG_DEBUG("DB transform");
348 printTransform(dbtransform);
349
350 // tweak applies the transform onto already existing transform in the DB
351 // or sets it if it doesn't exist yet
352 if(m_trtAlignDbSvc->tweakAlignTransform(modID,dbtransform,level).isFailure())
353 ATH_MSG_ERROR("Error setting constants for module \'"<<module->name()<<"\'");
354 else
355 ATH_MSG_DEBUG("Module \'"<<module->name()<<"\': Level "<<level<<" constants updated.");
356 }
357 ATH_MSG_INFO("-------------------------------------------------------");
358
359}
360
361//________________________________________________________________________
363{
364 // splitting the whole TRT into barrel and two endcaps
365 // or the whole Inner Detector into TRT and the rest
366 // the Identifier has to be equal to:
367 // - L1 TRT barrel Identifier for TRT level 0 update
368 // - L1 Pixel Identifier for Inner Detector level 0 update
369 if( idL0 != m_pixHelper->wafer_id(0,0,0,0) && idL0 != m_trtHelper->barrel_ec_id(-1) ) {
370 ATH_MSG_ERROR("wrong identifier for L0 TRT in updateL0asL1() : "<<idL0);
371 ATH_MSG_ERROR("allowed are "<<m_trtHelper->barrel_ec_id(-1)<<" for TRT L0");
372 ATH_MSG_ERROR(" and "<<m_pixHelper->wafer_id(0,0,0,0)<<" for InDet L0");
373 return;
374 }
375
376 // for both L0 and L1 the global frame is equal to the DB frame
377 // for L0 it is also equal to align frame so no additional transform is needed
378
379 ATH_MSG_INFO("Splitting TRT into L1 modules and updating in the DB");
380
381 std::vector<Identifier> ids;
382 ids.push_back(m_trtHelper->barrel_ec_id(-1)); // barrel
383 ids.push_back(m_trtHelper->barrel_ec_id(-2)); // endcap A
384 ids.push_back(m_trtHelper->barrel_ec_id(2)); // endcap C
385
386 ATH_MSG_INFO("Updating L1 constants for TRT");
387 for(unsigned int i=0; i<ids.size(); ++i) {
388 // tweak applies the transform onto already existing transform in the DB
389 if(m_trtAlignDbSvc->tweakAlignTransform(ids[i],transform,1).isFailure())
390 ATH_MSG_ERROR("Error setting constants.");
391 else
392 ATH_MSG_INFO("Module "<<i<<". - id "<<ids[i]<<": Constants updated.");
393 }
394}
395
396//________________________________________________________________________
398{
399 ATH_MSG_DEBUG(" - translation: "<<tr.translation().x()<<" "<<tr.translation().y()<<" "<<tr.translation().z());
400 ATH_MSG_DEBUG(" - rotation:");
401 ATH_MSG_DEBUG(" "<<tr(0,0)<<" "<<tr(0,1)<<" "<<tr(0,2));
402 ATH_MSG_DEBUG(" "<<tr(1,0)<<" "<<tr(1,1)<<" "<<tr(1,2));
403 ATH_MSG_DEBUG(" "<<tr(2,0)<<" "<<tr(2,1)<<" "<<tr(2,2));
404 return;
405}
406
407//________________________________________________________________________
409{
410 ATH_MSG_DEBUG("in containsTRT()");
411 const Trk::AlignModule::DetElementCollection * coll = module->detElementCollection(Trk::AlignModule::TRT);
412 if(!coll || coll->size()==0)
413 return false;
414
415 return true;
416}
417//________________________________________________________________________
418
419} // end namespace
#define M_PI
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
An STL vector of pointers that by default owns its pointed-to elements.
abstract interface to Service to manage TRT alignment conditions
This is an Identifier helper class for the Pixel subdetector.
This is an Identifier helper class for the TRT subdetector.
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
const ServiceHandle< StoreGateSvc > & detStore() const
Derived DataVector<T>.
Definition DataVector.h:795
const T * at(size_type n) const
Access an element, as an rvalue.
size_type size() const noexcept
Returns the number of elements in the collection.
bool containsTRT(Trk::AlignModule *module) const
checks whether the AlignModule contains TRT detector elements
TRTTrkAlignDBTool(const std::string &type, const std::string &name, const IInterface *parent)
void printTransform(const Amg::Transform3D &tr) const
print translation to stdout
void updateDB()
updates alignment constants in the DB
void writeAlignPar()
Writes alignment parameters.
ToolHandle< Trk::IGeometryManagerTool > m_trtGeoManager
void updateL0asL1(Identifier idL0, const Amg::Transform3D &transform)
updates level 0 transform as level 1 constants in the DB
ToolHandle< Trk::IAlignModuleTool > m_alignModuleTool
ServiceHandle< ITRT_AlignDbSvc > m_trtAlignDbSvc
std::vector< const TrkDetElementBase * > DetElementCollection
typedefs to contain detector element pointers and transforms
Definition AlignModule.h:60
std::ostream * m_logStream
logfile output stream
Eigen::AngleAxisd AngleAxis3D
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D
Eigen::Translation< double, 3 > Translation3D
Primary Vertex Finder.