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  for (const auto* sp : *collection) {
129  if (m_fastTracking and skipSpacePoint(sp->x()-beamPos.x(), sp->y()-beamPos.y(), sp->z()-beamPos.z()))
130  continue;
131  selectedSpacePoints.push_back( sp );
132  }
133  }
134 
135  ATH_MSG_DEBUG( " \\__ Total input space points: " << selectedSpacePoints.size());
136  m_stat[kNSpacepoints] += selectedSpacePoints.size();
137 
138  // Early Exit in case no space points at this stage
139  if (selectedSpacePoints.empty()) {
140  ATH_MSG_DEBUG("No input space points found, we stop seeding");
141  return StatusCode::SUCCESS;
142  }
143 
144  // ================================================== //
145  // ===================== CONDS ====================== //
146  // ================================================== //
147 
148  // Read the b-field information
150  ATH_CHECK( readHandle.isValid() );
151 
152  const AtlasFieldCacheCondObj* fieldCondObj{ *readHandle };
153  if (fieldCondObj == nullptr) {
154  ATH_MSG_ERROR("Failed to retrieve AtlasFieldCacheCondObj with key " << m_fieldCondObjInputKey.key());
155  return StatusCode::FAILURE;
156  }
157 
158  // Get the magnetic field
159  // Using ACTS classes in order to be sure we are consistent
160  Acts::MagneticFieldContext magFieldContext(fieldCondObj);
161  ATLASMagneticFieldWrapper magneticField;
162  Acts::MagneticFieldProvider::Cache magFieldCache = magneticField.makeCache( magFieldContext );
163  Acts::Vector3 bField = *magneticField.getField( Acts::Vector3(beamPos.x(), beamPos.y(), 0),
164  magFieldCache );
165 
167  ATH_CHECK( detEleHandle.isValid() );
168  const InDetDD::SiDetectorElementCollection* detEle = detEleHandle.retrieve();
169  if ( detEle == nullptr ) {
170  ATH_MSG_FATAL( m_detEleCollKey.fullKey() << " is not available." );
171  return StatusCode::FAILURE;
172  }
173 
174 
175  // ================================================== //
176  // ===================== COMPUTATION ================ //
177  // ================================================== //
178 
179  ATH_MSG_DEBUG("Running Seed Finding ...");
180  time_seedCreation.start();
181  ATH_CHECK( m_seedsTool->createSeeds( ctx,
182  selectedSpacePoints,
183  beamPos,
184  bField,
185  *seedPtrs ) );
186  time_seedCreation.stop();
187  ATH_MSG_DEBUG(" \\__ Created " << seedPtrs->size() << " seeds");
188  m_stat[kNSeeds] += seedPtrs->size();
189 
190  // ================================================== //
191  // ================ PARAMS ESTIMATION =============== //
192  // ================================================== //
193 
194  ATH_MSG_DEBUG( "Estimating Track Parameters from seed ..." );
195 
196  // Estimate Track Parameters
197  auto retrieveSurfaceFunction =
198  [this, &detEle] (const Acts::Seed<xAOD::SpacePoint>& seed) -> const Acts::Surface&
199  {
200  const InDetDD::SiDetectorElement* Element = detEle->getDetectorElement(seed.sp().front()->elementIdList()[0]);
201  const Trk::Surface& atlas_surface = Element->surface();
202  return this->m_ATLASConverterTool->trkSurfaceToActsSurface(atlas_surface);
203  };
204 
205  auto geo_context = m_trackingGeometryTool->getNominalGeometryContext();
206 
207  time_parameterEstimation.start();
208  for (const ActsTrk::Seed* seed : *seedPtrs) {
209  std::optional<Acts::BoundTrackParameters> optTrackParams =
210  m_paramEstimationTool->estimateTrackParameters(ctx,
211  *seed,
212  geo_context.context(),
213  magFieldContext,
214  retrieveSurfaceFunction);
215 
216  if ( optTrackParams.has_value() ) {
217  Acts::BoundTrackParameters *toAdd =
218  new Acts::BoundTrackParameters( optTrackParams.value() );
219  trackParams->push_back( toAdd );
220  }
221  }
222  m_stat[kNSeedsWithoutParam] += (seedPtrs->size() - trackParams->size() );
223  time_parameterEstimation.stop();
224 
225  mon_nSeeds = seedPtrs->size();
226 
227  return StatusCode::SUCCESS;
228  }
229 
230 } // namespace
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
ActsTrk::Seed
Acts::Seed< xAOD::SpacePoint > Seed
Definition: Seed.h:13
InDetDD::SolidStateDetectorElementBase::surface
Trk::Surface & surface()
Element Surface.
SG::ReadCondHandle::isValid
bool isValid()
Definition: ReadCondHandle.h:205
ATLASMagneticFieldWrapper.h
ActsTrk::SeedingAlg::kNSeedsWithoutParam
@ kNSeedsWithoutParam
Definition: SeedingAlg.h:76
ActsTrk::SeedingAlg::m_seedKey
SG::WriteHandleKey< ActsTrk::SeedContainer > m_seedKey
Definition: SeedingAlg.h:66
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
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:269
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
python.web.makeTable
def makeTable(name, ids)
Definition: web.py:28
SG::ReadCondHandle::retrieve
const_pointer_type retrieve()
Definition: ReadCondHandle.h:161
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:74
ActsTrk::SeedingAlg::m_paramEstimationTool
ToolHandle< ActsTrk::ITrackParamsEstimationTool > m_paramEstimationTool
Definition: SeedingAlg.h:54
ActsTrk::SeedingAlg::skipSpacePoint
bool skipSpacePoint(float x, float y, float z) const
Definition: SeedingAlg.h:83
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
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:195
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:75
ActsTrk::SeedingAlg::m_spacePointKey
SG::ReadHandleKeyArray< xAOD::SpacePointContainer > m_spacePointKey
Definition: SeedingAlg.h:65
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
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MuonDetectorBuilderTool.cxx:34
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
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