Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Track parameters lookup estimation examples #3823

Merged
merged 26 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Examples/Algorithms/TrackFinding/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ add_library(
src/TrackParamsEstimationAlgorithm.cpp
src/MuonHoughSeeder.cpp
src/GbtsSeedingAlgorithm.cpp
src/TrackParamsLookupAccumulator.cpp
src/TrackParamsLookupEstimation.cpp
)

target_include_directories(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#pragma once

#include "ActsExamples/TrackFinding/TrackParamsLookupTable.hpp"

namespace ActsExamples {

/// @brief Interface for reading track parameter lookup tables
class ITrackParamsLookupReader {
public:
/// Virtual Destructor
virtual ~ITrackParamsLookupReader() = default;

/// Reader method
///
/// @param path the path to the file to read
virtual Lookup readLookup(const std::string& path) = 0;
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#pragma once

#include "ActsExamples/TrackFinding/TrackParamsLookupTable.hpp"

namespace ActsExamples {

/// @brief Interface for writing track parameter lookup tables
class ITrackParamsLookupWriter {
public:
/// Virtual Destructor
virtual ~ITrackParamsLookupWriter() = default;

/// Writer method
///
/// @param lookup track lookup to write
virtual void writeLookup(const Lookup& lookup) = 0;
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#pragma once

#include "ActsExamples/TrackFinding/TrackParamsLookupTable.hpp"

namespace ActsExamples {

/// @brief Class to accumulate and average track lookup tables
///
/// This class is used to accumulate track parameters in
/// reference layer grids and average them to create a lookup
/// table for track parameter estimation in seeding
class TrackParamsLookupAccumulator {
public:
/// @brief Nested configuration struct
struct Config {
/// Axis generator
LookupAxisGen axisGen;
};

/// @brief Constructor
TrackParamsLookupAccumulator(const Config& config)
: m_cfg(std::move(config)),
m_ipGrid(m_cfg.axisGen()),
m_refGrid(m_cfg.axisGen()) {}

/// @brief Add track parameters to the accumulator
///
/// @param ipTrackParameters the track parameters at the IP
/// @param refTrackParameters the track parameters at the reference layer
/// @param position local position of the track hit on the reference layer
void addTrack(const Acts::CurvilinearTrackParameters& ipTrackParameters,
const Acts::CurvilinearTrackParameters& refTrackParameters,
const Acts::Vector2& position);

/// @brief Finalize the lookup table
///
/// Return the grid with the bin track parameters averaged
LookupGrid finalizeLookup();

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
/// Configuration
Config m_cfg;

/// Mutex for modifying the grid
std::mutex m_writeMutex;

/// Grids to accumulate IP and reference
/// layer track parameters
LookupAccumGrid m_ipGrid;
LookupAccumGrid m_refGrid;
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#pragma once

#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimParticle.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
#include "ActsExamples/Framework/IAlgorithm.hpp"
#include "ActsExamples/TrackFinding/ITrackParamsLookupWriter.hpp"
#include "ActsExamples/TrackFinding/TrackParamsLookupAccumulator.hpp"

namespace ActsExamples {

/// @brief Algorithm to estimate track parameters lookup tables
///
/// This algorithm is used to estimate track parameters lookup tables
/// for track parameter estimation in seeding. The algorithm imposes
/// grids onto the reference tracking layers and accumulates track
/// parameters in the grid bins. The track parameters are then averaged
/// to create a lookup table for track parameter estimation in seeding.
class TrackParamsLookupEstimation : public IAlgorithm {
public:
/// @brief Nested configuration struct
struct Config {
/// Reference tracking layers
std::unordered_map<Acts::GeometryIdentifier, const Acts::Surface*>
refLayers;
/// Binning of the grid to be emposed
/// onto the reference layers
std::pair<std::size_t, std::size_t> bins;
/// Input SimHit container
std::string inputHits = "InputHits";
/// Input SimParticle container
std::string inputParticles = "InputParticles";
/// Track lookup writers
std::vector<std::shared_ptr<ITrackParamsLookupWriter>>
trackLookupGridWriters{};
};

/// @brief Constructor
TrackParamsLookupEstimation(const Config& config, Acts::Logging::Level level);

/// @brief Destructor
~TrackParamsLookupEstimation();

/// @brief The execute method
ProcessCode execute(const AlgorithmContext& ctx) const override;

/// Get readonly access to the config parameters
const Config& config() const { return m_cfg; }

private:
/// Configuration
Config m_cfg;

/// Input data handles
ReadDataHandle<SimParticleContainer> m_inputParticles{this,
"InputSimParticles"};

ReadDataHandle<SimHitContainer> m_inputSimHits{this, "InputSimHits"};

/// Accumulators for the track parameters
std::unordered_map<Acts::GeometryIdentifier,
std::unique_ptr<TrackParamsLookupAccumulator>>
m_accumulators;
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#pragma once

#include "Acts/EventData/TrackParameters.hpp"
#include "Acts/Utilities/Axis.hpp"
#include "Acts/Utilities/Grid.hpp"
#include "Acts/Utilities/GridAxisGenerators.hpp"

namespace ActsExamples {

/// @brief Track parameters lookup table axis used
/// in the track estimation algorithm
using LookupAxis =
Acts::Axis<Acts::AxisType::Equidistant, Acts::AxisBoundaryType::Open>;

/// @brief Track parameters lookup table axis generator
/// used in the track estimation algorithm
using LookupAxisGen = Acts::GridAxisGenerators::EqOpenEqOpen;

/// @brief Grid used to accumulate IP track parameters and
/// reference layer track parameters for a given position
/// in the track estimation algorithm
using LookupAccumGrid =
Acts::Grid<std::vector<Acts::CurvilinearTrackParameters>, LookupAxis,
ssdetlab marked this conversation as resolved.
Show resolved Hide resolved
LookupAxis>;

/// @brief Container for the lookup accumulation grids to
/// handle multiple reference layers in the track estimation
using LookupAccumGridContainer =
std::unordered_map<Acts::GeometryIdentifier, LookupAccumGrid>;

/// @brief IP-reference layer track parameters lookup pair
using LookupPair = std::pair<std::shared_ptr<Acts::CurvilinearTrackParameters>,
std::shared_ptr<Acts::CurvilinearTrackParameters>>;

/// @brief Lookup grid for track parameters estimation
/// in a given layer
using LookupGrid = Acts::Grid<LookupPair, LookupAxis, LookupAxis>;

/// @brief Lookup table for track parameters estimation
/// in the track estimation algorithm
using Lookup = std::unordered_map<Acts::GeometryIdentifier, LookupGrid>;
ssdetlab marked this conversation as resolved.
Show resolved Hide resolved

} // namespace ActsExamples
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include "ActsExamples/TrackFinding/TrackParamsLookupAccumulator.hpp"

#include "Acts/EventData/TrackParameters.hpp"
#include "Acts/Utilities/Grid.hpp"
#include "Acts/Utilities/GridIterator.hpp"

void ActsExamples::TrackParamsLookupAccumulator::addTrack(
const Acts::CurvilinearTrackParameters& ipTrackParameters,
const Acts::CurvilinearTrackParameters& refTrackParameters,
ssdetlab marked this conversation as resolved.
Show resolved Hide resolved
const Acts::Vector2& position) {
// Lock the write mutex
std::lock_guard<std::mutex> lock(m_writeMutex);

// Get the local bins from the position
auto bin = m_ipGrid.localBinsFromPosition(position);

// Add the track parameters to the grid
m_ipGrid.atLocalBins(bin).push_back(ipTrackParameters);
m_refGrid.atLocalBins(bin).push_back(refTrackParameters);
}

ActsExamples::LookupGrid
ActsExamples::TrackParamsLookupAccumulator::finalizeLookup() {
// Average track parameters in a given bin
auto meanTrack =
[](const std::vector<Acts::CurvilinearTrackParameters>& tracks) {
Acts::Vector4 fourPosition = Acts::Vector4::Zero();
Acts::Vector3 direction = Acts::Vector3::Zero();
Acts::ActsScalar qOverP = 0;
for (const auto& track : tracks) {
fourPosition += track.fourPosition();
direction += track.direction();
qOverP += track.qOverP();
}
fourPosition /= tracks.size();
direction /= tracks.size();
qOverP /= tracks.size();

return Acts::CurvilinearTrackParameters(
fourPosition, direction, qOverP, std::nullopt,
tracks.front().particleHypothesis());
ssdetlab marked this conversation as resolved.
Show resolved Hide resolved
};

// Create a lookup grid with the same parameters as
// the accumulation grids
ActsExamples::LookupGrid lookupGrid(m_cfg.axisGen());

// Iterate over the bins and average the track parameters
for (auto it = m_ipGrid.begin(); it != m_ipGrid.end(); ++it) {
auto bin = it.localBinsIndices();
if (m_ipGrid.atLocalBins(bin).empty() ||
m_refGrid.atLocalBins(bin).empty()) {
continue;
}

// Store the IP-reference-position correspondence
lookupGrid.atLocalBins(bin) =
std::make_pair(std::make_shared<Acts::CurvilinearTrackParameters>(
meanTrack(m_ipGrid.atLocalBins(bin))),
std::make_shared<Acts::CurvilinearTrackParameters>(
meanTrack(m_refGrid.atLocalBins(bin))));
}

return lookupGrid;
}
Loading
Loading