Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-02 08:25:05

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2026 ePIC Collaboration
0003 
0004 #pragma once
0005 
0006 #include <edm4hep/CaloHitContribution.h>
0007 #include <edm4hep/MCParticle.h>
0008 #include <gsl/pointers>
0009 #include <podio/LinkNavigator.h>
0010 #include <optional>
0011 #include <utility>
0012 
0013 namespace eicrecon::truth {
0014 
0015 template <typename LinkCollectionT> class EventLinkNavigator {
0016 public:
0017   explicit EventLinkNavigator(const LinkCollectionT* links)
0018       : m_enabled(links != nullptr && !links->empty()) {
0019     if (m_enabled) {
0020       m_nav.emplace(*links);
0021     }
0022   }
0023 
0024   bool enabled() const { return m_enabled; }
0025   template <typename SrcT> auto linked(const SrcT& src) const {
0026     using ReturnT = decltype(std::declval<podio::LinkNavigator<LinkCollectionT>>().getLinked(src));
0027     return m_enabled ? m_nav->getLinked(src) : ReturnT{};
0028   }
0029 
0030 private:
0031   bool m_enabled = false;
0032   std::optional<podio::LinkNavigator<LinkCollectionT>> m_nav;
0033 };
0034 
0035 template <typename RecT, typename SimT, typename LinkCollT, typename AssocCollT>
0036 inline void addWeightedRelation(const RecT& rec, const SimT& sim, float weight,
0037                                 gsl::not_null<LinkCollT*> links,
0038                                 gsl::not_null<AssocCollT*> assocs) {
0039   auto link = links->create();
0040   link.setFrom(rec);
0041   link.setTo(sim);
0042   link.setWeight(weight);
0043 
0044   auto assoc = assocs->create();
0045   assoc.setRec(rec);
0046   assoc.setSim(sim);
0047   assoc.setWeight(weight);
0048 }
0049 
0050 inline edm4hep::MCParticle primaryFrom(const edm4hep::CaloHitContribution& contrib) {
0051   edm4hep::MCParticle primary = contrib.getParticle();
0052   while (primary.parents_size() > 0) {
0053     if (primary.getGeneratorStatus() != 0) {
0054       break;
0055     }
0056     primary = primary.getParents(0);
0057   }
0058   return primary;
0059 }
0060 
0061 } // namespace eicrecon::truth