Skip to content

Commit

Permalink
Add a first version of a PIDHandler
Browse files Browse the repository at this point in the history
Rename the EDM4HEP::utils target to not have a library that is called
libutils.so
  • Loading branch information
tmadlener committed Apr 4, 2024
1 parent a811b33 commit 2c17d83
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 5 deletions.
4 changes: 4 additions & 0 deletions cmake/EDM4HEPConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ include("${CMAKE_CURRENT_LIST_DIR}/EDM4HEPTargets.cmake")
# print the default "Found:" message and check library location
include(FindPackageHandleStandardArgs)
get_property(TEST_EDM4HEP_LIBRARY TARGET EDM4HEP::edm4hep PROPERTY LOCATION)

# For backwards compatibility at least for some time
add_library(EDM4HEP::utils ALIAS EDM4HEP::edm4hepUtils)

find_package_handle_standard_args(EDM4HEP DEFAULT_MSG CMAKE_CURRENT_LIST_FILE TEST_EDM4HEP_LIBRARY)
1 change: 1 addition & 0 deletions test/downstream-project-cmake-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ endif()

add_executable(appUsingEDM4hep main.cxx)
target_link_libraries(appUsingEDM4hep EDM4HEP::edm4hep EDM4HEP::utils edm4dis)
# target_link_libraries(appUsingEDM4hep EDM4HEP::edm4hep EDM4HEP::edm4hepUtils edm4dis)
2 changes: 1 addition & 1 deletion test/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include(Catch)

add_executable(unittests_edm4hep
test_kinematics.cpp test_vector_utils.cpp)
target_link_libraries(unittests_edm4hep edm4hep EDM4HEP::utils Catch2::Catch2 Catch2::Catch2WithMain)
target_link_libraries(unittests_edm4hep edm4hep EDM4HEP::edm4hepUtils Catch2::Catch2 Catch2::Catch2WithMain)

option(SKIP_CATCH_DISCOVERY "Skip the Catch2 test discovery" OFF)

Expand Down
16 changes: 12 additions & 4 deletions utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ target_include_directories(kinematics
target_link_libraries(kinematics PUBLIC INTERFACE ROOT::Core)
target_compile_features(kinematics INTERFACE cxx_std_17)

add_library(utils INTERFACE)
add_library(EDM4HEP::utils ALIAS utils)
target_link_libraries(utils INTERFACE kinematics)
set(utils_sources
src/ParticleIDUtils.cc
)

add_library(edm4hepUtils SHARED ${utils_sources})
add_library(EDM4HEP::edm4hepUtils ALIAS edm4hepUtils)
target_include_directories(edm4hepUtils
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(edm4hepUtils INTERFACE kinematics)
target_link_libraries(edm4hepUtils PUBLIC EDM4HEP::edm4hep kinematics)


set(sources src/dataframe.cc)
Expand All @@ -26,7 +34,7 @@ target_link_libraries(edm4hepRDF PUBLIC edm4hep ROOT::Physics ROOT::ROOTVecOps)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
PATTERN "CMakeLists.txt" EXCLUDE)

install(TARGETS utils kinematics edm4hepRDF
install(TARGETS edm4hepUtils kinematics edm4hepRDF
EXPORT EDM4HEPTargets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
RUNTIME DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
Expand Down
48 changes: 48 additions & 0 deletions utils/include/edm4hep/utils/ParticleIDUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef EDM4HEP_UTILS_PARTICLEIDUTILS_H
#define EDM4HEP_UTILS_PARTICLEIDUTILS_H

#include <edm4hep/ParticleIDCollection.h>
#include <edm4hep/ReconstructedParticle.h>

#include <map>
#include <type_traits>
#include <vector>

namespace edm4hep::utils {

/// Utility class to invert the ParticleID to ReconstructedParticle relation
class PIDHandler {

using MapType = std::multimap<edm4hep::ReconstructedParticle, edm4hep::ParticleID>;

MapType m_recoPidMap{}; ///< The internal map from recos to pids

public:
PIDHandler() = default;
~PIDHandler() = default;
PIDHandler(const PIDHandler&) = default;
PIDHandler& operator=(const PIDHandler&) = default;
PIDHandler(PIDHandler&&) = default;
PIDHandler& operator=(PIDHandler&&) = default;

/// Construct a PIDHandler from an arbitrary number of ParticleIDCollections
template <typename... PIDColls>
static PIDHandler from(const ParticleIDCollection& coll, const PIDColls&... pidColls) {
static_assert((std::is_same_v<PIDColls, edm4hep::ParticleIDCollection> && ...),
"PIDHandler can only be constructed from ParticleIDCollections");
PIDHandler handler{};
handler.addColl(coll);
(handler.addColl(pidColls), ...);
return handler;
}

/// Add the information from one ParticleIDCollection to the handler
void addColl(const edm4hep::ParticleIDCollection& coll);

/// Retrieve all ParticleIDs that are related to the passed
/// RecontstructedParticle
std::vector<edm4hep::ParticleID> getPIDs(const edm4hep::ReconstructedParticle& reco) const;
};
} // namespace edm4hep::utils

#endif // EDM4HEP_UTILS_PARTICLEIDUTILS_H
26 changes: 26 additions & 0 deletions utils/src/ParticleIDUtils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "edm4hep/ReconstructedParticle.h"
#include <edm4hep/utils/ParticleIDUtils.h>

#include <iterator>

namespace edm4hep::utils {

void PIDHandler::addColl(const edm4hep::ParticleIDCollection& coll) {
for (const auto pid : coll) {
m_recoPidMap.emplace(pid.getParticle(), pid);
}
}

std::vector<edm4hep::ParticleID> PIDHandler::getPIDs(const edm4hep::ReconstructedParticle& reco) const {
std::vector<edm4hep::ParticleID> pids;
const auto& [begin, end] = m_recoPidMap.equal_range(reco);
pids.reserve(std::distance(begin, end));

for (auto it = begin; it != end; ++it) {
pids.emplace_back(it->second);
}

return pids;
}

} // namespace edm4hep::utils

0 comments on commit 2c17d83

Please sign in to comment.