ATLAS Offline Software
SeedingAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "src/SeedingAlg.h"
6 
7 // ACTS
8 #include "Acts/Definitions/Units.hpp"
9 #include "Acts/MagneticField/MagneticFieldContext.hpp"
11 #include "Acts/Seeding/BinnedGroup.hpp"
12 #include "Acts/Seeding/SeedFilter.hpp"
13 #include "Acts/Seeding/SeedFinder.hpp"
14 
20 
21 
23 
24 #include "ActsInterop/TableUtils.h"
25 
26 namespace ActsTrk {
27  SeedingAlg::SeedingAlg( const std::string &name,
28  ISvcLocator *pSvcLocator )
29  : AthReentrantAlgorithm( name,pSvcLocator )
30  {}
31 
33  ATH_MSG_INFO( "Initializing " << name() << " ... " );
34  if (m_fastTracking)
35  ATH_MSG_INFO( " using fast tracking configuration.");
36 
37  // Retrieve seed tool
38  ATH_CHECK( m_seedsTool.retrieve() );
39  ATH_CHECK( m_paramEstimationTool.retrieve() );
40  ATH_CHECK( m_trackingGeometryTool.retrieve() );
41  ATH_CHECK( m_ATLASConverterTool.retrieve() );
42 
43  // Cond
47 
48  // Read and Write handles
49  ATH_CHECK( m_spacePointKey.initialize() );
50  ATH_CHECK( m_seedKey.initialize() );
51  ATH_CHECK( m_actsTrackParamsKey.initialize() );
52 
53  ATH_CHECK( m_monTool.retrieve(EnableTool{not m_monTool.empty()}) );
54 
55  return StatusCode::SUCCESS;
56  }
57 
59  ATH_MSG_INFO("Seed statistics" << std::endl << makeTable(m_stat,
60  std::array<std::string, kNStat>{
61  "Spacepoints",
62  "Seeds",
63  "No track parameters"
64  }).columnWidth(10));
65  return StatusCode::SUCCESS;
66  }
67 
68  StatusCode SeedingAlg::execute(const EventContext& ctx) const {
69  ATH_MSG_DEBUG( "Executing " << name() <<" ... " );
70 
71  auto timer = Monitored::Timer<std::chrono::milliseconds>( "TIME_execute" );
72  auto time_seedCreation = Monitored::Timer<std::chrono::milliseconds>( "TIME_seedCreation" );
73  auto time_parameterEstimation = Monitored::Timer<std::chrono::milliseconds>( "TIME_parameterEstimation" );
74  auto mon_nSeeds = Monitored::Scalar<int>("nSeeds");
75  auto mon = Monitored::Group( m_monTool, timer, time_seedCreation, time_parameterEstimation, mon_nSeeds );
76 
77  // ================================================== //
78  // ===================== OUTPUTS ==================== //
79  // ================================================== //
80 
82  ATH_MSG_DEBUG( " \\__ Seed Container `" << m_seedKey.key() << "` created ..." );
83  ATH_CHECK( seedHandle.record( std::make_unique< ActsTrk::SeedContainer >() ) );
84  ActsTrk::SeedContainer *seedPtrs = seedHandle.ptr();
85 
87  ATH_MSG_DEBUG( " \\__ Track Params Estimated `"<< m_actsTrackParamsKey.key() << "` created ..." );
88  ATH_CHECK( boundTrackParamsHandle.record( std::make_unique< ActsTrk::BoundTrackParametersContainer >() ) );
89  ActsTrk::BoundTrackParametersContainer *trackParams = boundTrackParamsHandle.ptr();
90 
91  // ================================================== //
92  // ===================== INPUTS ===================== //
93  // ================================================== //
94 
95  // Read the Beam Spot information
97  ATH_CHECK( beamSpotHandle.isValid() );
98  if (beamSpotHandle.cptr() == nullptr) {
99  ATH_MSG_ERROR("Retrieved Beam Spot Handle contains a nullptr");
100  return StatusCode::FAILURE;
101  }
102  auto beamSpotData = beamSpotHandle.cptr();
103  // Beam Spot Position
104  Acts::Vector3 beamPos( beamSpotData->beamPos().x() * Acts::UnitConstants::mm,
105  beamSpotData->beamPos().y() * Acts::UnitConstants::mm,
106  beamSpotData->beamPos().z() * Acts::UnitConstants::mm);
107 
108 
109  ATH_MSG_DEBUG( "Retrieving elements from " << m_spacePointKey.size() << " input collections...");
110  std::vector<const xAOD::SpacePointContainer *> all_input_collections;
111  all_input_collections.reserve(m_spacePointKey.size());
112 
113  std::size_t number_input_space_points = 0;
114  for (const auto& spacePointKey : m_spacePointKey) {
115  ATH_MSG_DEBUG( "Retrieving from Input Collection '" << spacePointKey.key() << "' ..." );
116  SG::ReadHandle< xAOD::SpacePointContainer > handle = SG::makeHandle( spacePointKey, ctx );
117  ATH_CHECK( handle.isValid() );
118  all_input_collections.push_back(handle.cptr());
119  ATH_MSG_DEBUG( " \\__ " << handle->size() << " elements!");
120  number_input_space_points += handle->size();
121  }
122 
123  // Apply selection on which SPs you want to use from the input container
124  std::vector<const xAOD::SpacePoint*> selectedSpacePoints;
125  selectedSpacePoints.reserve(number_input_space_points);
126 
127  for (const auto* collection : all_input_collections) {
128  selectedSpacePoints.insert(selectedSpacePoints.end(), collection->begin(), collection->end());
129  }
130 
131  ATH_MSG_DEBUG( " \\__ Total input space points: " << selectedSpacePoints.size());
132  m_stat[kNSpacepoints] += selectedSpacePoints.size();
133 
134  // Early Exit in case no space points at this stage
135  if (selectedSpacePoints.empty()) {
136  ATH_MSG_DEBUG("No input space points found, we stop seeding");
137  return StatusCode::SUCCESS;
138  }
139 
140  // ================================================== //
141  // ===================== CONDS ====================== //
142  // ================================================== //
143 
144  // Read the b-field information
146  ATH_CHECK( readHandle.isValid() );
147 
148  const AtlasFieldCacheCondObj* fieldCondObj{ *readHandle };
149  if (fieldCondObj == nullptr) {
150  ATH_MSG_ERROR("Failed to retrieve AtlasFieldCacheCondObj with key " << m_fieldCondObjInputKey.key());
151  return StatusCode::FAILURE;
152  }
153 
154  // Get the magnetic field
155  // Using ACTS classes in order to be sure we are consistent
156  Acts::MagneticFieldContext magFieldContext(fieldCondObj);
157  ATLASMagneticFieldWrapper magneticField;
158  Acts::MagneticFieldProvider::Cache magFieldCache = magneticField.makeCache( magFieldContext );
159  Acts::Vector3 bField = *magneticField.getField( Acts::Vector3(beamPos.x(), beamPos.y(), 0),
160  magFieldCache );
161 
163  ATH_CHECK( detEleHandle.isValid() );
164  const InDetDD::SiDetectorElementCollection* detEle = detEleHandle.retrieve();
165  if ( detEle == nullptr ) {
166  ATH_MSG_FATAL( m_detEleCollKey.fullKey() << " is not available." );
167  return StatusCode::FAILURE;
168  }
169 
170 
171  // ================================================== //
172  // ===================== COMPUTATION ================ //
173  // ================================================== //
174 
175  ActsTrk::SpacePointCollector backEnd(selectedSpacePoints);
176 
177  Acts::SpacePointContainerConfig spConfig;
178  spConfig.useDetailedDoubleMeasurementInfo = not m_usePixel;
179  // Options
180  Acts::SpacePointContainerOptions spOptions;
181  spOptions.beamPos = Acts::Vector2(beamPos.x(), beamPos.y());
182 
183  Acts::SpacePointContainer<decltype(backEnd), Acts::detail::RefHolder> collect(spConfig, spOptions, backEnd);
184 
185  ATH_MSG_DEBUG("Running Seed Finding ...");
186  time_seedCreation.start();
187  ATH_CHECK( m_seedsTool->createSeeds( ctx,
188  collect,
189  beamPos,
190  bField,
191  *seedPtrs ) );
192  time_seedCreation.stop();
193  ATH_MSG_DEBUG(" \\__ Created " << seedPtrs->size() << " seeds");
194  m_stat[kNSeeds] += seedPtrs->size();
195 
196  // ================================================== //
197  // ================ PARAMS ESTIMATION =============== //
198  // ================================================== //
199 
200  ATH_MSG_DEBUG( "Estimating Track Parameters from seed ..." );
201 
202  // Estimate Track Parameters
203  auto retrieveSurfaceFunction =
204  [this, &detEle] (const ActsTrk::Seed& seed) -> const Acts::Surface&
205  {
206  const xAOD::SpacePoint* sp = this->m_useTopSp ? seed.sp().back() : seed.sp().front();
207  const InDetDD::SiDetectorElement* Element = detEle->getDetectorElement(
208  this->m_useTopSp ? sp->elementIdList().back()
209  : sp->elementIdList().front());
210  const Trk::Surface& atlas_surface = Element->surface();
211  return this->m_ATLASConverterTool->trkSurfaceToActsSurface(atlas_surface);
212  };
213 
214  auto geo_context = m_trackingGeometryTool->getNominalGeometryContext();
215 
216  time_parameterEstimation.start();
217  for (const ActsTrk::Seed* seed : *seedPtrs) {
218  std::optional<Acts::BoundTrackParameters> optTrackParams =
219  m_paramEstimationTool->estimateTrackParameters(ctx,
220  *seed,
221  geo_context.context(),
222  magFieldContext,
223  retrieveSurfaceFunction);
224 
225  if ( optTrackParams.has_value() ) {
226  Acts::BoundTrackParameters *toAdd =
227  new Acts::BoundTrackParameters( optTrackParams.value() );
228  trackParams->push_back( toAdd );
229  }
230  }
231  m_stat[kNSeedsWithoutParam] += (seedPtrs->size() - trackParams->size() );
232  time_parameterEstimation.stop();
233 
234  mon_nSeeds = seedPtrs->size();
235 
236  return StatusCode::SUCCESS;
237  }
238 
239 } // namespace
ActsTrk::SpacePointCollector
Definition: SpacePointCollector.h:20
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ActsTrk::SeedingAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: SeedingAlg.cxx:68
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
ATLASMagneticFieldWrapper
Definition: ATLASMagneticFieldWrapper.h:15
ActsTrk::SeedingAlg::finalize
virtual StatusCode finalize() override
Definition: SeedingAlg.cxx:58
InDetDD::SiDetectorElementCollection
Definition: SiDetectorElementCollection.h:30
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
AtlasFieldCacheCondObj
Definition: AtlasFieldCacheCondObj.h:19
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
ITkSiSpacePointForSeed.h
ActsTrk::SeedingAlg::m_trackingGeometryTool
PublicToolHandle< IActsTrackingGeometryTool > m_trackingGeometryTool
Definition: SeedingAlg.h:55
InDetDD::SolidStateDetectorElementBase::surface
Trk::Surface & surface()
Element Surface.
SG::ReadCondHandle::isValid
bool isValid()
Definition: ReadCondHandle.h:206
ATLASMagneticFieldWrapper.h
xAOD::SpacePoint_v1
Definition: SpacePoint_v1.h:29
ActsTrk::SeedingAlg::kNSeedsWithoutParam
@ kNSeedsWithoutParam
Definition: SeedingAlg.h:77
ActsTrk::SeedingAlg::m_seedKey
SG::WriteHandleKey< ActsTrk::SeedContainer > m_seedKey
Definition: SeedingAlg.h:66
ActsTrk::SeedingAlg::m_usePixel
Gaudi::Property< bool > m_usePixel
Definition: SeedingAlg.h:71
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
python.utils.AtlRunQueryTimer.timer
def timer(name, disabled=False)
Definition: AtlRunQueryTimer.py:86
ATLASMagneticFieldWrapper::makeCache
MagneticFieldProvider::Cache makeCache(const Acts::MagneticFieldContext &mctx) const override
Definition: ATLASMagneticFieldWrapper.h:34
ActsTrk::SeedingAlg::m_seedsTool
ToolHandle< ActsTrk::ISeedingTool > m_seedsTool
Definition: SeedingAlg.h:53
SeedingAlg.h
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
xAOD::SpacePoint_v1::elementIdList
const std::vector< DetectorIDHashType > & elementIdList() const
Returns the IdentifierHash of the spacepoint (corresponds to the detector element IdentifierHash)
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
ActsTrk::SeedingAlg::m_actsTrackParamsKey
SG::WriteHandleKey< ActsTrk::BoundTrackParametersContainer > m_actsTrackParamsKey
Definition: SeedingAlg.h:67
ActsTrk::SeedingAlg::m_fastTracking
Gaudi::Property< bool > m_fastTracking
Definition: SeedingAlg.h:69
SpacePointCollection.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadCondHandle::retrieve
const_pointer_type retrieve()
Definition: ReadCondHandle.h:162
makeTable
TableUtils::StatTable< T > makeTable(const std::array< T, N > &counter, const std::array< std::string, N > &label)
Definition: TableUtils.h:521
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ActsTrk::SeedingAlg::m_monTool
ToolHandle< GenericMonitoringTool > m_monTool
Definition: SeedingAlg.h:57
SiSpacePointForSeed.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
ActsTrk::SeedingAlg::initialize
virtual StatusCode initialize() override
Definition: SeedingAlg.cxx:32
ActsTrk::SeedingAlg::kNSpacepoints
@ kNSpacepoints
Definition: SeedingAlg.h:75
ActsTrk::Seed
Acts::Seed< xAOD::SpacePoint, 3ul > Seed
Definition: Seed.h:12
ActsTrk::SeedingAlg::m_paramEstimationTool
ToolHandle< ActsTrk::ITrackParamsEstimationTool > m_paramEstimationTool
Definition: SeedingAlg.h:54
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
DeMoUpdate.toAdd
bool toAdd
Definition: DeMoUpdate.py:1304
Monitored.h
Header file to be included by clients of the Monitored infrastructure.
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
ActsTrk::SeedingAlg::m_detEleCollKey
SG::ReadCondHandleKey< InDetDD::SiDetectorElementCollection > m_detEleCollKey
Definition: SeedingAlg.h:63
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
InDetDD::SiDetectorElement
Definition: SiDetectorElement.h:109
ActsTrk::SeedingAlg::m_beamSpotKey
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
Definition: SeedingAlg.h:60
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
SiDetectorElementCollection.h
ActsTrk::SeedingAlg::SeedingAlg
SeedingAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: SeedingAlg.cxx:27
SiDetectorElement.h
ActsTrk::SeedingAlg::m_fieldCondObjInputKey
SG::ReadCondHandleKey< AtlasFieldCacheCondObj > m_fieldCondObjInputKey
Definition: SeedingAlg.h:61
python.SystemOfUnits.mm
int mm
Definition: SystemOfUnits.py:83
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
ActsTrk::SeedingAlg::kNSeeds
@ kNSeeds
Definition: SeedingAlg.h:76
ActsTrk::SeedingAlg::m_spacePointKey
SG::ReadHandleKeyArray< xAOD::SpacePointContainer > m_spacePointKey
Definition: SeedingAlg.h:65
xAOD::SpacePointContainer
SpacePointContainer_v1 SpacePointContainer
Define the version of the space point container.
Definition: Event/xAOD/xAODInDetMeasurement/xAODInDetMeasurement/SpacePointContainer.h:14
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATLASMagneticFieldWrapper::getField
Acts::Result< Acts::Vector3 > getField(const Acts::Vector3 &position, Acts::MagneticFieldProvider::Cache &gcache) const override
Definition: ATLASMagneticFieldWrapper.h:39
plotBeamSpotMon.mon
mon
Definition: plotBeamSpotMon.py:67
Trig::FeatureAccessImpl::collect
void collect(const HLT::TriggerElement *te, std::vector< Trig::Feature< T > > &data, const std::string &label, unsigned int condition, const std::string &teName, const HLT::TrigNavStructure *navstructure)
actual feature acceess implementation It has (thanks to the ClassTraits) functionality to flatten con...
Definition: FeatureCollectAthena.h:299
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MuonDetectorBuilderTool.cxx:54
TableUtils.h
Monitored::Scalar
Declare a monitored scalar variable.
Definition: MonitoredScalar.h:34
Trk::Surface
Definition: Tracking/TrkDetDescr/TrkSurfaces/TrkSurfaces/Surface.h:75
ActsTrk::SeedingAlg::m_ATLASConverterTool
ToolHandle< ActsTrk::IActsToTrkConverterTool > m_ATLASConverterTool
Definition: SeedingAlg.h:56
ActsTrk::SeedingAlg::m_useTopSp
Gaudi::Property< bool > m_useTopSp
Definition: SeedingAlg.h:70
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
InDetDD::SiDetectorElementCollection::getDetectorElement
const SiDetectorElement * getDetectorElement(const IdentifierHash &hash) const
Definition: SiDetectorElementCollection.cxx:15
Monitored::Timer
A monitored timer.
Definition: MonitoredTimer.h:32