From fbeb64531d94d59c6647bb80387c04e614361dff Mon Sep 17 00:00:00 2001 From: Juraj Smiesko Date: Mon, 18 Sep 2023 17:41:55 +0200 Subject: [PATCH] Implementing selection by type for Reconstructed particles --- .../FCCAnalyses/ReconstructedParticle.h | 16 ++++++ .../dataframe/src/ReconstructedParticle.cc | 41 ++++++++++++++ tests/unittest/CMakeLists.txt | 6 ++- tests/unittest/ReconstructedParticle.cpp | 54 +++++++++++++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 tests/unittest/ReconstructedParticle.cpp diff --git a/analyzers/dataframe/FCCAnalyses/ReconstructedParticle.h b/analyzers/dataframe/FCCAnalyses/ReconstructedParticle.h index 8236e4100f..e306d83d6f 100644 --- a/analyzers/dataframe/FCCAnalyses/ReconstructedParticle.h +++ b/analyzers/dataframe/FCCAnalyses/ReconstructedParticle.h @@ -35,6 +35,22 @@ namespace ReconstructedParticle{ float operator() (ROOT::VecOps::RVec in) ; }; + /// select ReconstructedParticles by type + /// Note: type might not correspond to PDG ID + struct sel_type { + sel_type(const int type); + const int m_type; + ROOT::VecOps::RVec operator() (ROOT::VecOps::RVec in); + }; + + /// select ReconstructedParticles by type absolut value + /// Note: type might not correspond to PDG ID + struct sel_absType { + sel_absType(const int type); + const int m_type; + ROOT::VecOps::RVec operator() (ROOT::VecOps::RVec in); + }; + /// select ReconstructedParticles with transverse momentum greater than a minimum value [GeV] struct sel_pt { sel_pt(float arg_min_pt); diff --git a/analyzers/dataframe/src/ReconstructedParticle.cc b/analyzers/dataframe/src/ReconstructedParticle.cc index fd34a17f23..b010689a20 100644 --- a/analyzers/dataframe/src/ReconstructedParticle.cc +++ b/analyzers/dataframe/src/ReconstructedParticle.cc @@ -1,10 +1,51 @@ #include "FCCAnalyses/ReconstructedParticle.h" + +// std #include +#include +#include namespace FCCAnalyses{ namespace ReconstructedParticle{ +/// sel_type +sel_type::sel_type(const int type) : m_type(type) {} + +ROOT::VecOps::RVec +sel_type::operator() (ROOT::VecOps::RVec in) { + ROOT::VecOps::RVec result; + result.reserve(in.size()); + for (size_t i = 0; i < in.size(); ++i) { + if (in[i].type == m_type) { + result.emplace_back(in[i]); + } + } + return result; +} + + +/// sel_absType +sel_absType::sel_absType(const int type) : m_type(type) { + if (m_type < 0) { + throw std::invalid_argument("ReconstructedParticle::sel_absType: Received negative value!"); + } +} + +ROOT::VecOps::RVec +sel_absType::operator() (ROOT::VecOps::RVec in) { + ROOT::VecOps::RVec result; + result.reserve(in.size()); + for (size_t i = 0; i < in.size(); ++i) { + if (std::abs(in[i].type) == m_type) { + result.emplace_back(in[i]); + } + } + return result; +} + + +/// sel_pt sel_pt::sel_pt(float arg_min_pt) : m_min_pt(arg_min_pt) {}; ROOT::VecOps::RVec sel_pt::operator() (ROOT::VecOps::RVec in) { ROOT::VecOps::RVec result; diff --git a/tests/unittest/CMakeLists.txt b/tests/unittest/CMakeLists.txt index e458f7f3b0..485ff7e46b 100644 --- a/tests/unittest/CMakeLists.txt +++ b/tests/unittest/CMakeLists.txt @@ -4,7 +4,11 @@ find_catch_instance() # list of labels that we want to ignore set(filter_tests "") -add_executable(unittest unittest.cpp myutils.cpp algorithms.cpp) +add_executable(unittest unittest.cpp + myutils.cpp + algorithms.cpp + ReconstructedParticle.cpp +) target_link_libraries(unittest PUBLIC FCCAnalyses gfortran PRIVATE Catch2::Catch2WithMain) target_include_directories(unittest PUBLIC ${VDT_INCLUDE_DIR}) target_compile_definitions(unittest PUBLIC "-DTEST_INPUT_DATA_DIR=${TEST_INPUT_DATA_DIR}") diff --git a/tests/unittest/ReconstructedParticle.cpp b/tests/unittest/ReconstructedParticle.cpp new file mode 100644 index 0000000000..a649c3cb5f --- /dev/null +++ b/tests/unittest/ReconstructedParticle.cpp @@ -0,0 +1,54 @@ +#include "FCCAnalyses/ReconstructedParticle.h" + +// Catch2 +#include "catch2/catch_test_macros.hpp" +#include + + +TEST_CASE("sel_type", "[ReconstructedParticle]") { + ROOT::VecOps::RVec pVec; + edm4hep::ReconstructedParticleData p1; + p1.type = 11; + pVec.push_back(p1); + edm4hep::ReconstructedParticleData p2; + p2.type = 13; + pVec.push_back(p2); + edm4hep::ReconstructedParticleData p3; + p3.type = -11; + pVec.push_back(p3); + edm4hep::ReconstructedParticleData p4; + p4.type = -13; + pVec.push_back(p4); + FCCAnalyses::ReconstructedParticle::sel_type selType{11}; + auto res = selType(pVec); + REQUIRE(res.size() == 1); + REQUIRE(res[0].type == 11); +} + + +TEST_CASE("sel_absType", "[ReconstructedParticle]") { + ROOT::VecOps::RVec pVec; + edm4hep::ReconstructedParticleData p1; + p1.type = 11; + pVec.push_back(p1); + edm4hep::ReconstructedParticleData p2; + p2.type = 13; + pVec.push_back(p2); + edm4hep::ReconstructedParticleData p3; + p3.type = -11; + pVec.push_back(p3); + edm4hep::ReconstructedParticleData p4; + p4.type = -13; + pVec.push_back(p4); + FCCAnalyses::ReconstructedParticle::sel_absType selAbsType{11}; + auto res = selAbsType(pVec); + REQUIRE(res.size() == 2); + REQUIRE(res[0].type == 11); + REQUIRE(res[1].type == -11); +} + + +TEST_CASE("sel_absType__neg_type", "[ReconstructedParticle]") { + REQUIRE_THROWS_AS(FCCAnalyses::ReconstructedParticle::sel_absType(-17), + std::invalid_argument); +}