MyAnalyzer

MyAnalyzer2

testExpressionEvaluator

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#include <cppunit/extensions/HelperMacros.h>

#include "CommonTools/Utils/interface/ExpressionEvaluator.h"
#include "CommonTools/Utils/interface/ExpressionEvaluatorTemplates.h"

#include "DataFormats/TrackReco/interface/Track.h"
#include "DataFormats/TrackReco/interface/TrackExtra.h"
#include "DataFormats/Candidate/interface/CompositeCandidate.h"
#include "DataFormats/Candidate/interface/LeafCandidate.h"
#include "DataFormats/PatCandidates/interface/Jet.h"
#include "DataFormats/PatCandidates/interface/Muon.h"

class testExpressionEvaluator : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(testExpressionEvaluator);
  CPPUNIT_TEST(checkAll);
  CPPUNIT_TEST_SUITE_END();

public:
  testExpressionEvaluator() {}  // for crappy pats
  ~testExpressionEvaluator() {}
  void checkAll();
};

CPPUNIT_TEST_SUITE_REGISTRATION(testExpressionEvaluator);

#include <iostream>
namespace {
  void checkCandidate(reco::LeafCandidate const &cand, const std::string &expression, double x) {
    std::cerr << "testing " << expression << std::endl;
    try {
      //provide definition of the virtual function as a string
      std::string sexpr = "double eval(reco::LeafCandidate const& cand) const override { return ";
      sexpr += expression + ";}";
      // construct the expression evaluator (pkg where precompile.h resides, name of base class, declaration of overloaded member function)
      reco::ExpressionEvaluator eval(
          "CommonTools/CandUtils", "reco::ValueOnObject<reco::LeafCandidate>", sexpr.c_str());
      // obtain a pointer to the base class  (to be stored in Filter and Analyser at thier costruction time!)
      reco::ValueOnObject<reco::LeafCandidate> const *expr = eval.expr<reco::ValueOnObject<reco::LeafCandidate>>();
      CPPUNIT_ASSERT(expr);
      // invoke
      CPPUNIT_ASSERT(std::abs(expr->eval(cand) - x) < 1.e-6);
    } catch (cms::Exception const &e) {
      // if compilation fails, the compiler output is part of the exception message
      std::cerr << e.what() << std::endl;
      CPPUNIT_ASSERT("ExpressionEvaluator threw" == 0);
    }
  }

  std::vector<reco::LeafCandidate> generate() {
    reco::Candidate::LorentzVector p1(10, -10, -10, 15);
    reco::Candidate::LorentzVector incr(0, 3, 3, 0);

    int sign = 1;
    std::vector<reco::LeafCandidate> ret;
    for (int i = 0; i < 10; ++i) {
      ret.emplace_back(sign, p1);
      sign = -sign;
      p1 += incr;
    }
    return ret;
  }

  struct MyAnalyzer {
    using Selector = reco::MaskCollection<reco::LeafCandidate>;
    explicit MyAnalyzer(std::string const &cut) {
      std::string sexpr = "void eval(Collection const & c, Mask & m) const override{";
      sexpr += "\n auto cut = [](reco::LeafCandidate const & cand){ return " + cut + ";};\n";
      sexpr += "mask(c,m,cut); }";
      std::cerr << "testing " << sexpr << std::endl;
      try {
        reco::ExpressionEvaluator eval(
            "CommonTools/CandUtils", "reco::MaskCollection<reco::LeafCandidate>", sexpr.c_str());
        m_selector = eval.expr<Selector>();
        CPPUNIT_ASSERT(m_selector);
      } catch (cms::Exception const &e) {
        std::cerr << e.what() << std::endl;
        CPPUNIT_ASSERT("ExpressionEvaluator threw" == 0);
      }
    }

    void analyze() const {
      auto inputColl = generate();
      Selector::Collection cands;
      cands.reserve(inputColl.size());
      for (auto const &c : inputColl)
        cands.push_back(&c);
      Selector::Mask mask;
      m_selector->eval(cands, mask);
      CPPUNIT_ASSERT(2 == std::count(mask.begin(), mask.end(), true));
      int ind = 0;
      cands.erase(
          std::remove_if(
              cands.begin(), cands.end(), [&](Selector::Collection::value_type const &) { return !mask[ind++]; }),
          cands.end());
      CPPUNIT_ASSERT(2 == cands.size());
    }

    Selector const *m_selector = nullptr;
  };

  struct MyAnalyzer2 {
    using Selector = reco::SelectInCollection<reco::LeafCandidate>;
    explicit MyAnalyzer2(std::string const &cut) {
      std::string sexpr = "void eval(Collection & c) const override{";
      sexpr += "\n auto cut = [](reco::LeafCandidate const & cand){ return " + cut + ";};\n";
      sexpr += "select(c,cut); }";
      std::cerr << "testing " << sexpr << std::endl;
      try {
        reco::ExpressionEvaluator eval(
            "CommonTools/CandUtils", "reco::SelectInCollection<reco::LeafCandidate>", sexpr.c_str());
        m_selector = eval.expr<Selector>();
        CPPUNIT_ASSERT(m_selector);
      } catch (cms::Exception const &e) {
        std::cerr << e.what() << std::endl;
        CPPUNIT_ASSERT("ExpressionEvaluator threw" == 0);
      }
    }

    void analyze() const {
      auto inputColl = generate();
      Selector::Collection cands;
      cands.reserve(inputColl.size());
      for (auto const &c : inputColl)
        cands.push_back(&c);
      m_selector->eval(cands);
      CPPUNIT_ASSERT(2 == cands.size());
    }

    Selector const *m_selector = nullptr;
  };

}  // namespace

void testExpressionEvaluator::checkAll() {
  reco::CompositeCandidate cand;

  reco::Candidate::LorentzVector p1(1, 2, 3, 4);
  reco::Candidate::LorentzVector p2(1.1, -2.5, 4.3, 13.7);
  reco::LeafCandidate c1(+1, p1);
  reco::LeafCandidate c2(-1, p2);
  cand.addDaughter(c1);
  cand.addDaughter(c2);
  CPPUNIT_ASSERT(cand.numberOfDaughters() == 2);
  CPPUNIT_ASSERT(cand.daughter(0) != 0);
  CPPUNIT_ASSERT(cand.daughter(1) != 0);
  {
    checkCandidate(cand, "cand.numberOfDaughters()", cand.numberOfDaughters());
    checkCandidate(cand, "cand.daughter(0)->isStandAloneMuon()", cand.daughter(0)->isStandAloneMuon());
    checkCandidate(cand, "cand.daughter(1)->isStandAloneMuon()", cand.daughter(1)->isStandAloneMuon());
    checkCandidate(cand, "cand.daughter(0)->pt()", cand.daughter(0)->pt());
    checkCandidate(cand, "cand.daughter(1)->pt()", cand.daughter(1)->pt());
    checkCandidate(cand,
                   "std::min(cand.daughter(0)->pt(), cand.daughter(1)->pt())",
                   std::min(cand.daughter(0)->pt(), cand.daughter(1)->pt()));
    checkCandidate(cand,
                   "std::max(cand.daughter(0)->pt(), cand.daughter(1)->pt())",
                   std::max(cand.daughter(0)->pt(), cand.daughter(1)->pt()));
    checkCandidate(cand,
                   "reco::deltaPhi(cand.daughter(0)->phi(), cand.daughter(1)->phi())",
                   reco::deltaPhi(cand.daughter(0)->phi(), cand.daughter(1)->phi()));
    // check also opposite order, to see that the sign is correct
    checkCandidate(cand,
                   "reco::deltaPhi(cand.daughter(1)->phi(), cand.daughter(0)->phi())",
                   reco::deltaPhi(cand.daughter(1)->phi(), cand.daughter(0)->phi()));
    checkCandidate(
        cand, "reco::deltaR(*cand.daughter(0), *cand.daughter(1))", reco::deltaR(*cand.daughter(0), *cand.daughter(1)));
  }

  MyAnalyzer analyzer("cand.pt()>15 & std::abs(cand.eta())<2");
  analyzer.analyze();

  MyAnalyzer2 analyzer2("cand.pt()>15 & std::abs(cand.eta())<2");
  analyzer2.analyze();

  // pat

  std::vector<reco::LeafCandidate> cands;
  cands.push_back(c1);
  cands.push_back(c2);
  edm::TestHandle<std::vector<reco::LeafCandidate>> constituentsHandle(&cands, edm::ProductID(42));
  reco::Jet::Constituents constituents;
  constituents.push_back(reco::Jet::Constituent(constituentsHandle, 0));
  constituents.push_back(reco::Jet::Constituent(constituentsHandle, 1));
  reco::CaloJet::Specific caloSpecific;
  caloSpecific.mMaxEInEmTowers = 0.5;
  pat::Jet jet(reco::CaloJet(p1 + p2, reco::Jet::Point(), caloSpecific, constituents));
  {
    std::string expression = "jet.userData<math::XYZVector>(\"my2int\")";
    std::cerr << "testing " << expression << std::endl;
    try {
      //provide definition of the virtual function as a string
      std::string sexpr = "math::XYZVector const * operator()(pat::Jet const& jet) const override { return ";
      sexpr += expression + ";}";
      // obtain a pointer to the base class  (to be stored in Filter and Analyser at thier costruction time!)
      auto const *expr =
          reco_expressionEvaluator("CommonTools/RecoUtils",
                                   SINGLE_ARG(reco::genericExpression<math::XYZVector const *, pat::Jet const &>),
                                   sexpr);
      CPPUNIT_ASSERT(expr);
      // invoke
      CPPUNIT_ASSERT((*expr)(jet) == nullptr);
      jet.addUserData("my2int", math::XYZVector(-2, -1, 2));
      CPPUNIT_ASSERT(jet.userData<math::XYZVector>("my2int")->z() == 2);
      std::cout << "now expr eval" << std::endl;
      CPPUNIT_ASSERT((*expr)(jet)->z() == 2);
    } catch (cms::Exception const &e) {
      // if compilation fails, the compiler output is part of the exception message
      std::cerr << e.what() << std::endl;
      CPPUNIT_ASSERT("ExpressionEvaluator threw" == 0);
    }
  }
}