Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-31 02:19:01

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 #include <thread>
0012 
0013 int main() {
0014   // build fake test package...
0015   std::string pkg = "ExpressionEvaluatorTests/EEUnitTest";
0016 
0017   using reco::ExpressionEvaluator;
0018 
0019   using vcut = reco::genericExpression<bool, int, int>;
0020   using Cand = eetest::CandForTest;
0021   using MyExpr = reco::MaskCollection<eetest::CandForTest>;
0022 
0023   std::vector<Cand> oriC;
0024   oriC.reserve(10);  // pre-allocate so it doesn't reallocate
0025   MyExpr::Collection c;
0026   for (int i = 5; i < 15; ++i) {
0027     oriC.emplace_back(Cand(i, 1, 1));
0028     c.push_back(&oriC.back());
0029   }
0030   MyExpr::Mask r;
0031 
0032   std::string expr =
0033       "void eval(Collection const & c,  Mask & r) const override{ r.resize(c.size()); "
0034       "std::transform(c.begin(),c.end(),r.begin(), [](Collection::value_type const & c){ return (*c).pt()>10;}); }";
0035 
0036   ExpressionEvaluator parser("CommonTools/Utils", "reco::MaskCollection<eetest::CandForTest>", expr);
0037 
0038   auto func = parser.expr<MyExpr>();
0039 
0040   func->eval(c, r);
0041 
0042   std::cout << r.size() << ' ' << std::count(r.begin(), r.end(), true) << std::endl;
0043 
0044   std::string cut = "bool operator()(int i, int j) const override { return i<10&& j<5; }";
0045 
0046   // ExpressionEvaluator parser2("ExpressionEvaluatorTests/EEUnitTest","eetest::vcut",cut.c_str());
0047   // auto mcut = parser2.expr<eetest::vcut>();
0048 
0049   auto const& mcut =
0050       *reco_expressionEvaluator("CommonTools/Utils", SINGLE_ARG(reco::genericExpression<bool, int, int>), cut);
0051 
0052   std::cout << mcut(2, 7) << ' ' << mcut(3, 4) << std::endl;
0053 
0054   try {
0055     std::string cut = "bool operator()(int i, int j) const override { return i<10&& j<5; }";
0056     ExpressionEvaluator parser2("Bla/Blo", "eetest::vcut", cut);
0057     auto mcut = parser2.expr<vcut>();
0058     std::cout << (*mcut)(2, 7) << ' ' << (*mcut)(3, 4) << std::endl;
0059   } catch (cms::Exception const& e) {
0060     std::cout << e.what() << std::endl;
0061   } catch (...) {
0062     std::cout << "unknown error...." << std::endl;
0063   }
0064 
0065   try {
0066     std::string cut = "bool operator()(int i, int j) const override { return i<10&& j<5; }";
0067     auto const& mcut =
0068         *reco_expressionEvaluator("CommonTools/Utils", SINGLE_ARG(reco::genericExpression<bool, int, int>), cut);
0069     std::cout << mcut(2, 7) << ' ' << mcut(3, 4) << std::endl;
0070 
0071   } catch (cms::Exception const& e) {
0072     std::cout << e.what() << std::endl;
0073   } catch (...) {
0074     std::cout << "unknown error...." << std::endl;
0075   }
0076 
0077   // stress test
0078   std::atomic<int> j(0);
0079   //wait for both threads to start before running the test
0080   std::atomic<int> waitForAll(2);
0081 
0082   auto work = [&]() {
0083     --waitForAll;
0084     while (waitForAll > 0)
0085       ;
0086     reco::genericExpression<bool, int, int> const* acut = nullptr;
0087     for (int i = 0; i < 20; ++i) {
0088       acut = reco_expressionEvaluator("CommonTools/Utils", SINGLE_ARG(reco::genericExpression<bool, int, int>), cut);
0089       (*acut)(2, 7);
0090       std::cerr << j++ << ',';
0091     }
0092   };
0093 
0094   std::thread t1(work);
0095   std::thread t2(work);
0096   t1.join();
0097   t2.join();
0098   std::cerr << std::endl;
0099 
0100   std::cout << "If HERE OK" << std::endl;
0101 
0102   return 0;
0103 }