Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:37:23

0001 #include "CommonTools/Utils/interface/ExpressionEvaluator.h"
0002 #include "CommonTools/Utils/interface/ExpressionEvaluatorTemplates.h"
0003 
0004 #include "PhysicsTools/SelectorUtils/interface/CutApplicatorBase.h"
0005 
0006 class ExpressionEvaluatorCut : public CutApplicatorBase {
0007 public:
0008   ExpressionEvaluatorCut(const edm::ParameterSet& c);
0009   ~ExpressionEvaluatorCut() override {}
0010 
0011   result_type asCandidate(const argument_type& cand) const final { return (*cut_)(cand); }
0012 
0013   double value(const reco::CandidatePtr& cand) const final { return cut_->value(cand); }
0014 
0015   const std::string& name() const final { return realname_; }
0016 
0017 private:
0018   const std::string realname_;
0019   CutApplicatorBase* cut_;
0020 };
0021 
0022 ExpressionEvaluatorCut::ExpressionEvaluatorCut(const edm::ParameterSet& c)
0023     : CutApplicatorBase(c), realname_(c.getParameter<std::string>("realCutName")) {
0024   const std::string newline("\n");
0025   const std::string close_function("; };");
0026   const std::string candTypePreamble("CandidateType candidateType() const override final { return ");
0027 
0028   //construct the overload of candidateType()
0029   const std::string& candType = c.getParameter<std::string>("candidateType");
0030   const std::string candTypeExpr = candTypePreamble + candType + close_function;
0031 
0032   // read in the overload of operator()
0033   const std::string& oprExpr = c.getParameter<std::string>("functionDef");
0034 
0035   // read in the overload of value()
0036   const std::string& valExpr = c.getParameter<std::string>("valueDef");
0037 
0038   // concatenate and evaluate the expression
0039   const std::string total_expr = (candTypeExpr + newline + oprExpr + newline + valExpr);
0040   reco::ExpressionEvaluator eval("PhysicsTools/SelectorUtils", "CutApplicatorBase", total_expr);
0041   cut_ = eval.expr<CutApplicatorBase>();
0042 }
0043 
0044 DEFINE_EDM_PLUGIN(CutApplicatorFactory, ExpressionEvaluatorCut, "ExpressionEvaluatorCut");