Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:49

0001 #include <cppunit/extensions/HelperMacros.h>
0002 #include "DataFormats/Candidate/interface/LeafCandidate.h"
0003 #include "DataFormats/Candidate/interface/CompositeCandidate.h"
0004 
0005 class testCompositeCandidate : public CppUnit::TestFixture {
0006   CPPUNIT_TEST_SUITE(testCompositeCandidate);
0007   CPPUNIT_TEST(checkAll);
0008   CPPUNIT_TEST_SUITE_END();
0009 
0010 public:
0011   void setUp() {}
0012   void tearDown() {}
0013   void checkAll();
0014 };
0015 
0016 CPPUNIT_TEST_SUITE_REGISTRATION(testCompositeCandidate);
0017 
0018 namespace test {
0019   class DummyCandidate : public reco::LeafCandidate {
0020   public:
0021     DummyCandidate(const LorentzVector& p, Charge q = 0, int x = 0) : reco::LeafCandidate(q, p), x_(x) {}
0022     virtual DummyCandidate* clone() const { return new DummyCandidate(*this); }
0023     int x() const { return x_; }
0024 
0025   private:
0026     int x_;
0027   };
0028 }  // namespace test

0029 
0030 namespace reco {
0031   GET_DEFAULT_CANDIDATE_COMPONENT(test::DummyCandidate, int, x);
0032 }
0033 
0034 namespace test {
0035   struct DummyCandSelector {
0036     DummyCandSelector(int x) : x_(x) {}
0037     bool operator()(const reco::Candidate& c) const { return c.get<int>() == x_; }
0038 
0039   private:
0040     int x_;
0041   };
0042 }  // namespace test

0043 
0044 void testCompositeCandidate::checkAll() {
0045   reco::Particle::LorentzVector p1(1.0, 2.0, 3.0, 4.0), p2(1.5, 2.5, 3.5, 4.5);
0046   reco::Particle::Charge q1(1), q2(-1);
0047   {
0048     test::DummyCandidate t1(p1, q1);
0049     test::DummyCandidate t2(p2, q2);
0050     reco::CompositeCandidate c;
0051     CPPUNIT_ASSERT(c.numberOfDaughters() == 0);
0052     c.addDaughter(t1);
0053     CPPUNIT_ASSERT(c.numberOfDaughters() == 1);
0054     c.addDaughter(t2);
0055     CPPUNIT_ASSERT(c.numberOfDaughters() == 2);
0056 
0057     const reco::Candidate* d[2];
0058     int idx = 0;
0059     const reco::CompositeCandidate& cand = c;
0060     CPPUNIT_ASSERT(c.numberOfDaughters() == 2);
0061     for (reco::Candidate::const_iterator i = cand.begin(); i != cand.end(); ++i) {
0062       d[idx++] = &*i;
0063     }
0064     CPPUNIT_ASSERT(d[0]->charge() == q1);
0065     CPPUNIT_ASSERT((d[0]->p4() - p1).M2() < 1.e-4);
0066     CPPUNIT_ASSERT(d[1]->charge() == q2);
0067     CPPUNIT_ASSERT((d[1]->p4() - p2).M2() < 1.e-4);
0068   }
0069 }