Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-21 09:06:06

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2024 Simon Gardner
0003 
0004 #pragma once
0005 
0006 #include <spdlog/spdlog.h>
0007 #include <algorithms/algorithm.h>
0008 #include <string>
0009 #include <string_view>
0010 
0011 #include "services/log/Log_service.h"
0012 #include "algorithms/interfaces/WithPodConfig.h"
0013 
0014 namespace eicrecon {
0015 
0016 template <class ToFilterObjectT, class FilterByObjectT>
0017 using FilterMatchingAlgorithm =
0018     algorithms::Algorithm<typename algorithms::Input<typename ToFilterObjectT::collection_type,
0019                                                      typename FilterByObjectT::collection_type>,
0020                           typename algorithms::Output<typename ToFilterObjectT::collection_type,
0021                                                       typename ToFilterObjectT::collection_type>>;
0022 
0023 /// Filters a collection by the members of another collection
0024 /// The first collection is divided up into two collections where its elements either passed the filter or not
0025 /// The second collection provides a filter
0026 /// Functions need to be provided along with the collections to form the link between the data types
0027 /// These functions are envisioned to link the objectIDs of the collection/associations but could be anything
0028 template <typename ToFilterObjectT, auto ToFilterFunction, typename FilterByObjectT,
0029           auto FilterByFunction>
0030 class FilterMatching : public FilterMatchingAlgorithm<ToFilterObjectT, FilterByObjectT>,
0031                        public WithPodConfig<NoConfig> {
0032 
0033 public:
0034   FilterMatching(std::string_view name)
0035       : FilterMatchingAlgorithm<ToFilterObjectT, FilterByObjectT>{
0036             name,
0037             {"inputCollection", "inputMatchedCollection"},
0038             {"outputMatchedAssociations", "outputUnmatchedAssociations"},
0039             "Filter by matching to a collection"} {};
0040 
0041   void init() final {};
0042 
0043   void
0044   process(const typename FilterMatchingAlgorithm<ToFilterObjectT, FilterByObjectT>::Input& input,
0045           const typename FilterMatchingAlgorithm<ToFilterObjectT, ToFilterObjectT>::Output& output)
0046       const final {
0047 
0048     const auto [toFilterEntries, filterByEntries] = input;
0049     auto [is_matched, is_not_matched]             = output;
0050 
0051     is_matched->setSubsetCollection();
0052     is_not_matched->setSubsetCollection();
0053 
0054     for (const auto& matchedEntry : *toFilterEntries) {
0055 
0056       auto ref_value = ToFilterFunction(&matchedEntry);
0057 
0058       bool found_match = false;
0059 
0060       // Tries to find the association in the entries
0061       for (const auto& entry : *filterByEntries) {
0062         auto other_value = FilterByFunction(&entry);
0063         if (other_value == ref_value) {
0064           is_matched->push_back(matchedEntry);
0065           found_match = true;
0066           break;
0067         }
0068       }
0069 
0070       if (!found_match) {
0071         is_not_matched->push_back(matchedEntry);
0072       }
0073     }
0074   };
0075 };
0076 
0077 } // namespace eicrecon