BackgroundEstimator.hh

00001 #ifndef __FASTJET_BACKGROUND_ESTIMATOR_HH__
00002 #define __FASTJET_BACKGROUND_ESTIMATOR_HH__
00003 
00004 #include <fastjet/ClusterSequenceAreaBase.hh>
00005 #include <fastjet/Selector.hh>
00006 #include <iostream>
00007 
00008 FASTJET_BEGIN_NAMESPACE     // defined in fastjet/internal/base.hh
00009 
00010 
00011 /// @ingroup tools
00012 /// \class BackgroundEstimator
00013 /// Class to estimate the density of the background per unit area
00014 ///
00015 /// The default behaviour of this class is to compute the global
00016 /// properties of the background as it is done in ClusterSequenceArea.
00017 /// The list of jets included in the computation of the median
00018 /// background is the one that passed a selection test (this could at
00019 /// the same time select jets in a given "range" and jets satisfying
00020 /// various kinematic cuts)
00021 ///
00022 /// Default behaviour:
00023 ///   by default the list of included jets is the inclusive jets from
00024 ///   the given ClusterSequence; the list of explicitly excluded jets 
00025 ///   is empty; we use 4-vector area
00026 ///
00027 /// Beware: 
00028 ///   by default, to correctly handle partially empty events, the
00029 ///   class attempts to calculate an "empty area", based
00030 ///   (schematically) on
00031 ///
00032 ///          range.total_area() - sum_{jets_in_range} jets.area()
00033 ///  
00034 ///   For ranges with small areas, this can be innacurate (particularly 
00035 ///   relevant in dense events where empty_area should be zero and ends
00036 ///   up not being zero).
00037 ///
00038 ///   This calculation of empty area can be avoided if you supply a
00039 ///   ClusterSequenceArea class with explicit ghosts
00040 ///   (ActiveAreaExplicitGhosts). This is _recommended_!
00041 ///
00042 class BackgroundEstimator{
00043 public:
00044   /// @name constructors and destructors
00045   //\{
00046   //----------------------------------------------------------------
00047   /// ctor from a ClusterSequence with area
00048   ///
00049   /// \param csa         the ClusterSequenceArea to use
00050   /// \param rho_range   the range over which jets will be considered
00051   ///
00052   /// Pre-conditions: 
00053   ///  - one should be able to estimate the "empty area" (i.e. the area
00054   ///    not occupied by jets). This is feasible is one of the following
00055   ///    conditions is satisfied:
00056   ///     ( i) the ClusterSequence has explicit ghosts
00057   ///     (ii) the range has a computable area.
00058   ///  - the jet algorithm must be suited for median computation
00059   ///    (otherwise a warning will be issues)
00060   /// Note that selectors with e.g. hardest-jets exclusion do not have a
00061   /// well-defined area
00062   /// For these reasons, it is STRONGLY advised to use explicit ghosts
00063   BackgroundEstimator(const ClusterSequenceAreaBase &csa, const Selector &rho_range);
00064   
00065   /// ctor from a list of jets
00066   ///
00067   /// \param jets        the jets to use
00068   /// \param rho_range   the range over which jets will be considered
00069   ///
00070   /// Pre-conditions:
00071   ///  - all the jets must come from the same cluster sequence
00072   ///  - As for the above ctor, one needs to be able to estimate the
00073   ///    empty area. The conditions that the ClusterSequence must have
00074   ///    explicit ghosts is checked on the ClusterSequence shared by
00075   ///    the jets.
00076   ///  - As for the above ctor, the jet alg must be adequate
00077   BackgroundEstimator(const std::vector<PseudoJet> &jets, const Selector &rho_range);
00078   
00079   /// default dtor
00080   ~BackgroundEstimator();
00081 
00082   //\}
00083 
00084   /// @ name  retrieving fundamental information
00085   //\{
00086   //----------------------------------------------------------------
00087 
00088   /// get rho, the median background density oer unit area
00089   double rho() {
00090     _recompute_if_needed();
00091     return _rho;
00092   }
00093 
00094   /// get sigma, the background fluctuations per unit area
00095   double sigma() {
00096     _recompute_if_needed();
00097     return _sigma;
00098   }
00099 
00100   /// get rho, the median background density oer unit area,
00101   /// locally at the position of a given jet.
00102   ///
00103   /// This requires a relocatable range
00104   double rho(const PseudoJet jet) {
00105     set_reference(jet);
00106     return rho();
00107   }
00108 
00109   /// get sigma, the background fluctuations per unit area,
00110   /// locally at the position of a given jet.
00111   ///
00112   /// This requires a relocatable range
00113   double sigma(const PseudoJet &jet) {
00114     set_reference(jet);
00115     return sigma();
00116   }
00117 
00118   //\}
00119   
00120   /// @ name  retrieving additional useful information
00121   //\{
00122   //----------------------------------------------------------------
00123   /// get the median area of the jets used to actually compute the background properties
00124   double mean_area(){
00125     _require_uptodate();
00126     return _mean_area;
00127   }
00128   
00129   /// get the number of jets used to actually compute the background properties
00130   unsigned int n_jets_used(){
00131     _require_uptodate();
00132     return _n_jets_used;
00133   }
00134 
00135   /// get the number of empty jets used when computing the background properties;
00136   /// (it is deduced from the empty area with an assumption about the average
00137   /// area of jets)
00138   double n_empty_jets(){
00139     _require_uptodate();
00140     return _n_empty_jets;
00141   }
00142 
00143   /// returns the estimate of the area (within Range) that is not occupied
00144   /// by the jets (excluded jets are removed from this count)
00145   double empty_area(){
00146     _require_uptodate();
00147     return _empty_area;
00148   }
00149 
00150   //}
00151 
00152   /// @name configuring behaviour
00153   //\{
00154   //----------------------------------------------------------------
00155 
00156   /// for estimation using a relocatable selector (i.e. local range)
00157   /// this allows to set its position. Note that this HAS to be called
00158   /// before any attempt to compute the background properties
00159   BackgroundEstimator & set_reference(const PseudoJet &jet);
00160 
00161   /// reset to default values
00162   /// set the list of included jets to the inclusive jets and clear the excluded ones
00163   void reset();
00164 
00165   /// specify if one uses the scalar or 4-vector area
00166   ///  \param use_it             whether one uses the 4-vector area or not (true by default)
00167   void set_use_area_4vector(bool use_it = true){
00168     _use_area_4vector = use_it;
00169     _uptodate = false;
00170   }  
00171   //\}
00172   
00173 private:
00174   /// do the actual job
00175   void _compute();
00176   
00177   /// check if the properties need to be recomputed 
00178   /// and do so if needed
00179   void _require_uptodate(){
00180     if (!_uptodate){
00181       throw Error("The requested information can only be obtained once the background has actually been computed");
00182     }
00183   }
00184 
00185   /// check if the properties need to be recomputed 
00186   /// and do so if needed
00187   void _recompute_if_needed(){
00188     if (!_uptodate)
00189       _compute();
00190     _uptodate = true;
00191   }
00192 
00193   /// check that the underlying structure is still alive
00194   /// throw an error otherwise
00195   void _check_csa_alive();
00196 
00197   /// check that the algorithm used for the clustering is adapted for
00198   /// background estimation (i.e. either kt or C/A)
00199   /// Issue a warning otherwise
00200   void _check_jet_alg_good_for_median();
00201   
00202   // the information needed to do the computation
00203   Selector _rho_range;                      ///< range to compute the background in
00204   std::vector<PseudoJet> _included_jets;    ///< jets to be used
00205   std::vector<PseudoJet> _selected_jets;    ///< jets used in practice
00206   bool _use_area_4vector;
00207   
00208   // the actual results of the computation
00209   double _rho;                              ///< background estimated density per unit area
00210   double _sigma;                            ///< background estimated fluctuations
00211   double _mean_area;                        ///< mean area of the jets used to estimate the background
00212   unsigned int _n_jets_used;                ///< number of jets used to estimate the background
00213   double _n_empty_jets;                     ///< number of empty (pure-ghost) jets
00214   double _empty_area;                       ///< the empty (pure-ghost/unclustered) area!
00215 
00216   // internal variables
00217   SharedPtr<PseudoJetStructureBase> _csi;   ///< allows to check if _csa is still valid
00218   bool _uptodate;                           ///< true when the background computation is up-to-date
00219 
00220   /// handle warning messages
00221   static LimitedWarning _warnings;
00222   static LimitedWarning _warnings_zero_area;
00223   static LimitedWarning _warnings_relocation;
00224 };
00225 
00226 FASTJET_END_NAMESPACE
00227 
00228 #endif  // __BACKGROUND_ESTIMATOR_HH__
00229