diff --git a/cmake/EDM4HEPConfig.cmake.in b/cmake/EDM4HEPConfig.cmake.in index 7bf6d54f2..f08a5535f 100644 --- a/cmake/EDM4HEPConfig.cmake.in +++ b/cmake/EDM4HEPConfig.cmake.in @@ -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) diff --git a/test/downstream-project-cmake-test/CMakeLists.txt b/test/downstream-project-cmake-test/CMakeLists.txt index b2a143b12..d4b552d1f 100644 --- a/test/downstream-project-cmake-test/CMakeLists.txt +++ b/test/downstream-project-cmake-test/CMakeLists.txt @@ -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) diff --git a/test/utils/CMakeLists.txt b/test/utils/CMakeLists.txt index ae6eaf952..c875e4324 100644 --- a/test/utils/CMakeLists.txt +++ b/test/utils/CMakeLists.txt @@ -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) diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 232d0ffce..f5465dc06 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -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 $ + $) +target_link_libraries(edm4hepUtils INTERFACE kinematics) +target_link_libraries(edm4hepUtils PUBLIC EDM4HEP::edm4hep kinematics) set(sources src/dataframe.cc) @@ -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 diff --git a/utils/include/edm4hep/utils/ParticleIDUtils.h b/utils/include/edm4hep/utils/ParticleIDUtils.h new file mode 100644 index 000000000..14f275bd0 --- /dev/null +++ b/utils/include/edm4hep/utils/ParticleIDUtils.h @@ -0,0 +1,48 @@ +#ifndef EDM4HEP_UTILS_PARTICLEIDUTILS_H +#define EDM4HEP_UTILS_PARTICLEIDUTILS_H + +#include +#include + +#include +#include +#include + +namespace edm4hep::utils { + +/// Utility class to invert the ParticleID to ReconstructedParticle relation +class PIDHandler { + + using MapType = std::multimap; + + 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 + static PIDHandler from(const ParticleIDCollection& coll, const PIDColls&... pidColls) { + static_assert((std::is_same_v && ...), + "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 getPIDs(const edm4hep::ReconstructedParticle& reco) const; +}; +} // namespace edm4hep::utils + +#endif // EDM4HEP_UTILS_PARTICLEIDUTILS_H diff --git a/utils/src/ParticleIDUtils.cc b/utils/src/ParticleIDUtils.cc new file mode 100644 index 000000000..28c4ba247 --- /dev/null +++ b/utils/src/ParticleIDUtils.cc @@ -0,0 +1,26 @@ +#include "edm4hep/ReconstructedParticle.h" +#include + +#include + +namespace edm4hep::utils { + +void PIDHandler::addColl(const edm4hep::ParticleIDCollection& coll) { + for (const auto pid : coll) { + m_recoPidMap.emplace(pid.getParticle(), pid); + } +} + +std::vector PIDHandler::getPIDs(const edm4hep::ReconstructedParticle& reco) const { + std::vector 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