2 #ifndef RIVET_CENTRALITYBINNER_HH 3 #define RIVET_CENTRALITYBINNER_HH 5 #include "Rivet/Config/RivetCommon.hh" 6 #include "Rivet/Tools/RivetYODA.hh" 49 const HepMC::HeavyIon * hi = e.
genEvent()->heavy_ion();
50 if ( hi ) _estimate = hi->impact_parameter() > 0.0?
51 1.0/hi->impact_parameter(): numeric_limits<double>::max();
82 return T(t->newclone());
86 static void add(T & t,
const T & o) {
91 static void scale(T & t,
double f) {
98 if ( t->sumW() > 0.0 ) t->normalize(t->sumW()/sumw);
122 static double dist(
double cestLo,
double cestHi,
double weight,
123 double clo,
double chi,
double,
double) {
124 return (cestHi - cestLo)*weight/(cestHi*(chi - clo));
136 template <
typename T = Histo1DPtr,
typename MDist = MergeDistance>
145 : _currentCEst(-1.0), _maxBins(maxbins), _warnlimit(wlim), _weightsum(0.0) {
146 _percentiles.insert(0.0);
147 _percentiles.insert(1.0);
158 virtual std::string
name()
const {
159 return "Rivet::CentralityBinner";
174 void add(T t,
double cmin,
double cmax,
175 double cestmin = -1.0,
double cestmax = -1.0 ) {
176 _percentiles.insert(
max(1.0 - cmax/100.0, 0.0));
177 _percentiles.insert(
min(1.0 - cmin/100.0, 1.0));
178 if ( _unfilled.empty() && _ready.empty() )
181 _unfilled.push_back(Bin(t, 1.0 - cmax/100.0, 1.0 - cmin/100.0));
183 _ready[t] = Bin(t, 1.0 - cmax/100.0, 1.0 - cmin/100.0, cestmin, cestmax);
195 return select(applyProjection<CentralityEstimator>
196 (event, _estimator).
estimate(), weight);
205 T select(
double cest,
double weight = 1.0);
218 for (
auto & b : _ready ) b.second.normalizePerEvent();
224 map<double,double> ret;
225 for (
auto & b : _ready ) {
226 ret[1.0 - b.second._centLo] = b.second._cestLo;
227 ret[1.0 - b.second._centHi] = b.second._cestHi;
243 vector<T> allObjects() {
245 for (
auto & fb : _flexiBins ) ret.push_back(fb._t);
246 if ( !ret.empty() )
return ret;
247 for (
auto b : _ready ) ret.push_back(b.second._t);
258 FlexiBin(T & t,
double cest = 0.0,
double weight = 0.0)
259 : _t(t), _cestLo(cest), _cestHi(cest), _weightsum(weight), _n(1), _m(0) {}
262 FlexiBin(
double cest)
263 : _cestLo(cest), _cestHi(cest), _weightsum(0.0), _n(0), _m(0) {}
266 void merge(
const FlexiBin & fb) {
267 _cestLo =
min(_cestLo, fb._cestLo);
268 _cestHi =
max(_cestHi, fb._cestHi);
269 _weightsum += fb._weightsum;
276 bool operator< (
const FlexiBin & fb)
const {
277 return _cestLo < fb._cestLo;
282 bool inRange(
double cest)
const {
283 return cest == _cestLo || ( _cestLo < cest && cest < _cestHi );
291 double _cestLo, _cestHi;
295 mutable double _weightsum;
309 : _centLo(-1.0), _centHi(-1.0), _cestLo(-1.0), _cestHi(-1.0),
310 _weightsum(0.0), _underflow(0.0), _overflow(0.0),
311 _ambiguous(0), _ambweight(0.0) {}
317 Bin(T t,
double centLo,
double centHi,
318 double cestLo = -1.0,
double cestHi = -1.0)
319 : _t(t), _centLo(centLo), _centHi(centHi),
320 _cestLo(cestLo), _cestHi(cestHi),
321 _weightsum(0.0), _underflow(0.0), _overflow(0.0),
322 _ambiguous(0.0), _ambweight(0.0) {}
326 bool inRange(
double cest)
const {
327 return _cestLo >= 0 && _cestLo <= cest &&
328 ( _cestHi < 0.0 || cest <= _cestHi );
332 void normalizePerEvent() {
340 double _centLo, _centHi;
343 double _cestLo, _cestHi;
371 typename FlexiBinSet::iterator _findBin(
double cest) {
372 if ( _flexiBins.empty() )
return _flexiBins.end();
373 auto it = _flexiBins.lower_bound(FlexiBin(cest));
374 if ( it->_cestLo == cest )
return it;
375 if ( it != _flexiBins.begin() ) --it;
376 if ( it->_cestLo < cest && cest < it->_cestHi )
return it;
377 return _flexiBins.end();
400 vector<Bin> _unfilled;
403 FlexiBinSet _flexiBins;
409 set<double> _percentiles;
431 typedef Profile1DPtr T;
435 return Profile1DPtr(t->newclone());
439 static void add(T & t,
const T & o) {
444 static void scale(T & t,
double f) {
448 static void normalize(T & t,
double sumw) {}
462 typedef Profile2DPtr T;
466 return Profile2DPtr(t->newclone());
470 static void add(T & t,
const T & o) {
475 static void scale(T & t,
double f) {
479 static void normalize(T & t,
double sumw) {}
488 template <
typename T>
492 static vector<T>
clone(
const vector<T> & tv) {
499 static void add(vector<T> & tv,
const vector<T> & ov) {
500 for (
int i = 0, N = tv.size(); i < N; ++i )
505 static void scale(vector<T> & tv,
double f) {
509 static void normalize(vector<T> & tv,
double sumw) {
514 static string path(
const vector<T> & tv) {
515 string ret =
"(vector:";
516 for (
auto t : tv ) {
526 template <
size_t I,
typename... Types>
529 typedef tuple<Types...> Tuple;
530 typedef typename tuple_element<I-1,Tuple>::type T;
532 static void clone(Tuple & ret,
const Tuple & tup) {
537 static void add(Tuple & tup,
const Tuple & otup) {
542 static void scale(Tuple & tup,
double f) {
547 static void normalize(Tuple & tup,
double sumw) {
552 static string path(
const Tuple & tup) {
558 template <
typename... Types>
561 typedef tuple<Types...> Tuple;
563 static void clone(Tuple &,
const Tuple &) {}
564 static void add(Tuple & tup,
const Tuple & otup) {}
565 static void scale(Tuple & tup,
double f) {}
566 static void normalize(Tuple & tup,
double sumw) {}
567 static string path(
const Tuple & tup) {
return "";}
571 template <
typename... Types>
574 typedef tuple<Types...> Tuple;
575 static const size_t N = tuple_size<Tuple>::value;
578 static Tuple
clone(
const Tuple & tup) {
585 static void add(Tuple & tup,
const Tuple & otup) {
590 static void scale(Tuple & tup,
double f) {
594 static void normalize(Tuple & tup,
double sumw) {
599 static string path(
const Tuple & tup) {
600 string ret =
"(tuple:";
608 template <
typename T,
typename MDist>
612 _weightsum += weight;
615 if ( _currentCEst < 0.0 )
return _currenT;
620 if ( _unfilled.empty() ) {
621 for (
auto & b : _ready )
if ( b.second.inRange(_currentCEst) ) {
622 b.second._weightsum += weight;
628 auto it = _findBin(cest);
629 if ( it == _flexiBins.end() ) {
631 it = _flexiBins.insert(FlexiBin(_currenT, _currentCEst, weight)).first;
633 it->_weightsum += weight;
638 if ( (
int)_flexiBins.size() <= _maxBins )
return _currenT;
641 set<double>::iterator citn = _percentiles.begin();
642 set<double>::iterator cit0 = citn++;
643 auto selectit = _flexiBins.end();
644 double mindist = -1.0;
646 auto next = _flexiBins.begin();
648 for ( ; next != _flexiBins.end(); prev = next++ ) {
649 acc += prev->_weightsum/_weightsum;
654 if ( acc + next->_weightsum/_weightsum > *citn )
continue;
655 double dist = MDist::dist(prev->_cestLo, next->_cestHi,
656 next->_weightsum + prev->_weightsum,
657 *cit0, *citn, next->_n + prev->_n,
658 next->_m + prev->_m);
659 if ( mindist < 0.0 || dist < mindist ) {
665 if ( selectit == _flexiBins.end() )
return _currenT;
666 auto mergeit = selectit++;
667 FlexiBin merged = *mergeit;
668 merged.merge(*selectit);
669 if ( merged.inRange(cest) || selectit->inRange(cest) )
670 _currenT = merged._t;
671 _flexiBins.erase(mergeit);
672 _flexiBins.erase(selectit);
673 _flexiBins.insert(merged);
680 template <
typename T,
typename MDist>
687 for (
const FlexiBin & fb : _flexiBins ) {
688 double chi =
min(clo + fb._weightsum/_weightsum, 1.0);
689 for ( Bin & bin : _unfilled ) {
690 double olo = bin._centLo;
691 double ohi = bin._centHi;
692 if ( clo > ohi || chi <= olo )
continue;
694 double lo =
max(olo, clo);
695 double hi =
min(ohi, chi);
697 double frac = (hi - lo)/(chi - clo);
700 bin._weightsum += fb._weightsum*frac;
701 if ( clo <= olo ) bin._cestLo = fb._cestLo +
702 (fb._cestHi - fb._cestLo)*(olo - clo)/(chi - clo);
704 bin._underflow = clo;
705 bin._ambiguous += fb._n*frac;
706 bin._ambweight += fb._weightsum*frac*(1.0 - frac);
710 fb._cestLo + (fb._cestHi - fb._cestLo)*(ohi - clo)/(chi - clo);
712 bin._ambiguous += fb._n*frac;
713 bin._ambweight += fb._weightsum*frac*(1.0 - frac);
719 for ( Bin & bin : _unfilled ) {
720 if ( bin._overflow == 0.0 ) bin._overflow = 1.0;
721 _ready[bin._t] = bin;
722 if ( bin._ambweight/bin._weightsum >_warnlimit )
724 <<
"\", contains events with centralities between " 725 << bin._underflow*100.0
726 <<
" and " << bin._overflow*100.0 <<
"% (" 727 <<
int(bin._ambiguous + 0.5)
728 <<
" ambiguous events with effectively " 729 << 100.0*bin._ambweight/bin._weightsum
730 <<
"% of the weights)." 731 <<
"Consider increasing the number of bins.");
738 template <
typename T,
typename MDist>
742 set<double>::iterator citn = _percentiles.begin();
743 set<double>::iterator cit0 = citn++;
745 for (
auto it = _flexiBins.begin(); it != _flexiBins.end(); ) {
748 double w = curr->_weightsum/_weightsum;
750 if ( curr == _flexiBins.begin() || it == _flexiBins.end() || acc > *citn )
754 if ( acc > *citn ) cit0 = citn++;
756 << setw(12) << acc - w
758 << setw(8) << curr->_n
759 << setw(8) << curr->_m
760 << setw(12) << curr->_cestLo
761 << setw(12) << curr->_cestHi << endl;
763 cerr <<
"Number of sampler bins: " << _flexiBins.size() << endl;
766 template <
typename T,
typename MDist>
771 set<double>::iterator citn = _percentiles.begin();
772 set<double>::iterator cit0 = citn++;
773 for (
auto it = _flexiBins.begin(); it != _flexiBins.end(); ) {
776 double w = curr->_weightsum/_weightsum;
778 if ( curr == _flexiBins.begin() || it == _flexiBins.end() || acc > *citn ) {
779 if ( acc > *citn ) cit0 = citn++;
781 << setw(12) << acc - w
783 << setw(8) << curr->_n
784 << setw(8) << curr->_m
785 << setw(12) << curr->_cestLo
786 << setw(12) << curr->_cestHi << endl;
790 cerr <<
"Number of sampler bins: " << _flexiBins.size() << endl;
810 #if HEPMC_VERSION_CODE >= 3000000 811 const HepMC::HeavyIon * hi = e.
genEvent()->heavy_ion();
812 if ( hi ) _estimate = 100.0 - hi->centrality;
Definition: CentralityBinner.hh:527
Definition: ALICE_2010_I880049.cc:13
static string path(T t)
Return the name of an AnalysisObject.
Definition: CentralityBinner.hh:482
static void add(T &t, const T &o)
Add the contents of o to t.
Definition: CentralityBinner.hh:86
int compare(const Projection &p) const
Compare projections.
Definition: CentralityBinner.hh:55
static T clone(const T &t)
Make a clone of the given object.
Definition: CentralityBinner.hh:81
virtual unique_ptr< Projection > clone() const =0
Clone on the heap.
CentralityBinner(int maxbins=200, double wlim=0.02)
Definition: CentralityBinner.hh:144
static T clone(const T &t)
Make a clone of the given object.
Definition: CentralityBinner.hh:465
void debug()
Print out the _flexiBins to cerr.
Definition: CentralityBinner.hh:767
static void add(vector< T > &tv, const vector< T > &ov)
Add the contents of o to t.
Definition: CentralityBinner.hh:499
void project(const Event &e)
Perform the projection on the Event.
Definition: CentralityBinner.hh:47
DEFAULT_RIVET_PROJ_CLONE(CentralityEstimator)
Clone on the heap.
CentralityEstimator()
Constructor.
Definition: CentralityBinner.hh:39
Definition: CentralityBinner.hh:78
static void scale(Tuple &tup, double f)
Scale the contents of a given object.
Definition: CentralityBinner.hh:590
static string path(const vector< T > &tv)
Return the path of an AnalysisObject.
Definition: CentralityBinner.hh:514
static T clone(const T &t)
Make a clone of the given object.
Definition: CentralityBinner.hh:434
const GenEvent * genEvent() const
The generated event obtained from an external event generator.
Definition: Event.hh:51
void add(T t, double cmin, double cmax, double cestmin=-1.0, double cestmax=-1.0)
Definition: CentralityBinner.hh:174
double estimator() const
Definition: CentralityBinner.hh:239
static void scale(T &t, double f)
Scale the contents of a given object.
Definition: CentralityBinner.hh:444
set< FlexiBin > FlexiBinSet
Convenient typedefs.
Definition: CentralityBinner.hh:367
static void add(Tuple &tup, const Tuple &otup)
Add the contents of o to t.
Definition: CentralityBinner.hh:585
void finalize()
Definition: CentralityBinner.hh:681
Common base class for Projection and Analysis, used for internal polymorphism.
Definition: ProjectionApplier.hh:21
Base class for projections giving the value of an observable sensitive to the centrality of a collisi...
Definition: CentralityBinner.hh:35
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value &&std::is_arithmetic< N3 >::value, bool >::type inRange(N1 value, N2 low, N3 high, RangeBoundary lowbound=CLOSED, RangeBoundary highbound=OPEN)
Determine if value is in the range low to high, for floating point numbers.
Definition: MathUtils.hh:103
map< double, double > edges() const
Definition: CentralityBinner.hh:223
virtual std::string name() const
Return the class name.
Definition: CentralityBinner.hh:158
void setProjection(const CentralityEstimator &p, string pname)
Definition: CentralityBinner.hh:152
Definition: CentralityBinner.hh:795
void project(const Event &e)
Perform the projection on the Event.
Definition: CentralityBinner.hh:808
Cmp< Projection > mkNamedPCmp(const Projection &otherparent, const std::string &pname) const
Definition: Projection.cc:51
static string path(T t)
Return the path of an AnalysisObject.
Definition: CentralityBinner.hh:102
double estimate() const
The value of the centrality estimate.
Definition: CentralityBinner.hh:63
static string path(const Tuple &tup)
Return the path of an AnalysisObject.
Definition: CentralityBinner.hh:599
const PROJ & declare(const PROJ &proj, const std::string &name)
Register a contained projection (user-facing version)
Definition: ProjectionApplier.hh:160
static void scale(vector< T > &tv, double f)
Scale the contents of a given object.
Definition: CentralityBinner.hh:505
double max(const vector< double > &in, double errval=DBL_NAN)
Find the maximum value in the vector.
Definition: Utils.hh:465
static double dist(double cestLo, double cestHi, double weight, double clo, double chi, double, double)
Definition: CentralityBinner.hh:122
static Tuple clone(const Tuple &tup)
Make a clone of the given object.
Definition: CentralityBinner.hh:578
const T & current() const
Return the current AnalysisObject from the latest call to select().
Definition: CentralityBinner.hh:233
static string path(T t)
Return the path of an AnalysisObject.
Definition: CentralityBinner.hh:451
static vector< T > clone(const vector< T > &tv)
Make a clone of the given object.
Definition: CentralityBinner.hh:492
static void add(T &t, const T &o)
Add the contents of o to t.
Definition: CentralityBinner.hh:439
static void normalize(T &t, double sumw)
Definition: CentralityBinner.hh:97
void normalizePerEvent()
Definition: CentralityBinner.hh:217
static void scale(T &t, double f)
Scale the contents of a given object.
Definition: CentralityBinner.hh:475
static void scale(T &t, double f)
Scale the contents of a given object.
Definition: CentralityBinner.hh:91
Base class for all Rivet projections.
Definition: Projection.hh:29
int compare(const Projection &p) const
Compare projections.
Definition: CentralityBinner.hh:817
Definition: CentralityBinner.hh:137
T select(const Event &event, double weight=1.0)
Definition: CentralityBinner.hh:194
static void add(T &t, const T &o)
Add the contents of o to t.
Definition: CentralityBinner.hh:470
double min(const vector< double > &in, double errval=DBL_NAN)
Find the minimum value in the vector.
Definition: Utils.hh:459
Definition: CentralityBinner.hh:111
GeneratedCentrality()
Constructor.
Definition: CentralityBinner.hh:800