Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:54:28

0001 #include <cppunit/extensions/HelperMacros.h>
0002 #include <algorithm>
0003 #include <iterator>
0004 #include <iostream>
0005 #include <iomanip>
0006 
0007 #include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
0008 
0009 class testPackedGenParticle : public CppUnit::TestFixture {
0010   CPPUNIT_TEST_SUITE(testPackedGenParticle);
0011 
0012   CPPUNIT_TEST(testDefaultConstructor);
0013   CPPUNIT_TEST(testCopyConstructor);
0014   CPPUNIT_TEST(testPackUnpack);
0015   CPPUNIT_TEST(testSimulateReadFromRoot);
0016 
0017   CPPUNIT_TEST_SUITE_END();
0018 
0019 public:
0020   void setUp() {}
0021   void tearDown() {}
0022 
0023   void testDefaultConstructor();
0024   void testCopyConstructor();
0025   void testPackUnpack();
0026   void testSimulateReadFromRoot();
0027 
0028 private:
0029 };
0030 
0031 CPPUNIT_TEST_SUITE_REGISTRATION(testPackedGenParticle);
0032 
0033 void testPackedGenParticle::testDefaultConstructor() {
0034   pat::PackedGenParticle pc;
0035 
0036   CPPUNIT_ASSERT(pc.polarP4() == pat::PackedGenParticle::PolarLorentzVector(0, 0, 0, 0));
0037   CPPUNIT_ASSERT(pc.p4() == pat::PackedGenParticle::LorentzVector(0, 0, 0, 0));
0038   CPPUNIT_ASSERT(pc.vertex() == pat::PackedGenParticle::Point(0, 0, 0));
0039   CPPUNIT_ASSERT(pc.packedPt_ == 0);
0040   CPPUNIT_ASSERT(pc.packedY_ == 0);
0041   CPPUNIT_ASSERT(pc.packedPhi_ == 0);
0042   CPPUNIT_ASSERT(pc.packedM_ == 0);
0043 }
0044 
0045 static bool tolerance(double iLHS, double iRHS, double fraction) {
0046   return std::abs(iLHS - iRHS) <= fraction * std::abs(iLHS + iRHS) / 2.;
0047 }
0048 
0049 void testPackedGenParticle::testCopyConstructor() {
0050   pat::PackedGenParticle::LorentzVector lv(1., 0.5, 0., std::sqrt(1. + 0.25 + 0.120 * 0.120));
0051   pat::PackedGenParticle::PolarLorentzVector plv(lv.Pt(), lv.Eta(), lv.Phi(), lv.M());
0052 
0053   pat::PackedGenParticle::Point v(0.01, 0.02, 0.);
0054 
0055   pat::PackedGenParticle pc(reco::GenParticle(-1., lv, v, 11, 0, false), edm::Ref<reco::GenParticleCollection>());
0056 
0057   CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
0058   CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
0059   CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
0060   CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
0061   CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
0062   CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
0063   CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
0064   CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));
0065   //CPPUNIT_ASSERT(pc.vertex() == v);
0066 
0067   pat::PackedGenParticle copy_pc(pc);
0068 
0069   CPPUNIT_ASSERT(copy_pc.polarP4() == pc.polarP4());
0070   CPPUNIT_ASSERT(copy_pc.p4() == pc.p4());
0071   CPPUNIT_ASSERT(copy_pc.vertex() == pc.vertex());
0072   CPPUNIT_ASSERT(copy_pc.packedPt_ == pc.packedPt_);
0073   CPPUNIT_ASSERT(copy_pc.packedY_ == pc.packedY_);
0074   CPPUNIT_ASSERT(copy_pc.packedPhi_ == pc.packedPhi_);
0075   CPPUNIT_ASSERT(copy_pc.packedM_ == pc.packedM_);
0076 
0077   CPPUNIT_ASSERT(&copy_pc.polarP4() != &pc.polarP4());
0078   CPPUNIT_ASSERT(&copy_pc.p4() != &pc.p4());
0079   CPPUNIT_ASSERT(&copy_pc.vertex() != &pc.vertex());
0080   CPPUNIT_ASSERT(&copy_pc.packedPt_ != &pc.packedPt_);
0081   CPPUNIT_ASSERT(&copy_pc.packedY_ != &pc.packedY_);
0082   CPPUNIT_ASSERT(&copy_pc.packedPhi_ != &pc.packedPhi_);
0083   CPPUNIT_ASSERT(&copy_pc.packedM_ != &pc.packedM_);
0084 }
0085 
0086 void testPackedGenParticle::testPackUnpack() {
0087   pat::PackedGenParticle::LorentzVector lv(1., 1., 0., std::sqrt(2. + 0.120 * 0.120));
0088   pat::PackedGenParticle::PolarLorentzVector plv(lv.Pt(), lv.Eta(), lv.Phi(), lv.M());
0089 
0090   pat::PackedGenParticle::Point v(-0.005, 0.005, 0.1);
0091 
0092   pat::PackedGenParticle pc(reco::GenParticle(-1., lv, v, 11, 0, false), edm::Ref<reco::GenParticleCollection>());
0093 
0094   CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
0095   CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
0096   CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
0097   CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
0098   CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
0099   CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
0100   CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
0101   CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));
0102 
0103   pc.pack();
0104 
0105   CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
0106   CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
0107   CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
0108   CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
0109   CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
0110   CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
0111   CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
0112   CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));
0113 }
0114 
0115 void testPackedGenParticle::testSimulateReadFromRoot() {
0116   pat::PackedGenParticle::LorentzVector lv(1., 1., 0., std::sqrt(2. + 0.120 * 0.120));
0117   pat::PackedGenParticle::PolarLorentzVector plv(lv.Pt(), lv.Eta(), lv.Phi(), lv.M());
0118 
0119   pat::PackedGenParticle::Point v(-0.005, 0.005, 0.1);
0120 
0121   pat::PackedGenParticle pc(reco::GenParticle(-1., lv, v, 11, 0, false), edm::Ref<reco::GenParticleCollection>());
0122 
0123   CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
0124   CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
0125   CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
0126   CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
0127   CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
0128   CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
0129   CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
0130   CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));
0131 
0132   //When reading back from ROOT, these were not stored and are nulled out
0133   delete pc.p4_.exchange(nullptr);
0134   delete pc.p4c_.exchange(nullptr);
0135 
0136   CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
0137   CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
0138   CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
0139   CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
0140   CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
0141   CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
0142   CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
0143   CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));
0144 }