FastJet 3.0beta1
GridMedianBackgroundEstimator.cc
00001 //STARTHEADER
00002 // $Id: GridMedianBackgroundEstimator.cc 2472 2011-07-26 11:25:42Z cacciari $
00003 //
00004 // Copyright (c) 2005-2011, Matteo Cacciari, Gavin Salam and Gregory Soyez
00005 //
00006 //----------------------------------------------------------------------
00007 // This file is part of FastJet.
00008 //
00009 //  FastJet is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU General Public License as published by
00011 //  the Free Software Foundation; either version 2 of the License, or
00012 //  (at your option) any later version.
00013 //
00014 //  The algorithms that underlie FastJet have required considerable
00015 //  development and are described in hep-ph/0512210. If you use
00016 //  FastJet as part of work towards a scientific publication, please
00017 //  include a citation to the FastJet paper.
00018 //
00019 //  FastJet is distributed in the hope that it will be useful,
00020 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 //  GNU General Public License for more details.
00023 //
00024 //  You should have received a copy of the GNU General Public License
00025 //  along with FastJet; if not, write to the Free Software
00026 //  Foundation, Inc.:
00027 //      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //----------------------------------------------------------------------
00029 //ENDHEADER
00030 
00031 
00032 #include "fastjet/tools/GridMedianBackgroundEstimator.hh"
00033 using namespace std;
00034 
00035 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00036 
00037 //----------------------------------------------------------------------
00038 // setting a new event
00039 //----------------------------------------------------------------------
00040 // tell the background estimator that it has a new event, composed
00041 // of the specified particles.
00042 void GridMedianBackgroundEstimator::set_particles(const vector<PseudoJet> & particles) {
00043   fill(_scalar_pt.begin(), _scalar_pt.end(), 0.0);
00044   for (unsigned i = 0; i < particles.size(); i++) {
00045     int j = igrid(particles[i]);
00046     if (j >= 0){
00047       if (_rescaling_class == 0)
00048         _scalar_pt[j] += particles[i].perp();
00049       else
00050         _scalar_pt[j] += particles[i].perp()/(*_rescaling_class)(particles[i]);
00051     }
00052   }
00053   sort(_scalar_pt.begin(), _scalar_pt.end());
00054 
00055   _has_particles = true;
00056 }
00057 
00058 
00059 //----------------------------------------------------------------------
00060 // retrieving fundamental information
00061 //----------------------------------------------------------------------
00062 // get rho, the median background density per unit area
00063 double GridMedianBackgroundEstimator::rho() const {
00064   verify_particles_set();
00065   return _percentile(_scalar_pt, 0.5) / _cell_area;
00066 }
00067 
00068 
00069 //----------------------------------------------------------------------
00070 // get sigma, the background fluctuations per unit area; must be
00071 // multipled by sqrt(area) to get fluctuations for a region of a
00072 // given area.
00073 double GridMedianBackgroundEstimator::sigma() const{
00074   verify_particles_set();
00075   // watch out: by definition, our sigma is the standard deviation of
00076   // the pt density multiplied by the square root of the cell area
00077   return (_percentile(_scalar_pt, 0.5) -
00078           _percentile(_scalar_pt, (1.0-0.6827)/2.0)
00079           )/sqrt(_cell_area);
00080 }
00081 
00082 //----------------------------------------------------------------------
00083 // get rho, the background density per unit area, locally at the
00084 // position of a given jet. Note that this is not const, because a
00085 // user may then wish to query other aspects of the background that
00086 // could depend on the position of the jet last used for a rho(jet)
00087 // determination.
00088 double GridMedianBackgroundEstimator::rho(const PseudoJet & jet)  {
00089   verify_particles_set();
00090   double rescaling = (_rescaling_class == 0) ? 1.0 : (*_rescaling_class)(jet);
00091   return rescaling*rho();
00092 }
00093 
00094 
00095 //----------------------------------------------------------------------
00096 // get sigma, the background fluctuations per unit area, locally at
00097 // the position of a given jet. As for rho(jet), it is non-const.
00098 double GridMedianBackgroundEstimator::sigma(const PseudoJet & jet){
00099   verify_particles_set();
00100   double rescaling = (_rescaling_class == 0) ? 1.0 : (*_rescaling_class)(jet);
00101   return rescaling*sigma();
00102 }
00103 
00104 //----------------------------------------------------------------------
00105 // verify that particles have been set and throw an error if not
00106 void GridMedianBackgroundEstimator::verify_particles_set() const {
00107   if (!_has_particles) throw Error("GridMedianBackgroundEstimator::rho() or sigma() called without particles having been set");
00108 }
00109 
00110 
00111 //----------------------------------------------------------------------
00112 // description
00113 //----------------------------------------------------------------------
00114 string GridMedianBackgroundEstimator::description() const { 
00115   ostringstream desc;
00116   desc << "GridMedianBackgroundEstimator, with grid extension |y| < " << _ymax 
00117        << " and requested grid spacing = " << _requested_grid_spacing;
00118   return desc.str();
00119 }       
00120 
00121 
00122 //----------------------------------------------------------------------
00123 // configuring the behaviour
00124 //----------------------------------------------------------------------
00125 // Set a pointer to a class that calculates the rescaling factor as
00126 // a function of the jet (position). Note that the rescaling factor
00127 // is used both in the determination of the "global" rho (the pt/A
00128 // of each jet is divided by this factor) and when asking for a
00129 // local rho (the result is multiplied by this factor).
00130 //
00131 // The BackgroundRescalingYPolynomial class can be used to get a
00132 // rescaling that depends just on rapidity.
00133 //
00134 // Note that this has to be called BEFORE any attempt to do an
00135 // actual computation
00136 void GridMedianBackgroundEstimator::set_rescaling_class(const FunctionOfPseudoJet<double> * rescaling_class) {
00137   // The rescaling is taken into account when particles are set. So
00138   // you need to call set_particles again if you set the rescaling
00139   // class. We thus warn if there are already some available
00140   // particles
00141   if (_has_particles)
00142     _warning_rescaling.warn("GridMedianBackgroundEstimator::set_rescaling_class(): trying to set the rescaling class when there are already particles that have been set is dangerous: the rescaling will not affect the already existing particles resulting in mis-estimation of rho. You need to call set_particles() again before proceeding with any background estimation.");
00143   
00144   BackgroundEstimatorBase::set_rescaling_class(rescaling_class);
00145 }
00146 
00147 
00148 //----------------------------------------------------------------------
00149 // protected material
00150 //----------------------------------------------------------------------
00151 // configure the grid
00152 void GridMedianBackgroundEstimator::setup_grid() {
00153 
00154   // since we've exchanged the arguments of the grid constructor,
00155   // there's a danger of calls with exchanged ymax,spacing arguments -- 
00156   // the following check should catch most such situations.
00157   assert(_ymax>0 && _ymax - _ymin >= _requested_grid_spacing);
00158 
00159   // this grid-definition code is becoming repetitive -- it should
00160   // probably be moved somewhere central...
00161   double ny_double = (_ymax-_ymin) / _requested_grid_spacing;
00162   _ny = int(ny_double+0.5);
00163   _dy = (_ymax-_ymin) / _ny;
00164   
00165   _nphi = int (twopi / _requested_grid_spacing + 0.5);
00166   _dphi = twopi / _nphi;
00167 
00168   // some sanity checking (could throw a fastjet::Error)
00169   assert(_ny >= 1 && _nphi >= 1);
00170 
00171   _ntotal = _nphi * _ny;
00172   _scalar_pt.resize(_ntotal);
00173   _cell_area = _dy * _dphi;
00174 }
00175 
00176 
00177 //----------------------------------------------------------------------
00178 // retrieve the grid cell index for a given PseudoJet
00179 int GridMedianBackgroundEstimator::igrid(const PseudoJet & p) const {
00180   // directly taking int does not work for values between -1 and 0
00181   // so use floor instead
00182   // double iy_double = (p.rap() - _ymin) / _dy;
00183   // if (iy_double < 0.0) return -1;
00184   // int iy = int(iy_double);
00185   // if (iy >= _ny) return -1;
00186 
00187   // writing it as below gives a huge speed gain (factor two!). Even
00188   // though answers are identical and the routine here is not the
00189   // speed-critical step. It's not at all clear why.
00190   int iy = int(floor( (p.rap() - _ymin) / _dy ));
00191   if (iy < 0 || iy >= _ny) return -1;
00192 
00193   int iphi = int( p.phi()/_dphi );
00194   assert(iphi >= 0 && iphi <= _nphi);
00195   if (iphi == _nphi) iphi = 0; // just in case of rounding errors
00196 
00197   int igrid_res = iy*_nphi + iphi;
00198   assert (igrid_res >= 0 && igrid_res < _ny*_nphi);
00199   return igrid_res;
00200 }
00201 
00202 
00203 FASTJET_END_NAMESPACE        // defined in fastjet/internal/base.hh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends