Skip to content

Commit

Permalink
Merge pull request #12583 from makortel/packedCandidateFixes76x
Browse files Browse the repository at this point in the history
Fixes to PackedCandidate (76X)
  • Loading branch information
davidlange6 committed Dec 3, 2015
2 parents 9291ec1 + 1729af1 commit 063e673
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 20 deletions.
39 changes: 22 additions & 17 deletions DataFormats/PatCandidates/interface/liblogintpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@

namespace logintpack
{
constexpr int8_t smallestPositive = 0;
// note that abs(unpack(smallestNegative)) == unpack(1), i.e. there
// is no "x" such that "unpack(x) == -unpack(0)"
constexpr int8_t smallestNegative = -1;

int8_t pack8logCeil(double x,double lmin, double lmax, uint8_t base=128)
{
if(base>128) base=128;
float l =log(fabs(x));
float centered = (l-lmin)/(lmax-lmin)*base;
int8_t r=ceil(centered);
const double l = std::log(std::abs(x));
const double centered = (l-lmin)/(lmax-lmin)*base;
int8_t r=std::ceil(centered);
if(centered >= base-1) r=base-1;
if(centered < 0) r=0;
if(x<0) r=-r;
if(x<0) r = r==0 ? -1 : -r;
return r;
}

int8_t pack8log(double x,double lmin, double lmax, uint8_t base=128)
{
if(base>128) base=128;
float l =log(fabs(x));
float centered = (l-lmin)/(lmax-lmin)*base;
const double l = std::log(std::abs(x));
const double centered = (l-lmin)/(lmax-lmin)*base;
int8_t r=centered;
if(centered >= base-1) r=base-1;
if(centered < 0) r=0;
if(x<0) r=-r;
if(x<0) r = r==0 ? -1 : -r;
return r;
}

Expand All @@ -34,33 +39,33 @@ namespace logintpack
int8_t pack8logClosed(double x,double lmin, double lmax, uint8_t base=128)
{
if(base>128) base=128;
float l =log(fabs(x));
float centered = (l-lmin)/(lmax-lmin)*(base-1);
const double l = std::log(std::abs(x));
const double centered = (l-lmin)/(lmax-lmin)*(base-1);
int8_t r=round(centered);
if(centered >= base-1) r=base-1;
if(centered < 0) r=0;
if(x<0) r=-r;
if(x<0) r = r==0 ? -1 : -r;
return r;
}


double unpack8log(int8_t i,double lmin, double lmax, uint8_t base=128)
{
if(base>128) base=128;
float basef=base;
float l=lmin+abs(i)/basef*(lmax-lmin);
float val=exp(l);
const double basef=base;
const double l=lmin+std::abs(i)/basef*(lmax-lmin);
const double val=std::exp(l);
if(i<0) return -val; else return val;
}

/// reverse of pack8logClosed
double unpack8logClosed(int8_t i,double lmin, double lmax, uint8_t base=128)
{
if(base>128) base=128;
float basef=base-1;
float l=lmin+abs(i)/basef*(lmax-lmin);
if (abs(i) == base-1) l = lmax;
float val=exp(l);
const double basef=base-1;
double l=lmin+std::abs(i)/basef*(lmax-lmin);
if (std::abs(i) == base-1) l = lmax;
const double val=std::exp(l);
if(i<0) return -val; else return val;
}

Expand Down
6 changes: 4 additions & 2 deletions DataFormats/PatCandidates/src/PackedCandidate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,13 @@ pat::PackedCandidate::~PackedCandidate() {

float pat::PackedCandidate::dxy(const Point &p) const {
maybeUnpackBoth();
return -(vertex_.load()->X()-p.X()) * std::sin(float(p4_.load()->Phi())) + (vertex_.load()->Y()-p.Y()) * std::cos(float(p4_.load()->Phi()));
const float phi = float(p4_.load()->Phi())+dphi_;
return -(vertex_.load()->X()-p.X()) * std::sin(phi) + (vertex_.load()->Y()-p.Y()) * std::cos(phi);
}
float pat::PackedCandidate::dz(const Point &p) const {
maybeUnpackBoth();
return (vertex_.load()->Z()-p.Z()) - ((vertex_.load()->X()-p.X()) * std::cos(float(p4_.load()->Phi())) + (vertex_.load()->Y()-p.Y()) * std::sin(float(p4_.load()->Phi()))) * p4_.load()->Pz()/p4_.load()->Pt();
const float phi = float(p4_.load()->Phi())+dphi_;
return (vertex_.load()->Z()-p.Z()) - ((vertex_.load()->X()-p.X()) * std::cos(phi) + (vertex_.load()->Y()-p.Y()) * std::sin(phi)) * p4_.load()->Pz()/p4_.load()->Pt();
}

void pat::PackedCandidate::unpackTrk() const {
Expand Down
2 changes: 1 addition & 1 deletion DataFormats/PatCandidates/test/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<use name="boost"/>
<use name="cppunit"/>
<use name="DataFormats/PatCandidates"/>
<bin name="testDataFormatsPatCandidates" file="testPackedCandidate.cc,testPackedGenParticle.cc,testRunner.cpp"/>
<bin name="testDataFormatsPatCandidates" file="testPackedCandidate.cc,testPackedGenParticle.cc,testlogintpack.cpp,testRunner.cpp"/>
<bin name="testKinResolutions" file="testKinParametrizations.cc,testKinResolutions.cc,testRunner.cpp">
<flags NO_TESTRUN="1"/>
</bin>
92 changes: 92 additions & 0 deletions DataFormats/PatCandidates/test/testlogintpack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <cppunit/extensions/HelperMacros.h>
#include <iostream>
#include <iomanip>
#include <limits>

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

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

CPPUNIT_TEST(test);

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

void test();

private:
};

namespace {
int8_t pack(double x) { return logintpack::pack8log (x, -15, 0); }
int8_t packceil(double x) { return logintpack::pack8logCeil (x, -15, 0); }
int8_t packclosed(double x) { return logintpack::pack8log (x, -15, 0); }
double unpack(int8_t x) { return logintpack::unpack8log (x, -15, 0); }
double unpackclosed(int8_t x) { return logintpack::unpack8logClosed(x, -15, 0); }
}

void testlogintpack::test() {
using logintpack::smallestPositive;
using logintpack::smallestNegative;
constexpr int8_t largestPositive = 127;
constexpr int8_t largestNegative = -127;

const double smallestValuePos = std::exp(-15.);
const double smallestValueNeg = -std::exp(-15.+1./128.*15.);
const double smallestValueNegForClosed = -std::exp(-15.+1./127.*15.);
CPPUNIT_ASSERT(pack(smallestValuePos) == smallestPositive);
CPPUNIT_ASSERT(packceil(smallestValuePos) == smallestPositive);
CPPUNIT_ASSERT(packclosed(smallestValuePos) == smallestPositive);
CPPUNIT_ASSERT(unpack(smallestPositive) == smallestValuePos);
CPPUNIT_ASSERT(unpackclosed(smallestPositive) == smallestValuePos);

CPPUNIT_ASSERT(pack(smallestValueNeg) == smallestNegative);
CPPUNIT_ASSERT(packceil(smallestValueNeg) == smallestNegative);
CPPUNIT_ASSERT(unpack(smallestNegative) == smallestValueNeg);
CPPUNIT_ASSERT(unpack(pack(smallestValueNeg)) == smallestValueNeg);
CPPUNIT_ASSERT(unpack(packceil(smallestValueNeg)) == smallestValueNeg);
CPPUNIT_ASSERT(unpackclosed(packclosed(smallestValueNegForClosed)) == smallestValueNegForClosed);

const double largestValuePos = std::exp(-15.+127./128.*15.);
const double largestValueNeg = -largestValuePos;
CPPUNIT_ASSERT(pack(largestValuePos) == largestPositive);
CPPUNIT_ASSERT(packceil(largestValuePos) == largestPositive);
CPPUNIT_ASSERT(unpack(largestPositive) == largestValuePos);

CPPUNIT_ASSERT(pack(largestValueNeg) == largestNegative);
CPPUNIT_ASSERT(packceil(largestValueNeg) == largestNegative);
CPPUNIT_ASSERT(unpack(largestNegative) == largestValueNeg);

const double largestValueClosedPos = std::exp(0.);
const double largestValueClosedNeg = -largestValueClosedPos;
CPPUNIT_ASSERT(packclosed(largestValueClosedPos) == largestPositive);
CPPUNIT_ASSERT(unpackclosed(largestPositive) == largestValueClosedPos);
CPPUNIT_ASSERT(packclosed(largestValueClosedNeg) == largestNegative);
CPPUNIT_ASSERT(unpackclosed(largestNegative) == largestValueClosedNeg);

const double someValue = std::exp(-15. + 1/128.*15.);
const float someValueFloat = std::exp(-15.f + 1/128.f*15.f);
CPPUNIT_ASSERT(unpack(packceil(someValue)) == someValue);
CPPUNIT_ASSERT(static_cast<float>(unpack(packceil(someValue))) == someValueFloat);
{
union { float flt; uint32_t i32; } conv;
conv.flt = someValueFloat;
conv.i32 += 1;
const float someValuePlus1Ulp32 = conv.flt;
CPPUNIT_ASSERT(static_cast<float>(unpack(packceil(someValuePlus1Ulp32))) >= someValuePlus1Ulp32);
}
{
union { double flt; uint64_t i64; } conv;
conv.flt = someValue;
conv.i64 += 1;
const float someValuePlus1Ulp64 = conv.flt;
CPPUNIT_ASSERT(unpack(packceil(someValuePlus1Ulp64)) >= someValuePlus1Ulp64);
}

}

CPPUNIT_TEST_SUITE_REGISTRATION(testlogintpack);

0 comments on commit 063e673

Please sign in to comment.