ATLAS Offline Software
Loading...
Searching...
No Matches
HGTD_MisalignAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "HGTD_MisalignAlg.h"
6#include "GeoModelKernel/GeoAlignableTransform.h"
8
13
14#include <fstream>
15
17{
18 ATH_MSG_INFO("=== HGTD_MisalignAlg initialize ===");
19
20 //------------------------------------------------------------
21 // Retrieve services
22 //------------------------------------------------------------
23 ATH_CHECK(m_rndmSvc.retrieve());
24 ATH_CHECK(m_alignDBTool.retrieve());
25
26 ATH_MSG_DEBUG("Retrieved HGTD_AlignDBTool");
27 ATH_MSG_DEBUG("AlignDBTool = " << m_alignDBTool.typeAndName());
28 ATH_MSG_DEBUG("WriteToDB = " << m_writeToDB);
29 ATH_MSG_DEBUG("SQLiteTag = " << m_sqliteTag);
30 ATH_MSG_DEBUG("RandomStream = " << m_randomStream);
31
32 //------------------------------------------------------------
33 // Retrieve HGTD identifier helper
34 //------------------------------------------------------------
35 ATH_CHECK(detStore()->retrieve(m_hgtdIdHelper,"HGTD_ID"));
36
37 //------------------------------------------------------------
38 // Retrieve HGTD detector manager
39 //------------------------------------------------------------
40 ATH_CHECK(detStore()->retrieve(m_hgtdManager, "HGTD"));
41 ATH_MSG_DEBUG("Retrieved HGTD_DetectorManager");
42
43 //------------------------------------------------------------
44 // Open output file
45 //------------------------------------------------------------
46 m_outfile.open(m_outputFile.value());
47
48 if (!m_outfile.is_open()) {
49
51 "Cannot open output file "
52 << m_outputFile.value());
53
54 return StatusCode::FAILURE;
55 }
56 ATH_MSG_DEBUG("Output file = " << m_outputFile.value());
57
58 //------------------------------------------------------------
59 // ASCII validation output
60 //------------------------------------------------------------
62 << "# hash "
63 << "endcap "
64 << "layer "
65 << "phi_module "
66 << "eta_module "
67 << "local_dx "
68 << "local_dy "
69 << "local_dz "
70 << "center_x "
71 << "center_y "
72 << "center_z "
73 << "misaligned_x "
74 << "misaligned_y "
75 << "misaligned_z\n";
76
77 SmartIF<ITHistSvc> histSvc{
78 Gaudi::svcLocator()->service("THistSvc")};
79 ATH_CHECK(histSvc.isValid());
80
81 m_tree = new TTree("HGTDMisalignment", "HGTD alignment validation");
82
84 histSvc->regTree("/CREATEMISALIGN/HGTDMisalignment", m_tree));
85
86 //------------------------------------------------------------
87 // Validation tree branches
88 // These quantities are used to validate the generated
89 // misalignment and compare the nominal and shifted geometry.
90 //------------------------------------------------------------
91
92 m_tree->Branch("hash", &m_hash, "hash/i");
93
94 // HGTD detector identifier
95 m_tree->Branch("endcap", &m_endcap, "endcap/I");
96 m_tree->Branch("layer", &m_layer, "layer/I");
97 m_tree->Branch("phi_module", &m_phiModule, "phi_module/I");
98 m_tree->Branch("eta_module", &m_etaModule, "eta_module/I");
99
100 // Applied local misalignment
101 m_tree->Branch("local_dx", &m_localDx, "local_dx/F");
102 m_tree->Branch("local_dy", &m_localDy, "local_dy/F");
103 m_tree->Branch("local_dz", &m_localDz, "local_dz/F");
104
105 // Nominal global center
106 m_tree->Branch("center_x", &m_centerX, "center_x/F");
107 m_tree->Branch("center_y", &m_centerY, "center_y/F");
108 m_tree->Branch("center_z", &m_centerZ, "center_z/F");
109
110 // Misaligned global center
111 m_tree->Branch("misaligned_x", &m_shiftedX, "misaligned_x/F");
112 m_tree->Branch("misaligned_y", &m_shiftedY, "misaligned_y/F");
113 m_tree->Branch("misaligned_z", &m_shiftedZ, "misaligned_z/F");
114
115 //------------------------------------------------------------
116 // Print configuration
117 //------------------------------------------------------------
118 ATH_MSG_DEBUG("MisalignMode = " << m_mode.value());
119 ATH_MSG_DEBUG("ShiftX = " << m_shiftX.value());
120 ATH_MSG_DEBUG("ShiftY = " << m_shiftY.value());
121 ATH_MSG_DEBUG("ShiftZ = " << m_shiftZ.value());
122 ATH_MSG_DEBUG("SigmaX = " << m_sigmaX.value());
123 ATH_MSG_DEBUG("SigmaY = " << m_sigmaY.value());
124 ATH_MSG_DEBUG("SigmaZ = " << m_sigmaZ.value());
125 ATH_MSG_DEBUG("ApplyTranslation = " << m_applyTranslation.value());
126
127 return StatusCode::SUCCESS;
128}
129
130StatusCode HGTD_MisalignAlg::execute(const EventContext&)
131{
132 ATH_MSG_DEBUG("HGTD_MisalignAlg execute()");
133
134 ++m_nEvents;
135
136 //------------------------------------------------------------
137 // Nothing to do if misalignment is disabled
138 //------------------------------------------------------------
139 if (!m_applyTranslation.value()) {
140 ATH_MSG_DEBUG("Translation disabled");
141 return StatusCode::SUCCESS;
142 }
143
144 //------------------------------------------------------------
145 // Generate the alignment only once
146 //------------------------------------------------------------
147 if (!m_firstEvent) {
148 return StatusCode::SUCCESS;
149 }
150
151 ATH_MSG_DEBUG("Generating HGTD misalignment");
152
153 //------------------------------------------------------------
154 // Create a fresh alignment database
155 //------------------------------------------------------------
156 if (m_createFreshDB) {
157
158 ATH_MSG_DEBUG("Creating HGTD alignment database");
159
160 ATH_CHECK(m_alignDBTool->createDB());
161
162 m_createFreshDB = false;
163 }
164
165 //------------------------------------------------------------
166 // Generate all module misalignments
167 //------------------------------------------------------------
169
170 //------------------------------------------------------------
171 // Write AlignableTransforms
172 //------------------------------------------------------------
173 ATH_MSG_DEBUG("Writing HGTD alignment constants");
174
175 if (m_writeToDB) {
176 ATH_MSG_DEBUG("Calling outputObjs()");
177 ATH_CHECK(m_alignDBTool->outputObjs());
178
179 ATH_MSG_DEBUG("Finished outputObjs()");
180 ATH_MSG_DEBUG("Calling fillDB()");
181
182 //------------------------------------------------------------
183 // Write SQLite database
184 //------------------------------------------------------------
185 ATH_CHECK(
186 m_alignDBTool->fillDB(
192
193 ATH_MSG_DEBUG("Finished fillDB()");
194 }
195 else {
196 ATH_MSG_DEBUG("WriteToDB = FALSE, skipping database writing.");
197 }
198
199 //------------------------------------------------------------
200 // Do not regenerate on later events
201 //------------------------------------------------------------
202 m_firstEvent = false;
203
204 return StatusCode::SUCCESS;
205}
206
208{
209 const auto* elements = m_hgtdManager->getDetectorElementCollection();
210
211 if (!elements) {
212 ATH_MSG_ERROR("HGTD detector element collection is null");
213 return StatusCode::FAILURE;
214 }
215
216 ATH_MSG_DEBUG("Number of HGTD detector elements = "
217 << elements->size());
218
219 ATH_MSG_DEBUG("Misalignment mode = "
220 << m_mode.value());
221
222 //------------------------------------------------------------
223 // Random generators
224 //------------------------------------------------------------
225 Rndm::Numbers gaussX(
226 m_rndmSvc.operator->(),
227 Rndm::Gauss(0., m_sigmaX.value()));
228
229 Rndm::Numbers gaussY(
230 m_rndmSvc.operator->(),
231 Rndm::Gauss(0., m_sigmaY.value()));
232
233 Rndm::Numbers gaussZ(
234 m_rndmSvc.operator->(),
235 Rndm::Gauss(0., m_sigmaZ.value()));
236
237 int count = 0;
238
239 //------------------------------------------------------------
240 // Loop over all HGTD detector elements
241 //------------------------------------------------------------
242 for (const auto* element : *elements) {
243
244 if (!element)
245 continue;
246
247 Identifier id = element->identify();
248 IdentifierHash hash = element->identifyHash();
249
250 //--------------------------------------------------------
251 // Detector identifier information
252 //--------------------------------------------------------
253 m_hash = hash.value();
254 m_endcap = m_hgtdIdHelper->endcap(id);
255 m_layer = m_hgtdIdHelper->layer(id);
256 m_phiModule = m_hgtdIdHelper->phi_module(id);
257 m_etaModule = m_hgtdIdHelper->eta_module(id);
258
259 //--------------------------------------------------------
260 // Misalignment parameters
261 //--------------------------------------------------------
262 double dx = 0.0;
263 double dy = 0.0;
264 double dz = 0.0;
265
266 if (m_mode.value() == 0) {
267 // no misalignment
268 }
269 else if (m_mode.value() == 1) {
270 // Constant translation
271 dx = m_shiftX.value();
272 dy = m_shiftY.value();
273 dz = m_shiftZ.value();
274 }
275 else if (m_mode.value() == 2) {
276 // Random Gaussian translation
277 dx = gaussX();
278 dy = gaussY();
279 dz = gaussZ();
280 }
281 else {
282 ATH_MSG_WARNING("Unknown MisalignMode = "
283 << m_mode.value()
284 << ". No misalignment applied.");
285 continue;
286 }
287 //--------------------------------------------------------
288 // Update alignment constants
289 //--------------------------------------------------------
290 bool ok =
291 m_alignDBTool->tweakTrans(
292 id,
293 1,
294 Amg::Vector3D(dx, dy, dz),
295 0.,
296 0.,
297 0.);
298
299 if (!ok) {
300
302 "Failed to update module "
303 << hash);
304
305 continue;
306 }
307
308 //--------------------------------------------------------
309 // Fill validation tree
310 // For the current implementation only translations are
311 // applied, therefore the shifted position is simply
312 // nominal position + translation.
313 //--------------------------------------------------------
314
315 m_localDx = dx;
316 m_localDy = dy;
317 m_localDz = dz;
318
319 //--------------------------------------------------------
320 // Nominal module position
321 //--------------------------------------------------------
322 const Amg::Vector3D& center = element->center();
323
324 m_centerX = center.x();
325 m_centerY = center.y();
326 m_centerZ = center.z();
327
331
332 //--------------------------------------------------------
333 // Print first few modules
334 //--------------------------------------------------------
335 if (count < 10) {
336
338 "Module "
339 << hash
340 << " -> shift ("
341 << dx << ", "
342 << dy << ", "
343 << dz << ")");
344 }
345
346 //--------------------------------------------------------
347 // Save to text file
348 //--------------------------------------------------------
350 << m_hash << " "
351 << m_endcap << " "
352 << m_layer << " "
353 << m_phiModule << " "
354 << m_etaModule << " "
355 << m_localDx << " "
356 << m_localDy << " "
357 << m_localDz << " "
358 << m_centerX << " "
359 << m_centerY << " "
360 << m_centerZ << " "
361 << m_shiftedX << " "
362 << m_shiftedY << " "
363 << m_shiftedZ
364 << '\n';
365
366 m_tree->Fill();
367
368 ++count;
369 }
370
372 "Processed "
373 << count
374 << " HGTD modules");
375
376 return StatusCode::SUCCESS;
377}
378
380{
381 ATH_MSG_INFO("HGTD_MisalignAlg finalize()");
382
383 //------------------------------------------------------------
384 // Close output file
385 //------------------------------------------------------------
386 if (m_outfile.is_open()) {
387 m_outfile.close();
388 }
389
390 ATH_CHECK(m_alignDBTool->outputObjs());
391
392 ATH_CHECK(
393 m_alignDBTool->fillDB(
399 )
400 );
401
402 ATH_MSG_INFO("Processed " << m_nEvents << " event(s)");
403
404 return StatusCode::SUCCESS;
405}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Basic time unit for IOVSvc.
const ServiceHandle< StoreGateSvc > & detStore() const
Gaudi::Property< double > m_sigmaY
virtual StatusCode initialize() override
Gaudi::Property< bool > m_createFreshDB
Gaudi::Property< bool > m_writeToDB
std::ofstream m_outfile
Gaudi::Property< std::string > m_outputFile
Gaudi::Property< double > m_sigmaX
Gaudi::Property< double > m_shiftZ
Gaudi::Property< double > m_shiftX
StatusCode GenerateMisalignment()
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
Gaudi::Property< double > m_shiftY
const HGTD_DetectorManager * m_hgtdManager
Gaudi::Property< double > m_sigmaZ
virtual StatusCode finalize() override
Gaudi::Property< std::string > m_randomStream
Gaudi::Property< std::string > m_sqliteTag
Gaudi::Property< int > m_mode
unsigned int m_nEvents
ServiceHandle< IRndmGenSvc > m_rndmSvc
const HGTD_ID * m_hgtdIdHelper
Gaudi::Property< bool > m_applyTranslation
ToolHandle< IHGTD_AlignDBTool > m_alignDBTool
static constexpr uint32_t MAXRUN
Definition IOVTime.h:48
static constexpr uint32_t MINEVENT
Definition IOVTime.h:50
static constexpr uint32_t MAXEVENT
Definition IOVTime.h:51
static constexpr uint32_t MINRUN
Definition IOVTime.h:44
This is a "hash" representation of an Identifier.
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:148
Eigen::Matrix< double, 3, 1 > Vector3D