FastJet 3.0alpha3
Selector.hh
00001 #ifndef __SELECTOR_HH__
00002 #define __SELECTOR_HH__
00003 
00004 //STARTHEADER
00005 // $Id: Selector.hh 2188 2011-05-31 15:44:46Z soyez $
00006 //
00007 // Copyright (c) 2009-2010, Matteo Cacciari, Gavin Salam and Gregory Soyez
00008 //
00009 //----------------------------------------------------------------------
00010 // This file is part of FastJet.
00011 //
00012 //  FastJet is free software; you can redistribute it and/or modify
00013 //  it under the terms of the GNU General Public License as published by
00014 //  the Free Software Foundation; either version 2 of the License, or
00015 //  (at your option) any later version.
00016 //
00017 //  The algorithms that underlie FastJet have required considerable
00018 //  development and are described in hep-ph/0512210. If you use
00019 //  FastJet as part of work towards a scientific publication, please
00020 //  include a citation to the FastJet paper.
00021 //
00022 //  FastJet is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU General Public License
00028 //  along with FastJet; if not, write to the Free Software
00029 //  Foundation, Inc.:
00030 //      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00031 //----------------------------------------------------------------------
00032 //ENDHEADER
00033 
00034 #include "fastjet/PseudoJet.hh"
00035 #include "fastjet/RangeDefinition.hh"  // for initialisation from a RangeDefinition
00036 #include <limits>
00037 #include <cmath>
00038 
00039 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00040 
00041 //----------------------------------------------------------------------
00042 /// @ingroup selectors
00043 /// \class Selector
00044 /// Class that encodes information about cuts and other selection
00045 /// criteria that can be applied to PseudoJet(s).
00046 ///
00047 class Selector;
00048 //----------------------------------------------------------------------
00049 
00050 /// @ingroup selectors
00051 /// \class SelectorWorker
00052 /// default selector worker is an abstract virtual base class
00053 ///
00054 /// The Selector class is only an interface, it is the SelectorWorker
00055 /// that really does the work. To implement various selectors, one
00056 /// thus has to overload this class.
00057 class SelectorWorker {
00058 public:
00059   //----------------------------------------------------------
00060   // fundamental info
00061   //----------------------------------------------------------
00062   /// default dtor
00063   virtual ~SelectorWorker() {}
00064 
00065   //----------------------------------------------------------
00066   // basic operations for checking what gets selected
00067   //----------------------------------------------------------
00068 
00069   /// returns true if a given object passes the selection criterion.
00070   /// This has to be overloaded by derived workers
00071   virtual bool pass(const PseudoJet & jet) const = 0;
00072 
00073   /// For each jet that does not pass the cuts, this routine sets the 
00074   /// pointer to 0. 
00075   ///
00076   /// It does not assume that the PseudoJet* passed as argumetn are not NULL
00077   virtual void terminator(std::vector<const PseudoJet *> & jets) const {
00078     for (unsigned i = 0; i < jets.size(); i++) {
00079       if (jets[i] && !pass(*jets[i])) jets[i] = NULL;
00080     }
00081   }
00082 
00083   /// returns true if this can be applied jet by jet
00084   virtual bool applies_jet_by_jet() const {return true;}
00085 
00086   /// returns a description of the worker
00087   virtual std::string description() const {return "missing description";}
00088 
00089   //----------------------------------------------------------
00090   // operations for dealing with reference jets
00091   //----------------------------------------------------------
00092 
00093   /// returns true if the worker is defined with respect to a reference jet
00094   virtual bool takes_reference() const { return false;}
00095 
00096   /// sets the reference jet for the selector
00097   virtual void set_reference(const PseudoJet & reference){
00098     throw Error("set_reference(...) cannot be used for a selector worker that does not take a reference");
00099   }
00100 
00101   /// return a copy of the current object.
00102   ///
00103   /// This function is only called for objects that take a reference and need
00104   /// not be reimplemented otherwise.
00105   virtual SelectorWorker* copy(){ 
00106     throw Error("this SelectorWorker has nothing to copy");
00107   }
00108 
00109   //----------------------------------------------------------
00110   // operations for area and extent
00111   //----------------------------------------------------------
00112 
00113   /// returns the rapidity range for which it may return "true"
00114   virtual void get_rapidity_extent(double & rapmin, double & rapmax) const {
00115     rapmax = std::numeric_limits<double>::infinity();
00116     rapmin = -rapmax; 
00117   }
00118 
00119   /// check if it is a geometric selector (i.e. only puts constraints
00120   /// on rapidity and azimuthal angle)
00121   virtual bool is_geometric() const { return false;}
00122 
00123   /// check if it has a finite area
00124   virtual bool has_finite_area() const;
00125 
00126   /// check if it has an analytically computable area
00127   virtual bool has_known_area() const { return false;}
00128 
00129   /// if it has a computable area, return it
00130   virtual double known_area() const{
00131     throw Error("this selector has no computable area");
00132   }
00133 };
00134 
00135 //----------------------------------------------------------------------
00136 // class Selector
00137 //
00138 // Class that encodes information about cuts that 
00139 class Selector{
00140 public:
00141   /// default constructor produces a Selector whose action is undefined
00142   /// (any attempt to use it will lead to an error)
00143   Selector() {}
00144 
00145   /// constructor that causes the Selector to use the supplied worker
00146   ///
00147   /// Note that the Selector takes ownership of the pointer to the
00148   /// worker (and so will delete automatically when appropriate).
00149   Selector(SelectorWorker * worker) {_worker.reset(worker);}
00150 
00151 
00152   /// ctor from a RangeDefinition
00153   ///
00154   /// This is provided for backward compatibility and will be removed in
00155   /// a future major release of FastJet
00156   ///
00157   /// Watch out that the Selector will only hold a pointer to the
00158   /// range so the selector will crash if one tries to use it after
00159   /// the range has gone out of scope. We thus strongly advise against
00160   /// the direct use of this constructor.
00161   Selector(const RangeDefinition &range);
00162 
00163   /// dummy virtual dtor
00164   virtual ~Selector(){}
00165 
00166   /// return true if the jet passes the selection
00167   bool pass(const PseudoJet & jet) const {
00168     if (!validated_worker()->applies_jet_by_jet()) {
00169       throw Error("Cannot apply this selector to an individual jet");
00170     }
00171     return _worker->pass(jet);
00172   }
00173 
00174   /// an operator way of knowing whether a given jet passes the selection or not
00175   bool operator()(const PseudoJet & jet) const {
00176     return pass(jet);
00177   }
00178 
00179   /// Return a count of the objects that pass the selection.
00180   ///
00181   /// This will often be more efficient that getting the vector of objects that
00182   /// passes and then evaluating the size of the vector
00183   unsigned int count(const std::vector<PseudoJet> & jets) const;
00184 
00185   /// sift the input jets into two vectors -- those that pass the selector
00186   /// and those that do not
00187   void sift(const std::vector<PseudoJet> & jets,
00188                   std::vector<PseudoJet> & jets_that_pass,
00189                   std::vector<PseudoJet> & jets_that_fail) const;
00190 
00191   /// returns true if this can be applied jet by jet
00192   bool applies_jet_by_jet() const {
00193     return validated_worker()->applies_jet_by_jet();
00194   }
00195 
00196   /// returns a vector with the jets that pass the selection
00197   std::vector<PseudoJet> operator()(const std::vector<PseudoJet> & jets) const;
00198 
00199   /// For each jet that does not pass the cuts, this routine sets the 
00200   /// pointer to 0. 
00201   ///
00202   /// It does not assume that the PseudoJet* passed as argumetn are not NULL
00203   virtual void nullify_non_selected(std::vector<const PseudoJet *> & jets) const {
00204     validated_worker()->terminator(jets);
00205   }
00206 
00207   /// returns the rapidity range for which it may return "true"
00208   void get_rapidity_extent(double &rapmin, double &rapmax) const {
00209     return validated_worker()->get_rapidity_extent(rapmin, rapmax);
00210   }
00211 
00212   /// return a textual description of the selector
00213   std::string description() const {
00214     return validated_worker()->description();
00215   }
00216 
00217   /// check if it is a geometric selector (i.e. one that only puts
00218   /// constraints on rapidities and azimuthal angles)
00219   bool is_geometric() const{
00220     return validated_worker()->is_geometric();
00221   }
00222 
00223   /// check if it has a meaningful and finite area
00224   bool has_finite_area() const{
00225     return validated_worker()->has_finite_area();
00226   }
00227 
00228   /// returns the rapidity-phi area associated with the Selector
00229   /// (throws InvalidArea if the area does not make sense).
00230   ///
00231   /// The argument passed is the requested cell area, which is used
00232   /// for obtaining a Monte Carlo type estimate of the area in case
00233   /// the Selector does not have an analytically known error. The
00234   /// Monte Carlo estimate involves a time penalty proportional to
00235   /// rapidity extent of the Selector.
00236   ///
00237   double area(double cell_area) const;
00238 
00239   /// estimate of area, which will use the default ghost area from
00240   /// the ghosted_area_spec
00241   double area() const;
00242 
00243   /// returns a (reference to) the underlying worker's shared pointer
00244   const SharedPtr<SelectorWorker> & worker() const {return _worker;}
00245 
00246   /// returns a worker if there is a valid one, otherwise throws an InvalidWorker error
00247   const SelectorWorker* validated_worker() const {
00248     const SelectorWorker* worker_ptr = _worker.get();
00249     if (worker_ptr == 0) throw InvalidWorker();
00250     return worker_ptr;
00251   }
00252 
00253   /// returns true if this can be applied jet by jet
00254   bool takes_reference() const {
00255     return validated_worker()->takes_reference();
00256   }
00257 
00258   /// set the reference jet for this Selector
00259   const Selector & set_reference(const PseudoJet &reference){
00260 
00261     // if the worker does not take a reference jet, do nothing 
00262     if (! validated_worker()->takes_reference()){
00263       return *this;
00264     }
00265     
00266     // since this is a non-const operation, make sure we have a
00267     // correct behaviour with respect to shared workers
00268     _copy_worker_if_needed();
00269 
00270     _worker->set_reference(reference);
00271     return *this;
00272   }
00273 
00274   /// class that gets throw when a Selector is applied despite it not
00275   /// having a valid underlying worker.
00276   class InvalidWorker : public Error {
00277   public:
00278     InvalidWorker() : Error("Attempt to use Selector with no valid underlying worker") {}
00279   };
00280 
00281   /// class that gets throw when a Selector is applied despite it not
00282   /// having a valid underlying worker.
00283   class InvalidArea : public Error {
00284   public:
00285     InvalidArea() : Error("Attempt to obtain area from Selector for which this is not meaningful") {}
00286   };
00287 
00288 
00289 
00290 protected:
00291   /// Helper for copying selector workers if needed
00292   ///
00293   /// The following is needed if we want to modify a selectors that
00294   /// shares a worker with another selector. In that case, we need to
00295   /// get another copy of the worker to avoid interferences
00296   ///
00297   /// Note that any non-const operation has to call this to behave
00298   /// correctly w.r.t shared workers!
00299   void _copy_worker_if_needed(){
00300     // do nothing if there's a sinlge user of the worker
00301     if (_worker.unique()) return;
00302 
00303     // call the worker's copy
00304     //std::cout << "will make a copy of " << description() << std::endl;
00305     _worker.reset(_worker->copy());
00306   }
00307 
00308 private:
00309   SharedPtr<SelectorWorker> _worker; ///< the underlying worker
00310 };
00311 
00312 
00313 //----------------------------------------------------------------------
00314 // a list of specific selectors
00315 //----------------------------------------------------------------------
00316 
00317 /// \addtogroup selectors
00318 /// @{
00319 
00320 
00321 // fundamental selectors
00322 //----------------------------------------------------------------------
00323 
00324 // "identity" selector that lets everything pass
00325 Selector SelectorIdentity();
00326 
00327 // logical operations
00328 //----------------------------------------------------------------------
00329 
00330 /// logical not applied on a selector
00331 ///
00332 /// This will keep objects that do not pass the 's' selector
00333 Selector operator!(const Selector & s);
00334 
00335 /// logical or between two selectors
00336 ///
00337 /// this will keep the objects that are selected by s1 or s2
00338 Selector operator ||(const Selector & s1, const Selector & s2);
00339 
00340 
00341 /// logical and between two selectors
00342 ///
00343 /// this will keep the objects that are selected by both s1 and s2
00344 /// 
00345 /// watch out: for both s1 and s2, the selection is applied on the
00346 ///   original list of objects. For successive applications of two
00347 ///   selectors (convolution/multiplication) see the operator *
00348 Selector operator&&(const Selector & s1, const Selector & s2);
00349 
00350 /// successive application of 2 selectors
00351 ///
00352 /// Apply the selector s2, then the selector s1.
00353 ///
00354 /// watch out: the operator * acts like an operator product i.e. does
00355 ///   not commute. The order of its arguments is therefore important.
00356 ///   Whenever they commute (in particluar, when they apply jet by
00357 ///   jet), this would have the same effect as the logical &&.
00358 Selector operator*(const Selector & s1, const Selector & s2);
00359 
00360 
00361 // selection with kinematic cuts
00362 //----------------------------------------------------------------------
00363 Selector SelectorPtMin(double ptmin);                    ///< select objects with pt >= ptmin
00364 Selector SelectorPtMax(double ptmax);                    ///< select objects with pt <= ptmax
00365 Selector SelectorPtRange(double ptmin, double ptmax);    ///< select objects with ptmin <= pt <= ptmax
00366 
00367 Selector SelectorEtMin(double Etmin);                    ///< select objects with Et >= Etmin
00368 Selector SelectorEtMax(double Etmax);                    ///< select objects with Et <= Etmax
00369 Selector SelectorEtRange(double Etmin, double Etmax);    ///< select objects with Etmin <= Et <= Etmax
00370 
00371 Selector SelectorEMin(double Emin);                      ///< select objects with E >= Emin
00372 Selector SelectorEMax(double Emax);                      ///< select objects with E <= Emax
00373 Selector SelectorERange(double Emin, double Emax);       ///< select objects with Emin <= E <= Emax
00374 
00375 Selector SelectorMMin(double Mmin);                      ///< select objects with M >= Mmin
00376 Selector SelectorMMax(double Mmax);                      ///< select objects with M <= Mmax
00377 Selector SelectorMRange(double Mmin, double Mmax);       ///< select objects with Mmin <= M <= Mmax
00378 
00379 Selector SelectorRapMin(double rapmin);                  ///< select objects with rap >= rapmin
00380 Selector SelectorRapMax(double rapmax);                  ///< select objects with rap <= rapmax
00381 Selector SelectorRapRange(double rapmin, double rapmax); ///< select objects with rapmin <= rap <= rapmax
00382 
00383 Selector SelectorAbsRapMin(double absrapmin);                     ///< select objects with |rap| >= absrapmin
00384 Selector SelectorAbsRapMax(double absrapmax);                     ///< select objects with |rap| <= absrapmax
00385 Selector SelectorAbsRapRange(double absrapmin, double absrapmax); ///< select objects with absrapmin <= |rap| <= absrapmax
00386 
00387 Selector SelectorEtaMin(double etamin);                  ///< select objects with eta >= etamin
00388 Selector SelectorEtaMax(double etamax);                  ///< select objects with eta <= etamax
00389 Selector SelectorEtaRange(double etamin, double etamax); ///< select objects with etamin <= eta <= etamax
00390 
00391 Selector SelectorAbsEtaMin(double absetamin);                     ///< select objects with |eta| >= absetamin
00392 Selector SelectorAbsEtaMax(double absetamax);                     ///< select objects with |eta| <= absetamax
00393 Selector SelectorAbsEtaRange(double absetamin, double absetamax); ///< select objects with absetamin <= |eta| <= absetamax
00394 
00395 Selector SelectorPhiRange(double phimin, double phimax); ///< select objects with phimin <= phi <= phimax
00396 
00397 /// select objects with rapmin <= rap <= rapmax  &&  phimin <= phi <= phimax
00398 ///
00399 /// Note that this is essentially a combination of SelectorRapRange
00400 /// and SelectorPhiRange. We provide it as a Selector on its own in
00401 /// order to use the known area (which would otherwise be lost by the &&
00402 /// operator)
00403 Selector SelectorRapPhiRange(double rapmin, double rapmax, double phimin, double phimax);
00404 
00405 /// select the n hardest objects 
00406 Selector SelectorNHardest(unsigned int n); 
00407 
00408 
00409 // Selectors that take (require) a reference jet.
00410 //----------------------------------------------------------------------
00411 
00412 /// select objets within a distance 'radius' from the location of the
00413 /// reference jet, set by Selector::set_reference(...)
00414 Selector SelectorCircle(const double & radius); 
00415 
00416 /// select objets with distance from the reference jet is between 'radius_in'
00417 /// and 'radius_out'; the reference jet is set by Selector::set_reference(...)
00418 Selector SelectorDoughnut(const double & radius_in, const double & radius_out); 
00419 
00420 /// select objets within a rapidity distance 'half_width' from the
00421 /// location of the reference jet, set by Selector::set_reference(...)
00422 Selector SelectorStrip(const double & half_width);
00423 
00424 /// select objets within rapidity distance 'half_rap_width' from the
00425 /// reference jet and azimuthal-angle distance within 'half_phi_width'; the
00426 /// reference jet is set by Selector::set_reference(...)
00427 Selector SelectorRectangle(const double & half_rap_width, const double & half_phi_width);
00428 
00429 
00430 /// select objects that carry at least a fraction "fraction" of the
00431 /// reference jet. The reference jet must have been set with
00432 /// Selector::set_reference(...)
00433 Selector SelectorPtFractionMin(double fraction);
00434 
00435 
00436 // additional (mostly helper) selectors
00437 //----------------------------------------------------------------------
00438 
00439 /// select objects that are (or are only made of) ghosts.
00440 /// PseudoJets for which has_area() are considered non-pure-ghost.
00441 Selector SelectorIsPureGhost();
00442 
00443 /// @}
00444 
00445 FASTJET_END_NAMESPACE      // defined in fastjet/internal/base.hh
00446 
00447 #endif // __SELECTOR_HH__
00448 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends