Skip to content

Commit

Permalink
Merge branch 'main' into nopayloadclient
Browse files Browse the repository at this point in the history
  • Loading branch information
wdconinc authored May 8, 2024
2 parents bb79934 + 9fceb22 commit f67585a
Show file tree
Hide file tree
Showing 51 changed files with 895 additions and 355 deletions.
1 change: 1 addition & 0 deletions .github/iwyu.imp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Boost
{ ref: '/usr/local/share/include-what-you-use/boost-all.imp' },
{ include: ['@<boost/histogram/.*\.hpp>', private, '<boost/histogram.hpp>', public] },

# libc++
{ ref: '/usr/local/share/include-what-you-use/libcxx.imp' },
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/linux-eic-shell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ jobs:
platform-release: "${{ env.platform }}:${{ matrix.release }}"
run: |
export LD_LIBRARY_PATH=$PWD/install/lib:$LD_LIBRARY_PATH
export JANA_PLUGIN_PATH=$PWD/install/lib/EICrecon/plugins${JANA_PLUGIN_PATH:+:${JANA_PLUGIN_PATH}}
ctest --test-dir build -V
- name: Compress install directory
run: tar -caf install.tar.zst install/
Expand Down Expand Up @@ -267,8 +268,9 @@ jobs:
llvm-profdata-15 merge -sparse src/tests/algorithms_test/algorithms_test.profraw \
-o src/tests/algorithms_test/algorithms_test.profdata
LIB_PATHS=()
for LIB_NAME in algorithms_calorimetry algorithms_digi algorithms_pid algorithms_reco algorithms_tracking; do
LIB_PATH="$(find src/ -type f -name "*$LIB_NAME.so")"
for LIB_PATH in $(find src/ -type f -name "libalgorithms_*.so"); do
LIB_NAME="$(basename ${LIB_PATH%%.so})"
LIB_NAME="${LIB_NAME##lib}"
LIB_PATHS+=("$LIB_PATH")
llvm-cov-15 report $LIB_PATH \
-instr-profile=src/tests/algorithms_test/algorithms_test.profdata \
Expand Down Expand Up @@ -554,7 +556,6 @@ jobs:
detector_config: [craterlake]
test_plugins:
- geometry_navigation_test
- reco_test
- tracking_test
- track_propagation_test
steps:
Expand Down
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#
# cmake-format: on

cmake_minimum_required(VERSION 3.18)
cmake_minimum_required(VERSION 3.24)

project(EICrecon)

Expand Down Expand Up @@ -65,6 +65,13 @@ endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Infrastructure to support use of --no-as-needed
include(CheckLinkerFlag)
check_linker_flag(CXX "LINKER:--no-as-needed" CXX_LINKER_HAS_no_as_needed)
set(CMAKE_CXX_LINK_LIBRARY_USING_NO_AS_NEEDED_SUPPORTED TRUE)
set(CMAKE_CXX_LINK_LIBRARY_USING_NO_AS_NEEDED
"LINKER:--push-state,--no-as-needed" "<LINK_ITEM>" "LINKER:--pop-state")

# Export compile commands as json for run-clang-tidy
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

Expand Down
25 changes: 16 additions & 9 deletions cmake/jana_plugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ macro(plugin_add _name)
PUBLIC $<BUILD_INTERFACE:${EICRECON_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>)
target_include_directories(${_name}_plugin SYSTEM
PUBLIC ${JANA_INCLUDE_DIR})
target_include_directories(${_name}_plugin SYSTEM
PUBLIC ${ROOT_INCLUDE_DIRS})
PUBLIC ${JANA_INCLUDE_DIR} ${ROOT_INCLUDE_DIRS})
set_target_properties(
${_name}_plugin
PROPERTIES PREFIX ""
Expand Down Expand Up @@ -91,11 +89,14 @@ macro(plugin_add _name)
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>)
target_include_directories(${_name}_library SYSTEM
PUBLIC ${JANA_INCLUDE_DIR})
target_link_libraries(${_name}_library ${JANA_LIB} podio::podio
podio::podioRootIO spdlog::spdlog)
target_link_libraries(${_name}_library ${JANA_LIB} podio::podio
podio::podioRootIO fmt::fmt)
target_link_libraries(${_name}_library Microsoft.GSL::GSL)
target_link_libraries(
${_name}_library
${JANA_LIB}
podio::podio
podio::podioRootIO
spdlog::spdlog
fmt::fmt
Microsoft.GSL::GSL)

# Install library
install(
Expand All @@ -105,7 +106,13 @@ macro(plugin_add _name)
endif(${_name}_WITH_LIBRARY)

if(${_name}_WITH_LIBRARY AND ${_name}_WITH_PLUGIN)
target_link_libraries(${_name}_plugin ${_name}_library)
# Ensure that whenever a plugin is loaded its library is loaded as well
if(CXX_LINKER_HAS_no_as_needed)
target_link_libraries(${_name}_plugin
$<LINK_LIBRARY:NO_AS_NEEDED,${_name}_library>)
else()
target_link_libraries(${_name}_plugin ${_name}_library>)
endif()
endif()
endmacro()

Expand Down
1 change: 1 addition & 0 deletions src/algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_subdirectory(interfaces)
add_subdirectory(calorimetry)
add_subdirectory(tracking)
add_subdirectory(pid)
add_subdirectory(pid_lut)
add_subdirectory(digi)
add_subdirectory(reco)
add_subdirectory(fardetectors)
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/calorimetry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugin_add(${PLUGIN_NAME} WITH_SHARED_LIBRARY WITHOUT_PLUGIN)
plugin_glob_all(${PLUGIN_NAME})

# Find dependencies
plugin_add_algorithms(${PLUGIN_NAME} algocalorimetry)
plugin_add_algorithms(${PLUGIN_NAME})
plugin_add_dd4hep(${PLUGIN_NAME})
plugin_add_event_model(${PLUGIN_NAME})
plugin_add_eigen3(${PLUGIN_NAME})
14 changes: 14 additions & 0 deletions src/algorithms/pid_lut/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(PLUGIN_NAME "algorithms_pid_lut")

# Function creates ${PLUGIN_NAME}_plugin and ${PLUGIN_NAME}_library targets
# Setting default includes, libraries and installation paths
plugin_add(${PLUGIN_NAME} WITH_SHARED_LIBRARY WITHOUT_PLUGIN)

# The macro grabs sources as *.cc *.cpp *.c and headers as *.h *.hh *.hpp Then
# correctly sets sources for ${_name}_plugin and ${_name}_library targets Adds
# headers to the correct installation directory
plugin_glob_all(${PLUGIN_NAME})

# Find dependencies
plugin_add_algorithms(${PLUGIN_NAME})
plugin_add_event_model(${PLUGIN_NAME})
142 changes: 142 additions & 0 deletions src/algorithms/pid_lut/PIDLookup.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024, Nathan Brei, Dmitry Kalinkin

#include <algorithms/service.h>
#include <edm4eic/MCRecoParticleAssociationCollection.h>
#include <edm4eic/ReconstructedParticleCollection.h>
#include <edm4hep/MCParticleCollection.h>
#include <edm4hep/utils/vector_utils.h>
#include <fmt/core.h>
#include <podio/ObjectID.h>
#include <cmath>
#include <gsl/pointers>
#include <stdexcept>

#include "algorithms/pid_lut/PIDLookup.h"
#include "algorithms/pid_lut/PIDLookupConfig.h"
#include "services/pid_lut/PIDLookupTableSvc.h"

namespace eicrecon {

void PIDLookup::init() {
auto& serviceSvc = algorithms::ServiceSvc::instance();
auto lut_svc = serviceSvc.service<PIDLookupTableSvc>("PIDLookupTableSvc");

m_lut = lut_svc->load(m_cfg.filename, {
.pdg_values=m_cfg.pdg_values,
.charge_values=m_cfg.charge_values,
.momentum_edges=m_cfg.momentum_edges,
.polar_edges=m_cfg.polar_edges,
.azimuthal_binning=m_cfg.azimuthal_binning,
.azimuthal_bin_centers_in_lut=m_cfg.azimuthal_bin_centers_in_lut,
.momentum_bin_centers_in_lut=m_cfg.momentum_bin_centers_in_lut,
.polar_bin_centers_in_lut=m_cfg.polar_bin_centers_in_lut,
.skip_legacy_header=m_cfg.skip_legacy_header,
.use_radians=m_cfg.use_radians,
.missing_electron_prob=m_cfg.missing_electron_prob,
});
if (m_lut == nullptr) {
throw std::runtime_error("LUT not available");
}
}

void PIDLookup::process(const Input& input, const Output& output) const {
const auto [recoparts_in, partassocs_in] = input;
auto [recoparts_out, partassocs_out, partids_out] = output;

for (const auto& recopart_without_pid : *recoparts_in) {
edm4hep::MCParticle mcpart;
auto recopart = recopart_without_pid.clone();

// Find MCParticle from associations and propagate the relevant ones further
bool assoc_found = false;
for (auto assoc_in : *partassocs_in) {
if (assoc_in.getRec() == recopart_without_pid) {
if (assoc_found) {
warning("Found a duplicate association for ReconstructedParticle at index {}", recopart_without_pid.getObjectID().index);
warning("The previous MCParticle was at {} and the duplicate is at {}", mcpart.getObjectID().index, assoc_in.getSim().getObjectID().index);
}
assoc_found = true;
mcpart = assoc_in.getSim();
auto assoc_out = assoc_in.clone();
assoc_out.setRec(recopart);
partassocs_out->push_back(assoc_out);
}
}
if (not assoc_found) {
recoparts_out->push_back(recopart);
continue;
}

int true_pdg = mcpart.getPDG();
int true_charge = mcpart.getCharge();
int charge = recopart.getCharge();
double momentum = edm4hep::utils::magnitude(recopart.getMomentum());

double theta = edm4hep::utils::anglePolar(recopart.getMomentum()) / M_PI * 180.;
double phi = edm4hep::utils::angleAzimuthal(recopart.getMomentum()) / M_PI * 180.;

trace("lookup for true_pdg={}, true_charge={}, momentum={:.2f} GeV, polar={:.2f}, aziumthal={:.2f}",
true_pdg, true_charge, momentum, theta, phi);
auto entry = m_lut->Lookup(true_pdg, true_charge, momentum, theta, phi);

int identified_pdg = 0; // unknown

if ((entry != nullptr) && ((entry->prob_electron != 0.) || (entry->prob_pion != 0.) || (entry->prob_kaon != 0.) || (entry->prob_electron != 0.))) {
double random_unit_interval = m_dist(m_gen);

trace("entry with e:pi:K:P={}:{}:{}:{}", entry->prob_electron, entry->prob_pion, entry->prob_kaon, entry->prob_proton);

recopart.addToParticleIDs(partids_out->create(
m_cfg.system, // std::int32_t type
std::copysign(11, charge), // std::int32_t PDG
0, // std::int32_t algorithmType
static_cast<float>(entry->prob_electron) // float likelihood
));
recopart.addToParticleIDs(partids_out->create(
m_cfg.system, // std::int32_t type
std::copysign(211, charge), // std::int32_t PDG
0, // std::int32_t algorithmType
static_cast<float>(entry->prob_pion) // float likelihood
));
recopart.addToParticleIDs(partids_out->create(
m_cfg.system, // std::int32_t type
std::copysign(321, charge), // std::int32_t PDG
0, // std::int32_t algorithmType
static_cast<float>(entry->prob_kaon) // float likelihood
));
recopart.addToParticleIDs(partids_out->create(
m_cfg.system, // std::int32_t type
std::copysign(2212, charge), // std::int32_t PDG
0, // std::int32_t algorithmType
static_cast<float>(entry->prob_proton) // float likelihood
));

if (random_unit_interval < entry->prob_electron) {
identified_pdg = 11; // electron
recopart.setParticleIDUsed((*partids_out)[partids_out->size() - 4]);
} else if (random_unit_interval < (entry->prob_electron + entry->prob_pion)) {
identified_pdg = 211; // pion
recopart.setParticleIDUsed((*partids_out)[partids_out->size() - 3]);
} else if (random_unit_interval <
(entry->prob_electron + entry->prob_pion + entry->prob_kaon)) {
identified_pdg = 321; // kaon
recopart.setParticleIDUsed((*partids_out)[partids_out->size() - 2]);
} else if (random_unit_interval < (entry->prob_electron + entry->prob_pion +
entry->prob_kaon + entry->prob_electron)) {
identified_pdg = 2212; // proton
recopart.setParticleIDUsed((*partids_out)[partids_out->size() - 1]);
}
}

recopart.setPDG(std::copysign(identified_pdg, charge));

if (identified_pdg != 0) {
trace("randomized PDG is {}", recopart.getPDG());
}

recoparts_out->push_back(recopart);
}
}

} // namespace eicrecon
44 changes: 44 additions & 0 deletions src/algorithms/pid_lut/PIDLookup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024, Nathan Brei, Dmitry Kalinkin

#include <algorithms/algorithm.h>
#include <edm4eic/MCRecoParticleAssociationCollection.h>
#include <edm4eic/ReconstructedParticleCollection.h>
#include <edm4hep/ParticleIDCollection.h>
#include <random>
#include <string>
#include <string_view>

#include "PIDLookupConfig.h"
#include "algorithms/interfaces/WithPodConfig.h"
#include "services/pid_lut/PIDLookupTable.h"

namespace eicrecon {

using PIDLookupAlgorithm =
algorithms::Algorithm<algorithms::Input<edm4eic::ReconstructedParticleCollection,
edm4eic::MCRecoParticleAssociationCollection>,
algorithms::Output<edm4eic::ReconstructedParticleCollection,
edm4eic::MCRecoParticleAssociationCollection,
edm4hep::ParticleIDCollection>>;

class PIDLookup : public PIDLookupAlgorithm, public WithPodConfig<PIDLookupConfig> {

public:
PIDLookup(std::string_view name)
: PIDLookupAlgorithm{name,
{"inputParticlesCollection", "inputParticleAssociationsCollection"},
{"outputParticlesCollection", "outputParticleAssociationsCollection",
"outputParticleIDCollection"},
""} {}

void init() final;
void process(const Input&, const Output&) const final;

private:
mutable std::mt19937 m_gen{};
mutable std::uniform_real_distribution<double> m_dist{0, 1};
const PIDLookupTable* m_lut;
};

} // namespace eicrecon
26 changes: 26 additions & 0 deletions src/algorithms/pid_lut/PIDLookupConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (C) 2024, Nathan Brei, Dmitry Kalinkin

#pragma once

#include <vector>

namespace eicrecon {

struct PIDLookupConfig {
std::string filename;
int system;
std::vector<int> pdg_values;
std::vector<int> charge_values;
std::vector<double> momentum_edges;
std::vector<double> polar_edges;
std::vector<double> azimuthal_binning;
bool azimuthal_bin_centers_in_lut {false};
bool momentum_bin_centers_in_lut {false};
bool polar_bin_centers_in_lut {false};
bool skip_legacy_header {false};
bool use_radians {false};
bool missing_electron_prob {false};
};

} // namespace eicrecon
3 changes: 0 additions & 3 deletions src/algorithms/tracking/TracksToParticles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,13 @@ namespace eicrecon {
}
auto rec_part = parts->create();
rec_part.addToTracks(track);
int32_t best_pid = 0;
auto referencePoint = rec_part.getReferencePoint();
// float time = 0;
float mass = 0;
if (best_match >= 0) {
trace("Best match is found and is: {}", best_match);
mc_prt_is_consumed[best_match] = true;
const auto &best_mc_part = (*mc_particles)[best_match];
best_pid = best_mc_part.getPDG();
referencePoint = {
static_cast<float>(best_mc_part.getVertex().x),
static_cast<float>(best_mc_part.getVertex().y),
Expand All @@ -151,7 +149,6 @@ namespace eicrecon {
rec_part.setCharge(charge_rec);
rec_part.setMass(mass);
rec_part.setGoodnessOfPID(0); // assume no PID until proven otherwise
rec_part.setPDG(best_pid);
// rec_part.covMatrix() // @TODO: covariance matrix on 4-momentum

// Also write MC <--> truth particle association if match was found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
void femc_studiesProcessor::Init() {
std::string plugin_name = ("femc_studies");

// InitLogger(plugin_name);
// ===============================================================================================
// Get JANA application and seup general variables
// ===============================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
void lfhcal_studiesProcessor::Init() {
std::string plugin_name = ("lfhcal_studies");

// InitLogger(plugin_name);
// ===============================================================================================
// Get JANA application and seup general variables
// ===============================================================================================
Expand Down
Loading

0 comments on commit f67585a

Please sign in to comment.