Cmp.hh
// -*- C++ -*-
#ifndef RIVET_Cmp_HH
#define RIVET_Cmp_HH
#include "Rivet/Rivet.hh"
#include "Rivet/Projection.hh"
#include "Cmp.fhh"
#include <typeinfo>
namespace Rivet {
template <typename T>
class Cmp {
public:
Cmp(const T& t1, const T& t2)
: _value(UNDEFINED), _objects(&t1, &t2) { }
template <typename U>
Cmp(const Cmp<U>& x)
: _value(x), _objects(0, 0) { }
~Cmp() { };
template <typename U>
const Cmp<T>& operator=(const Cmp<U>& x) {
_value = x;
return *this;
}
public:
operator CmpState() const {
_compare();
return _value;
}
operator int() const {
_compare();
return _value;
}
template <typename U>
const Cmp<T>& operator||(const Cmp<U>& c) const {
_compare();
if (_value == EQUIVALENT) _value = c;
return *this;
}
private:
void _compare() const {
if (_value == UNDEFINED) {
less<T> l;
if ( l(*_objects.first, *_objects.second) ) _value = ORDERED;
else if ( l(*_objects.second, *_objects.first) ) _value = UNORDERED;
else _value = EQUIVALENT;
}
}
mutable CmpState _value;
pair<const T*, const T*> _objects;
};
template <>
class Cmp<Projection> {
public:
Cmp(const Projection& p1, const Projection& p2)
: _value(UNDEFINED), _objects(&p1, &p2)
{ }
template <typename U>
Cmp(const Cmp<U>& x)
: _value(x), _objects(0, 0)
{ }
~Cmp() { };
template <typename U>
const Cmp<Projection>& operator=(const Cmp<U>& x) {
_value = x;
return *this;
}
public:
operator CmpState() const {
_compare();
return _value;
}
operator int() const {
_compare();
return _value;
}
template <typename U>
const Cmp<Projection>& operator||(const Cmp<U>& c) const {
_compare();
if (_value == EQUIVALENT) _value = c;
return *this;
}
private:
void _compare() const {
if (_value == UNDEFINED) {
const std::type_info& id1 = typeid(*_objects.first);
const std::type_info& id2 = typeid(*_objects.second);
if (id1.before(id2)) _value = ORDERED;
else if (id2.before(id1)) _value = UNORDERED;
else {
int c = _objects.first->compare(*_objects.second);
if (c < 0) _value = ORDERED;
else if (c > 0) _value = UNORDERED;
else _value = EQUIVALENT;
}
}
}
private:
mutable CmpState _value;
pair<const Projection*, const Projection*> _objects;
};
template <>
class Cmp<double> {
public:
Cmp(const double p1, const double p2)
: _value(UNDEFINED), _numA(p1), _numB(p2)
{ }
template <typename U>
Cmp(const Cmp<U>& x)
: _value(x), _numA(0.0), _numB(0.0)
{ }
~Cmp() { }
template <typename U>
const Cmp<double>& operator=(const Cmp<U>& x) {
_value = x;
return *this;
}
public:
operator CmpState() const {
_compare();
return _value;
}
operator int() const {
_compare();
return _value;
}
template <typename U>
const Cmp<double>& operator||(const Cmp<U>& c) const {
_compare();
if (_value == EQUIVALENT) _value = c;
return *this;
}
private:
void _compare() const {
if (_value == UNDEFINED) {
if (fuzzyEquals(_numA,_numB)) _value = EQUIVALENT;
else if (_numA < _numB) _value = ORDERED;
else _value = UNORDERED;
}
}
private:
mutable CmpState _value;
double _numA, _numB;
};
template <typename T>
inline Cmp<T> cmp(const T& t1, const T& t2) {
return Cmp<T>(t1, t2);
}
typedef Cmp<Projection> PCmp;
inline Cmp<Projection> pcmp(const Projection& p1, const Projection& p2) {
return Cmp<Projection>(p1, p2);
}
inline Cmp<Projection> pcmp(const Projection& parent1, const Projection& parent2, const string& pname) {
return Cmp<Projection>(parent1.getProjection(pname), parent2.getProjection(pname));
}
inline Cmp<Projection> pcmp(const Projection* parent1, const Projection& parent2, const string& pname) {
assert(parent1);
return Cmp<Projection>(parent1->getProjection(pname), parent2.getProjection(pname));
}
inline Cmp<Projection> pcmp(const Projection& parent1, const Projection* parent2, const string& pname) {
assert(parent2);
return Cmp<Projection>(parent1.getProjection(pname), parent2->getProjection(pname));
}
inline Cmp<Projection> pcmp(const Projection* parent1, const Projection* parent2, const string& pname) {
assert(parent1);
assert(parent2);
return Cmp<Projection>(parent1->getProjection(pname), parent2->getProjection(pname));
}
}
#endif
Rivet
Definition: MC_JetAnalysis.hh:9
Rivet::Cmp::Cmp
Cmp(const Cmp< U > &x)
The copy constructor.
Definition: Cmp.hh:39
Rivet::Cmp< double >::operator||
const Cmp< double > & operator||(const Cmp< U > &c) const
If this state is equivalent, set this state to the state of c.
Definition: Cmp.hh:250
Rivet::Cmp< double >::Cmp
Cmp(const Cmp< U > &x)
The copy constructor.
Definition: Cmp.hh:219
Rivet::Cmp< double >::~Cmp
~Cmp()
The destructor is not virtual since this is not intended to be a base class.
Definition: Cmp.hh:224
Rivet::Cmp::Cmp
Cmp(const T &t1, const T &t2)
The default constructor.
Definition: Cmp.hh:34
Rivet::Cmp::~Cmp
~Cmp()
The destructor is not virtual since this is not intended to be a base class.
Definition: Cmp.hh:43
Rivet::Cmp< double >::operator=
const Cmp< double > & operator=(const Cmp< U > &x)
The assignment operator.
Definition: Cmp.hh:228
Rivet::Cmp< Projection >::operator=
const Cmp< Projection > & operator=(const Cmp< U > &x)
The assignment operator.
Definition: Cmp.hh:133
Rivet::Cmp::operator||
const Cmp< T > & operator||(const Cmp< U > &c) const
If this state is equivalent, set this state to the state of c.
Definition: Cmp.hh:70
Rivet::Cmp< Projection >::~Cmp
~Cmp()
The destructor is not virtual since this is not intended to be a base class.
Definition: Cmp.hh:129
Rivet::Cmp< Projection >
Specialization of Cmp for checking the ordering of two {Projection}s.
Definition: Cmp.hh:112
Rivet::pcmp
Cmp< Projection > pcmp(const Projection &p1, const Projection &p2)
Global helper function for easy creation of Cmp<Projection> objects.
Definition: Cmp.hh:295
Rivet::PCmp
Cmp< Projection > PCmp
Typedef for Cmp<Projection>
Definition: Cmp.hh:291
Rivet::Cmp< double >
Specialization of Cmp for checking the ordering of two floating point numbers.
Definition: Cmp.hh:207
Rivet::ProjectionApplier::getProjection
const PROJ & getProjection(const std::string &name) const
Get the named projection, specifying return type via a template argument.
Definition: ProjectionApplier.hh:49
Rivet::Cmp
Definition: Cmp.hh:27
Rivet::Cmp::operator=
const Cmp< T > & operator=(const Cmp< U > &x)
The assignment operator.
Definition: Cmp.hh:47
Rivet::Projection
Base class for all Rivet projections.
Definition: Projection.hh:28
Rivet::fuzzyEquals
bool fuzzyEquals(double a, double b, double tolerance=1E-5)
Compare two floating point numbers for equality with a degree of fuzziness.
Definition: MathUtils.hh:34
Rivet::Cmp< Projection >::operator||
const Cmp< Projection > & operator||(const Cmp< U > &c) const
If this state is equivalent, set this state to the state of c.
Definition: Cmp.hh:156
Rivet::Cmp< Projection >::Cmp
Cmp(const Cmp< U > &x)
The copy constructor.
Definition: Cmp.hh:124
Rivet::cmp
Cmp< T > cmp(const T &t1, const T &t2)
Global helper function for easy creation of Cmp objects.
Definition: Cmp.hh:285