Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:10:41

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2019 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/ContextualDetector/InternalAlignmentDecorator.hpp"
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Geometry/GeometryContext.hpp"
0013 #include "ActsExamples/ContextualDetector/InternallyAlignedDetectorElement.hpp"
0014 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0015 #include "ActsExamples/Framework/RandomNumbers.hpp"
0016 
0017 #include <ostream>
0018 #include <thread>
0019 #include <utility>
0020 
0021 ActsExamples::Contextual::InternalAlignmentDecorator::
0022     InternalAlignmentDecorator(const Config& cfg,
0023                                std::unique_ptr<const Acts::Logger> logger)
0024     : m_cfg(cfg), m_logger(std::move(logger)) {}
0025 
0026 ActsExamples::ProcessCode
0027 ActsExamples::Contextual::InternalAlignmentDecorator::decorate(
0028     AlgorithmContext& context) {
0029   // We need to lock the Decorator
0030   std::lock_guard<std::mutex> alignmentLock(m_alignmentMutex);
0031 
0032   // In which iov batch are we?
0033   unsigned int iov = context.eventNumber / m_cfg.iovSize;
0034 
0035   ACTS_VERBOSE("IOV handling in thread " << std::this_thread::get_id() << ".");
0036   ACTS_VERBOSE("IOV resolved to " << iov << " - from event "
0037                                   << context.eventNumber << ".");
0038 
0039   m_eventsSeen++;
0040 
0041   context.geoContext = InternallyAlignedDetectorElement::ContextType{iov};
0042 
0043   if (m_cfg.randomNumberSvc != nullptr) {
0044     if (auto it = m_activeIovs.find(iov); it != m_activeIovs.end()) {
0045       // Iov is already present, update last accessed
0046       it->second.lastAccessed = m_eventsSeen;
0047     } else {
0048       // Iov is not present yet, create it
0049 
0050       m_activeIovs.emplace(iov, IovStatus{m_eventsSeen});
0051 
0052       ACTS_VERBOSE("New IOV " << iov << " detected at event "
0053                               << context.eventNumber
0054                               << ", emulate new alignment.");
0055 
0056       // Create an algorithm local random number generator
0057       RandomEngine rng = m_cfg.randomNumberSvc->spawnGenerator(context);
0058 
0059       for (auto& lstore : m_cfg.detectorStore) {
0060         for (auto& ldet : lstore) {
0061           // get the nominal transform
0062           Acts::Transform3 tForm =
0063               ldet->nominalTransform(context.geoContext);  // copy
0064           // create a new transform
0065           applyTransform(tForm, m_cfg, rng, iov);
0066           // put it back into the store
0067           ldet->addAlignedTransform(tForm, iov);
0068         }
0069       }
0070     }
0071   }
0072 
0073   // Garbage collection
0074   if (m_cfg.doGarbageCollection) {
0075     for (auto it = m_activeIovs.begin(); it != m_activeIovs.end();) {
0076       unsigned int this_iov = it->first;
0077       auto& status = it->second;
0078       if (m_eventsSeen - status.lastAccessed > m_cfg.flushSize) {
0079         ACTS_DEBUG("IOV " << this_iov << " has not been accessed in the last "
0080                           << m_cfg.flushSize << " events, clearing");
0081         it = m_activeIovs.erase(it);
0082         for (auto& lstore : m_cfg.detectorStore) {
0083           for (auto& ldet : lstore) {
0084             ldet->clearAlignedTransform(this_iov);
0085           }
0086         }
0087       } else {
0088         it++;
0089       }
0090     }
0091   }
0092 
0093   return ProcessCode::SUCCESS;
0094 }