ClusterSequence.cc

Go to the documentation of this file.
00001 //STARTHEADER
00002 // $Id: ClusterSequence.cc 1567 2009-05-21 19:12:26Z salam $
00003 //
00004 // Copyright (c) 2005-2009, 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 #include "fastjet/Error.hh"
00032 #include "fastjet/PseudoJet.hh"
00033 #include "fastjet/ClusterSequence.hh"
00034 #include "fastjet/version.hh" // stores the current version number
00035 #include<iostream>
00036 #include<sstream>
00037 #include<fstream>
00038 #include<cmath>
00039 #include<cstdlib>
00040 #include<cassert>
00041 #include<string>
00042 #include<set>
00043 
00044 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00045 
00046 using namespace std;
00047 
00049 JetAlgorithm ClusterSequence::_default_jet_algorithm = kt_algorithm;
00050 //
00051 
00052 
00053 // destructor that does nothing
00054 ClusterSequence::~ClusterSequence () {}
00055 
00056 //----------------------------------------------------------------------
00057 void ClusterSequence::_initialise_and_run (
00058                                   const double & R,
00059                                   const Strategy & strategy,
00060                                   const bool & writeout_combinations) {
00061 
00062   JetDefinition jet_def(_default_jet_algorithm, R, strategy);
00063   _initialise_and_run(jet_def, writeout_combinations);
00064 }
00065 
00066 
00067 //----------------------------------------------------------------------
00068 void ClusterSequence::_initialise_and_run (
00069                                   const JetDefinition & jet_def,
00070                                   const bool & writeout_combinations) {
00071 
00072   // transfer all relevant info into internal variables
00073   _decant_options(jet_def, writeout_combinations);
00074 
00075   // set up the history entries for the initial particles (those
00076   // currently in _jets)
00077   _fill_initial_history();
00078 
00079   // don't run anything if the event is empty
00080   if (n_particles() == 0) return;
00081 
00082   // ----- deal with special cases: plugins & e+e- ------
00083   if (_jet_algorithm == plugin_algorithm) {
00084     // allows plugin_xyz() functions to modify cluster sequence
00085     _plugin_activated = true;
00086     // let the plugin do its work here
00087     _jet_def.plugin()->run_clustering( (*this) );
00088     _plugin_activated = false;
00089     return;
00090   } else if (_jet_algorithm == ee_kt_algorithm ||
00091              _jet_algorithm == ee_genkt_algorithm) {
00092     // ignore requested strategy
00093     _strategy = N2Plain;
00094     if (_jet_algorithm == ee_kt_algorithm) {
00095       // make sure that R is large enough so that "beam" recomb only
00096       // occurs when a single particle is left
00097       // Normally, this should be automatically set to 4 from JetDefinition
00098       assert(_Rparam > 2.0); 
00099       // this is used to renormalise the dij to get a "standard" form
00100       // and our convention in e+e- will be different from that
00101       // in long.inv case; NB: _invR2 name should be changed -> _renorm_dij?
00102       _invR2 = 1.0;
00103     } else {
00104       // as of 2009-01-09, choose R to be an angular distance, in
00105       // radians.  Since the algorithm uses 2(1-cos(theta)) as its
00106       // squared angular measure, make sure that the _R2 is defined
00107       // in a similar way.
00108       if (_Rparam > pi) {
00109         // choose a value that ensures that back-to-back particles will
00110         // always recombine 
00111         //_R2 = 4.0000000000001;
00112         _R2 = 2 * ( 3.0 + cos(_Rparam) );
00113       } else {
00114         _R2    = 2 * ( 1.0 - cos(_Rparam) );
00115       }
00116       _invR2 = 1.0/_R2;
00117     }
00118     //_simple_N2_cluster<EEBriefJet>();
00119     _simple_N2_cluster_EEBriefJet();
00120     return;
00121   }
00122 
00123 
00124   // automatically redefine the strategy according to N if that is
00125   // what the user requested -- transition points (and especially
00126   // their R-dependence) are based on empirical observations for a
00127   // R=0.4, 0.7 and 1.0, running on toth (3.4GHz, Pentium IV D [dual
00128   // core] with 2MB of cache).
00129   if (_strategy == Best) {
00130     int N = _jets.size();
00131     if (N > 6200/pow(_Rparam,2.0) 
00132         && jet_def.jet_algorithm() == cambridge_algorithm) {
00133       _strategy = NlnNCam;}
00134     else
00135 #ifndef DROP_CGAL
00136       if ((N > 16000/pow(_Rparam,1.15) && jet_def.jet_algorithm() != antikt_algorithm)
00137         || N > 35000/pow(_Rparam,1.15)) {
00138       _strategy = NlnN; }   
00139     else                    
00140 #endif  // DROP_CGAL
00141       if (N > 450) {
00142       _strategy = N2MinHeapTiled;
00143     }
00144     else if (N > 55*max(0.5,min(1.0,_Rparam))) {// empirical scaling with R
00145       _strategy = N2Tiled;
00146     } else {
00147       _strategy = N2Plain;
00148     }
00149   }
00150 
00151 
00152   // run the code containing the selected strategy
00153   if (_strategy == NlnN || _strategy == NlnN3pi 
00154       || _strategy == NlnN4pi ) {
00155     this->_delaunay_cluster();
00156   } else if (_strategy ==  N3Dumb ) {
00157     this->_really_dumb_cluster();
00158   } else if (_strategy == N2Tiled) {
00159     this->_faster_tiled_N2_cluster();
00160   } else if (_strategy == N2PoorTiled) {
00161     this->_tiled_N2_cluster();
00162   } else if (_strategy == N2Plain) {
00163     // BriefJet provides standard long.invariant kt alg.
00164     //this->_simple_N2_cluster<BriefJet>();
00165     this->_simple_N2_cluster_BriefJet();
00166   } else if (_strategy == N2MinHeapTiled) {
00167     this->_minheap_faster_tiled_N2_cluster();
00168   } else if (_strategy == NlnNCam4pi) {
00169     this->_CP2DChan_cluster();
00170   } else if (_strategy == NlnNCam2pi2R) {
00171     this->_CP2DChan_cluster_2pi2R();
00172   } else if (_strategy == NlnNCam) {
00173     this->_CP2DChan_cluster_2piMultD();
00174   } else {
00175     ostringstream err;
00176     err << "Unrecognised value for strategy: "<<_strategy;
00177     throw Error(err.str());
00178     //assert(false);
00179   }
00180 }
00181 
00182 
00183 // these needs to be defined outside the class definition.
00184 bool ClusterSequence::_first_time = true;
00185 int ClusterSequence::_n_exclusive_warnings = 0;
00186 
00187 
00188 //----------------------------------------------------------------------
00189 // the version string
00190 string fastjet_version_string() {
00191   return "FastJet version "+string(fastjet_version);
00192 }
00193 
00194 
00195 //----------------------------------------------------------------------
00196 // prints a banner on the first call
00197 void ClusterSequence::_print_banner() {
00198 
00199   if (!_first_time) {return;}
00200   _first_time = false;
00201   
00202   
00203   //Symp. Discr. Alg, p.472 (2002) and  CGAL (http://www.cgal.org);
00204 
00205   cout << "#--------------------------------------------------------------------------\n";
00206   cout << "#                      FastJet release " << fastjet_version << endl;
00207   cout << "#            Written by M. Cacciari, G.P. Salam and G. Soyez            \n"; 
00208   cout << "#                         http://www.fastjet.fr                         \n"; 
00209   cout << "#                                                                       \n";
00210   cout << "# Longitudinally invariant Kt, anti-Kt, and inclusive Cambridge/Aachen  \n";
00211   cout << "# clustering using fast geometric algorithms, with area measures and optional\n";
00212   cout << "# external jet-finder plugins.                                          \n";
00213   cout << "# Please cite Phys. Lett. B641 (2006) [hep-ph/0512210] if you use this code.\n";
00214   cout << "#                                                                       \n";
00215   cout << "# This package uses T.Chan's closest pair algorithm, Proc.13th ACM-SIAM \n";
00216   cout << "# Symp. Discr. Alg, p.472 (2002), S.Fortune's Voronoi algorithm and code " ;
00217 #ifndef DROP_CGAL
00218   cout << endl << "# and CGAL: http://www.cgal.org/";
00219 #endif  // DROP_CGAL
00220   cout << ".\n";
00221   cout << "#-------------------------------------------------------------------------\n";
00222 }
00223 
00224 //----------------------------------------------------------------------
00225 // transfer all relevant info into internal variables
00226 void ClusterSequence::_decant_options(const JetDefinition & jet_def,
00227                                       const bool & writeout_combinations) {
00228 
00229   // let the user know what's going on
00230   _print_banner();
00231 
00232   // make a local copy of the jet definition (for future use?)
00233   _jet_def = jet_def;
00234   
00235   _writeout_combinations = writeout_combinations;
00236   _jet_algorithm = jet_def.jet_algorithm();
00237   _Rparam = jet_def.R();  _R2 = _Rparam*_Rparam; _invR2 = 1.0/_R2;
00238   _strategy = jet_def.strategy();
00239 
00240   // disallow interference from the plugin
00241   _plugin_activated = false;
00242   
00243 }
00244 
00245 
00246 //----------------------------------------------------------------------
00247 // initialise the history in a standard way
00248 void ClusterSequence::_fill_initial_history () {
00249 
00250   //if (_jets.size() == 0) {throw Error("Cannot run jet-finder on empty event");}
00251 
00252   // reserve sufficient space for everything
00253   _jets.reserve(_jets.size()*2);
00254   _history.reserve(_jets.size()*2);
00255 
00256   _Qtot = 0;
00257 
00258   for (int i = 0; i < static_cast<int>(_jets.size()) ; i++) {
00259     history_element element;
00260     element.parent1 = InexistentParent;
00261     element.parent2 = InexistentParent;
00262     element.child   = Invalid;
00263     element.jetp_index = i;
00264     element.dij     = 0.0;
00265     element.max_dij_so_far = 0.0;
00266 
00267     _history.push_back(element);
00268     
00269     // do any momentum preprocessing needed by the recombination scheme
00270     _jet_def.recombiner()->preprocess(_jets[i]);
00271 
00272     // get cross-referencing right from PseudoJets
00273     _jets[i].set_cluster_hist_index(i);
00274 
00275     // determine the total energy in the event
00276     _Qtot += _jets[i].E();
00277   }
00278   _initial_n = _jets.size();
00279 }
00280 
00281 
00282 //----------------------------------------------------------------------
00283 // Return the component corresponding to the specified index.
00284 // taken from CLHEP
00285 string ClusterSequence::strategy_string ()  const {
00286   string strategy;
00287   switch(_strategy) {
00288   case NlnN:
00289     strategy = "NlnN"; break;
00290   case NlnN3pi:
00291     strategy = "NlnN3pi"; break;
00292   case NlnN4pi:
00293     strategy = "NlnN4pi"; break;
00294   case N2Plain:
00295     strategy = "N2Plain"; break;
00296   case N2Tiled:
00297     strategy = "N2Tiled"; break;
00298   case N2MinHeapTiled:
00299     strategy = "N2MinHeapTiled"; break;
00300   case N2PoorTiled:
00301     strategy = "N2PoorTiled"; break;
00302   case N3Dumb:
00303     strategy = "N3Dumb"; break;
00304   case NlnNCam4pi:
00305     strategy = "NlnNCam4pi"; break;
00306   case NlnNCam2pi2R:
00307     strategy = "NlnNCam2pi2R"; break;
00308   case NlnNCam:
00309     strategy = "NlnNCam"; break; // 2piMultD
00310   case plugin_strategy:
00311     strategy = "plugin strategy"; break;
00312   default:
00313     strategy = "Unrecognized";
00314   }
00315   return strategy;
00316 }  
00317 
00318 
00319 double ClusterSequence::jet_scale_for_algorithm(
00320                                   const PseudoJet & jet) const {
00321   if (_jet_algorithm == kt_algorithm)             {return jet.kt2();}
00322   else if (_jet_algorithm == cambridge_algorithm) {return 1.0;}
00323   else if (_jet_algorithm == antikt_algorithm) {
00324     double kt2=jet.kt2();
00325     return kt2 > 1e-300 ? 1.0/kt2 : 1e300;
00326   } else if (_jet_algorithm == genkt_algorithm) {
00327     double kt2 = jet.kt2();
00328     double p   = jet_def().extra_param();
00329     if (p <= 0 && kt2 < 1e-300) kt2 = 1e-300; // dodgy safety check
00330     return pow(kt2, p);
00331   } else if (_jet_algorithm == cambridge_for_passive_algorithm) {
00332     double kt2 = jet.kt2();
00333     double lim = _jet_def.extra_param();
00334     if (kt2 < lim*lim && kt2 != 0.0) {
00335       return 1.0/kt2;
00336     } else {return 1.0;}
00337   } else {throw Error("Unrecognised jet algorithm");}
00338 }
00339 
00340 
00341 //----------------------------------------------------------------------
00345 void ClusterSequence::transfer_from_sequence(ClusterSequence & from_seq) {
00346 
00347   // the metadata
00348   _jet_def                 = from_seq._jet_def                ;
00349   _writeout_combinations   = from_seq._writeout_combinations  ;
00350   _initial_n               = from_seq._initial_n              ;
00351   _Rparam                  = from_seq._Rparam                 ;
00352   _R2                      = from_seq._R2                     ;
00353   _invR2                   = from_seq._invR2                  ;
00354   _strategy                = from_seq._strategy               ;
00355   _jet_algorithm           = from_seq._jet_algorithm          ;
00356   _plugin_activated        = from_seq._plugin_activated       ;
00357 
00358   // the data
00359   _jets     = from_seq._jets;
00360   _history  = from_seq._history;
00361   // the following transferse ownership of the extras from the from_seq
00362   _extras   = from_seq._extras;
00363 
00364 }
00365 
00366 //----------------------------------------------------------------------
00367 // record an ij recombination and reset the _jets[newjet_k] momentum and
00368 // user index to be those of newjet
00369 void ClusterSequence::plugin_record_ij_recombination(
00370            int jet_i, int jet_j, double dij, 
00371            const PseudoJet & newjet, int & newjet_k) {
00372 
00373   plugin_record_ij_recombination(jet_i, jet_j, dij, newjet_k);
00374 
00375   // now transfer newjet into place
00376   int tmp_index = _jets[newjet_k].cluster_hist_index();
00377   _jets[newjet_k] = newjet;
00378   _jets[newjet_k].set_cluster_hist_index(tmp_index);
00379 }
00380 
00381 
00382 //----------------------------------------------------------------------
00383 // return all inclusive jets with pt > ptmin
00384 vector<PseudoJet> ClusterSequence::inclusive_jets (const double & ptmin) const{
00385   double dcut = ptmin*ptmin;
00386   int i = _history.size() - 1; // last jet
00387   vector<PseudoJet> jets;
00388   if (_jet_algorithm == kt_algorithm) {
00389     while (i >= 0) {
00390       // with our specific definition of dij and diB (i.e. R appears only in 
00391       // dij), then dij==diB is the same as the jet.perp2() and we can exploit
00392       // this in selecting the jets...
00393       if (_history[i].max_dij_so_far < dcut) {break;}
00394       if (_history[i].parent2 == BeamJet && _history[i].dij >= dcut) {
00395         // for beam jets
00396         int parent1 = _history[i].parent1;
00397         jets.push_back(_jets[_history[parent1].jetp_index]);}
00398       i--;
00399     }
00400   } else if (_jet_algorithm == cambridge_algorithm) {
00401     while (i >= 0) {
00402       // inclusive jets are all at end of clustering sequence in the
00403       // Cambridge algorithm -- so if we find a non-exclusive jet, then
00404       // we can exit
00405       if (_history[i].parent2 != BeamJet) {break;}
00406       int parent1 = _history[i].parent1;
00407       const PseudoJet & jet = _jets[_history[parent1].jetp_index];
00408       if (jet.perp2() >= dcut) {jets.push_back(jet);}
00409       i--;
00410     }
00411   } else if (_jet_algorithm == plugin_algorithm 
00412              || _jet_algorithm == ee_kt_algorithm
00413              || _jet_algorithm == antikt_algorithm
00414              || _jet_algorithm == genkt_algorithm
00415              || _jet_algorithm == ee_genkt_algorithm
00416              || _jet_algorithm == cambridge_for_passive_algorithm) {
00417     // for inclusive jets with a plugin algorithm, we make no
00418     // assumptions about anything (relation of dij to momenta,
00419     // ordering of the dij, etc.)
00420     while (i >= 0) {
00421       if (_history[i].parent2 == BeamJet) {
00422         int parent1 = _history[i].parent1;
00423         const PseudoJet & jet = _jets[_history[parent1].jetp_index];
00424         if (jet.perp2() >= dcut) {jets.push_back(jet);}
00425       }
00426       i--;
00427     }
00428   } else {throw Error("cs::inclusive_jets(...): Unrecognized jet algorithm");}
00429   return jets;
00430 }
00431 
00432 
00433 //----------------------------------------------------------------------
00434 // return the number of exclusive jets that would have been obtained
00435 // running the algorithm in exclusive mode with the given dcut
00436 int ClusterSequence::n_exclusive_jets (const double & dcut) const {
00437 
00438   // first locate the point where clustering would have stopped (i.e. the
00439   // first time max_dij_so_far > dcut)
00440   int i = _history.size() - 1; // last jet
00441   while (i >= 0) {
00442     if (_history[i].max_dij_so_far <= dcut) {break;}
00443     i--;
00444   }
00445   int stop_point = i + 1;
00446   // relation between stop_point, njets assumes one extra jet disappears
00447   // at each clustering.
00448   int njets = 2*_initial_n - stop_point;
00449   return njets;
00450 }
00451 
00452 //----------------------------------------------------------------------
00453 // return all exclusive jets that would have been obtained running
00454 // the algorithm in exclusive mode with the given dcut
00455 vector<PseudoJet> ClusterSequence::exclusive_jets (const double & dcut) const {
00456   int njets = n_exclusive_jets(dcut);
00457   return exclusive_jets(njets);
00458 }
00459 
00460 
00461 //----------------------------------------------------------------------
00462 // return the jets obtained by clustering the event to n jets.
00463 vector<PseudoJet> ClusterSequence::exclusive_jets (const int & njets) const {
00464 
00465   // make sure the user does not ask for more than jets than there
00466   // were particles in the first place.
00467   assert (njets <= _initial_n);
00468 
00469   // provide a warning when extracting exclusive jets for algorithms 
00470   // that does not support it explicitly.
00471   // Native algorithm that support it are: kt, ee_kt, cambridge, 
00472   //   genkt and ee_genkt (both with p>=0)
00473   // For plugins, we check Plugin::exclusive_sequence_meaningful()
00474   if (( _jet_def.jet_algorithm() != kt_algorithm) &&
00475       ( _jet_def.jet_algorithm() != cambridge_algorithm) &&
00476       ( _jet_def.jet_algorithm() != ee_kt_algorithm) &&
00477       (((_jet_def.jet_algorithm() != genkt_algorithm) && 
00478         (_jet_def.jet_algorithm() != ee_genkt_algorithm)) || 
00479        (_jet_def.extra_param() <0)) &&
00480       ((_jet_def.jet_algorithm() != plugin_algorithm) ||
00481        (!_jet_def.plugin()->exclusive_sequence_meaningful())) &&
00482       (_n_exclusive_warnings < 5)) {
00483     _n_exclusive_warnings++;
00484     cerr << "FastJet WARNING: dcut and exclusive jets for jet-finders other than kt should be interpreted with care." << endl;
00485   }
00486 
00487 
00488   // calculate the point where we have to stop the clustering.
00489   // relation between stop_point, njets assumes one extra jet disappears
00490   // at each clustering.
00491   int stop_point = 2*_initial_n - njets;
00492 
00493   // some sanity checking to make sure that e+e- does not give us
00494   // surprises (should we ever implement e+e-)...
00495   if (2*_initial_n != static_cast<int>(_history.size())) {
00496     ostringstream err;
00497     err << "2*_initial_n != _history.size() -- this endangers internal assumptions!\n";
00498     throw Error(err.str());
00499     //assert(false);
00500   }
00501 
00502   // now go forwards and reconstitute the jets that we have --
00503   // basically for any history element, see if the parent jets to
00504   // which it refers were created before the stopping point -- if they
00505   // were then add them to the list, otherwise they are subsequent
00506   // recombinations of the jets that we are looking for.
00507   vector<PseudoJet> jets;
00508   for (unsigned int i = stop_point; i < _history.size(); i++) {
00509     int parent1 = _history[i].parent1;
00510     if (parent1 < stop_point) {
00511       jets.push_back(_jets[_history[parent1].jetp_index]);
00512     }
00513     int parent2 = _history[i].parent2;
00514     if (parent2 < stop_point && parent2 > 0) {
00515       jets.push_back(_jets[_history[parent2].jetp_index]);
00516     }
00517     
00518   }
00519 
00520   // sanity check...
00521   if (static_cast<int>(jets.size()) != njets) {
00522     ostringstream err;
00523     err << "ClusterSequence::exclusive_jets: size of returned vector ("
00524          <<jets.size()<<") does not coincide with requested number of jets ("
00525          <<njets<<")";
00526     throw Error(err.str());
00527   }
00528 
00529   return jets;
00530 }
00531 
00532 //----------------------------------------------------------------------
00535 double ClusterSequence::exclusive_dmerge (const int & njets) const {
00536   assert(njets >= 0);
00537   if (njets >= _initial_n) {return 0.0;}
00538   return _history[2*_initial_n-njets-1].dij;
00539 }
00540 
00541 
00542 //----------------------------------------------------------------------
00547 double ClusterSequence::exclusive_dmerge_max (const int & njets) const {
00548   assert(njets >= 0);
00549   if (njets >= _initial_n) {return 0.0;}
00550   return _history[2*_initial_n-njets-1].max_dij_so_far;
00551 }
00552 
00553 
00554 //----------------------------------------------------------------------
00558 std::vector<PseudoJet> ClusterSequence::exclusive_subjets 
00559    (const PseudoJet & jet, const double & dcut) const {
00560 
00561   set<const history_element*> subhist;
00562 
00563   // get the set of history elements that correspond to subjets at
00564   // scale dcut
00565   get_subhist_set(subhist, jet, dcut, 0);
00566 
00567   // now transfer this into a sequence of jets
00568   vector<PseudoJet> subjets;
00569   subjets.reserve(subhist.size());
00570   for (set<const history_element*>::iterator elem = subhist.begin(); 
00571        elem != subhist.end(); elem++) {
00572     subjets.push_back(_jets[(*elem)->jetp_index]);
00573   }
00574   return subjets;
00575 }
00576 
00577 //----------------------------------------------------------------------
00581 int ClusterSequence::n_exclusive_subjets(const PseudoJet & jet, 
00582                         const double & dcut) const {
00583   set<const history_element*> subhist;
00584   // get the set of history elements that correspond to subjets at
00585   // scale dcut
00586   get_subhist_set(subhist, jet, dcut, 0);
00587   return subhist.size();
00588 }
00589 
00590 //----------------------------------------------------------------------
00594 std::vector<PseudoJet> ClusterSequence::exclusive_subjets 
00595    (const PseudoJet & jet, int n) const {
00596 
00597   set<const history_element*> subhist;
00598 
00599   // get the set of history elements that correspond to subjets at
00600   // scale dcut
00601   get_subhist_set(subhist, jet, -1.0, n);
00602 
00603   // now transfer this into a sequence of jets
00604   vector<PseudoJet> subjets;
00605   subjets.reserve(subhist.size());
00606   for (set<const history_element*>::iterator elem = subhist.begin(); 
00607        elem != subhist.end(); elem++) {
00608     subjets.push_back(_jets[(*elem)->jetp_index]);
00609   }
00610   return subjets;
00611 }
00612 
00613 
00614 //----------------------------------------------------------------------
00619 double ClusterSequence::exclusive_subdmerge(const PseudoJet & jet, int nsub) const {
00620   set<const history_element*> subhist;
00621 
00622   // get the set of history elements that correspond to subjets at
00623   // scale dcut
00624   get_subhist_set(subhist, jet, -1.0, nsub);
00625   
00626   set<const history_element*>::iterator highest = subhist.end();
00627   highest--;
00630   return (*highest)->dij;
00631 }
00632 
00633 
00634 //----------------------------------------------------------------------
00640 double ClusterSequence::exclusive_subdmerge_max(const PseudoJet & jet, int nsub) const {
00641 
00642   set<const history_element*> subhist;
00643 
00644   // get the set of history elements that correspond to subjets at
00645   // scale dcut
00646   get_subhist_set(subhist, jet, -1.0, nsub);
00647   
00648   set<const history_element*>::iterator highest = subhist.end();
00649   highest--;
00652   return (*highest)->max_dij_so_far;
00653 }
00654 
00655 
00656 
00657 //----------------------------------------------------------------------
00664 void ClusterSequence::get_subhist_set(set<const history_element*> & subhist,
00665                                      const  PseudoJet & jet, 
00666                                      double dcut, int maxjet) const {
00667   subhist.clear();
00668   subhist.insert(&(_history[jet.cluster_hist_index()]));
00669 
00670   // establish the set of jets that are relevant
00671   int njet = 1;
00672   while (true) {
00673     // first find out if we need to probe deeper into jet.
00674     // Get history element closest to end of sequence
00675     set<const history_element*>::iterator highest = subhist.end();
00676     assert (highest != subhist.begin()); 
00677     highest--;
00678     const history_element* elem = *highest;
00679     // make sure we haven't got too many jets
00680     if (njet == maxjet) break;
00681     // make sure it has parents
00682     if (elem->parent1 < 0)            break;
00683     // make sure that we still resolve it at scale dcut
00684     if (elem->max_dij_so_far <= dcut) break;
00685 
00686     // then do so: replace "highest" with its two parents
00687     subhist.erase(highest);
00688     subhist.insert(&(_history[elem->parent1]));
00689     subhist.insert(&(_history[elem->parent2]));
00690     njet++;
00691   }
00692 }
00693 
00694 //----------------------------------------------------------------------
00695 // work through the object's history until
00696 bool ClusterSequence::object_in_jet(const PseudoJet & object, 
00697                                     const PseudoJet & jet) const {
00698 
00699   // make sure the object conceivably belongs to this clustering
00700   // sequence
00701   assert(_potentially_valid(object) && _potentially_valid(jet));
00702 
00703   const PseudoJet * this_object = &object;
00704   const PseudoJet * childp;
00705   while(true) {
00706     if (this_object->cluster_hist_index() == jet.cluster_hist_index()) {
00707       return true;
00708     } else if (has_child(*this_object, childp)) {this_object = childp;}
00709     else {return false;}
00710   }
00711 }
00712 
00713 //----------------------------------------------------------------------
00719 bool ClusterSequence::has_parents(const PseudoJet & jet, PseudoJet & parent1, 
00720                               PseudoJet & parent2) const {
00721 
00722   const history_element & hist = _history[jet.cluster_hist_index()];
00723 
00724   // make sure we do not run into any unexpected situations --
00725   // i.e. both parents valid, or neither
00726   assert ((hist.parent1 >= 0 && hist.parent2 >= 0) || 
00727           (hist.parent1 < 0 && hist.parent2 < 0));
00728 
00729   if (hist.parent1 < 0) {
00730     parent1 = PseudoJet(0.0,0.0,0.0,0.0);
00731     parent2 = parent1;
00732     return false;
00733   } else {
00734     parent1 = _jets[_history[hist.parent1].jetp_index];
00735     parent2 = _jets[_history[hist.parent2].jetp_index];
00736     // order the parents in decreasing pt
00737     if (parent1.perp2() < parent2.perp2()) swap(parent1,parent2);
00738     return true;
00739   }
00740 }
00741 
00742 //----------------------------------------------------------------------
00745 bool ClusterSequence::has_child(const PseudoJet & jet, PseudoJet & child) const {
00746 
00747   //const history_element & hist = _history[jet.cluster_hist_index()];
00748   //
00749   //if (hist.child >= 0) {
00750   //  child = _jets[_history[hist.child].jetp_index];
00751   //  return true;
00752   //} else {
00753   //  child = PseudoJet(0.0,0.0,0.0,0.0);
00754   //  return false;
00755   //}
00756   const PseudoJet * childp;
00757   bool res = has_child(jet, childp);
00758   if (res) {
00759     child = *childp;
00760     return true;
00761   } else {
00762     child = PseudoJet(0.0,0.0,0.0,0.0);
00763     return false;
00764   }
00765 }
00766 
00767 bool ClusterSequence::has_child(const PseudoJet & jet, const PseudoJet * & childp) const {
00768 
00769   const history_element & hist = _history[jet.cluster_hist_index()];
00770 
00771   // check that this jet has a child and that the child corresponds to
00772   // a true jet [RETHINK-IF-CHANGE-NUMBERING: what is the right
00773   // behaviour if the child is the same jet but made inclusive...?]
00774   if (hist.child >= 0 && _history[hist.child].jetp_index >= 0) {
00775     childp = &(_jets[_history[hist.child].jetp_index]);
00776     return true;
00777   } else {
00778     childp = NULL;
00779     return false;
00780   }
00781 }
00782 
00783 
00784 //----------------------------------------------------------------------
00788 bool ClusterSequence::has_partner(const PseudoJet & jet, 
00789                               PseudoJet & partner) const {
00790 
00791   const history_element & hist = _history[jet.cluster_hist_index()];
00792 
00793   // make sure we have a child and that the child does not correspond
00794   // to a clustering with the beam (or some other invalid quantity)
00795   if (hist.child >= 0 && _history[hist.child].parent2 >= 0) {
00796     const history_element & child_hist = _history[hist.child];
00797     if (child_hist.parent1 == jet.cluster_hist_index()) {
00798       // partner will be child's parent2 -- for iB clustering
00799       // parent2 will not be valid
00800       partner = _jets[_history[child_hist.parent2].jetp_index];
00801     } else {
00802       // partner will be child's parent1
00803       partner = _jets[_history[child_hist.parent1].jetp_index];
00804     }
00805     return true;
00806   } else {
00807     partner = PseudoJet(0.0,0.0,0.0,0.0);
00808     return false;
00809   }
00810 }
00811 
00812 
00813 //----------------------------------------------------------------------
00814 // return a vector of the particles that make up a jet
00815 vector<PseudoJet> ClusterSequence::constituents (const PseudoJet & jet) const {
00816   vector<PseudoJet> subjets;
00817   add_constituents(jet, subjets);
00818   return subjets;
00819 }
00820 
00821 //----------------------------------------------------------------------
00830 void ClusterSequence::print_jets_for_root(const std::vector<PseudoJet> & jets, 
00831                                           ostream & ostr) const {
00832   for (unsigned i = 0; i < jets.size(); i++) {
00833     ostr << i  << " "
00834          << jets[i].px() << " "
00835          << jets[i].py() << " "
00836          << jets[i].pz() << " "
00837          << jets[i].E() << endl;
00838     vector<PseudoJet> cst = constituents(jets[i]);
00839     for (unsigned j = 0; j < cst.size() ; j++) {
00840       ostr << " " << j << " "
00841            << cst[j].rap() << " "
00842            << cst[j].phi() << " "
00843            << cst[j].perp() << endl;
00844     }
00845     ostr << "#END" << endl;
00846   }
00847 }
00848 
00849 void ClusterSequence::print_jets_for_root(const std::vector<PseudoJet> & jets, 
00850                                           const std::string & filename,
00851                                           const std::string & comment ) const {
00852   std::ofstream ostr(filename.c_str());
00853   if (comment != "") ostr << "# " << comment << endl;
00854   print_jets_for_root(jets, ostr);
00855 }
00856 
00857 
00858 // Not yet. Perhaps in a future release
00859 // //----------------------------------------------------------------------
00860 // // print out all inclusive jets with pt > ptmin
00861 // void ClusterSequence::print_jets (const double & ptmin) const{
00862 //     vector<PseudoJet> jets = sorted_by_pt(inclusive_jets(ptmin));
00863 // 
00864 //     for (size_t j = 0; j < jets.size(); j++) {
00865 //        printf("%5u %7.3f %7.3f %9.3f\n",
00866 //        j,jets[j].rap(),jets[j].phi(),jets[j].perp());
00867 //     }
00868 // }
00869 
00870 //----------------------------------------------------------------------
00875 vector<int> ClusterSequence::particle_jet_indices(
00876                         const vector<PseudoJet> & jets) const {
00877 
00878   vector<int> indices(n_particles());
00879 
00880   // first label all particles as not belonging to any jets
00881   for (unsigned ipart = 0; ipart < n_particles(); ipart++) 
00882     indices[ipart] = -1;
00883 
00884   // then for each of the jets relabel its consituents as belonging to
00885   // that jet
00886   for (unsigned ijet = 0; ijet < jets.size(); ijet++) {
00887 
00888     vector<PseudoJet> jet_constituents(constituents(jets[ijet]));
00889 
00890     for (unsigned ip = 0; ip < jet_constituents.size(); ip++) {
00891       // a safe (if slightly redundant) way of getting the particle
00892       // index (for initial particles it is actually safe to assume
00893       // ipart=iclust).
00894       unsigned iclust = jet_constituents[ip].cluster_hist_index();
00895       unsigned ipart = history()[iclust].jetp_index;
00896       indices[ipart] = ijet;
00897     }
00898   }
00899 
00900   return indices;
00901 }
00902 
00903 
00904 //----------------------------------------------------------------------
00905 // recursive routine that adds on constituents of jet to the subjet_vector
00906 void ClusterSequence::add_constituents (
00907            const PseudoJet & jet, vector<PseudoJet> & subjet_vector) const {
00908   // find out position in cluster history
00909   int i = jet.cluster_hist_index();
00910   int parent1 = _history[i].parent1;
00911   int parent2 = _history[i].parent2;
00912 
00913   if (parent1 == InexistentParent) {
00914     // It is an original particle (labelled by its parent having value
00915     // InexistentParent), therefore add it on to the subjet vector
00916     // Note: we add the initial particle and not simply 'jet' so that
00917     //       calling add_constituents with a subtracted jet containing
00918     //       only one particle will work.
00919     subjet_vector.push_back(_jets[i]);
00920     return;
00921   } 
00922 
00923   // add parent 1
00924   add_constituents(_jets[_history[parent1].jetp_index], subjet_vector);
00925 
00926   // see if parent2 is a real jet; if it is then add its constituents
00927   if (parent2 != BeamJet) {
00928     add_constituents(_jets[_history[parent2].jetp_index], subjet_vector);
00929   }
00930 }
00931 
00932 
00933 
00934 //----------------------------------------------------------------------
00935 // initialise the history in a standard way
00936 void ClusterSequence::_add_step_to_history (
00937                const int & step_number, const int & parent1, 
00938                const int & parent2, const int & jetp_index,
00939                const double & dij) {
00940 
00941   history_element element;
00942   element.parent1 = parent1;
00943   element.parent2 = parent2;
00944   element.jetp_index = jetp_index;
00945   element.child = Invalid;
00946   element.dij   = dij;
00947   element.max_dij_so_far = max(dij,_history[_history.size()-1].max_dij_so_far);
00948   _history.push_back(element);
00949 
00950   int local_step = _history.size()-1;
00951   assert(local_step == step_number);
00952 
00953   assert(parent1 >= 0);
00954   _history[parent1].child = local_step;
00955   if (parent2 >= 0) {_history[parent2].child = local_step;}
00956 
00957   // get cross-referencing right from PseudoJets
00958   if (jetp_index != Invalid) {
00959     assert(jetp_index >= 0);
00960     //cout << _jets.size() <<" "<<jetp_index<<"\n";
00961     _jets[jetp_index].set_cluster_hist_index(local_step);
00962   }
00963 
00964   if (_writeout_combinations) {
00965     cout << local_step << ": " 
00966          << parent1 << " with " << parent2
00967          << "; y = "<< dij<<endl;
00968   }
00969 
00970 }
00971 
00972 
00973 
00974 
00975 //======================================================================
00976 // Return an order in which to read the history such that _history[order[i]] 
00977 // will always correspond to the same set of consituent particles if 
00978 // two branching histories are equivalent in terms of the particles
00979 // contained in any given pseudojet.
00980 vector<int> ClusterSequence::unique_history_order() const {
00981 
00982   // first construct an array that will tell us the lowest constituent
00983   // of a given jet -- this will always be one of the original
00984   // particles, whose order is well defined and so will help us to
00985   // follow the tree in a unique manner.
00986   valarray<int> lowest_constituent(_history.size());
00987   int hist_n = _history.size();
00988   lowest_constituent = hist_n; // give it a large number
00989   for (int i = 0; i < hist_n; i++) {
00990     // sets things up for the initial partons
00991     lowest_constituent[i] = min(lowest_constituent[i],i); 
00992     // propagates them through to the children of this parton
00993     if (_history[i].child > 0) lowest_constituent[_history[i].child] 
00994       = min(lowest_constituent[_history[i].child],lowest_constituent[i]);
00995   }
00996 
00997   // establish an array for what we have and have not extracted so far
00998   valarray<bool> extracted(_history.size()); extracted = false;
00999   vector<int> unique_tree;
01000   unique_tree.reserve(_history.size());
01001 
01002   // now work our way through the tree
01003   for (unsigned i = 0; i < n_particles(); i++) {
01004     if (!extracted[i]) {
01005       unique_tree.push_back(i);
01006       extracted[i] = true;
01007       _extract_tree_children(i, extracted, lowest_constituent, unique_tree);
01008     }
01009   }
01010 
01011   return unique_tree;
01012 }
01013 
01014 //======================================================================
01015 // helper for unique_history_order
01016 void ClusterSequence::_extract_tree_children(
01017        int position, 
01018        valarray<bool> & extracted, 
01019        const valarray<int> & lowest_constituent,
01020        vector<int> & unique_tree) const {
01021   if (!extracted[position]) {
01022     // that means we may have unidentified parents around, so go and
01023     // collect them (extracted[position]) will then be made true)
01024     _extract_tree_parents(position,extracted,lowest_constituent,unique_tree);
01025   } 
01026   
01027   // now look after the children...
01028   int child = _history[position].child;
01029   if (child  >= 0) _extract_tree_children(child,extracted,lowest_constituent,unique_tree);
01030 }
01031 
01032 
01033 //======================================================================
01034 // return the list of unclustered particles
01035 vector<PseudoJet> ClusterSequence::unclustered_particles() const {
01036   vector<PseudoJet> unclustered;
01037   for (unsigned i = 0; i < n_particles() ; i++) {
01038     if (_history[i].child == Invalid) 
01039       unclustered.push_back(_jets[_history[i].jetp_index]);
01040   }
01041   return unclustered;
01042 }
01043 
01044 
01045 
01046 //======================================================================
01047 // helper for unique_history_order
01048 void ClusterSequence::_extract_tree_parents(
01049        int position, 
01050        valarray<bool> & extracted, 
01051        const valarray<int> & lowest_constituent,
01052        vector<int> & unique_tree) const {
01053 
01054   if (!extracted[position]) {
01055     int parent1 = _history[position].parent1;
01056     int parent2 = _history[position].parent2;
01057     // where relevant order parents so that we will first treat the
01058     // one containing the smaller "lowest_constituent"
01059     if (parent1 >= 0 && parent2 >= 0) {
01060       if (lowest_constituent[parent1] > lowest_constituent[parent2]) 
01061         swap(parent1, parent2);
01062     }
01063     // then actually run through the parents to extract the constituents...
01064     if (parent1 >= 0 && !extracted[parent1]) 
01065       _extract_tree_parents(parent1,extracted,lowest_constituent,unique_tree);
01066     if (parent2 >= 0 && !extracted[parent2]) 
01067       _extract_tree_parents(parent2,extracted,lowest_constituent,unique_tree);
01068     // finally declare this position to be accounted for and push it
01069     // onto our list.
01070     unique_tree.push_back(position);
01071     extracted[position] = true;
01072   }
01073 }
01074 
01075 
01076 //======================================================================
01080 void ClusterSequence::_do_ij_recombination_step(
01081                                const int & jet_i, const int & jet_j, 
01082                                const double & dij, 
01083                                int & newjet_k) {
01084 
01085   // create the new jet by recombining the first two
01086   PseudoJet newjet;
01087   _jet_def.recombiner()->recombine(_jets[jet_i], _jets[jet_j], newjet);
01088   _jets.push_back(newjet);
01089   // original version...
01090   //_jets.push_back(_jets[jet_i] + _jets[jet_j]);
01091 
01092   // get its index
01093   newjet_k = _jets.size()-1;
01094 
01095   // get history index
01096   int newstep_k = _history.size();
01097   // and provide jet with the info
01098   _jets[newjet_k].set_cluster_hist_index(newstep_k);
01099 
01100   // finally sort out the history 
01101   int hist_i = _jets[jet_i].cluster_hist_index();
01102   int hist_j = _jets[jet_j].cluster_hist_index();
01103 
01104   _add_step_to_history(newstep_k, min(hist_i, hist_j), max(hist_i,hist_j),
01105                        newjet_k, dij);
01106 
01107 }
01108 
01109 
01110 //======================================================================
01113 void ClusterSequence::_do_iB_recombination_step(
01114                                   const int & jet_i, const double & diB) {
01115   // get history index
01116   int newstep_k = _history.size();
01117 
01118   // recombine the jet with the beam
01119   _add_step_to_history(newstep_k,_jets[jet_i].cluster_hist_index(),BeamJet,
01120                        Invalid, diB);
01121 
01122 }
01123 
01124 FASTJET_END_NAMESPACE
01125 

Generated on Fri May 29 20:29:03 2009 for fastjet by  doxygen 1.5.7.1