Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:52:55

0001 #include <cppunit/extensions/HelperMacros.h>
0002 #include "DataFormats/Candidate/interface/LeafCandidate.h"
0003 #include "DataFormats/Candidate/interface/Candidate.h"
0004 #include "DataFormats/Candidate/interface/CandidateWithRef.h"
0005 #include <memory>
0006 #include <iostream>
0007 
0008 class testCandidate : public CppUnit::TestFixture {
0009   CPPUNIT_TEST_SUITE(testCandidate);
0010   CPPUNIT_TEST(checkAll);
0011   CPPUNIT_TEST_SUITE_END();
0012 
0013 public:
0014   void setUp() {}
0015   void tearDown() {}
0016   void checkAll();
0017 };
0018 
0019 CPPUNIT_TEST_SUITE_REGISTRATION(testCandidate);
0020 
0021 namespace test {
0022 
0023   struct DummyComponent {
0024     int x;
0025   };
0026 
0027   struct DummyComponent2 {
0028     int x;
0029   };
0030 
0031   class DummyCandidate1 : public reco::LeafCandidate {
0032   public:
0033     DummyCandidate1(const LorentzVector& p, Charge q, int x, int y1, int y2) : reco::LeafCandidate(q, p) {
0034       c.x = x;
0035       cc[0].x = y1;
0036       cc[1].x = y2;
0037     }
0038     virtual DummyCandidate1* clone() const { return new DummyCandidate1(*this); }
0039     DummyComponent cmp() const { return c; }
0040     DummyComponent2 cmp2(size_t i) const { return cc[i]; }
0041     size_t cmpSize2() const { return 2; }
0042 
0043   private:
0044     DummyComponent c;
0045     DummyComponent2 cc[2];
0046   };
0047 
0048   struct DummyRef {
0049     bool isNull() const { return true; }
0050     bool isNonnull() const { return false; }
0051     bool operator==(const DummyRef& ref) const { return true; }
0052     bool operator!=(const DummyRef& ref) const { return false; }
0053   };
0054 
0055 }  // namespace test

0056 
0057 namespace reco {
0058   GET_DEFAULT_CANDIDATE_COMPONENT(test::DummyCandidate1, test::DummyComponent, cmp);
0059   GET_CANDIDATE_MULTIPLECOMPONENTS(test::DummyCandidate1, test::DummyComponent2, cmp2, cmpSize2, DefaultComponentTag);
0060 }  // namespace reco

0061 
0062 void testCandidate::checkAll() {
0063   reco::Particle::LorentzVector p(1.0, 2.0, 3.0, 5.0);
0064   GlobalVector v(1.0, 2.0, 3.0);
0065   reco::LeafCandidate::PolarLorentzVector pl(p);
0066 
0067   reco::Particle::Charge q(1);
0068   int x = 123, y0 = 111, y1 = 222;
0069   std::unique_ptr<reco::Candidate> c(new test::DummyCandidate1(p, q, x, y0, y1));
0070   CPPUNIT_ASSERT(c->charge() == q);
0071   CPPUNIT_ASSERT((c->p4() - p).M2() < 1.e-4);
0072   CPPUNIT_ASSERT(c->numberOfDaughters() == 0);
0073   CPPUNIT_ASSERT(c->get<test::DummyComponent>().x == x);
0074   CPPUNIT_ASSERT(c->numberOf<test::DummyComponent2>() == 2);
0075   CPPUNIT_ASSERT(c->get<test::DummyComponent2>(0).x == y0);
0076   CPPUNIT_ASSERT(c->get<test::DummyComponent2>(1).x == y1);
0077 
0078   reco::CandidateWithRef<test::DummyRef> cwr;
0079   test::DummyRef dr;
0080   cwr.setRef(dr);
0081 
0082   reco::Candidate::const_iterator b = c->begin(), e = c->end();
0083   CPPUNIT_ASSERT(b == e);
0084   CPPUNIT_ASSERT(e - b == 0);
0085 
0086   // test constructors

0087 
0088   reco::LeafCandidate c1(q, p);
0089   reco::LeafCandidate c2(q, pl);
0090   reco::LeafCandidate c3(q, v, 5.f, c1.mass());
0091 
0092   auto ftoi = [](float x) -> int {
0093     int i;
0094     memcpy(&i, &x, 4);
0095     return i;
0096   };
0097   auto print = [](float a, float b) -> bool {
0098     std::cout << "\nwhat? " << a << ' ' << b << std::endl;
0099     return false;
0100   };
0101   auto ok = [&](float a, float b) -> bool { return std::abs(ftoi(a) - ftoi(b)) < 10 ? true : print(a, b); };
0102 
0103   CPPUNIT_ASSERT(ok(c1.pt(), c2.pt()));
0104   CPPUNIT_ASSERT(ok(c1.eta(), c2.eta()));
0105   CPPUNIT_ASSERT(ok(c1.phi(), c2.phi()));
0106   CPPUNIT_ASSERT(ok(c1.mass(), c2.mass()));
0107 
0108   CPPUNIT_ASSERT(ok(c1.y(), c2.y()));
0109 
0110   CPPUNIT_ASSERT(ok(c1.pt(), c3.pt()));
0111   CPPUNIT_ASSERT(ok(c1.eta(), c3.eta()));
0112   CPPUNIT_ASSERT(ok(c1.phi(), c3.phi()));
0113   CPPUNIT_ASSERT(ok(c1.mass(), c3.mass()));
0114 
0115   CPPUNIT_ASSERT(ok(c1.y(), c3.y()));
0116 
0117   CPPUNIT_ASSERT(ok(c1.px(), c2.px()));
0118   CPPUNIT_ASSERT(ok(c1.py(), c2.py()));
0119   CPPUNIT_ASSERT(ok(c1.pz(), c2.pz()));
0120   CPPUNIT_ASSERT(ok(c1.energy(), c2.energy()));
0121 
0122   CPPUNIT_ASSERT(ok(c1.px(), c3.px()));
0123   CPPUNIT_ASSERT(ok(c1.py(), c3.py()));
0124   CPPUNIT_ASSERT(ok(c1.pz(), c3.pz()));
0125   CPPUNIT_ASSERT(ok(c1.energy(), c3.energy()));
0126 }