testPackedGenParticle

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
#include <cppunit/extensions/HelperMacros.h>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <iomanip>

#include "DataFormats/PatCandidates/interface/PackedGenParticle.h"

class testPackedGenParticle : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(testPackedGenParticle);

  CPPUNIT_TEST(testDefaultConstructor);
  CPPUNIT_TEST(testCopyConstructor);
  CPPUNIT_TEST(testPackUnpack);
  CPPUNIT_TEST(testSimulateReadFromRoot);

  CPPUNIT_TEST_SUITE_END();

public:
  void setUp() {}
  void tearDown() {}

  void testDefaultConstructor();
  void testCopyConstructor();
  void testPackUnpack();
  void testSimulateReadFromRoot();

private:
};

CPPUNIT_TEST_SUITE_REGISTRATION(testPackedGenParticle);

void testPackedGenParticle::testDefaultConstructor() {
  pat::PackedGenParticle pc;

  CPPUNIT_ASSERT(pc.polarP4() == pat::PackedGenParticle::PolarLorentzVector(0, 0, 0, 0));
  CPPUNIT_ASSERT(pc.p4() == pat::PackedGenParticle::LorentzVector(0, 0, 0, 0));
  CPPUNIT_ASSERT(pc.vertex() == pat::PackedGenParticle::Point(0, 0, 0));
  CPPUNIT_ASSERT(pc.packedPt_ == 0);
  CPPUNIT_ASSERT(pc.packedY_ == 0);
  CPPUNIT_ASSERT(pc.packedPhi_ == 0);
  CPPUNIT_ASSERT(pc.packedM_ == 0);
}

static bool tolerance(double iLHS, double iRHS, double fraction) {
  return std::abs(iLHS - iRHS) <= fraction * std::abs(iLHS + iRHS) / 2.;
}

void testPackedGenParticle::testCopyConstructor() {
  pat::PackedGenParticle::LorentzVector lv(1., 0.5, 0., std::sqrt(1. + 0.25 + 0.120 * 0.120));
  pat::PackedGenParticle::PolarLorentzVector plv(lv.Pt(), lv.Eta(), lv.Phi(), lv.M());

  pat::PackedGenParticle::Point v(0.01, 0.02, 0.);

  pat::PackedGenParticle pc(reco::GenParticle(-1., lv, v, 11, 0, false), edm::Ref<reco::GenParticleCollection>());

  CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));
  //CPPUNIT_ASSERT(pc.vertex() == v);

  pat::PackedGenParticle copy_pc(pc);

  CPPUNIT_ASSERT(copy_pc.polarP4() == pc.polarP4());
  CPPUNIT_ASSERT(copy_pc.p4() == pc.p4());
  CPPUNIT_ASSERT(copy_pc.vertex() == pc.vertex());
  CPPUNIT_ASSERT(copy_pc.packedPt_ == pc.packedPt_);
  CPPUNIT_ASSERT(copy_pc.packedY_ == pc.packedY_);
  CPPUNIT_ASSERT(copy_pc.packedPhi_ == pc.packedPhi_);
  CPPUNIT_ASSERT(copy_pc.packedM_ == pc.packedM_);

  CPPUNIT_ASSERT(&copy_pc.polarP4() != &pc.polarP4());
  CPPUNIT_ASSERT(&copy_pc.p4() != &pc.p4());
  CPPUNIT_ASSERT(&copy_pc.vertex() != &pc.vertex());
  CPPUNIT_ASSERT(&copy_pc.packedPt_ != &pc.packedPt_);
  CPPUNIT_ASSERT(&copy_pc.packedY_ != &pc.packedY_);
  CPPUNIT_ASSERT(&copy_pc.packedPhi_ != &pc.packedPhi_);
  CPPUNIT_ASSERT(&copy_pc.packedM_ != &pc.packedM_);
}

void testPackedGenParticle::testPackUnpack() {
  pat::PackedGenParticle::LorentzVector lv(1., 1., 0., std::sqrt(2. + 0.120 * 0.120));
  pat::PackedGenParticle::PolarLorentzVector plv(lv.Pt(), lv.Eta(), lv.Phi(), lv.M());

  pat::PackedGenParticle::Point v(-0.005, 0.005, 0.1);

  pat::PackedGenParticle pc(reco::GenParticle(-1., lv, v, 11, 0, false), edm::Ref<reco::GenParticleCollection>());

  CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));

  pc.pack();

  CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));
}

void testPackedGenParticle::testSimulateReadFromRoot() {
  pat::PackedGenParticle::LorentzVector lv(1., 1., 0., std::sqrt(2. + 0.120 * 0.120));
  pat::PackedGenParticle::PolarLorentzVector plv(lv.Pt(), lv.Eta(), lv.Phi(), lv.M());

  pat::PackedGenParticle::Point v(-0.005, 0.005, 0.1);

  pat::PackedGenParticle pc(reco::GenParticle(-1., lv, v, 11, 0, false), edm::Ref<reco::GenParticleCollection>());

  CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));

  //When reading back from ROOT, these were not stored and are nulled out
  delete pc.p4_.exchange(nullptr);
  delete pc.p4c_.exchange(nullptr);

  CPPUNIT_ASSERT(tolerance(pc.polarP4().Pt(), plv.Pt(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Eta(), plv.Eta(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().Phi(), plv.Phi(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.polarP4().M(), plv.M(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().X(), lv.X(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Y(), lv.Y(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().Z(), lv.Z(), 0.001));
  CPPUNIT_ASSERT(tolerance(pc.p4().E(), lv.E(), 0.001));
}