testParticle

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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
#include <cppunit/extensions/HelperMacros.h>
#include "DataFormats/Candidate/interface/Particle.h"
#include "TBufferFile.h"
#include "TClass.h"
#include <iostream>

class testParticle : public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(testParticle);
  CPPUNIT_TEST(checkXYZ);
  CPPUNIT_TEST(checkPolar);
  CPPUNIT_TEST(checkFloat);
  CPPUNIT_TEST(checkXYZLarge);
  CPPUNIT_TEST(checkPolarLarge);
  CPPUNIT_TEST(checkFloatLarge);
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp() {}
  void tearDown() {}
  void checkXYZ();
  void checkPolar();
  void checkFloat();
  void checkXYZLarge();
  void checkPolarLarge();
  void checkFloatLarge();

  void testAbs(const reco::Particle &t,
               reco::Particle::Charge q,
               const reco::Particle::Point &,
               const reco::Particle::LorentzVector &p,
               const reco::Particle::PolarLorentzVector &pp);
  void testRel(const reco::Particle &t,
               reco::Particle::Charge q,
               const reco::Particle::Point &,
               const reco::Particle::LorentzVector &p,
               const reco::Particle::PolarLorentzVector &pp);

  const double mass = 123.456;
  const double pz = 100.000;
  const reco::Particle::Point vtx = reco::Particle::Point(6, 7, 8);
  const reco::Particle::Point vtx1 = reco::Particle::Point(8, 7, 6);
  const reco::Particle::Charge q = 1;
  const reco::Particle::Charge q1 = 9;
};

CPPUNIT_TEST_SUITE_REGISTRATION(testParticle);

void testParticle::testAbs(const reco::Particle &t,
                           reco::Particle::Charge q,
                           const reco::Particle::Point &vtx,
                           const reco::Particle::LorentzVector &p,
                           const reco::Particle::PolarLorentzVector &pp) {
  const double epsilon = 1.e-5;
  const double epsilon2 = 5.e-4;
  CPPUNIT_ASSERT(t.charge() == q);
  CPPUNIT_ASSERT(t.threeCharge() == q * 3);
  CPPUNIT_ASSERT((t.polarP4() - pp).M2() < epsilon);
  CPPUNIT_ASSERT((t.p4() - p).M2() < epsilon);
  CPPUNIT_ASSERT(std::abs(t.p() - p.P()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.energy() - p.E()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.px() - p.Px()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.py() - p.Py()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.pz() - p.Pz()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.phi() - p.Phi()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.theta() - p.Theta()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.eta() - p.Eta()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.mass() - p.M()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.massSqr() - p.M2()) < epsilon2);
  CPPUNIT_ASSERT(std::abs(t.mt() - p.Mt()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.mtSqr() - p.Mt2()) < epsilon2);
  CPPUNIT_ASSERT(std::abs(t.rapidity() - p.Rapidity()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.vx() - vtx.x()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.vy() - vtx.y()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.vz() - vtx.z()) < epsilon);
}

void testParticle::testRel(const reco::Particle &t,
                           reco::Particle::Charge q,
                           const reco::Particle::Point &vtx,
                           const reco::Particle::LorentzVector &p,
                           const reco::Particle::PolarLorentzVector &pp) {
  const double epsilon = 1.e-5;
  const double epsilon2 = 5.e-4;
  CPPUNIT_ASSERT(t.charge() == q);
  CPPUNIT_ASSERT(t.threeCharge() == q * 3);
  // these should be zero
  CPPUNIT_ASSERT((t.polarP4() - pp).M2() < epsilon);
  CPPUNIT_ASSERT((t.p4() - p).M2() < epsilon);

  CPPUNIT_ASSERT(std::abs(t.p() - p.P()) / p.P() < epsilon);
  CPPUNIT_ASSERT(std::abs(t.energy() - p.E()) / p.E() < epsilon);

  CPPUNIT_ASSERT(std::abs((t.px() - p.px()) / p.px()) < epsilon);
  CPPUNIT_ASSERT(std::abs((t.py() - p.py()) / p.py()) < epsilon);
  CPPUNIT_ASSERT(std::abs((t.pz() - p.pz()) / p.pz()) < epsilon);
  // here resolution should be absolute no matter what
  CPPUNIT_ASSERT(std::abs(t.phi() - p.Phi()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.theta() - p.Theta()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.eta() - p.Eta()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.rapidity() - p.Rapidity()) < epsilon);

  CPPUNIT_ASSERT(std::abs((t.mass() - p.M()) / p.M()) < epsilon);
  CPPUNIT_ASSERT(std::abs((t.massSqr() - p.M2()) / p.M2()) < epsilon2);
  CPPUNIT_ASSERT(std::abs((t.mt() - p.Mt()) / p.Mt()) < epsilon);
  CPPUNIT_ASSERT(std::abs((t.mtSqr() - p.Mt2()) / p.Mt2()) < epsilon2);

  // not really tested....  absolute makes sense
  CPPUNIT_ASSERT(std::abs(t.vx() - vtx.x()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.vy() - vtx.y()) < epsilon);
  CPPUNIT_ASSERT(std::abs(t.vz() - vtx.z()) < epsilon);
}

reco::Particle stream(reco::Particle &t) {
  auto m_class = TClass::GetClass(typeid(reco::Particle));
  TBufferFile wbuffer(TBufferFile::kWrite);
  wbuffer.InitMap();
  wbuffer.StreamObject((&t), m_class);

  TBufferFile rbuffer(TBufferFile::kRead, wbuffer.Length(), (wbuffer.Buffer()), kFALSE);

  rbuffer.InitMap();

  reco::Particle t2;
  rbuffer.StreamObject(&t2, m_class);
  return t2;
}

void testParticle::checkXYZ() {
  reco::Particle::LorentzVector p(1.0, 2.0, 3.0, 4.0);
  reco::Particle::PolarLorentzVector pp(p);
  reco::Particle t(q, p, vtx);
  testAbs(t, q, vtx, p, pp);

  auto t2 = stream(t);
  testAbs(t2, q, vtx, p, pp);

  reco::Particle::LorentzVector p1(1.1, 2.2, 3.3, 4.4);
  reco::Particle::PolarLorentzVector pp1(p1);
  t.setP4(p1);
  testAbs(t, q, vtx, p1, pp1);
  t.setMass(mass);
  pp1.SetM(mass);
  p1 = reco::Particle::LorentzVector(pp1);
  testAbs(t, q, vtx, p1, pp1);
  t.setPz(pz);
  p1.SetPz(pz);
  pp1 = reco::Particle::PolarLorentzVector(p1);
  testAbs(t, q, vtx, p1, pp1);
  t.setVertex(vtx1);
  testAbs(t, q, vtx1, p1, pp1);
  t.setCharge(q1);
  testAbs(t, q1, vtx1, p1, pp1);
  t.setThreeCharge(q1);
  testAbs(t, q1 / 3, vtx1, p1, pp1);
}

void testParticle::checkPolar() {
  reco::Particle::PolarLorentzVector pp(1.0, 2.0, 3.0, 4.0);
  reco::Particle::LorentzVector p(pp);
  reco::Particle t(q, pp, vtx);
  testAbs(t, q, vtx, p, pp);

  auto t2 = stream(t);
  testAbs(t2, q, vtx, p, pp);

  reco::Particle::PolarLorentzVector pp1(1.1, 2.2, 3.3, 4.4);
  reco::Particle::LorentzVector p1(pp1);
  t.setP4(pp1);
  testAbs(t, q, vtx, p1, pp1);
  t.setMass(mass);
  pp1.SetM(mass);
  p1 = reco::Particle::LorentzVector(pp1);
  testAbs(t, q, vtx, p1, pp1);
  t.setPz(pz);
  p1.SetPz(pz);
  pp1 = reco::Particle::PolarLorentzVector(p1);
  testAbs(t, q, vtx, p1, pp1);
  t.setVertex(vtx1);
  testAbs(t, q, vtx1, p1, pp1);
  t.setCharge(q1);
  testAbs(t, q1, vtx1, p1, pp1);
  t.setThreeCharge(q1);
  testAbs(t, q1 / 3, vtx1, p1, pp1);
}

void testParticle::checkFloat() {
  reco::Particle::PolarLorentzVector pp(1.0, 2.0, 3.0, 4.0);
  PtEtaPhiMass pepm(1.0f, 2.0f, 3.0f, 4.0f);
  reco::Particle::LorentzVector p(pp);
  reco::Particle t(q, pepm, vtx);

  testAbs(t, q, vtx, p, pp);

  auto t2 = stream(t);
  testAbs(t2, q, vtx, p, pp);

  reco::Particle::PolarLorentzVector pp1(1.1, 2.2, 3.3, 4.4);
  reco::Particle::LorentzVector p1(pp1);
  t.setP4(pp1);
  testAbs(t, q, vtx, p1, pp1);
  t.setMass(mass);
  pp1.SetM(mass);
  p1 = reco::Particle::LorentzVector(pp1);
  testAbs(t, q, vtx, p1, pp1);
  t.setPz(pz);
  p1.SetPz(pz);
  pp1 = reco::Particle::PolarLorentzVector(p1);
  testAbs(t, q, vtx, p1, pp1);
  t.setVertex(vtx1);
  testAbs(t, q, vtx1, p1, pp1);
  t.setCharge(q1);
  testAbs(t, q1, vtx1, p1, pp1);
  t.setThreeCharge(q1);
  testAbs(t, q1 / 3, vtx1, p1, pp1);
}

void testParticle::checkXYZLarge() {
  auto E = std::sqrt(321. * 321. + 163. * 163. + 678 * 678. + 11.21 * 11.21);
  reco::Particle::LorentzVector p(321., 163., 678., E);
  reco::Particle::PolarLorentzVector pp(p);
  reco::Particle t(q, p, vtx);
  testRel(t, q, vtx, p, pp);

  auto t2 = stream(t);
  testRel(t2, q, vtx, p, pp);

  reco::Particle::LorentzVector p1(321.1, 163.1, 678.1, E + 0.5);
  reco::Particle::PolarLorentzVector pp1(p1);
  t.setP4(p1);
  testRel(t, q, vtx, p1, pp1);
  t.setMass(mass);
  pp1.SetM(mass);
  p1 = reco::Particle::LorentzVector(pp1);
  testRel(t, q, vtx, p1, pp1);
  t.setPz(pz);
  p1.SetPz(pz);
  pp1 = reco::Particle::PolarLorentzVector(p1);
  testRel(t, q, vtx, p1, pp1);
  t.setVertex(vtx1);
  testRel(t, q, vtx1, p1, pp1);
  t.setCharge(q1);
  testRel(t, q1, vtx1, p1, pp1);
  t.setThreeCharge(q1);
  testRel(t, q1 / 3, vtx1, p1, pp1);
}

void testParticle::checkPolarLarge() {
  reco::Particle::PolarLorentzVector pp(1234.56, 1.96, 2.45, 11.21);
  reco::Particle::LorentzVector p(pp);
  reco::Particle t(q, pp, vtx);
  testRel(t, q, vtx, p, pp);

  auto t2 = stream(t);
  testRel(t2, q, vtx, p, pp);

  reco::Particle::PolarLorentzVector pp1(1234.76, 1.86, 2.75, 11.11);
  reco::Particle::LorentzVector p1(pp1);
  t.setP4(pp1);
  testRel(t, q, vtx, p1, pp1);
  t.setMass(mass);
  pp1.SetM(mass);
  p1 = reco::Particle::LorentzVector(pp1);
  testRel(t, q, vtx, p1, pp1);
  t.setPz(pz);
  p1.SetPz(pz);
  pp1 = reco::Particle::PolarLorentzVector(p1);
  testRel(t, q, vtx, p1, pp1);
  t.setVertex(vtx1);
  testRel(t, q, vtx1, p1, pp1);
  t.setCharge(q1);
  testRel(t, q1, vtx1, p1, pp1);
  t.setThreeCharge(q1);
  testRel(t, q1 / 3, vtx1, p1, pp1);
}

void testParticle::checkFloatLarge() {
  reco::Particle::PolarLorentzVector pp(1234.56, 1.96, 2.45, 11.21);
  PtEtaPhiMass pepm(1234.56f, 1.96f, 2.45f, 11.21f);
  reco::Particle::LorentzVector p(pp);
  reco::Particle t(q, pepm, vtx);

  testRel(t, q, vtx, p, pp);

  auto t2 = stream(t);
  testRel(t2, q, vtx, p, pp);

  reco::Particle::PolarLorentzVector pp1(1.1, 2.2, 3.3, 4.4);
  reco::Particle::LorentzVector p1(pp1);
  t.setP4(pp1);
  testRel(t, q, vtx, p1, pp1);
  t.setMass(mass);
  pp1.SetM(mass);
  p1 = reco::Particle::LorentzVector(pp1);
  testRel(t, q, vtx, p1, pp1);
  t.setPz(pz);
  p1.SetPz(pz);
  pp1 = reco::Particle::PolarLorentzVector(p1);
  testRel(t, q, vtx, p1, pp1);
  t.setVertex(vtx1);
  testRel(t, q, vtx1, p1, pp1);
  t.setCharge(q1);
  testRel(t, q1, vtx1, p1, pp1);
  t.setThreeCharge(q1);
  testRel(t, q1 / 3, vtx1, p1, pp1);
}