Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 07:55:41

0001 // Copyright 2024, Simon Gardner
0002 // Subject to the terms in the LICENSE file found in the top-level directory.
0003 //
0004 #pragma once
0005 
0006 #include <algorithms/geo.h>
0007 
0008 namespace eicrecon {
0009 
0010 // ----------------------------------------------------------------------------
0011 // Functor to split collection based on a range of values
0012 // ----------------------------------------------------------------------------
0013 template <auto MemberFunctionPtr> class RangeSplit {
0014 public:
0015   RangeSplit(std::vector<std::pair<double, double>> ranges) : m_ranges(ranges){};
0016 
0017   template <typename T> std::vector<int> operator()(T& instance) const {
0018     std::vector<int> ids;
0019     //Check if requested value is within the ranges
0020     for (std::size_t i = 0; i < m_ranges.size(); i++) {
0021       if ((instance.*MemberFunctionPtr)() > m_ranges[i].first &&
0022           (instance.*MemberFunctionPtr)() < m_ranges[i].second) {
0023         ids.push_back(i);
0024       }
0025     }
0026     return ids;
0027   }
0028 
0029 private:
0030   std::vector<std::pair<double, double>> m_ranges;
0031 };
0032 
0033 // ----------------------------------------------------------------------------
0034 // Functor to split collection based on geometry
0035 // ----------------------------------------------------------------------------
0036 class GeometrySplit {
0037 public:
0038   GeometrySplit(std::vector<std::vector<long int>> ids, std::string readout,
0039                 std::vector<std::string> divisions)
0040       : m_ids(ids)
0041       , m_divisions(divisions)
0042       , m_readout(readout)
0043       , is_init(std::make_shared<std::once_flag>())
0044       , m_id_dec(std::make_shared<dd4hep::DDSegmentation::BitFieldCoder*>())
0045       , m_div_ids(std::make_shared<std::vector<std::size_t>>()){};
0046 
0047   template <typename T> std::vector<int> operator()(T& instance) const {
0048 
0049     // Initialize the decoder and division ids on the first function call
0050     std::call_once(*is_init, &GeometrySplit::init, this);
0051 
0052     //Check which detector division to put the hit into
0053     auto cellID = instance.getCellID();
0054     std::vector<long int> det_ids;
0055     for (auto d : *m_div_ids) {
0056       det_ids.push_back((*m_id_dec)->get(cellID, d));
0057     }
0058 
0059     auto index = std::find(m_ids.begin(), m_ids.end(), det_ids);
0060 
0061     std::vector<int> ids;
0062     if (index != m_ids.end()) {
0063       ids.push_back(std::distance(m_ids.begin(), index));
0064     }
0065     return ids;
0066   }
0067 
0068 private:
0069   void init() const {
0070     *m_id_dec = algorithms::GeoSvc::instance().detector()->readout(m_readout).idSpec().decoder();
0071     for (auto d : m_divisions) {
0072       m_div_ids->push_back((*m_id_dec)->index(d));
0073     }
0074   }
0075 
0076   std::vector<std::vector<long int>> m_ids;
0077   std::vector<std::string> m_divisions;
0078   std::string m_readout;
0079 
0080   std::shared_ptr<std::once_flag> is_init;
0081   std::shared_ptr<dd4hep::DDSegmentation::BitFieldCoder*> m_id_dec;
0082   std::shared_ptr<std::vector<std::size_t>> m_div_ids;
0083 };
0084 
0085 // ----------------------------------------------------------------------------
0086 // Functor to split collection based on any number of collection values
0087 // ----------------------------------------------------------------------------
0088 template <auto... MemberFunctionPtrs> class ValueSplit {
0089 public:
0090   ValueSplit(std::vector<std::vector<int>> ids) : m_ids(ids){};
0091 
0092   template <typename T> std::vector<int> operator()(T& instance) const {
0093     std::vector<int> ids;
0094     // Check if requested value matches any configuration combinations
0095     std::vector<int> values;
0096     (values.push_back((instance.*MemberFunctionPtrs)()), ...);
0097     auto index = std::find(m_ids.begin(), m_ids.end(), values);
0098     if (index != m_ids.end()) {
0099       ids.push_back(std::distance(m_ids.begin(), index));
0100     }
0101     return ids;
0102   }
0103 
0104 private:
0105   std::vector<std::vector<int>> m_ids;
0106 };
0107 
0108 } // namespace eicrecon