Selector.hh

00001 #ifndef __SELECTOR_HH__
00002 #define __SELECTOR_HH__
00003 
00004 //STARTHEADER
00005 // $Id: Selector.hh 1867 2011-01-26 17:53:32Z salam $
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/GhostedAreaSpec.hh"  // for area support
00036 #include <limits>
00037 #include <cmath>
00038 
00039 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00040 
00041 //----------------------------------------------------------------------
00047 class Selector;
00048 //----------------------------------------------------------------------
00049 
00057 class SelectorWorker {
00058 public:
00059   //----------------------------------------------------------
00060   // fundamental info
00061   //----------------------------------------------------------
00063   virtual ~SelectorWorker() {}
00064 
00065   //----------------------------------------------------------
00066   // basic operations for checking what gets selected
00067   //----------------------------------------------------------
00068 
00071   virtual bool pass(const PseudoJet & jet) const = 0;
00072 
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 
00084   virtual bool applies_jet_by_jet() const {return true;}
00085 
00087   virtual std::string description() const {return "missing description";}
00088 
00089   //----------------------------------------------------------
00090   // operations for relocation
00091   //----------------------------------------------------------
00092 
00094   virtual bool is_relocatable() const { return false;}
00095 
00097   virtual void relocate(const PseudoJet &centre){
00098     throw Error("relocate undefined for a non-relocatable selector worker");
00099   }
00100 
00105   virtual SelectorWorker* copy(){ 
00106     throw Error("this SelectorWorker has nothing to copy");
00107   }
00108 
00109   //----------------------------------------------------------
00110   // operations for area and extent
00111   //----------------------------------------------------------
00112 
00114   virtual void get_rapidity_extent(double & rapmin, double & rapmax) const {
00115     rapmax = std::numeric_limits<double>::max();
00116     rapmin = -rapmax; 
00117   }
00118 
00120   virtual bool has_area() const { return false;}
00121 
00123   virtual bool has_known_area() const { return false;}
00124 
00126   virtual double known_area() const{
00127     throw Error("this selector has no computable area");
00128   }
00129 
00130 
00131 
00132 };
00133 
00134 //----------------------------------------------------------------------
00135 // class Selector
00136 //
00137 // Class that encodes information about cuts that 
00138 class Selector{
00139 public:
00142   Selector() {}
00143 
00148   Selector(SelectorWorker * worker) {_worker.reset(worker);}
00149 
00151   virtual ~Selector(){}
00152 
00154   bool pass(const PseudoJet & jet) const {
00155     if (!validated_worker()->applies_jet_by_jet()) {
00156       throw Error("Cannot apply this selector to an individual jet");
00157     }
00158     return _worker->pass(jet);
00159   }
00160 
00162   bool operator()(const PseudoJet & jet) const {
00163     return pass(jet);
00164   }
00165 
00167   bool applies_jet_by_jet() const {
00168     return validated_worker()->applies_jet_by_jet();
00169   }
00170 
00172   std::vector<PseudoJet> operator()(const std::vector<PseudoJet> & jets) const;
00173 
00175   void get_rapidity_extent(double &rapmin, double &rapmax) const {
00176     return validated_worker()->get_rapidity_extent(rapmin, rapmax);
00177   }
00178 
00180   std::string description() const {
00181     return validated_worker()->description();
00182   }
00183 
00185   bool has_area() const{
00186     return validated_worker()->has_area();
00187   }
00188 
00198   double area(double cell_area=gas::def_ghost_area) const;
00199 
00201   const SharedPtr<SelectorWorker> & worker() const {return _worker;}
00202 
00204   const SelectorWorker* validated_worker() const {
00205     const SelectorWorker* worker_ptr = _worker.get();
00206     if (worker_ptr == 0) throw InvalidWorker();
00207     return worker_ptr;
00208   }
00209 
00212   class InvalidWorker : public Error {
00213   public:
00214     InvalidWorker() : Error("Attempt to use Selector with no valid underlying worker") {}
00215   };
00216 
00219   class InvalidArea : public Error {
00220   public:
00221     InvalidArea() : Error("Attempt to obtain area from Selector for which this is not meaningful") {}
00222   };
00223 
00224   //----------------------------------------------------
00225   // non-const operations
00226   //----------------------------------------------------
00227 
00228 protected:
00237   void _copy_worker_if_needed(){
00238     // do nothing if there's a sinlge user of the worker
00239     if (_worker.unique()) return;
00240 
00241     // call the worker's copy
00242     //std::cout << "will make a copy of " << description() << std::endl;
00243     _worker.reset(_worker->copy());
00244   }
00245 public:
00246 
00248   bool is_relocatable() const {
00249     return validated_worker()->is_relocatable();
00250   }
00251 
00253   const Selector & relocate(const PseudoJet &centre){
00254 
00255     // if the worker is not relocatable, do nothing 
00256     if (! validated_worker()->is_relocatable()){
00257       return *this;
00258     }
00259     
00260     // since this is a non-const operation, make sure we have a
00261     // correct behaviour with respect to shared workers
00262     _copy_worker_if_needed();
00263 
00264     _worker->relocate(centre);
00265     return *this;
00266   }
00267 
00268 private:
00269   SharedPtr<SelectorWorker> _worker; 
00270 };
00271 
00272 
00273 //----------------------------------------------------------------------
00274 // a list of specific selectors
00275 //----------------------------------------------------------------------
00276 
00279 
00280 // logical operations
00281 //----------------------------------------------------------------------
00282 
00286 Selector operator!(const Selector & s);
00287 
00291 Selector operator ||(const Selector & s1, const Selector & s2);
00292 
00293 
00301 Selector operator&&(const Selector & s1, const Selector & s2);
00302 
00311 Selector operator*(const Selector & s1, const Selector & s2);
00312 
00313 
00314 // selection with kinematic cuts
00315 //----------------------------------------------------------------------
00316 Selector SelectorPtMin(double ptmin);                    
00317 Selector SelectorPtMax(double ptmax);                    
00318 Selector SelectorPtRange(double ptmin, double ptmax);    
00319 
00320 Selector SelectorEtMin(double Etmin);                    
00321 Selector SelectorEtMax(double Etmax);                    
00322 Selector SelectorEtRange(double Etmin, double Etmax);    
00323 
00324 Selector SelectorEMin(double Emin);                      
00325 Selector SelectorEMax(double Emax);                      
00326 Selector SelectorERange(double Emin, double Emax);       
00327 
00328 Selector SelectorMMin(double Mmin);                      
00329 Selector SelectorMMax(double Mmax);                      
00330 Selector SelectorMRange(double Mmin, double Mmax);       
00331 
00332 Selector SelectorRapMin(double rapmin);                  
00333 Selector SelectorRapMax(double rapmax);                  
00334 Selector SelectorRapRange(double rapmin, double rapmax); 
00335 
00336 Selector SelectorAbsRapMin(double absrapmin);                     
00337 Selector SelectorAbsRapMax(double absrapmax);                     
00338 Selector SelectorAbsRapRange(double absrapmin, double absrapmax); 
00339 
00340 Selector SelectorEtaMin(double etamin);                  
00341 Selector SelectorEtaMax(double etamax);                  
00342 Selector SelectorEtaRange(double etamin, double etamax); 
00343 
00344 Selector SelectorAbsEtaMin(double absetamin);                     
00345 Selector SelectorAbsEtaMax(double absetamax);                     
00346 Selector SelectorAbsEtaRange(double absetamin, double absetamax); 
00347 
00348 Selector SelectorPhiRange(double phimin, double phimax); 
00349 
00356 Selector SelectorRapPhiRange(double rapmin, double rapmax, double phimin, double phimax);
00357 
00359 Selector SelectorNHardest(unsigned int n); 
00360 
00361 
00362 // selection with geometric objects
00363 //----------------------------------------------------------------------
00364 
00366 Selector SelectorCircle(const double & radius); 
00367 
00370 Selector SelectorDoughnut(const double & radius_in, const double & radius_out); 
00371 
00374 Selector SelectorStrip(const double & half_width);
00375 
00379 Selector SelectorRectangle(const double & half_rap_width, const double & half_phi_width);
00380 
00382 
00383 FASTJET_END_NAMESPACE      // defined in fastjet/internal/base.hh
00384 
00385 #endif // __SELECTOR_HH__
00386