1#ifndef RIVET_RIVETYODA_HH
2#define RIVET_RIVETYODA_HH
4#include "Rivet/Config/RivetCommon.hh"
5#include "YODA/AnalysisObject.h"
6#include "YODA/Counter.h"
7#include "YODA/Histo1D.h"
8#include "YODA/Histo2D.h"
9#include "YODA/Profile1D.h"
10#include "YODA/Profile2D.h"
11#include "YODA/Scatter1D.h"
12#include "YODA/Scatter2D.h"
13#include "YODA/Scatter3D.h"
20 typedef std::shared_ptr<YODA::AnalysisObject> AnalysisObjectPtr;
22 typedef std::shared_ptr<YODA::Counter> CounterPtr;
23 typedef std::shared_ptr<YODA::Histo1D> Histo1DPtr;
24 typedef std::shared_ptr<YODA::Histo2D> Histo2DPtr;
25 typedef std::shared_ptr<YODA::Profile1D> Profile1DPtr;
26 typedef std::shared_ptr<YODA::Profile2D> Profile2DPtr;
27 typedef std::shared_ptr<YODA::Scatter1D> Scatter1DPtr;
28 typedef std::shared_ptr<YODA::Scatter2D> Scatter2DPtr;
29 typedef std::shared_ptr<YODA::Scatter3D> Scatter3DPtr;
49 using Fill = pair<typename T::FillType, Weight>;
54 using Fills = multiset<Fill<T>>;
72 class TupleWrapper<YODA::Counter> :
public YODA::Counter {
76 typedef shared_ptr<TupleWrapper<YODA::Counter>> Ptr;
79 TupleWrapper(
const YODA::Counter& h) : YODA::Counter(h) {}
84 void fill(
double weight=1.0,
double fraction=1.0) {
86 _fills.insert( { YODA::Counter::FillType(), weight } );
90 void reset() { _fills.clear(); }
93 const Fills<YODA::Counter>& fills()
const {
return _fills; }
96 using YODA::Counter::operator =;
100 Fills<YODA::Counter> _fills;
107 class TupleWrapper<YODA::Histo1D> :
public YODA::Histo1D {
111 typedef shared_ptr<TupleWrapper<YODA::Histo1D>> Ptr;
114 TupleWrapper(
const YODA::Histo1D& h) : YODA::Histo1D(h) {}
119 void fill(
double x,
double weight=1.0,
double fraction=1.0 ) {
121 if ( std::isnan(x) )
throw YODA::RangeError(
"X is NaN");
122 _fills.insert( { x, weight } );
126 void reset() { _fills.clear(); }
129 const Fills<YODA::Histo1D>& fills()
const {
return _fills; }
132 using YODA::Histo1D::operator =;
136 Fills<YODA::Histo1D> _fills;
143 class TupleWrapper<YODA::Profile1D> :
public YODA::Profile1D {
147 typedef shared_ptr<TupleWrapper<YODA::Profile1D>> Ptr;
150 TupleWrapper(
const YODA::Profile1D& h) : YODA::Profile1D(h) {}
155 void fill(
double x,
double y,
double weight=1.0,
double fraction=1.0 ) {
157 if ( std::isnan(x) )
throw YODA::RangeError(
"X is NaN");
158 if ( std::isnan(y) )
throw YODA::RangeError(
"Y is NaN");
159 _fills.insert( { YODA::Profile1D::FillType{x,y}, weight } );
163 void reset() { _fills.clear(); }
166 const Fills<YODA::Profile1D>& fills()
const {
return _fills; }
169 using YODA::Profile1D::operator =;
173 Fills<YODA::Profile1D> _fills;
180 class TupleWrapper<YODA::Histo2D> :
public YODA::Histo2D {
184 typedef shared_ptr<TupleWrapper<YODA::Histo2D>> Ptr;
187 TupleWrapper(
const YODA::Histo2D& h) : YODA::Histo2D(h) {}
192 void fill(
double x,
double y,
double weight=1.0,
double fraction=1.0 ) {
194 if ( std::isnan(x) )
throw YODA::RangeError(
"X is NaN");
195 if ( std::isnan(y) )
throw YODA::RangeError(
"Y is NaN");
196 _fills.insert( { YODA::Histo2D::FillType{x,y}, weight } );
200 void reset() { _fills.clear(); }
203 const Fills<YODA::Histo2D>& fills()
const {
return _fills; }
206 using YODA::Histo2D::operator =;
210 Fills<YODA::Histo2D> _fills;
217 class TupleWrapper<YODA::Profile2D> :
public YODA::Profile2D {
221 typedef shared_ptr<TupleWrapper<YODA::Profile2D>> Ptr;
224 TupleWrapper(
const YODA::Profile2D& h) : YODA::Profile2D(h) {}
229 void fill(
double x,
double y,
double z,
double weight=1.0,
double fraction=1.0 ) {
231 if ( std::isnan(x) )
throw YODA::RangeError(
"X is NaN");
232 if ( std::isnan(y) )
throw YODA::RangeError(
"Y is NaN");
233 if ( std::isnan(z) )
throw YODA::RangeError(
"Z is NaN");
234 _fills.insert( { YODA::Profile2D::FillType{x,y,z}, weight } );
238 void reset() { _fills.clear(); }
241 const Fills<YODA::Profile2D>& fills()
const {
return _fills; }
244 using YODA::Profile2D::operator =;
248 Fills<YODA::Profile2D> _fills;
255 class TupleWrapper<YODA::Scatter1D> :
public YODA::Scatter1D {
259 typedef shared_ptr<TupleWrapper<YODA::Scatter1D>> Ptr;
262 TupleWrapper(
const YODA::Scatter1D& h) : YODA::Scatter1D(h) {}
265 using YODA::Scatter1D::operator =;
272 class TupleWrapper<YODA::Scatter2D> :
public YODA::Scatter2D {
276 typedef shared_ptr<TupleWrapper<YODA::Scatter2D>> Ptr;
279 TupleWrapper(
const YODA::Scatter2D& h) : YODA::Scatter2D(h) {}
282 using YODA::Scatter2D::operator =;
289 class TupleWrapper<YODA::Scatter3D> :
public YODA::Scatter3D {
293 typedef shared_ptr<TupleWrapper<YODA::Scatter3D>> Ptr;
296 TupleWrapper(
const YODA::Scatter3D& h) : YODA::Scatter3D(h) {}
299 using YODA::Scatter3D::operator =;
318 class AnalysisObjectWrapper {
321 virtual ~AnalysisObjectWrapper() {}
324 virtual YODA::AnalysisObject* operator -> () = 0;
326 virtual YODA::AnalysisObject* operator -> ()
const = 0;
328 virtual const YODA::AnalysisObject& operator * ()
const = 0;
331 virtual void setActiveWeightIdx(
size_t iWeight) = 0;
334 virtual void setActiveFinalWeightIdx(
size_t iWeight) = 0;
339 virtual void unsetActiveWeight() = 0;
342 bool operator == (
const AnalysisObjectWrapper&
p) {
return (
this == &
p); }
344 bool operator != (
const AnalysisObjectWrapper&
p) {
return (
this != &
p); }
433 class MultiweightAOWrapper :
public AnalysisObjectWrapper {
437 using Inner = YODA::AnalysisObject;
440 virtual void newSubEvent() = 0;
443 virtual void pushToPersistent(
const vector<std::valarray<double> >& weight,
double nlowfrac=0.0) = 0;
446 virtual void pushToFinal() = 0;
449 virtual YODA::AnalysisObjectPtr activeYODAPtr()
const = 0;
452 virtual string basePath()
const = 0;
472 class Wrapper :
public MultiweightAOWrapper {
475 friend class Analysis;
476 friend class AnalysisHandler;
482 using TPtr = shared_ptr<T>;
487 Wrapper(
const vector<string>& weightnames,
const T&
p);
493 shared_ptr<T> active()
const;
496 string basePath()
const {
return _basePath; }
499 string baseName()
const {
return _baseName; }
503 explicit operator bool()
const {
return static_cast<bool>(_active); }
506 bool operator ! ()
const {
return !_active; }
510 T* operator -> () {
return active().get(); }
513 T* operator -> ()
const {
return active().get(); }
516 T& operator * () {
return *active(); }
519 const T& operator * ()
const {
return *active(); }
524 friend bool operator == (Wrapper a, Wrapper b){
525 if (a._persistent.size() != b._persistent.size())
528 for (
size_t i = 0; i < a._persistent.size(); i++) {
529 if (a._persistent.at(i) != b._persistent.at(i)) {
537 friend bool operator != (Wrapper a, Wrapper b) {
542 friend bool operator < (Wrapper a, Wrapper b) {
543 if (a._persistent.size() >= b._persistent.size())
545 for (
size_t i = 0; i < a._persistent.size(); i++) {
546 if (*(a._persistent.at(i)) >= *(b._persistent.at(i))) {
562 T* _getPersistent(
size_t iWeight) {
return _persistent.at(iWeight).get(); }
582 void setActiveWeightIdx(
size_t iWeight) {
583 _active = _persistent.at(iWeight);
587 void setActiveFinalWeightIdx(
size_t iWeight) {
588 _active = _final.at(iWeight);
592 void unsetActiveWeight() { _active.reset(); }
595 void reset() { active()->reset(); }
605 void pushToPersistent(
const vector<std::valarray<double> >& weight,
double nlowfrac=0.0);
612 const vector<shared_ptr<T>>& persistent()
const {
return _persistent; }
615 const vector<shared_ptr<T>>&
final()
const {
return _final; }
618 virtual YODA::AnalysisObjectPtr activeYODAPtr()
const {
return _active; }
630 vector<shared_ptr<T>> _persistent;
633 vector<shared_ptr<T>> _final;
636 vector<shared_ptr<TupleWrapper<T>>> _evgroup;
639 shared_ptr<T> _active;
660 template <
typename T>
661 class rivet_shared_ptr {
663 typedef T value_type;
665 rivet_shared_ptr() =
default;
667 rivet_shared_ptr(
decltype(
nullptr)) : _p(nullptr) {}
670 rivet_shared_ptr(
const vector<string>& weightNames,
const typename T::Inner&
p)
671 : _p( make_shared<T>(weightNames,
p) )
675 template <
typename U>
676 rivet_shared_ptr(
const shared_ptr<U>&
p)
681 template <
typename U>
682 rivet_shared_ptr(
const rivet_shared_ptr<U>&
p)
688 if (_p ==
nullptr)
throw Error(
"Dereferencing null AnalysisObject pointer. Is there an unbooked histogram variable?");
693 const T& operator -> ()
const {
694 if (_p ==
nullptr)
throw Error(
"Dereferencing null AnalysisObject pointer. Is there an unbooked histogram variable?");
699 typename T::Inner & operator * () {
return **_p; }
700 const typename T::Inner & operator * ()
const {
return **_p; }
703 explicit operator bool()
const {
return _p && bool(*_p); }
706 bool operator ! ()
const {
return !_p || !(*_p); }
709 template <
typename U>
710 bool operator == (
const rivet_shared_ptr<U>& other)
const {
711 return _p == other._p;
715 template <
typename U>
716 bool operator != (
const rivet_shared_ptr<U>& other)
const {
717 return _p != other._p;
721 template <
typename U>
722 bool operator < (
const rivet_shared_ptr<U>& other)
const {
723 return _p < other._p;
727 template <
typename U>
728 bool operator > (
const rivet_shared_ptr<U>& other)
const {
729 return _p > other._p;
733 template <
typename U>
734 bool operator <= (
const rivet_shared_ptr<U> & other)
const {
735 return _p <= other._p;
739 template <
typename U>
740 bool operator >= (
const rivet_shared_ptr<U> & other)
const {
741 return _p >= other._p;
745 shared_ptr<T> get()
const {
return _p; }
762 using MultiweightAOPtr = rivet_shared_ptr<MultiweightAOWrapper>;
764 using Histo1DPtr = rivet_shared_ptr<Wrapper<YODA::Histo1D>>;
765 using Histo2DPtr = rivet_shared_ptr<Wrapper<YODA::Histo2D>>;
766 using Profile1DPtr = rivet_shared_ptr<Wrapper<YODA::Profile1D>>;
767 using Profile2DPtr = rivet_shared_ptr<Wrapper<YODA::Profile2D>>;
768 using CounterPtr = rivet_shared_ptr<Wrapper<YODA::Counter>>;
769 using Scatter1DPtr = rivet_shared_ptr<Wrapper<YODA::Scatter1D>>;
770 using Scatter2DPtr = rivet_shared_ptr<Wrapper<YODA::Scatter2D>>;
771 using Scatter3DPtr = rivet_shared_ptr<Wrapper<YODA::Scatter3D>>;
775 using YODA::HistoBin1D;
777 using YODA::HistoBin2D;
778 using YODA::Profile1D;
779 using YODA::ProfileBin1D;
780 using YODA::Profile2D;
781 using YODA::ProfileBin2D;
782 using YODA::Scatter1D;
784 using YODA::Scatter2D;
786 using YODA::Scatter3D;
798 inline bool isTmpPath(
const std::string& path) {
799 return path.find(
"/TMP/") != string::npos || path.find(
"/_") != string::npos;
804 map<string, YODA::AnalysisObjectPtr>
getRefData(
const string& papername);
813 template<
typename T>
struct ReferenceTraits {};
814 template <>
struct ReferenceTraits<Counter> {
typedef Counter RefT; };
815 template <>
struct ReferenceTraits<Scatter1D> {
typedef Scatter1D RefT; };
816 template <>
struct ReferenceTraits<Histo1D> {
typedef Scatter2D RefT; };
817 template <>
struct ReferenceTraits<Profile1D> {
typedef Scatter2D RefT; };
818 template <>
struct ReferenceTraits<Scatter2D> {
typedef Scatter2D RefT; };
819 template <>
struct ReferenceTraits<Histo2D> {
typedef Scatter3D RefT; };
820 template <>
struct ReferenceTraits<Profile2D> {
typedef Scatter3D RefT; };
821 template <>
struct ReferenceTraits<Scatter3D> {
typedef Scatter3D RefT; };
826 template <
typename T>
827 inline bool aocopy(YODA::AnalysisObjectPtr src, YODA::AnalysisObjectPtr dst) {
828 shared_ptr<T> tsrc = dynamic_pointer_cast<T>(src);
829 if ( !tsrc )
return false;
830 shared_ptr<T> tdst = dynamic_pointer_cast<T>(dst);
831 if ( !tdst )
return false;
840 template <
typename T>
841 inline bool aocopy(YODA::AnalysisObjectPtr src, YODA::AnalysisObjectPtr dst,
double scale) {
842 if (!aocopy<T>(src, dst))
return false;
843 dynamic_pointer_cast<T>(dst)->scaleW(scale);
850 template <
typename T>
851 inline bool aoadd(YODA::AnalysisObjectPtr dst, YODA::AnalysisObjectPtr src,
double scale) {
852 shared_ptr<T> tsrc = dynamic_pointer_cast<T>(src);
853 if ( !tsrc )
return false;
854 shared_ptr<T> tdst = dynamic_pointer_cast<T>(dst);
855 if ( !tdst )
return false;
859 }
catch (YODA::LogicError&) {
867 bool copyao(YODA::AnalysisObjectPtr src, YODA::AnalysisObjectPtr dst,
double scale=1.0);
872 bool addaos(YODA::AnalysisObjectPtr dst, YODA::AnalysisObjectPtr src,
double scale);
876 template <
typename TPtr>
878 return a->sameBinning(*b);
884 return a->numPoints() == b->numPoints();
887 return a->numPoints() == b->numPoints();
890 return a->numPoints() == b->numPoints();
896 return a->numPoints() == b->numPoints();
899 return a->numPoints() == b->numPoints();
902 return a->numPoints() == b->numPoints();
914 AOPath(
string fullpath)
915 : _valid(false), _path(fullpath), _raw(false), _tmp(false), _ref(false) {
916 _valid = init(fullpath);
920 string path()
const {
return _path; }
923 string analysis()
const {
return _analysis; }
926 string analysisWithOptions()
const {
return _analysis + _optionstring; }
929 string name()
const {
return _name; }
932 string weight()
const {
return _weight; }
935 string weightComponent()
const {
936 if (_weight ==
"")
return _weight;
937 return "[" + _weight +
"]";
941 bool isRaw()
const {
return _raw; }
944 bool isTmp()
const {
return _tmp; }
947 bool isRef()
const {
return _ref; }
950 string optionString()
const {
return _optionstring; }
953 bool hasOptions()
const {
return !_options.empty(); }
956 void removeOption(
string opt) { _options.erase(opt); fixOptionString(); }
959 void setOption(
string opt,
string val) { _options[opt] = val; fixOptionString();}
962 bool hasOption(
string opt)
const {
return _options.find(opt) != _options.end(); }
965 string getOption(
string opt)
const {
966 auto it = _options.find(opt);
967 if ( it != _options.end() )
return it->second;
972 void fixOptionString();
975 string mkPath()
const;
976 string setPath() {
return _path = mkPath(); }
982 bool operator<(
const AOPath & other)
const {
983 return _path < other._path;
987 bool valid()
const {
return _valid; };
988 bool operator!()
const {
return !valid(); }
993 bool init(
string fullpath);
994 bool chopweight(
string & fullpath);
995 bool chopoptions(
string & anal);
1000 string _optionstring;
1006 map<string,string> _options;
map< string, YODA::AnalysisObjectPtr > getRefData(const string &papername)
bool addaos(YODA::AnalysisObjectPtr dst, YODA::AnalysisObjectPtr src, double scale)
bool bookingCompatible(TPtr a, TPtr b)
Definition RivetYODA.hh:877
bool aoadd(YODA::AnalysisObjectPtr dst, YODA::AnalysisObjectPtr src, double scale)
Definition RivetYODA.hh:851
string getDatafilePath(const string &papername)
Get the file system path to the reference file for this paper.
bool aocopy(YODA::AnalysisObjectPtr src, YODA::AnalysisObjectPtr dst)
Definition RivetYODA.hh:827
bool copyao(YODA::AnalysisObjectPtr src, YODA::AnalysisObjectPtr dst, double scale=1.0)
double Weight
Typedef for weights.
Definition RivetYODA.hh:45
multiset< Fill< T > > Fills
Definition RivetYODA.hh:54
pair< typename T::FillType, Weight > Fill
A single fill is a (FillType, Weight) pair.
Definition RivetYODA.hh:49
double p(const ParticleBase &p)
Unbound function access to p.
Definition ParticleBaseUtils.hh:684
Definition MC_Cent_pPb.hh:10
Cut operator!(const Cut &cptr)
Logical NOT operation on a cut.