Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-06-17 07:06:19

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