Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:18

0001 #ifndef CommonToolsUtilsExpressionEvaluatorTemplates_H
0002 #define CommonToolsUtilsExpressionEvaluatorTemplates_H
0003 #include <vector>
0004 #include <algorithm>
0005 #include <numeric>
0006 #include <limits>
0007 #include <memory>
0008 #include <tuple>
0009 
0010 namespace reco {
0011 
0012   template <typename Ret, typename... Args>
0013   struct genericExpression {
0014     virtual Ret operator()(Args...) const = 0;
0015     virtual ~genericExpression(){};
0016   };
0017 
0018   template <typename Object>
0019   struct CutOnObject {
0020     virtual bool eval(Object const&) const = 0;
0021     virtual ~CutOnObject(){};
0022   };
0023 
0024   template <typename Object>
0025   struct ValueOnObject {
0026     virtual double eval(Object const&) const = 0;
0027     virtual ~ValueOnObject(){};
0028   };
0029 
0030   template <typename Object>
0031   struct MaskCollection {
0032     using Collection = std::vector<Object const*>;
0033     using Mask = std::vector<bool>;
0034     template <typename F>
0035     void mask(Collection const& cands, Mask& mask, F f) const {
0036       mask.resize(cands.size());
0037       std::transform(
0038           cands.begin(), cands.end(), mask.begin(), [&](typename Collection::value_type const& c) { return f(*c); });
0039     }
0040     virtual void eval(Collection const&, Mask&) const = 0;
0041     virtual ~MaskCollection(){};
0042   };
0043 
0044   template <typename Object>
0045   struct SelectInCollection {
0046     using Collection = std::vector<Object const*>;
0047     template <typename F>
0048     void select(Collection& cands, F f) const {
0049       cands.erase(
0050           std::remove_if(cands.begin(), cands.end(), [&](typename Collection::value_type const& c) { return !f(*c); }),
0051           cands.end());
0052     }
0053     virtual void eval(Collection&) const = 0;
0054     virtual ~SelectInCollection(){};
0055   };
0056 
0057   template <typename Object>
0058   struct SelectIndecesInCollection {
0059     using Collection = std::vector<Object const*>;
0060     using Indices = std::vector<unsigned int>;
0061     template <typename F>
0062     void select(Collection const& cands, Indices& inds, F f) const {
0063       unsigned int i = 0;
0064       for (auto const& c : cands) {
0065         if (f(*c))
0066           inds.push_back(i);
0067         ++i;
0068       }
0069     }
0070     virtual void eval(Collection const&, Indices&) const = 0;
0071     virtual ~SelectIndecesInCollection(){};
0072   };
0073 
0074 }  // namespace reco
0075 
0076 #endif  // CommonToolsUtilsExpressionEvaluatorTemplates_H