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/Particle.h"
0003 #include "TBufferFile.h"
0004 #include "TClass.h"
0005 #include <iostream>
0006 
0007 class testParticle : public CppUnit::TestFixture {
0008   CPPUNIT_TEST_SUITE(testParticle);
0009   CPPUNIT_TEST(checkXYZ);
0010   CPPUNIT_TEST(checkPolar);
0011   CPPUNIT_TEST(checkFloat);
0012   CPPUNIT_TEST(checkXYZLarge);
0013   CPPUNIT_TEST(checkPolarLarge);
0014   CPPUNIT_TEST(checkFloatLarge);
0015   CPPUNIT_TEST_SUITE_END();
0016 
0017 public:
0018   void setUp() {}
0019   void tearDown() {}
0020   void checkXYZ();
0021   void checkPolar();
0022   void checkFloat();
0023   void checkXYZLarge();
0024   void checkPolarLarge();
0025   void checkFloatLarge();
0026 
0027   void testAbs(const reco::Particle &t,
0028                reco::Particle::Charge q,
0029                const reco::Particle::Point &,
0030                const reco::Particle::LorentzVector &p,
0031                const reco::Particle::PolarLorentzVector &pp);
0032   void testRel(const reco::Particle &t,
0033                reco::Particle::Charge q,
0034                const reco::Particle::Point &,
0035                const reco::Particle::LorentzVector &p,
0036                const reco::Particle::PolarLorentzVector &pp);
0037 
0038   const double mass = 123.456;
0039   const double pz = 100.000;
0040   const reco::Particle::Point vtx = reco::Particle::Point(6, 7, 8);
0041   const reco::Particle::Point vtx1 = reco::Particle::Point(8, 7, 6);
0042   const reco::Particle::Charge q = 1;
0043   const reco::Particle::Charge q1 = 9;
0044 };
0045 
0046 CPPUNIT_TEST_SUITE_REGISTRATION(testParticle);
0047 
0048 void testParticle::testAbs(const reco::Particle &t,
0049                            reco::Particle::Charge q,
0050                            const reco::Particle::Point &vtx,
0051                            const reco::Particle::LorentzVector &p,
0052                            const reco::Particle::PolarLorentzVector &pp) {
0053   const double epsilon = 1.e-5;
0054   const double epsilon2 = 5.e-4;
0055   CPPUNIT_ASSERT(t.charge() == q);
0056   CPPUNIT_ASSERT(t.threeCharge() == q * 3);
0057   CPPUNIT_ASSERT((t.polarP4() - pp).M2() < epsilon);
0058   CPPUNIT_ASSERT((t.p4() - p).M2() < epsilon);
0059   CPPUNIT_ASSERT(std::abs(t.p() - p.P()) < epsilon);
0060   CPPUNIT_ASSERT(std::abs(t.energy() - p.E()) < epsilon);
0061   CPPUNIT_ASSERT(std::abs(t.px() - p.Px()) < epsilon);
0062   CPPUNIT_ASSERT(std::abs(t.py() - p.Py()) < epsilon);
0063   CPPUNIT_ASSERT(std::abs(t.pz() - p.Pz()) < epsilon);
0064   CPPUNIT_ASSERT(std::abs(t.phi() - p.Phi()) < epsilon);
0065   CPPUNIT_ASSERT(std::abs(t.theta() - p.Theta()) < epsilon);
0066   CPPUNIT_ASSERT(std::abs(t.eta() - p.Eta()) < epsilon);
0067   CPPUNIT_ASSERT(std::abs(t.mass() - p.M()) < epsilon);
0068   CPPUNIT_ASSERT(std::abs(t.massSqr() - p.M2()) < epsilon2);
0069   CPPUNIT_ASSERT(std::abs(t.mt() - p.Mt()) < epsilon);
0070   CPPUNIT_ASSERT(std::abs(t.mtSqr() - p.Mt2()) < epsilon2);
0071   CPPUNIT_ASSERT(std::abs(t.rapidity() - p.Rapidity()) < epsilon);
0072   CPPUNIT_ASSERT(std::abs(t.vx() - vtx.x()) < epsilon);
0073   CPPUNIT_ASSERT(std::abs(t.vy() - vtx.y()) < epsilon);
0074   CPPUNIT_ASSERT(std::abs(t.vz() - vtx.z()) < epsilon);
0075 }
0076 
0077 void testParticle::testRel(const reco::Particle &t,
0078                            reco::Particle::Charge q,
0079                            const reco::Particle::Point &vtx,
0080                            const reco::Particle::LorentzVector &p,
0081                            const reco::Particle::PolarLorentzVector &pp) {
0082   const double epsilon = 1.e-5;
0083   const double epsilon2 = 5.e-4;
0084   CPPUNIT_ASSERT(t.charge() == q);
0085   CPPUNIT_ASSERT(t.threeCharge() == q * 3);
0086   // these should be zero

0087   CPPUNIT_ASSERT((t.polarP4() - pp).M2() < epsilon);
0088   CPPUNIT_ASSERT((t.p4() - p).M2() < epsilon);
0089 
0090   CPPUNIT_ASSERT(std::abs(t.p() - p.P()) / p.P() < epsilon);
0091   CPPUNIT_ASSERT(std::abs(t.energy() - p.E()) / p.E() < epsilon);
0092 
0093   CPPUNIT_ASSERT(std::abs((t.px() - p.px()) / p.px()) < epsilon);
0094   CPPUNIT_ASSERT(std::abs((t.py() - p.py()) / p.py()) < epsilon);
0095   CPPUNIT_ASSERT(std::abs((t.pz() - p.pz()) / p.pz()) < epsilon);
0096   // here resolution should be absolute no matter what

0097   CPPUNIT_ASSERT(std::abs(t.phi() - p.Phi()) < epsilon);
0098   CPPUNIT_ASSERT(std::abs(t.theta() - p.Theta()) < epsilon);
0099   CPPUNIT_ASSERT(std::abs(t.eta() - p.Eta()) < epsilon);
0100   CPPUNIT_ASSERT(std::abs(t.rapidity() - p.Rapidity()) < epsilon);
0101 
0102   CPPUNIT_ASSERT(std::abs((t.mass() - p.M()) / p.M()) < epsilon);
0103   CPPUNIT_ASSERT(std::abs((t.massSqr() - p.M2()) / p.M2()) < epsilon2);
0104   CPPUNIT_ASSERT(std::abs((t.mt() - p.Mt()) / p.Mt()) < epsilon);
0105   CPPUNIT_ASSERT(std::abs((t.mtSqr() - p.Mt2()) / p.Mt2()) < epsilon2);
0106 
0107   // not really tested....  absolute makes sense

0108   CPPUNIT_ASSERT(std::abs(t.vx() - vtx.x()) < epsilon);
0109   CPPUNIT_ASSERT(std::abs(t.vy() - vtx.y()) < epsilon);
0110   CPPUNIT_ASSERT(std::abs(t.vz() - vtx.z()) < epsilon);
0111 }
0112 
0113 reco::Particle stream(reco::Particle &t) {
0114   auto m_class = TClass::GetClass(typeid(reco::Particle));
0115   TBufferFile wbuffer(TBufferFile::kWrite);
0116   wbuffer.InitMap();
0117   wbuffer.StreamObject((&t), m_class);
0118 
0119   TBufferFile rbuffer(TBufferFile::kRead, wbuffer.Length(), (wbuffer.Buffer()), kFALSE);
0120 
0121   rbuffer.InitMap();
0122 
0123   reco::Particle t2;
0124   rbuffer.StreamObject(&t2, m_class);
0125   return t2;
0126 }
0127 
0128 void testParticle::checkXYZ() {
0129   reco::Particle::LorentzVector p(1.0, 2.0, 3.0, 4.0);
0130   reco::Particle::PolarLorentzVector pp(p);
0131   reco::Particle t(q, p, vtx);
0132   testAbs(t, q, vtx, p, pp);
0133 
0134   auto t2 = stream(t);
0135   testAbs(t2, q, vtx, p, pp);
0136 
0137   reco::Particle::LorentzVector p1(1.1, 2.2, 3.3, 4.4);
0138   reco::Particle::PolarLorentzVector pp1(p1);
0139   t.setP4(p1);
0140   testAbs(t, q, vtx, p1, pp1);
0141   t.setMass(mass);
0142   pp1.SetM(mass);
0143   p1 = reco::Particle::LorentzVector(pp1);
0144   testAbs(t, q, vtx, p1, pp1);
0145   t.setPz(pz);
0146   p1.SetPz(pz);
0147   pp1 = reco::Particle::PolarLorentzVector(p1);
0148   testAbs(t, q, vtx, p1, pp1);
0149   t.setVertex(vtx1);
0150   testAbs(t, q, vtx1, p1, pp1);
0151   t.setCharge(q1);
0152   testAbs(t, q1, vtx1, p1, pp1);
0153   t.setThreeCharge(q1);
0154   testAbs(t, q1 / 3, vtx1, p1, pp1);
0155 }
0156 
0157 void testParticle::checkPolar() {
0158   reco::Particle::PolarLorentzVector pp(1.0, 2.0, 3.0, 4.0);
0159   reco::Particle::LorentzVector p(pp);
0160   reco::Particle t(q, pp, vtx);
0161   testAbs(t, q, vtx, p, pp);
0162 
0163   auto t2 = stream(t);
0164   testAbs(t2, q, vtx, p, pp);
0165 
0166   reco::Particle::PolarLorentzVector pp1(1.1, 2.2, 3.3, 4.4);
0167   reco::Particle::LorentzVector p1(pp1);
0168   t.setP4(pp1);
0169   testAbs(t, q, vtx, p1, pp1);
0170   t.setMass(mass);
0171   pp1.SetM(mass);
0172   p1 = reco::Particle::LorentzVector(pp1);
0173   testAbs(t, q, vtx, p1, pp1);
0174   t.setPz(pz);
0175   p1.SetPz(pz);
0176   pp1 = reco::Particle::PolarLorentzVector(p1);
0177   testAbs(t, q, vtx, p1, pp1);
0178   t.setVertex(vtx1);
0179   testAbs(t, q, vtx1, p1, pp1);
0180   t.setCharge(q1);
0181   testAbs(t, q1, vtx1, p1, pp1);
0182   t.setThreeCharge(q1);
0183   testAbs(t, q1 / 3, vtx1, p1, pp1);
0184 }
0185 
0186 void testParticle::checkFloat() {
0187   reco::Particle::PolarLorentzVector pp(1.0, 2.0, 3.0, 4.0);
0188   PtEtaPhiMass pepm(1.0f, 2.0f, 3.0f, 4.0f);
0189   reco::Particle::LorentzVector p(pp);
0190   reco::Particle t(q, pepm, vtx);
0191 
0192   testAbs(t, q, vtx, p, pp);
0193 
0194   auto t2 = stream(t);
0195   testAbs(t2, q, vtx, p, pp);
0196 
0197   reco::Particle::PolarLorentzVector pp1(1.1, 2.2, 3.3, 4.4);
0198   reco::Particle::LorentzVector p1(pp1);
0199   t.setP4(pp1);
0200   testAbs(t, q, vtx, p1, pp1);
0201   t.setMass(mass);
0202   pp1.SetM(mass);
0203   p1 = reco::Particle::LorentzVector(pp1);
0204   testAbs(t, q, vtx, p1, pp1);
0205   t.setPz(pz);
0206   p1.SetPz(pz);
0207   pp1 = reco::Particle::PolarLorentzVector(p1);
0208   testAbs(t, q, vtx, p1, pp1);
0209   t.setVertex(vtx1);
0210   testAbs(t, q, vtx1, p1, pp1);
0211   t.setCharge(q1);
0212   testAbs(t, q1, vtx1, p1, pp1);
0213   t.setThreeCharge(q1);
0214   testAbs(t, q1 / 3, vtx1, p1, pp1);
0215 }
0216 
0217 void testParticle::checkXYZLarge() {
0218   auto E = std::sqrt(321. * 321. + 163. * 163. + 678 * 678. + 11.21 * 11.21);
0219   reco::Particle::LorentzVector p(321., 163., 678., E);
0220   reco::Particle::PolarLorentzVector pp(p);
0221   reco::Particle t(q, p, vtx);
0222   testRel(t, q, vtx, p, pp);
0223 
0224   auto t2 = stream(t);
0225   testRel(t2, q, vtx, p, pp);
0226 
0227   reco::Particle::LorentzVector p1(321.1, 163.1, 678.1, E + 0.5);
0228   reco::Particle::PolarLorentzVector pp1(p1);
0229   t.setP4(p1);
0230   testRel(t, q, vtx, p1, pp1);
0231   t.setMass(mass);
0232   pp1.SetM(mass);
0233   p1 = reco::Particle::LorentzVector(pp1);
0234   testRel(t, q, vtx, p1, pp1);
0235   t.setPz(pz);
0236   p1.SetPz(pz);
0237   pp1 = reco::Particle::PolarLorentzVector(p1);
0238   testRel(t, q, vtx, p1, pp1);
0239   t.setVertex(vtx1);
0240   testRel(t, q, vtx1, p1, pp1);
0241   t.setCharge(q1);
0242   testRel(t, q1, vtx1, p1, pp1);
0243   t.setThreeCharge(q1);
0244   testRel(t, q1 / 3, vtx1, p1, pp1);
0245 }
0246 
0247 void testParticle::checkPolarLarge() {
0248   reco::Particle::PolarLorentzVector pp(1234.56, 1.96, 2.45, 11.21);
0249   reco::Particle::LorentzVector p(pp);
0250   reco::Particle t(q, pp, vtx);
0251   testRel(t, q, vtx, p, pp);
0252 
0253   auto t2 = stream(t);
0254   testRel(t2, q, vtx, p, pp);
0255 
0256   reco::Particle::PolarLorentzVector pp1(1234.76, 1.86, 2.75, 11.11);
0257   reco::Particle::LorentzVector p1(pp1);
0258   t.setP4(pp1);
0259   testRel(t, q, vtx, p1, pp1);
0260   t.setMass(mass);
0261   pp1.SetM(mass);
0262   p1 = reco::Particle::LorentzVector(pp1);
0263   testRel(t, q, vtx, p1, pp1);
0264   t.setPz(pz);
0265   p1.SetPz(pz);
0266   pp1 = reco::Particle::PolarLorentzVector(p1);
0267   testRel(t, q, vtx, p1, pp1);
0268   t.setVertex(vtx1);
0269   testRel(t, q, vtx1, p1, pp1);
0270   t.setCharge(q1);
0271   testRel(t, q1, vtx1, p1, pp1);
0272   t.setThreeCharge(q1);
0273   testRel(t, q1 / 3, vtx1, p1, pp1);
0274 }
0275 
0276 void testParticle::checkFloatLarge() {
0277   reco::Particle::PolarLorentzVector pp(1234.56, 1.96, 2.45, 11.21);
0278   PtEtaPhiMass pepm(1234.56f, 1.96f, 2.45f, 11.21f);
0279   reco::Particle::LorentzVector p(pp);
0280   reco::Particle t(q, pepm, vtx);
0281 
0282   testRel(t, q, vtx, p, pp);
0283 
0284   auto t2 = stream(t);
0285   testRel(t2, q, vtx, p, pp);
0286 
0287   reco::Particle::PolarLorentzVector pp1(1.1, 2.2, 3.3, 4.4);
0288   reco::Particle::LorentzVector p1(pp1);
0289   t.setP4(pp1);
0290   testRel(t, q, vtx, p1, pp1);
0291   t.setMass(mass);
0292   pp1.SetM(mass);
0293   p1 = reco::Particle::LorentzVector(pp1);
0294   testRel(t, q, vtx, p1, pp1);
0295   t.setPz(pz);
0296   p1.SetPz(pz);
0297   pp1 = reco::Particle::PolarLorentzVector(p1);
0298   testRel(t, q, vtx, p1, pp1);
0299   t.setVertex(vtx1);
0300   testRel(t, q, vtx1, p1, pp1);
0301   t.setCharge(q1);
0302   testRel(t, q1, vtx1, p1, pp1);
0303   t.setThreeCharge(q1);
0304   testRel(t, q1 / 3, vtx1, p1, pp1);
0305 }