diff --git a/include/Rivet/Tools/RivetYODA.hh b/include/Rivet/Tools/RivetYODA.hh --- a/include/Rivet/Tools/RivetYODA.hh +++ b/include/Rivet/Tools/RivetYODA.hh @@ -1,497 +1,501 @@ #ifndef RIVET_RIVETYODA_HH #define RIVET_RIVETYODA_HH /// @author Andy Buckley /// @date 2009-01-30 /// @author David Grellscheid /// @date 2011-07-18 /// @author David Grellscheid /// @date 2016-09-27 #include "YODA/AnalysisObject.h" #include "YODA/Counter.h" #include "YODA/Histo1D.h" #include "YODA/Histo2D.h" #include "YODA/Profile1D.h" #include "YODA/Profile2D.h" #include "YODA/Scatter1D.h" #include "YODA/Scatter2D.h" #include "YODA/Scatter3D.h" #include #include namespace YODA { typedef std::shared_ptr AnalysisObjectPtr; } namespace Rivet { - class AnalysisObjectWrapper { - public: - virtual ~AnalysisObjectWrapper() {} + class AnalysisObjectWrapper { + public: + virtual ~AnalysisObjectWrapper() {} - virtual YODA::AnalysisObject* operator->() = 0; - virtual YODA::AnalysisObject* operator->() const = 0; - virtual const YODA::AnalysisObject & operator*() const = 0; + virtual YODA::AnalysisObject* operator->() = 0; + virtual YODA::AnalysisObject* operator->() const = 0; + virtual const YODA::AnalysisObject & operator*() const = 0; - /// @todo Rename to setActive(idx) - virtual void setActiveWeightIdx(unsigned int iWeight) = 0; + /// @todo Rename to setActive(idx) + virtual void setActiveWeightIdx(unsigned int iWeight) = 0; - bool operator ==(const AnalysisObjectWrapper& p) { return (this == &p); } - bool operator !=(const AnalysisObjectWrapper& p) { return (this != &p); } + bool operator ==(const AnalysisObjectWrapper& p) { return (this == &p); } + bool operator !=(const AnalysisObjectWrapper& p) { return (this != &p); } - protected: - /// @todo do we need this? - // virtual void reset() = 0; - }; + protected: + /// @todo do we need this? + // virtual void reset() = 0; + }; - /// @todo - /// implement scatter1dptr and scatter2dptr here - /// these need to be multi-weighted eventually. - /* + /// @todo + /// implement scatter1dptr and scatter2dptr here + /// these need to be multi-weighted eventually. + /* class Scatter1DPtr : public AnalysisObjectPtr { - public: - Scatter1DPtr() : _persistent() { } + public: + Scatter1DPtr() : _persistent() { } - Scatter1DPtr(size_t len_of_weightvec, const YODA::Scatter1D& p) { - for (size_t m = 0; m < len_of_weightvec; ++m) - _persistent.push_back(make_shared(p)); - } + Scatter1DPtr(size_t len_of_weightvec, const YODA::Scatter1D& p) { + for (size_t m = 0; m < len_of_weightvec; ++m) + _persistent.push_back(make_shared(p)); + } - bool operator!() const { return !_persistent; } - explicit operator bool() const { return bool(_persistent); } + bool operator!() const { return !_persistent; } + explicit operator bool() const { return bool(_persistent); } - YODA::Scatter1D* operator->() { return _persistent.get(); } + YODA::Scatter1D* operator->() { return _persistent.get(); } - YODA::Scatter1D* operator->() const { return _persistent.get(); } + YODA::Scatter1D* operator->() const { return _persistent.get(); } - YODA::Scatter1D & operator*() { return *_persistent; } + YODA::Scatter1D & operator*() { return *_persistent; } - const YODA::Scatter1D & operator*() const { return *_persistent; } + const YODA::Scatter1D & operator*() const { return *_persistent; } - protected: - vector _persistent; + protected: + vector _persistent; }; class Scatter2DPtr : public AnalysisObjectPtr { - public: - Scatter2DPtr(size_t len_of_weightvec, const YODA::Scatter2D& p) { - for (size_t m = 0; m < len_of_weightvec; ++m) - _persistent.push_back(make_shared(p)); - } + public: + Scatter2DPtr(size_t len_of_weightvec, const YODA::Scatter2D& p) { + for (size_t m = 0; m < len_of_weightvec; ++m) + _persistent.push_back(make_shared(p)); + } - Scatter2DPtr() : _persistent() { } + Scatter2DPtr() : _persistent() { } - bool operator!() { return !_persistent; } - explicit operator bool() { return bool(_persistent); } + bool operator!() { return !_persistent; } + explicit operator bool() { return bool(_persistent); } - YODA::Scatter2D* operator->() { return _persistent.get(); } + YODA::Scatter2D* operator->() { return _persistent.get(); } - YODA::Scatter2D* operator->() const { return _persistent.get(); } + YODA::Scatter2D* operator->() const { return _persistent.get(); } - YODA::Scatter2D & operator*() { return *_persistent; } + YODA::Scatter2D & operator*() { return *_persistent; } - const YODA::Scatter2D & operator*() const { return *_persistent; } + const YODA::Scatter2D & operator*() const { return *_persistent; } - protected: - vector _persistent; + protected: + vector _persistent; }; class Scatter3DPtr : public AnalysisObjectPtr { - public: - Scatter3DPtr(size_t len_of_weightvec, const YODA::Scatter3D& p) { - for (size_t m = 0; m < len_of_weightvec; ++m) - _persistent.push_back(make_shared(p)); - } + public: + Scatter3DPtr(size_t len_of_weightvec, const YODA::Scatter3D& p) { + for (size_t m = 0; m < len_of_weightvec; ++m) + _persistent.push_back(make_shared(p)); + } - Scatter3DPtr() : _persistent() { } + Scatter3DPtr() : _persistent() { } - bool operator!() { return !_persistent; } - explicit operator bool() { return bool(_persistent); } + bool operator!() { return !_persistent; } + explicit operator bool() { return bool(_persistent); } - YODA::Scatter3D* operator->() { return _persistent.get(); } + YODA::Scatter3D* operator->() { return _persistent.get(); } - YODA::Scatter3D* operator->() const { return _persistent.get(); } + YODA::Scatter3D* operator->() const { return _persistent.get(); } - YODA::Scatter3D & operator*() { return *_persistent; } + YODA::Scatter3D & operator*() { return *_persistent; } - const YODA::Scatter3D & operator*() const { return *_persistent; } + const YODA::Scatter3D & operator*() const { return *_persistent; } - protected: - vector _persistent; + protected: + vector _persistent; }; - */ + */ - class MultiweightAOWrapper : public AnalysisObjectWrapper { + class MultiweightAOWrapper : public AnalysisObjectWrapper { - public: - using Inner = YODA::AnalysisObject; + public: + using Inner = YODA::AnalysisObject; - virtual void newSubEvent() = 0; + virtual void newSubEvent() = 0; - virtual void pushToPersistent(const vector >& weight) = 0; + virtual void pushToPersistent(const vector >& weight) = 0; - virtual YODA::AnalysisObjectPtr activeYODAPtr() const = 0; - }; + virtual YODA::AnalysisObjectPtr activeYODAPtr() const = 0; + }; -using Weight = double; + using Weight = double; -template - using Fill = pair; + template + using Fill = pair; -template - using Fills = multiset>; + template + using Fills = multiset>; -// TODO TODO TODO -// need to override the old fill method too! -// otherwise we can't intercept existing fill calls in analysis code -// TODO TODO TODO + // TODO TODO TODO + // need to override the old fill method too! + // otherwise we can't intercept existing fill calls in analysis code + // TODO TODO TODO -template -class TupleWrapper; -template<> -class TupleWrapper : public YODA::Counter { -public: + /// Wrappers for analysis objects to store all fills unaggregated, until collapsed + template + class TupleWrapper; + + template<> + class TupleWrapper : public YODA::Counter { + public: typedef shared_ptr> Ptr; TupleWrapper(const YODA::Counter & h) : YODA::Counter(h) {} // todo: do we need to deal with users using fractions directly? void fill( double weight=1.0, double fraction=1.0 ) { - fills_.insert( {YODA::Counter::FillType(),weight} ); + fills_.insert( {YODA::Counter::FillType(),weight} ); } void reset() { fills_.clear(); } const Fills & fills() const { return fills_; } -private: + private: // x / weight pairs Fills fills_; -}; + }; -template<> -class TupleWrapper : public YODA::Histo1D { -public: + template<> + class TupleWrapper : public YODA::Histo1D { + public: typedef shared_ptr> Ptr; TupleWrapper(const YODA::Histo1D & h) : YODA::Histo1D(h) {} // todo: do we need to deal with users using fractions directly? void fill( double x, double weight=1.0, double fraction=1.0 ) { - if ( std::isnan(x) ) throw YODA::RangeError("X is NaN"); - fills_.insert( { x , weight } ); + if ( std::isnan(x) ) throw YODA::RangeError("X is NaN"); + fills_.insert( { x , weight } ); } void reset() { fills_.clear(); } const Fills & fills() const { return fills_; } -private: + private: // x / weight pairs Fills fills_; -}; + }; -template<> -class TupleWrapper : public YODA::Profile1D { -public: + template<> + class TupleWrapper : public YODA::Profile1D { + public: typedef shared_ptr> Ptr; TupleWrapper(const YODA::Profile1D & h) : YODA::Profile1D(h) {} // todo: do we need to deal with users using fractions directly? void fill( double x, double y, double weight=1.0, double fraction=1.0 ) { - if ( std::isnan(x) ) throw YODA::RangeError("X is NaN"); - if ( std::isnan(y) ) throw YODA::RangeError("Y is NaN"); - fills_.insert( { YODA::Profile1D::FillType{x,y}, weight } ); + if ( std::isnan(x) ) throw YODA::RangeError("X is NaN"); + if ( std::isnan(y) ) throw YODA::RangeError("Y is NaN"); + fills_.insert( { YODA::Profile1D::FillType{x,y}, weight } ); } void reset() { fills_.clear(); } const Fills & fills() const { return fills_; } -private: + private: // x / weight pairs Fills fills_; -}; + }; -template<> -class TupleWrapper : public YODA::Histo2D { -public: + template<> + class TupleWrapper : public YODA::Histo2D { + public: typedef shared_ptr> Ptr; TupleWrapper(const YODA::Histo2D & h) : YODA::Histo2D(h) {} // todo: do we need to deal with users using fractions directly? void fill( double x, double y, double weight=1.0, double fraction=1.0 ) { - if ( std::isnan(x) ) throw YODA::RangeError("X is NaN"); - if ( std::isnan(y) ) throw YODA::RangeError("Y is NaN"); - fills_.insert( { YODA::Histo2D::FillType{x,y}, weight } ); + if ( std::isnan(x) ) throw YODA::RangeError("X is NaN"); + if ( std::isnan(y) ) throw YODA::RangeError("Y is NaN"); + fills_.insert( { YODA::Histo2D::FillType{x,y}, weight } ); } void reset() { fills_.clear(); } const Fills & fills() const { return fills_; } -private: + private: // x / weight pairs Fills fills_; -}; + }; -template<> -class TupleWrapper : public YODA::Profile2D { -public: + template<> + class TupleWrapper : public YODA::Profile2D { + public: typedef shared_ptr> Ptr; TupleWrapper(const YODA::Profile2D & h) : YODA::Profile2D(h) {} // todo: do we need to deal with users using fractions directly? void fill( double x, double y, double z, double weight=1.0, double fraction=1.0 ) { - if ( std::isnan(x) ) throw YODA::RangeError("X is NaN"); - if ( std::isnan(y) ) throw YODA::RangeError("Y is NaN"); - if ( std::isnan(z) ) throw YODA::RangeError("Z is NaN"); - fills_.insert( { YODA::Profile2D::FillType{x,y,z}, weight } ); + if ( std::isnan(x) ) throw YODA::RangeError("X is NaN"); + if ( std::isnan(y) ) throw YODA::RangeError("Y is NaN"); + if ( std::isnan(z) ) throw YODA::RangeError("Z is NaN"); + fills_.insert( { YODA::Profile2D::FillType{x,y,z}, weight } ); } void reset() { fills_.clear(); } const Fills & fills() const { return fills_; } -private: + private: // x / weight pairs Fills fills_; -}; + }; -template<> -class TupleWrapper : public YODA::Scatter1D { -public: + template<> + class TupleWrapper : public YODA::Scatter1D { + public: typedef shared_ptr> Ptr; TupleWrapper(const YODA::Scatter1D & h) : YODA::Scatter1D(h) {} -}; + }; -template<> -class TupleWrapper : public YODA::Scatter2D { -public: + template<> + class TupleWrapper : public YODA::Scatter2D { + public: typedef shared_ptr> Ptr; TupleWrapper(const YODA::Scatter2D & h) : YODA::Scatter2D(h) {} -}; + }; -template<> -class TupleWrapper : public YODA::Scatter3D { -public: + template<> + class TupleWrapper : public YODA::Scatter3D { + public: typedef shared_ptr> Ptr; TupleWrapper(const YODA::Scatter3D & h) : YODA::Scatter3D(h) {} -}; + }; - template - class Wrapper : public MultiweightAOWrapper { - friend class Analysis; - public: + template + class Wrapper : public MultiweightAOWrapper { + friend class Analysis; - using Inner = T; - /* @todo - * some things are not really well-defined here - * for instance: fill() in the finalize() method and integral() in - * the analyze() method. - */ + public: - Wrapper() = default; + using Inner = T; + /* @todo + * some things are not really well-defined here + * for instance: fill() in the finalize() method and integral() in + * the analyze() method. + */ - Wrapper(const vector& weightnames, const T & p); + Wrapper() = default; - ~Wrapper(); + Wrapper(const vector& weightnames, const T & p); - typename T::Ptr active() const; + ~Wrapper(); - /* @todo this probably need to loop over all? */ - bool operator!() const { return !_active; } // Don't use active() here, assert will catch + typename T::Ptr active() const; - explicit operator bool() const { return static_cast(_active); } // Don't use active() here, assert will catch + /* @todo this probably need to loop over all? */ + bool operator!() const { return !_active; } // Don't use active() here, assert will catch - T * operator->() { return active().get(); } + explicit operator bool() const { return static_cast(_active); } // Don't use active() here, assert will catch - T * operator->() const { return active().get(); } + T * operator->() { return active().get(); } - T & operator*() { return *active(); } + T * operator->() const { return active().get(); } - const T & operator*() const { return *active(); } - /* @todo - * these need to be re-thought out. + T & operator*() { return *active(); } - void reset() { active()->reset(); } - */ + const T & operator*() const { return *active(); } + /* @todo + * these need to be re-thought out. - /* @todo - * these probably need to loop over all? - * do we even want to provide equality? - */ - /* @todo - * how about no. - friend bool operator==(Wrapper a, Wrapper b){ - if (a._persistent.size() != b._persistent.size()) - return false; + void reset() { active()->reset(); } + */ - for (size_t i = 0; i < a._persistent.size(); i++) { - if (a._persistent.at(i) != b._persistent.at(i)) { - return false; - } - } + /* @todo + * these probably need to loop over all? + * do we even want to provide equality? + */ + /* @todo + * how about no. + friend bool operator==(Wrapper a, Wrapper b){ + if (a._persistent.size() != b._persistent.size()) + return false; - return true; - } + for (size_t i = 0; i < a._persistent.size(); i++) { + if (a._persistent.at(i) != b._persistent.at(i)) { + return false; + } + } - friend bool operator!=(Wrapper a, Wrapper b){ - return !(a == b); - } + return true; + } + friend bool operator!=(Wrapper a, Wrapper b){ + return !(a == b); + } - friend bool operator<(Wrapper a, Wrapper b){ - if (a._persistent.size() >= b._persistent.size()) - return false; - for (size_t i = 0; i < a._persistent.size(); i++) { - if (*(a._persistent.at(i)) >= *(b._persistent.at(i))) { - return false; - } - } + friend bool operator<(Wrapper a, Wrapper b){ + if (a._persistent.size() >= b._persistent.size()) + return false; - return true; - } - */ + for (size_t i = 0; i < a._persistent.size(); i++) { + if (*(a._persistent.at(i)) >= *(b._persistent.at(i))) { + return false; + } + } + return true; + } + */ - private: - void setActiveWeightIdx(unsigned int iWeight) { - _active = _persistent.at(iWeight); - } - /* this is for dev only---we shouldn't need this in real runs. */ - void unsetActiveWeight() { _active.reset(); } + private: + void setActiveWeightIdx(unsigned int iWeight) { + _active = _persistent.at(iWeight); + } - void newSubEvent(); + /* this is for dev only---we shouldn't need this in real runs. */ + void unsetActiveWeight() { _active.reset(); } - virtual YODA::AnalysisObjectPtr activeYODAPtr() const { return _active; } + void newSubEvent(); - const vector & persistent() const { return _persistent; } + virtual YODA::AnalysisObjectPtr activeYODAPtr() const { return _active; } - /* to be implemented for each type */ - void pushToPersistent(const vector >& weight); + const vector & persistent() const { return _persistent; } - /* M of these, one for each weight */ - vector _persistent; + /* to be implemented for each type */ + void pushToPersistent(const vector >& weight); - /* N of these, one for each event in evgroup */ - vector::Ptr> _evgroup; + /* M of these, one for each weight */ + vector _persistent; - typename T::Ptr _active; + /* N of these, one for each event in evgroup */ + vector::Ptr> _evgroup; - // do we need implicit cast? - // operator typename T::Ptr () { - // return _active; - // } + typename T::Ptr _active; - friend class AnalysisHandler; - }; + // do we need implicit cast? + // operator typename T::Ptr () { + // return _active; + // } -/// We need our own shared_ptr class, so we can dispatch -> and * -/// all the way down to the inner YODA analysis objects -/// -/// TODO: provide remaining functionality that shared_ptr has (not needed right now) -/// -template -class rivet_shared_ptr { -public: - rivet_shared_ptr() = default; + friend class AnalysisHandler; + }; - rivet_shared_ptr(decltype(nullptr)) : _p(nullptr) {} - /// Convenience constructor, pass through to the Wrapper constructor - rivet_shared_ptr(const vector& weightNames, const typename T::Inner & p) - : _p( make_shared(weightNames, p) ) + /// We need our own shared_ptr class, so we can dispatch -> and * + /// all the way down to the inner YODA analysis objects + /// + /// TODO: provide remaining functionality that shared_ptr has (not needed right now) + /// + template + class rivet_shared_ptr { + public: + rivet_shared_ptr() = default; + + rivet_shared_ptr(decltype(nullptr)) : _p(nullptr) {} + + /// Convenience constructor, pass through to the Wrapper constructor + rivet_shared_ptr(const vector& weightNames, const typename T::Inner & p) + : _p( make_shared(weightNames, p) ) {} - template - rivet_shared_ptr(const shared_ptr & p) - : _p(p) + template + rivet_shared_ptr(const shared_ptr & p) + : _p(p) {} - template - rivet_shared_ptr(const rivet_shared_ptr & p) - : _p(p.get()) + template + rivet_shared_ptr(const rivet_shared_ptr & p) + : _p(p.get()) {} - // Goes right through to the active YODA object's members - T & operator->() { return *_p; } - const T & operator->() const { return *_p; } + // Goes right through to the active YODA object's members + T & operator->() { return *_p; } + const T & operator->() const { return *_p; } - // The active YODA object - typename T::Inner & operator*() { return **_p; } - const typename T::Inner & operator*() const { return **_p; } + // The active YODA object + typename T::Inner & operator*() { return **_p; } + const typename T::Inner & operator*() const { return **_p; } - bool operator!() const { return !_p || !(*_p); } - explicit operator bool() const { return _p && bool(*_p); } + bool operator!() const { return !_p || !(*_p); } + explicit operator bool() const { return _p && bool(*_p); } - template - bool operator==(const rivet_shared_ptr & other) const { - return _p == other._p; - } + template + bool operator==(const rivet_shared_ptr & other) const { + return _p == other._p; + } - template - bool operator!=(const rivet_shared_ptr & other) const { - return _p != other._p; - } + template + bool operator!=(const rivet_shared_ptr & other) const { + return _p != other._p; + } - template - bool operator<(const rivet_shared_ptr & other) const { - return _p < other._p; - } + template + bool operator<(const rivet_shared_ptr & other) const { + return _p < other._p; + } - template - bool operator>(const rivet_shared_ptr & other) const { - return _p > other._p; - } + template + bool operator>(const rivet_shared_ptr & other) const { + return _p > other._p; + } - template - bool operator<=(const rivet_shared_ptr & other) const { - return _p <= other._p; - } + template + bool operator<=(const rivet_shared_ptr & other) const { + return _p <= other._p; + } - template - bool operator>=(const rivet_shared_ptr & other) const { - return _p >= other._p; - } + template + bool operator>=(const rivet_shared_ptr & other) const { + return _p >= other._p; + } - shared_ptr get() const { return _p; } -private: - shared_ptr _p; -}; + shared_ptr get() const { return _p; } + private: + shared_ptr _p; + }; - // every object listed here needs a virtual fill method in YODA, - // otherwise the Tuple fakery won't work. + // every object listed here needs a virtual fill method in YODA, + // otherwise the Tuple fakery won't work. - using MultiweightAOPtr = rivet_shared_ptr; + using MultiweightAOPtr = rivet_shared_ptr; - using Histo1DPtr = rivet_shared_ptr>; - using Histo2DPtr = rivet_shared_ptr>; - using Profile1DPtr = rivet_shared_ptr>; - using Profile2DPtr = rivet_shared_ptr>; - using CounterPtr = rivet_shared_ptr>; - using Scatter1DPtr = rivet_shared_ptr>; - using Scatter2DPtr = rivet_shared_ptr>; - using Scatter3DPtr = rivet_shared_ptr>; + using Histo1DPtr = rivet_shared_ptr>; + using Histo2DPtr = rivet_shared_ptr>; + using Profile1DPtr = rivet_shared_ptr>; + using Profile2DPtr = rivet_shared_ptr>; + using CounterPtr = rivet_shared_ptr>; + using Scatter1DPtr = rivet_shared_ptr>; + using Scatter2DPtr = rivet_shared_ptr>; + using Scatter3DPtr = rivet_shared_ptr>; - using YODA::Counter; - using YODA::Histo1D; - using YODA::HistoBin1D; - using YODA::Histo2D; - using YODA::HistoBin2D; - using YODA::Profile1D; - using YODA::ProfileBin1D; - using YODA::Profile2D; - using YODA::ProfileBin2D; - using YODA::Scatter1D; - using YODA::Point1D; - using YODA::Scatter2D; - using YODA::Point2D; - using YODA::Scatter3D; - using YODA::Point3D; + using YODA::Counter; + using YODA::Histo1D; + using YODA::HistoBin1D; + using YODA::Histo2D; + using YODA::HistoBin2D; + using YODA::Profile1D; + using YODA::ProfileBin1D; + using YODA::Profile2D; + using YODA::ProfileBin2D; + using YODA::Scatter1D; + using YODA::Point1D; + using YODA::Scatter2D; + using YODA::Point2D; + using YODA::Scatter3D; + using YODA::Point3D; - /// Function to get a map of all the refdata in a paper with the - /// given @a papername. - map getRefData(const string& papername); + /// Function to get a map of all the refdata in a paper with the + /// given @a papername. + map getRefData(const string& papername); - /// @todo Also provide a Scatter3D getRefData() version? + /// @todo Also provide a Scatter3D getRefData() version? - /// Get the file system path to the reference file for this paper. - string getDatafilePath(const string& papername); + /// Get the file system path to the reference file for this paper. + string getDatafilePath(const string& papername); } #endif