Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:06:46

0001 #ifndef __FASTJET_BACKGROUND_ESTIMATOR_BASE_HH__
0002 #define __FASTJET_BACKGROUND_ESTIMATOR_BASE_HH__
0003 
0004 //FJSTARTHEADER
0005 // $Id$
0006 //
0007 // Copyright (c) 2005-2021, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
0008 //
0009 //----------------------------------------------------------------------
0010 // This file is part of FastJet.
0011 //
0012 //  FastJet is free software; you can redistribute it and/or modify
0013 //  it under the terms of the GNU General Public License as published by
0014 //  the Free Software Foundation; either version 2 of the License, or
0015 //  (at your option) any later version.
0016 //
0017 //  The algorithms that underlie FastJet have required considerable
0018 //  development. They are described in the original FastJet paper,
0019 //  hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
0020 //  FastJet as part of work towards a scientific publication, please
0021 //  quote the version you use and include a citation to the manual and
0022 //  optionally also to hep-ph/0512210.
0023 //
0024 //  FastJet is distributed in the hope that it will be useful,
0025 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
0026 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0027 //  GNU General Public License for more details.
0028 //
0029 //  You should have received a copy of the GNU General Public License
0030 //  along with FastJet. If not, see <http://www.gnu.org/licenses/>.
0031 //----------------------------------------------------------------------
0032 //FJENDHEADER
0033 
0034 #include "fastjet/ClusterSequenceAreaBase.hh"
0035 #include "fastjet/FunctionOfPseudoJet.hh"
0036 #include "fastjet/Selector.hh"
0037 #include "fastjet/Error.hh"
0038 #include <iostream>
0039 
0040 FASTJET_BEGIN_NAMESPACE     // defined in fastjet/internal/base.hh
0041 
0042 
0043 /// @ingroup tools_background
0044 /// @name helpers to handle the result of the background estimation
0045 //\{
0046 ///
0047 /// /// a class that holds the result of the calculation
0048 ///
0049 /// By default it provides access to the main background properties:
0050 /// rho, rho_m, sigma and sigma_m. If background estimators derived
0051 /// from the base class want to store more information, this can be
0052 /// done using the "Extra" information.
0053 class BackgroundEstimate{
0054 public:
0055   /// ctor wo initialisation
0056   BackgroundEstimate()
0057     : _rho(0.0), _sigma(0.0), _rho_m(0.0), _sigma_m(0.0), 
0058       _has_sigma(false), _has_rho_m(false),
0059       _mean_area(0.0){}
0060 
0061   
0062   /// @name for accessing information about the background
0063   ///@{
0064 
0065   /// background density per unit area
0066   double rho() const {return _rho;}
0067 
0068   /// background fluctuations per unit square-root area
0069   /// must be multipled by sqrt(area) to get fluctuations for a region
0070   /// of a given area.
0071   double sigma() const {return _sigma;}
0072 
0073   /// true if this background estimate has a determination of sigma
0074   bool has_sigma() {return true;}
0075 
0076   /// purely longitudinal (particle-mass-induced)
0077   /// component of the background density per unit area
0078   double rho_m() const {return _rho_m;}
0079 
0080   /// fluctuations in the purely longitudinal (particle-mass-induced)
0081   /// component of the background density per unit square-root area
0082   double sigma_m() const {return _sigma_m;}
0083 
0084   /// true if this background estimate has a determination of rho_m.
0085   /// Support for sigma_m is automatic if one has sigma and rho_m support.
0086   bool has_rho_m() const {return _has_rho_m;}
0087 
0088   /// mean area of the patches used to compute the background properties
0089   double mean_area() const {return _mean_area;}
0090 
0091   /// base class for extra information
0092   class Extras {
0093   public:
0094     // dummy ctor
0095     Extras(){};
0096 
0097     // dummy virtual dtor
0098     // makes it polymorphic to allow for dynamic_cast
0099     virtual ~Extras(){}; 
0100   };
0101 
0102   /// returns true if the background estimate has extra info
0103   bool has_extras() const{
0104     return _extras.get();
0105   }
0106   
0107   /// returns true if the background estimate has extra info
0108   /// compatible with the provided template type
0109   template<typename T>
0110   bool has_extras() const{
0111     return _extras.get() && dynamic_cast<const T *>(_extras.get());
0112   }
0113 
0114   /// returns a reference to the extra information associated with a
0115   /// given BackgroundEstimator. It assumes that the extra
0116   /// information is reachable with class name
0117   /// BackgroundEstimator::Extras
0118   template<typename BackgroundEstimator>
0119   const typename BackgroundEstimator::Extras & extras() const{
0120     return dynamic_cast<const typename BackgroundEstimator::Extras &>(* _extras.get());
0121   }
0122 
0123   ///@}
0124 
0125 
0126   /// @name for setting information about the background (internal FJ use)
0127   ///@{
0128 
0129   /// reset to default
0130   void reset(){
0131     _rho = _sigma = _rho_m = _sigma_m = _mean_area = 0.0;
0132     _has_sigma = _has_rho_m = false;
0133     _extras.reset();
0134   }
0135   void set_rho(double rho_in) {_rho = rho_in;}
0136   void set_sigma(double sigma_in) {_sigma = sigma_in;}
0137   void set_has_sigma(bool has_sigma_in) {_has_sigma = has_sigma_in;}
0138   void set_rho_m(double rho_m_in) {_rho_m = rho_m_in;}
0139   void set_sigma_m(double sigma_m_in) {_sigma_m = sigma_m_in;}
0140   void set_has_rho_m(bool has_rho_m_in) {_has_rho_m = has_rho_m_in;}
0141   void set_mean_area(double mean_area_in) {_mean_area = mean_area_in;}
0142 
0143   /// apply a rescaling factor (to rho, rho_m, sigma, sigma_m)
0144   void apply_rescaling_factor(double rescaling_factor){
0145     _rho     *= rescaling_factor;
0146     _sigma   *= rescaling_factor;
0147     _rho_m   *= rescaling_factor;
0148     _sigma_m *= rescaling_factor;
0149   }
0150 
0151   /// sets the extra info based on the provided pointer
0152   ///
0153   /// When calling this method, the BackgroundEstimate class takes
0154   /// ownership of the pointer (and is responsible for deleting it)
0155   void set_extras(Extras *extras_in) {
0156     _extras.reset(extras_in);
0157   }
0158   ///@}
0159 
0160 
0161 protected:
0162   double _rho;       ///< background estimated density per unit area
0163   double _sigma;     ///< background estimated fluctuations
0164   double _rho_m;     ///< "mass" background estimated density per unit area
0165   double _sigma_m;   ///< "mass" background estimated fluctuations
0166   bool _has_sigma;   ///< true if this estimate has a determination of sigma
0167   bool _has_rho_m;   ///< true if this estimate has a determination of rho_m
0168   double _mean_area; ///< mean area of the patches used to compute the bkg properties
0169   
0170 
0171   SharedPtr<Extras> _extras;
0172 
0173 };
0174 
0175 
0176 /// @ingroup tools_background
0177 /// \class BackgroundEstimatorBase
0178 ///
0179 /// Abstract base class that provides the basic interface for classes
0180 /// that estimate levels of background radiation in hadron and
0181 /// heavy-ion collider events.
0182 ///
0183 class BackgroundEstimatorBase {
0184 public:
0185   /// @name  constructors and destructors
0186   //\{
0187   //----------------------------------------------------------------
0188   BackgroundEstimatorBase() : _rescaling_class(0) {
0189     _set_cache_unavailable();                                               
0190   }
0191 
0192 #ifdef FASTJET_HAVE_THREAD_SAFETY
0193   /// because of the internal atomic variale, we need to explicitly
0194   /// implement a copy ctor
0195   BackgroundEstimatorBase(const BackgroundEstimatorBase &other_bge);
0196 #endif
0197 
0198   /// a default virtual destructor that does nothing
0199   virtual ~BackgroundEstimatorBase() {}
0200   //\}
0201 
0202   /// @name setting a new event
0203   //\{
0204   //----------------------------------------------------------------
0205 
0206   /// tell the background estimator that it has a new event, composed
0207   /// of the specified particles.
0208   virtual void set_particles(const std::vector<PseudoJet> & particles) = 0;
0209 
0210   /// an alternative call that takes a random number generator seed
0211   /// (typically a vector of length 2) to ensure reproducibility of
0212   /// background estimators that rely on random numbers (specifically
0213   /// JetMedianBackgroundEstimator with ghosted areas)
0214   virtual void set_particles_with_seed(const std::vector<PseudoJet> & particles, const std::vector<int> & /*seed*/) {
0215     set_particles(particles);
0216   }
0217 
0218   //\}
0219 
0220   /// return a pointer to a copy of this BGE; the user is responsible
0221   /// for eventually deleting the resulting object.
0222   virtual BackgroundEstimatorBase * copy() const = 0;
0223 
0224   /// @name  retrieving fundamental information
0225   //\{
0226   //----------------------------------------------------------------
0227   /// get the full set of background properties
0228   virtual BackgroundEstimate estimate() const = 0;
0229   
0230   /// get the full set of background properties for a given reference jet
0231   virtual BackgroundEstimate estimate(const PseudoJet &jet) const = 0;
0232 
0233   /// get rho, the background density per unit area
0234   virtual double rho() const = 0;
0235 
0236   /// get sigma, the background fluctuations per unit square-root area;
0237   /// must be multipled by sqrt(area) to get fluctuations for a region
0238   /// of a given area.
0239   virtual double sigma() const { 
0240     throw Error("sigma() not supported for this Background Estimator");
0241   }
0242 
0243   /// get rho, the background density per unit area, locally at the
0244   /// position of a given jet. Note that this is not const, because a
0245   /// user may then wish to query other aspects of the background that
0246   /// could depend on the position of the jet last used for a rho(jet)
0247   /// determination.
0248   virtual double rho(const PseudoJet & jet) = 0;
0249 
0250   /// get sigma, the background fluctuations per unit area, locally at
0251   /// the position of a given jet. As for rho(jet), it is non-const.
0252   virtual double sigma(const PseudoJet & /*jet*/) { 
0253     throw Error("sigma(jet) not supported for this Background Estimator");
0254   }
0255 
0256   /// returns true if this background estimator has support for
0257   /// determination of sigma
0258   virtual bool has_sigma() const {return false;}
0259 
0260   //----------------------------------------------------------------
0261   // now do the same thing for rho_m and sigma_m
0262 
0263   /// returns rho_m, the purely longitudinal, particle-mass-induced
0264   /// component of the background density per unit area
0265   virtual double rho_m() const{
0266     throw Error("rho_m() not supported for this Background Estimator");
0267   }
0268 
0269   /// returns sigma_m, a measure of the fluctuations in the purely
0270   /// longitudinal, particle-mass-induced component of the background
0271   /// density per unit square-root area; must be multipled by sqrt(area) to get
0272   /// fluctuations for a region of a given area.
0273   virtual double sigma_m() const { 
0274     throw Error("sigma_m() not supported for this Background Estimator");
0275   }
0276 
0277   /// Returns rho_m locally at the jet position. As for rho(jet), it is non-const.
0278   virtual double rho_m(const PseudoJet & /*jet*/){
0279     throw Error("rho_m(jet) not supported for this Background Estimator");
0280   }
0281 
0282   /// Returns sigma_m locally at the jet position. As for rho(jet), it is non-const.
0283   virtual double sigma_m(const PseudoJet & /*jet*/) { 
0284     throw Error("sigma_m(jet) not supported for this Background Estimator");
0285   }
0286 
0287   /// Returns true if this background estimator has support for
0288   /// determination of rho_m.
0289   ///
0290   /// Note that support for sigma_m is automatic is one has sigma and
0291   /// rho_m support.
0292   virtual bool has_rho_m() const {return false;}
0293   //\}
0294 
0295 
0296   /// @name configuring the behaviour
0297   //\{
0298   //----------------------------------------------------------------
0299 
0300   /// Set a pointer to a class that calculates the rescaling factor as
0301   /// a function of the jet (position). Note that the rescaling factor
0302   /// is used both in the determination of the "global" rho (the pt/A
0303   /// of each jet is divided by this factor) and when asking for a
0304   /// local rho (the result is multiplied by this factor).
0305   ///
0306   /// The BackgroundRescalingYPolynomial class can be used to get a
0307   /// rescaling that depends just on rapidity.
0308   ///
0309   /// There is currently no support for different rescaling classes 
0310   /// for rho and rho_m determinations.
0311   virtual void set_rescaling_class(const FunctionOfPseudoJet<double> * rescaling_class_in) { _rescaling_class = rescaling_class_in; }
0312 
0313   /// return the pointer to the jet density class
0314   const FunctionOfPseudoJet<double> *  rescaling_class() const{
0315     return _rescaling_class;
0316   }
0317 
0318   //\}
0319 
0320   /// @name description
0321   //\{
0322   //----------------------------------------------------------------
0323 
0324   /// returns a textual description of the background estimator
0325   virtual std::string description() const = 0;
0326 
0327   //\}
0328 
0329   
0330 protected:
0331   /// @name helpers for derived classes
0332   ///
0333   /// Note that these helpers are related to median-based estimation
0334   /// of the background, so there is no guarantee that they will
0335   /// remain in this base class in the long term
0336   //\{
0337   //----------------------------------------------------------------
0338 
0339   /// given a quantity in a vector (e.g. pt_over_area) and knowledge
0340   /// about the number of empty jets, calculate the median and
0341   /// stand_dev_if_gaussian (roughly from the 16th percentile)
0342   ///
0343   /// If do_fj2_calculation is set to true then this performs FastJet
0344   /// 2.X estimation of the standard deviation, which has a spurious
0345   /// offset in the limit of a small number of jets.
0346   void _median_and_stddev(const std::vector<double> & quantity_vector, 
0347                           double n_empty_jets, 
0348                           double & median, 
0349                           double & stand_dev_if_gaussian,
0350                           bool do_fj2_calculation = false
0351                           ) const;
0352 
0353   /// computes a percentile of a given _sorted_ vector
0354   ///  \param sorted_quantity_vector   the vector contains the data sample
0355   ///  \param percentile               the percentile (defined between 0 and 1) to compute
0356   ///  \param nempty                   an additional number of 0's
0357   ///                                  (considered at the beginning of 
0358   ///                                  the quantity vector)
0359   ///  \param do_fj2_calculation       carry out the calculation as it
0360   ///                                  was done in fj2 (suffers from "edge effects")
0361   double _percentile(const std::vector<double> & sorted_quantity_vector, 
0362                      const double percentile, 
0363                      const double nempty=0.0,
0364                      const bool do_fj2_calculation = false) const;
0365 
0366   //\}
0367 
0368   const FunctionOfPseudoJet<double> * _rescaling_class;
0369   static LimitedWarning _warnings_empty_area;
0370 
0371 
0372   // cached actual results of the computation
0373   mutable bool _cache_available;
0374   mutable BackgroundEstimate _cached_estimate;    ///< all the info about what is computed
0375   //\}
0376 
0377   /// @name helpers for thread safety
0378   ///
0379   /// Note that these helpers are related to median-based estimation
0380   /// of the background, so there is no guarantee that they will
0381   /// remain in this base class in the long term
0382   //\{
0383 #ifdef FASTJET_HAVE_THREAD_SAFETY
0384   // true when one is currently writing to cache (i.e. when the spin lock is set)
0385   mutable std::atomic<bool> _writing_to_cache;
0386 
0387   void _set_cache_unavailable(){
0388     _cache_available = false;
0389     _writing_to_cache = false;
0390   }
0391 
0392   // // allows us to lock things down before caching basic (patches) info  
0393   // std::mutex _jets_caching_mutex;
0394 #else
0395   void _set_cache_unavailable(){
0396     _cache_available = false;
0397   }
0398 #endif
0399 
0400   void _lock_if_needed()   const;
0401   void _unlock_if_needed() const;
0402 
0403   //\}
0404 
0405 };
0406 
0407 
0408 
0409 //----------------------------------------------------------------------
0410 /// @ingroup tools_background
0411 /// A background rescaling that is a simple polynomial in y
0412 class BackgroundRescalingYPolynomial : public FunctionOfPseudoJet<double> {
0413 public:
0414   /// construct a background rescaling polynomial of the form
0415   /// a0 + a1*y + a2*y^2 + a3*y^3 + a4*y^4
0416   ///
0417   /// The following values give a reasonable reproduction of the
0418   /// Pythia8 tune 4C background shape for pp collisions at
0419   /// sqrt(s)=7TeV:
0420   ///
0421   /// - a0 =  1.157
0422   /// - a1 =  0
0423   /// - a2 = -0.0266
0424   /// - a3 =  0
0425   /// - a4 =  0.000048
0426   ///
0427   BackgroundRescalingYPolynomial(double a0=1, 
0428                                  double a1=0, 
0429                                  double a2=0, 
0430                                  double a3=0, 
0431                                  double a4=0) : _a0(a0), _a1(a1), _a2(a2), _a3(a3), _a4(a4) {}
0432 
0433   /// return the rescaling factor associated with this jet
0434   virtual double result(const PseudoJet & jet) const;
0435 private:
0436   double _a0, _a1, _a2, _a3, _a4;
0437 };
0438 
0439 
0440 
0441 
0442 
0443 FASTJET_END_NAMESPACE
0444 
0445 #endif  // __BACKGROUND_ESTIMATOR_BASE_HH__
0446 
0447 // //--------------------------------------------------
0448 // // deprecated
0449 // class JetMedianBGE{
0450 //   BackgroundEstimateDefinition();
0451 // 
0452 //   ....;
0453 // }
0454 // //--------------------------------------------------
0455 // 
0456 // class BackgroundEstimateDefinition{ 
0457 //   //const EventBackground get_event_background(particles, <seed>) const;
0458 // 
0459 // 
0460 //   //--------------------------------------------------
0461 //   // DEPRECATED
0462 //   void set_particles() {
0463 // 
0464 //     _worker = ...;
0465 //   double rho(const PseudoJet &/*jet*/) const{ _worker.rho();}
0466 // 
0467 //   //--------------------------------------------------
0468 //   
0469 //   EventBackground(Worker?) _cached_event_background;
0470 // };
0471 // 
0472 // class EventBackground{
0473 //   EventBackground(particles, BackgroundEstimateDefinition);
0474 // 
0475 //   
0476 //   class EventBackgroundWorker{
0477 //     
0478 //     ...
0479 //   };
0480 // 
0481 //   
0482 //   BackgroundEstimate estimate() const;
0483 //   BackgroundEstimate estimate(jet) const;
0484 //   
0485 //   // do we want this:
0486 //   double rho();
0487 //   double rho(jet);
0488 //   //?
0489 //    
0490 // 
0491 //   mutable BackgroundEstimate _estimate;
0492 // 
0493 // 
0494 //   SharedPtr<EventBackgroundWorker> _event_background_worker;
0495 // }
0496 // 
0497 // class BackgroundEstimate{
0498 // 
0499 //   double rho();
0500 //   
0501 //   SharedPtr<EventBackgroundWorker> _event_background_worker;
0502 // 
0503 // private:
0504 //   _rho;
0505 //   _sigma;
0506 //   _rho_m;
0507 //   _sigma_m;
0508 //   _has_rho_m;
0509 //   _has_sigma;
0510 // 
0511 //   // info specific to JMBGE: _reference_jet, mean_area, n_jets_used, n_empty_jets, _empty_area
0512 //   //                         all need to go in the estimate in general
0513 //   // info specific to GMBGE: RectangularGrid, _mean_area (can go either in the the def, the eventBG or the estimate
0514 //   
0515 // };