Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:16

0001 #include "CommonTools/Utils/src/CandForTest.h"
0002 
0003 #include "CommonTools/Utils/interface/ExpressionEvaluator.h"
0004 
0005 #include "CommonTools/Utils/interface/ExpressionEvaluatorTemplates.h"
0006 
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 
0009 #include <iostream>
0010 #include <atomic>
0011 
0012 int main() {
0013   // build fake test package...
0014   std::string pkg = "ExpressionEvaluatorTests/EEUnitTest";
0015 
0016   using reco::ExpressionEvaluator;
0017 
0018   using vcut = reco::genericExpression<bool, int, int>;
0019   using Cand = eetest::CandForTest;
0020   using MyExpr = reco::MaskCollection<eetest::CandForTest>;
0021 
0022   std::vector<Cand> oriC;
0023   oriC.reserve(10);  // pre-allocate so it doesn't reallocate
0024   MyExpr::Collection c;
0025   for (int i = 5; i < 15; ++i) {
0026     oriC.emplace_back(Cand(i, 1, 1));
0027     c.push_back(&oriC.back());
0028   }
0029   MyExpr::Mask r;
0030 
0031   std::string expr =
0032       "void eval(Collection const & c,  Mask & r) const override{ r.resize(c.size()); "
0033       "std::transform(c.begin(),c.end(),r.begin(), [](Collection::value_type const & c){ return (*c).pt()>10;}); }";
0034 
0035   ExpressionEvaluator parser("CommonTools/Utils", "reco::MaskCollection<eetest::CandForTest>", expr);
0036 
0037   auto func = parser.expr<MyExpr>();
0038 
0039   func->eval(c, r);
0040 
0041   std::cout << r.size() << ' ' << std::count(r.begin(), r.end(), true) << std::endl;
0042 
0043   std::string cut = "bool operator()(int i, int j) const override { return i<10&& j<5; }";
0044 
0045   // ExpressionEvaluator parser2("ExpressionEvaluatorTests/EEUnitTest","eetest::vcut",cut.c_str());
0046   // auto mcut = parser2.expr<eetest::vcut>();
0047 
0048   auto const& mcut =
0049       *reco_expressionEvaluator("CommonTools/Utils", SINGLE_ARG(reco::genericExpression<bool, int, int>), cut);
0050 
0051   std::cout << mcut(2, 7) << ' ' << mcut(3, 4) << std::endl;
0052 
0053   try {
0054     std::string cut = "bool operator()(int i, int j) const override { return i<10&& j<5; }";
0055     ExpressionEvaluator parser2("Bla/Blo", "eetest::vcut", cut);
0056     auto mcut = parser2.expr<vcut>();
0057     std::cout << (*mcut)(2, 7) << ' ' << (*mcut)(3, 4) << std::endl;
0058   } catch (cms::Exception const& e) {
0059     std::cout << e.what() << std::endl;
0060   } catch (...) {
0061     std::cout << "unknown error...." << std::endl;
0062   }
0063 
0064   try {
0065     std::string cut = "bool operator()(int i, int j) const override { return i<10&& j<5; }";
0066     auto const& mcut =
0067         *reco_expressionEvaluator("CommonTools/Utils", SINGLE_ARG(reco::genericExpression<bool, int, int>), cut);
0068     std::cout << mcut(2, 7) << ' ' << mcut(3, 4) << std::endl;
0069 
0070   } catch (cms::Exception const& e) {
0071     std::cout << e.what() << std::endl;
0072   } catch (...) {
0073     std::cout << "unknown error...." << std::endl;
0074   }
0075 
0076   // stress test
0077   std::atomic<int> j(0);
0078 #pragma omp parallel num_threads(2)
0079   {
0080     reco::genericExpression<bool, int, int> const* acut = nullptr;
0081     for (int i = 0; i < 20; ++i) {
0082       acut = reco_expressionEvaluator("CommonTools/Utils", SINGLE_ARG(reco::genericExpression<bool, int, int>), cut);
0083       (*acut)(2, 7);
0084       std::cerr << j++ << ',';
0085     }
0086   }
0087   std::cerr << std::endl;
0088 
0089   std::cout << "If HERE OK" << std::endl;
0090 
0091   return 0;
0092 }