Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-15 09:30:18

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026 Derek Anderson
0003 
0004 #pragma once
0005 
0006 #include <spdlog/spdlog.h>
0007 #include <algorithms/algorithm.h>
0008 #include <podio/LinkNavigator.h>
0009 #include <string>
0010 #include <string_view>
0011 #include <type_traits>
0012 
0013 #include "algorithms/interfaces/WithPodConfig.h"
0014 #include "services/log/Log_service.h"
0015 
0016 namespace eicrecon {
0017 
0018 template <class SourceT, class SourceLinkT, class TargetT = SourceT,
0019           class TargetLinkT = SourceLinkT>
0020 using CopyLinksAlgorithm =
0021     algorithms::Algorithm<typename algorithms::Input<const typename SourceT::collection_type,
0022                                                      const typename TargetT::collection_type,
0023                                                      const typename SourceLinkT::collection_type>,
0024                           typename algorithms::Output<typename TargetLinkT::collection_type>>;
0025 
0026 /*! Copy links from one collection to links from
0027  *  another collection.
0028  *
0029  *  Schematically, this operation looks like:
0030  *
0031  *      source --------> target
0032  *        |                |
0033  *      source link      target link
0034  *        |                |
0035  *        v                v
0036  *      link   --------> link
0037  *
0038  *  Here, "source" is the input collection you're copying from,
0039  *  "target" is the the collection you're copying to, and "link"
0040  *  is what you're linking to.
0041  *
0042  *  Algorithm is templated on 2 - 4 types:
0043  *    - `SourceT`, type of source;
0044  *    - `SourceLinkT`, type of source link;
0045  *    - `TargetT`, type of target (defaults to `SourceT`); and
0046  *    - `TargetLinkT`, type of target link.
0047  *
0048  *  For example, if source and target are the same type one
0049  *  would write:
0050  *
0051  *      CopyLinks<Cluster, TrackClusterLink> algo;
0052  *
0053  *  And if they differed, one would write:
0054  *
0055  *      CopyLinks<ProtoCluster, TrackProtoClusterLink,
0056  *                Cluster, TrackClusterLink> algo;
0057  *
0058  *  \note Source and target collections are assumed to be
0059  *    1-to-1, i.e. that the Nth object in the source
0060  *    collection corresponds to the Nth object in the target
0061  *    collection.
0062  *
0063  *  \note Algorithm assumes that links are a different
0064  *    type than source or target.
0065  */
0066 template <typename SourceT, typename SourceLinkT, typename TargetT = SourceT,
0067           typename TargetLinkT = SourceLinkT>
0068 class CopyLinks : public CopyLinksAlgorithm<SourceT, SourceLinkT, TargetT, TargetLinkT>,
0069                   public WithPodConfig<NoConfig> {
0070 
0071 public:
0072   CopyLinks(std::string_view name)
0073       : CopyLinksAlgorithm<SourceT, SourceLinkT, TargetT, TargetLinkT>{
0074             name,
0075             {"inputSourceCollection", "inputTargetCollection", "inputSourceLinks"},
0076             {"outputTargetLinks"},
0077             "Copies links from source collection onto links from target collection"} {}
0078 
0079   void process(
0080       const typename CopyLinksAlgorithm<SourceT, SourceLinkT, TargetT, TargetLinkT>::Input& input,
0081       const typename CopyLinksAlgorithm<SourceT, SourceLinkT, TargetT, TargetLinkT>::Output& output)
0082       const final {
0083 
0084     const auto [in_sources, in_targets, in_source_links] = input;
0085     auto [out_target_links]                              = output;
0086 
0087     // exit if no links in input collection
0088     if (in_source_links->size() == 0) {
0089       this->debug("No links in input link collection.");
0090       return;
0091     }
0092 
0093     // exit if source/target collection sizes
0094     // are different
0095     //   --> 1-to-1 ordering can't be assumed!
0096     if (in_sources->size() != in_targets->size()) {
0097       this->error("Size of source collection ({}) not the same as size of target collection ({})",
0098                   in_sources->size(), in_targets->size());
0099       return;
0100     }
0101 
0102     const auto navigator = podio::LinkNavigator(*in_source_links);
0103     for (std::size_t idx = 0; const auto& source : *in_sources) {
0104       const auto target         = in_targets->at(idx);
0105       const auto source_linkees = navigator.getLinked(source);
0106       for (const auto& [source_linkee, source_weight] : source_linkees) {
0107         auto target_link = out_target_links->create();
0108         target_link.template set<TargetT>(target);
0109         target_link.template set<std::remove_cvref_t<decltype(source_linkee)>>(source_linkee);
0110         target_link.setWeight(source_weight);
0111       }
0112       ++idx;
0113     }
0114   }
0115 };
0116 
0117 } // namespace eicrecon