ClusterSequence.cc

Go to the documentation of this file.
00001 //STARTHEADER
00002 // $Id: ClusterSequence.cc 1682 2010-02-18 21:56:19Z 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   // make sure we really have the output done.
00223   cout.flush();
00224 }
00225 
00226 //----------------------------------------------------------------------
00227 // transfer all relevant info into internal variables
00228 void ClusterSequence::_decant_options(const JetDefinition & jet_def,
00229                                       const bool & writeout_combinations) {
00230 
00231   // let the user know what's going on
00232   _print_banner();
00233 
00234   // make a local copy of the jet definition (for future use?)
00235   _jet_def = jet_def;
00236   
00237   _writeout_combinations = writeout_combinations;
00238   _jet_algorithm = jet_def.jet_algorithm();
00239   _Rparam = jet_def.R();  _R2 = _Rparam*_Rparam; _invR2 = 1.0/_R2;
00240   _strategy = jet_def.strategy();
00241 
00242   // disallow interference from the plugin
00243   _plugin_activated = false;
00244   
00245 }
00246 
00247 
00248 //----------------------------------------------------------------------
00249 // initialise the history in a standard way
00250 void ClusterSequence::_fill_initial_history () {
00251 
00252   //if (_jets.size() == 0) {throw Error("Cannot run jet-finder on empty event");}
00253 
00254   // reserve sufficient space for everything
00255   _jets.reserve(_jets.size()*2);
00256   _history.reserve(_jets.size()*2);
00257 
00258   _Qtot = 0;
00259 
00260   for (int i = 0; i < static_cast<int>(_jets.size()) ; i++) {
00261     history_element element;
00262     element.parent1 = InexistentParent;
00263     element.parent2 = InexistentParent;
00264     element.child   = Invalid;
00265     element.jetp_index = i;
00266     element.dij     = 0.0;
00267     element.max_dij_so_far = 0.0;
00268 
00269     _history.push_back(element);
00270     
00271     // do any momentum preprocessing needed by the recombination scheme
00272     _jet_def.recombiner()->preprocess(_jets[i]);
00273 
00274     // get cross-referencing right from PseudoJets
00275     _jets[i].set_cluster_hist_index(i);
00276 
00277     // determine the total energy in the event
00278     _Qtot += _jets[i].E();
00279   }
00280   _initial_n = _jets.size();
00281 }
00282 
00283 
00284 //----------------------------------------------------------------------
00285 // Return the component corresponding to the specified index.
00286 // taken from CLHEP
00287 string ClusterSequence::strategy_string ()  const {
00288   string strategy;
00289   switch(_strategy) {
00290   case NlnN:
00291     strategy = "NlnN"; break;
00292   case NlnN3pi:
00293     strategy = "NlnN3pi"; break;
00294   case NlnN4pi:
00295     strategy = "NlnN4pi"; break;
00296   case N2Plain:
00297     strategy = "N2Plain"; break;
00298   case N2Tiled:
00299     strategy = "N2Tiled"; break;
00300   case N2MinHeapTiled:
00301     strategy = "N2MinHeapTiled"; break;
00302   case N2PoorTiled:
00303     strategy = "N2PoorTiled"; break;
00304   case N3Dumb:
00305     strategy = "N3Dumb"; break;
00306   case NlnNCam4pi:
00307     strategy = "NlnNCam4pi"; break;
00308   case NlnNCam2pi2R:
00309     strategy = "NlnNCam2pi2R"; break;
00310   case NlnNCam:
00311     strategy = "NlnNCam"; break; // 2piMultD
00312   case plugin_strategy:
00313     strategy = "plugin strategy"; break;
00314   default:
00315     strategy = "Unrecognized";
00316   }
00317   return strategy;
00318 }  
00319 
00320 
00321 double ClusterSequence::jet_scale_for_algorithm(
00322                                   const PseudoJet & jet) const {
00323   if (_jet_algorithm == kt_algorithm)             {return jet.kt2();}
00324   else if (_jet_algorithm == cambridge_algorithm) {return 1.0;}
00325   else if (_jet_algorithm == antikt_algorithm) {
00326     double kt2=jet.kt2();
00327     return kt2 > 1e-300 ? 1.0/kt2 : 1e300;
00328   } else if (_jet_algorithm == genkt_algorithm) {
00329     double kt2 = jet.kt2();
00330     double p   = jet_def().extra_param();
00331     if (p <= 0 && kt2 < 1e-300) kt2 = 1e-300; // dodgy safety check
00332     return pow(kt2, p);
00333   } else if (_jet_algorithm == cambridge_for_passive_algorithm) {
00334     double kt2 = jet.kt2();
00335     double lim = _jet_def.extra_param();
00336     if (kt2 < lim*lim && kt2 != 0.0) {
00337       return 1.0/kt2;
00338     } else {return 1.0;}
00339   } else {throw Error("Unrecognised jet algorithm");}
00340 }
00341 
00342 
00343 //----------------------------------------------------------------------
00347 void ClusterSequence::transfer_from_sequence(ClusterSequence & from_seq) {
00348 
00349   // the metadata
00350   _jet_def                 = from_seq._jet_def                ;
00351   _writeout_combinations   = from_seq._writeout_combinations  ;
00352   _initial_n               = from_seq._initial_n              ;
00353   _Rparam                  = from_seq._Rparam                 ;
00354   _R2                      = from_seq._R2                     ;
00355   _invR2                   = from_seq._invR2                  ;
00356   _strategy                = from_seq._strategy               ;
00357   _jet_algorithm           = from_seq._jet_algorithm          ;
00358   _plugin_activated        = from_seq._plugin_activated       ;
00359 
00360   // the data
00361   _jets     = from_seq._jets;
00362   _history  = from_seq._history;
00363   // the following transferse ownership of the extras from the from_seq
00364   _extras   = from_seq._extras;
00365 
00366 }
00367 
00368 //----------------------------------------------------------------------
00369 // record an ij recombination and reset the _jets[newjet_k] momentum and
00370 // user index to be those of newjet
00371 void ClusterSequence::plugin_record_ij_recombination(
00372            int jet_i, int jet_j, double dij, 
00373            const PseudoJet & newjet, int & newjet_k) {
00374 
00375   plugin_record_ij_recombination(jet_i, jet_j, dij, newjet_k);
00376 
00377   // now transfer newjet into place
00378   int tmp_index = _jets[newjet_k].cluster_hist_index();
00379   _jets[newjet_k] = newjet;
00380   _jets[newjet_k].set_cluster_hist_index(tmp_index);
00381 }
00382 
00383 
00384 //----------------------------------------------------------------------
00385 // return all inclusive jets with pt > ptmin
00386 vector<PseudoJet> ClusterSequence::inclusive_jets (const double & ptmin) const{
00387   double dcut = ptmin*ptmin;
00388   int i = _history.size() - 1; // last jet
00389   vector<PseudoJet> jets;
00390   if (_jet_algorithm == kt_algorithm) {
00391     while (i >= 0) {
00392       // with our specific definition of dij and diB (i.e. R appears only in 
00393       // dij), then dij==diB is the same as the jet.perp2() and we can exploit
00394       // this in selecting the jets...
00395       if (_history[i].max_dij_so_far < dcut) {break;}
00396       if (_history[i].parent2 == BeamJet && _history[i].dij >= dcut) {
00397         // for beam jets
00398         int parent1 = _history[i].parent1;
00399         jets.push_back(_jets[_history[parent1].jetp_index]);}
00400       i--;
00401     }
00402   } else if (_jet_algorithm == cambridge_algorithm) {
00403     while (i >= 0) {
00404       // inclusive jets are all at end of clustering sequence in the
00405       // Cambridge algorithm -- so if we find a non-exclusive jet, then
00406       // we can exit
00407       if (_history[i].parent2 != BeamJet) {break;}
00408       int parent1 = _history[i].parent1;
00409       const PseudoJet & jet = _jets[_history[parent1].jetp_index];
00410       if (jet.perp2() >= dcut) {jets.push_back(jet);}
00411       i--;
00412     }
00413   } else if (_jet_algorithm == plugin_algorithm 
00414              || _jet_algorithm == ee_kt_algorithm
00415              || _jet_algorithm == antikt_algorithm
00416              || _jet_algorithm == genkt_algorithm
00417              || _jet_algorithm == ee_genkt_algorithm
00418              || _jet_algorithm == cambridge_for_passive_algorithm) {
00419     // for inclusive jets with a plugin algorithm, we make no
00420     // assumptions about anything (relation of dij to momenta,
00421     // ordering of the dij, etc.)
00422     while (i >= 0) {
00423       if (_history[i].parent2 == BeamJet) {
00424         int parent1 = _history[i].parent1;
00425         const PseudoJet & jet = _jets[_history[parent1].jetp_index];
00426         if (jet.perp2() >= dcut) {jets.push_back(jet);}
00427       }
00428       i--;
00429     }
00430   } else {throw Error("cs::inclusive_jets(...): Unrecognized jet algorithm");}
00431   return jets;
00432 }
00433 
00434 
00435 //----------------------------------------------------------------------
00436 // return the number of exclusive jets that would have been obtained
00437 // running the algorithm in exclusive mode with the given dcut
00438 int ClusterSequence::n_exclusive_jets (const double & dcut) const {
00439 
00440   // first locate the point where clustering would have stopped (i.e. the
00441   // first time max_dij_so_far > dcut)
00442   int i = _history.size() - 1; // last jet
00443   while (i >= 0) {
00444     if (_history[i].max_dij_so_far <= dcut) {break;}
00445     i--;
00446   }
00447   int stop_point = i + 1;
00448   // relation between stop_point, njets assumes one extra jet disappears
00449   // at each clustering.
00450   int njets = 2*_initial_n - stop_point;
00451   return njets;
00452 }
00453 
00454 //----------------------------------------------------------------------
00455 // return all exclusive jets that would have been obtained running
00456 // the algorithm in exclusive mode with the given dcut
00457 vector<PseudoJet> ClusterSequence::exclusive_jets (const double & dcut) const {
00458   int njets = n_exclusive_jets(dcut);
00459   return exclusive_jets(njets);
00460 }
00461 
00462 
00463 //----------------------------------------------------------------------
00464 // return the jets obtained by clustering the event to n jets.
00465 vector<PseudoJet> ClusterSequence::exclusive_jets (const int & njets) const {
00466 
00467   // make sure the user does not ask for more than jets than there
00468   // were particles in the first place.
00469   assert (njets <= _initial_n);
00470 
00471   // provide a warning when extracting exclusive jets for algorithms 
00472   // that does not support it explicitly.
00473   // Native algorithm that support it are: kt, ee_kt, cambridge, 
00474   //   genkt and ee_genkt (both with p>=0)
00475   // For plugins, we check Plugin::exclusive_sequence_meaningful()
00476   if (( _jet_def.jet_algorithm() != kt_algorithm) &&
00477       ( _jet_def.jet_algorithm() != cambridge_algorithm) &&
00478       ( _jet_def.jet_algorithm() != ee_kt_algorithm) &&
00479       (((_jet_def.jet_algorithm() != genkt_algorithm) && 
00480         (_jet_def.jet_algorithm() != ee_genkt_algorithm)) || 
00481        (_jet_def.extra_param() <0)) &&
00482       ((_jet_def.jet_algorithm() != plugin_algorithm) ||
00483        (!_jet_def.plugin()->exclusive_sequence_meaningful())) &&
00484       (_n_exclusive_warnings < 5)) {
00485     _n_exclusive_warnings++;
00486     cerr << "FastJet WARNING: dcut and exclusive jets for jet-finders other than kt should be interpreted with care." << endl;
00487   }
00488 
00489 
00490   // calculate the point where we have to stop the clustering.
00491   // relation between stop_point, njets assumes one extra jet disappears
00492   // at each clustering.
00493   int stop_point = 2*_initial_n - njets;
00494 
00495   // some sanity checking to make sure that e+e- does not give us
00496   // surprises (should we ever implement e+e-)...
00497   if (2*_initial_n != static_cast<int>(_history.size())) {
00498     ostringstream err;
00499     err << "2*_initial_n != _history.size() -- this endangers internal assumptions!\n";
00500     throw Error(err.str());
00501     //assert(false);
00502   }
00503 
00504   // now go forwards and reconstitute the jets that we have --
00505   // basically for any history element, see if the parent jets to
00506   // which it refers were created before the stopping point -- if they
00507   // were then add them to the list, otherwise they are subsequent
00508   // recombinations of the jets that we are looking for.
00509   vector<PseudoJet> jets;
00510   for (unsigned int i = stop_point; i < _history.size(); i++) {
00511     int parent1 = _history[i].parent1;
00512     if (parent1 < stop_point) {
00513       jets.push_back(_jets[_history[parent1].jetp_index]);
00514     }
00515     int parent2 = _history[i].parent2;
00516     if (parent2 < stop_point && parent2 > 0) {
00517       jets.push_back(_jets[_history[parent2].jetp_index]);
00518     }
00519     
00520   }
00521 
00522   // sanity check...
00523   if (static_cast<int>(jets.size()) != njets) {
00524     ostringstream err;
00525     err << "ClusterSequence::exclusive_jets: size of returned vector ("
00526          <<jets.size()<<") does not coincide with requested number of jets ("
00527          <<njets<<")";
00528     throw Error(err.str());
00529   }
00530 
00531   return jets;
00532 }
00533 
00534 //----------------------------------------------------------------------
00537 double ClusterSequence::exclusive_dmerge (const int & njets) const {
00538   assert(njets >= 0);
00539   if (njets >= _initial_n) {return 0.0;}
00540   return _history[2*_initial_n-njets-1].dij;
00541 }
00542 
00543 
00544 //----------------------------------------------------------------------
00549 double ClusterSequence::exclusive_dmerge_max (const int & njets) const {
00550   assert(njets >= 0);
00551   if (njets >= _initial_n) {return 0.0;}
00552   return _history[2*_initial_n-njets-1].max_dij_so_far;
00553 }
00554 
00555 
00556 //----------------------------------------------------------------------
00560 std::vector<PseudoJet> ClusterSequence::exclusive_subjets 
00561    (const PseudoJet & jet, const double & dcut) const {
00562 
00563   set<const history_element*> subhist;
00564 
00565   // get the set of history elements that correspond to subjets at
00566   // scale dcut
00567   get_subhist_set(subhist, jet, dcut, 0);
00568 
00569   // now transfer this into a sequence of jets
00570   vector<PseudoJet> subjets;
00571   subjets.reserve(subhist.size());
00572   for (set<const history_element*>::iterator elem = subhist.begin(); 
00573        elem != subhist.end(); elem++) {
00574     subjets.push_back(_jets[(*elem)->jetp_index]);
00575   }
00576   return subjets;
00577 }
00578 
00579 //----------------------------------------------------------------------
00583 int ClusterSequence::n_exclusive_subjets(const PseudoJet & jet, 
00584                         const double & dcut) const {
00585   set<const history_element*> subhist;
00586   // get the set of history elements that correspond to subjets at
00587   // scale dcut
00588   get_subhist_set(subhist, jet, dcut, 0);
00589   return subhist.size();
00590 }
00591 
00592 //----------------------------------------------------------------------
00596 std::vector<PseudoJet> ClusterSequence::exclusive_subjets 
00597    (const PseudoJet & jet, int n) const {
00598 
00599   set<const history_element*> subhist;
00600 
00601   // get the set of history elements that correspond to subjets at
00602   // scale dcut
00603   get_subhist_set(subhist, jet, -1.0, n);
00604 
00605   // now transfer this into a sequence of jets
00606   vector<PseudoJet> subjets;
00607   subjets.reserve(subhist.size());
00608   for (set<const history_element*>::iterator elem = subhist.begin(); 
00609        elem != subhist.end(); elem++) {
00610     subjets.push_back(_jets[(*elem)->jetp_index]);
00611   }
00612   return subjets;
00613 }
00614 
00615 
00616 //----------------------------------------------------------------------
00621 double ClusterSequence::exclusive_subdmerge(const PseudoJet & jet, int nsub) const {
00622   set<const history_element*> subhist;
00623 
00624   // get the set of history elements that correspond to subjets at
00625   // scale dcut
00626   get_subhist_set(subhist, jet, -1.0, nsub);
00627   
00628   set<const history_element*>::iterator highest = subhist.end();
00629   highest--;
00632   return (*highest)->dij;
00633 }
00634 
00635 
00636 //----------------------------------------------------------------------
00642 double ClusterSequence::exclusive_subdmerge_max(const PseudoJet & jet, int nsub) const {
00643 
00644   set<const history_element*> subhist;
00645 
00646   // get the set of history elements that correspond to subjets at
00647   // scale dcut
00648   get_subhist_set(subhist, jet, -1.0, nsub);
00649   
00650   set<const history_element*>::iterator highest = subhist.end();
00651   highest--;
00654   return (*highest)->max_dij_so_far;
00655 }
00656 
00657 
00658 
00659 //----------------------------------------------------------------------
00666 void ClusterSequence::get_subhist_set(set<const history_element*> & subhist,
00667                                      const  PseudoJet & jet, 
00668                                      double dcut, int maxjet) const {
00669   subhist.clear();
00670   subhist.insert(&(_history[jet.cluster_hist_index()]));
00671 
00672   // establish the set of jets that are relevant
00673   int njet = 1;
00674   while (true) {
00675     // first find out if we need to probe deeper into jet.
00676     // Get history element closest to end of sequence
00677     set<const history_element*>::iterator highest = subhist.end();
00678     assert (highest != subhist.begin()); 
00679     highest--;
00680     const history_element* elem = *highest;
00681     // make sure we haven't got too many jets
00682     if (njet == maxjet) break;
00683     // make sure it has parents
00684     if (elem->parent1 < 0)            break;
00685     // make sure that we still resolve it at scale dcut
00686     if (elem->max_dij_so_far <= dcut) break;
00687 
00688     // then do so: replace "highest" with its two parents
00689     subhist.erase(highest);
00690     subhist.insert(&(_history[elem->parent1]));
00691     subhist.insert(&(_history[elem->parent2]));
00692     njet++;
00693   }
00694 }
00695 
00696 //----------------------------------------------------------------------
00697 // work through the object's history until
00698 bool ClusterSequence::object_in_jet(const PseudoJet & object, 
00699                                     const PseudoJet & jet) const {
00700 
00701   // make sure the object conceivably belongs to this clustering
00702   // sequence
00703   assert(_potentially_valid(object) && _potentially_valid(jet));
00704 
00705   const PseudoJet * this_object = &object;
00706   const PseudoJet * childp;
00707   while(true) {
00708     if (this_object->cluster_hist_index() == jet.cluster_hist_index()) {
00709       return true;
00710     } else if (has_child(*this_object, childp)) {this_object = childp;}
00711     else {return false;}
00712   }
00713 }
00714 
00715 //----------------------------------------------------------------------
00721 bool ClusterSequence::has_parents(const PseudoJet & jet, PseudoJet & parent1, 
00722                               PseudoJet & parent2) const {
00723 
00724   const history_element & hist = _history[jet.cluster_hist_index()];
00725 
00726   // make sure we do not run into any unexpected situations --
00727   // i.e. both parents valid, or neither
00728   assert ((hist.parent1 >= 0 && hist.parent2 >= 0) || 
00729           (hist.parent1 < 0 && hist.parent2 < 0));
00730 
00731   if (hist.parent1 < 0) {
00732     parent1 = PseudoJet(0.0,0.0,0.0,0.0);
00733     parent2 = parent1;
00734     return false;
00735   } else {
00736     parent1 = _jets[_history[hist.parent1].jetp_index];
00737     parent2 = _jets[_history[hist.parent2].jetp_index];
00738     // order the parents in decreasing pt
00739     if (parent1.perp2() < parent2.perp2()) swap(parent1,parent2);
00740     return true;
00741   }
00742 }
00743 
00744 //----------------------------------------------------------------------
00747 bool ClusterSequence::has_child(const PseudoJet & jet, PseudoJet & child) const {
00748 
00749   //const history_element & hist = _history[jet.cluster_hist_index()];
00750   //
00751   //if (hist.child >= 0) {
00752   //  child = _jets[_history[hist.child].jetp_index];
00753   //  return true;
00754   //} else {
00755   //  child = PseudoJet(0.0,0.0,0.0,0.0);
00756   //  return false;
00757   //}
00758   const PseudoJet * childp;
00759   bool res = has_child(jet, childp);
00760   if (res) {
00761     child = *childp;
00762     return true;
00763   } else {
00764     child = PseudoJet(0.0,0.0,0.0,0.0);
00765     return false;
00766   }
00767 }
00768 
00769 bool ClusterSequence::has_child(const PseudoJet & jet, const PseudoJet * & childp) const {
00770 
00771   const history_element & hist = _history[jet.cluster_hist_index()];
00772 
00773   // check that this jet has a child and that the child corresponds to
00774   // a true jet [RETHINK-IF-CHANGE-NUMBERING: what is the right
00775   // behaviour if the child is the same jet but made inclusive...?]
00776   if (hist.child >= 0 && _history[hist.child].jetp_index >= 0) {
00777     childp = &(_jets[_history[hist.child].jetp_index]);
00778     return true;
00779   } else {
00780     childp = NULL;
00781     return false;
00782   }
00783 }
00784 
00785 
00786 //----------------------------------------------------------------------
00790 bool ClusterSequence::has_partner(const PseudoJet & jet, 
00791                               PseudoJet & partner) const {
00792 
00793   const history_element & hist = _history[jet.cluster_hist_index()];
00794 
00795   // make sure we have a child and that the child does not correspond
00796   // to a clustering with the beam (or some other invalid quantity)
00797   if (hist.child >= 0 && _history[hist.child].parent2 >= 0) {
00798     const history_element & child_hist = _history[hist.child];
00799     if (child_hist.parent1 == jet.cluster_hist_index()) {
00800       // partner will be child's parent2 -- for iB clustering
00801       // parent2 will not be valid
00802       partner = _jets[_history[child_hist.parent2].jetp_index];
00803     } else {
00804       // partner will be child's parent1
00805       partner = _jets[_history[child_hist.parent1].jetp_index];
00806     }
00807     return true;
00808   } else {
00809     partner = PseudoJet(0.0,0.0,0.0,0.0);
00810     return false;
00811   }
00812 }
00813 
00814 
00815 //----------------------------------------------------------------------
00816 // return a vector of the particles that make up a jet
00817 vector<PseudoJet> ClusterSequence::constituents (const PseudoJet & jet) const {
00818   vector<PseudoJet> subjets;
00819   add_constituents(jet, subjets);
00820   return subjets;
00821 }
00822 
00823 //----------------------------------------------------------------------
00832 void ClusterSequence::print_jets_for_root(const std::vector<PseudoJet> & jets, 
00833                                           ostream & ostr) const {
00834   for (unsigned i = 0; i < jets.size(); i++) {
00835     ostr << i  << " "
00836          << jets[i].px() << " "
00837          << jets[i].py() << " "
00838          << jets[i].pz() << " "
00839          << jets[i].E() << endl;
00840     vector<PseudoJet> cst = constituents(jets[i]);
00841     for (unsigned j = 0; j < cst.size() ; j++) {
00842       ostr << " " << j << " "
00843            << cst[j].rap() << " "
00844            << cst[j].phi() << " "
00845            << cst[j].perp() << endl;
00846     }
00847     ostr << "#END" << endl;
00848   }
00849 }
00850 
00851 void ClusterSequence::print_jets_for_root(const std::vector<PseudoJet> & jets, 
00852                                           const std::string & filename,
00853                                           const std::string & comment ) const {
00854   std::ofstream ostr(filename.c_str());
00855   if (comment != "") ostr << "# " << comment << endl;
00856   print_jets_for_root(jets, ostr);
00857 }
00858 
00859 
00860 // Not yet. Perhaps in a future release
00861 // //----------------------------------------------------------------------
00862 // // print out all inclusive jets with pt > ptmin
00863 // void ClusterSequence::print_jets (const double & ptmin) const{
00864 //     vector<PseudoJet> jets = sorted_by_pt(inclusive_jets(ptmin));
00865 // 
00866 //     for (size_t j = 0; j < jets.size(); j++) {
00867 //        printf("%5u %7.3f %7.3f %9.3f\n",
00868 //        j,jets[j].rap(),jets[j].phi(),jets[j].perp());
00869 //     }
00870 // }
00871 
00872 //----------------------------------------------------------------------
00877 vector<int> ClusterSequence::particle_jet_indices(
00878                         const vector<PseudoJet> & jets) const {
00879 
00880   vector<int> indices(n_particles());
00881 
00882   // first label all particles as not belonging to any jets
00883   for (unsigned ipart = 0; ipart < n_particles(); ipart++) 
00884     indices[ipart] = -1;
00885 
00886   // then for each of the jets relabel its consituents as belonging to
00887   // that jet
00888   for (unsigned ijet = 0; ijet < jets.size(); ijet++) {
00889 
00890     vector<PseudoJet> jet_constituents(constituents(jets[ijet]));
00891 
00892     for (unsigned ip = 0; ip < jet_constituents.size(); ip++) {
00893       // a safe (if slightly redundant) way of getting the particle
00894       // index (for initial particles it is actually safe to assume
00895       // ipart=iclust).
00896       unsigned iclust = jet_constituents[ip].cluster_hist_index();
00897       unsigned ipart = history()[iclust].jetp_index;
00898       indices[ipart] = ijet;
00899     }
00900   }
00901 
00902   return indices;
00903 }
00904 
00905 
00906 //----------------------------------------------------------------------
00907 // recursive routine that adds on constituents of jet to the subjet_vector
00908 void ClusterSequence::add_constituents (
00909            const PseudoJet & jet, vector<PseudoJet> & subjet_vector) const {
00910   // find out position in cluster history
00911   int i = jet.cluster_hist_index();
00912   int parent1 = _history[i].parent1;
00913   int parent2 = _history[i].parent2;
00914 
00915   if (parent1 == InexistentParent) {
00916     // It is an original particle (labelled by its parent having value
00917     // InexistentParent), therefore add it on to the subjet vector
00918     // Note: we add the initial particle and not simply 'jet' so that
00919     //       calling add_constituents with a subtracted jet containing
00920     //       only one particle will work.
00921     subjet_vector.push_back(_jets[i]);
00922     return;
00923   } 
00924 
00925   // add parent 1
00926   add_constituents(_jets[_history[parent1].jetp_index], subjet_vector);
00927 
00928   // see if parent2 is a real jet; if it is then add its constituents
00929   if (parent2 != BeamJet) {
00930     add_constituents(_jets[_history[parent2].jetp_index], subjet_vector);
00931   }
00932 }
00933 
00934 
00935 
00936 //----------------------------------------------------------------------
00937 // initialise the history in a standard way
00938 void ClusterSequence::_add_step_to_history (
00939                const int & step_number, const int & parent1, 
00940                const int & parent2, const int & jetp_index,
00941                const double & dij) {
00942 
00943   history_element element;
00944   element.parent1 = parent1;
00945   element.parent2 = parent2;
00946   element.jetp_index = jetp_index;
00947   element.child = Invalid;
00948   element.dij   = dij;
00949   element.max_dij_so_far = max(dij,_history[_history.size()-1].max_dij_so_far);
00950   _history.push_back(element);
00951 
00952   int local_step = _history.size()-1;
00953   assert(local_step == step_number);
00954 
00955   assert(parent1 >= 0);
00956   _history[parent1].child = local_step;
00957   if (parent2 >= 0) {_history[parent2].child = local_step;}
00958 
00959   // get cross-referencing right from PseudoJets
00960   if (jetp_index != Invalid) {
00961     assert(jetp_index >= 0);
00962     //cout << _jets.size() <<" "<<jetp_index<<"\n";
00963     _jets[jetp_index].set_cluster_hist_index(local_step);
00964   }
00965 
00966   if (_writeout_combinations) {
00967     cout << local_step << ": " 
00968          << parent1 << " with " << parent2
00969          << "; y = "<< dij<<endl;
00970   }
00971 
00972 }
00973 
00974 
00975 
00976 
00977 //======================================================================
00978 // Return an order in which to read the history such that _history[order[i]] 
00979 // will always correspond to the same set of consituent particles if 
00980 // two branching histories are equivalent in terms of the particles
00981 // contained in any given pseudojet.
00982 vector<int> ClusterSequence::unique_history_order() const {
00983 
00984   // first construct an array that will tell us the lowest constituent
00985   // of a given jet -- this will always be one of the original
00986   // particles, whose order is well defined and so will help us to
00987   // follow the tree in a unique manner.
00988   valarray<int> lowest_constituent(_history.size());
00989   int hist_n = _history.size();
00990   lowest_constituent = hist_n; // give it a large number
00991   for (int i = 0; i < hist_n; i++) {
00992     // sets things up for the initial partons
00993     lowest_constituent[i] = min(lowest_constituent[i],i); 
00994     // propagates them through to the children of this parton
00995     if (_history[i].child > 0) lowest_constituent[_history[i].child] 
00996       = min(lowest_constituent[_history[i].child],lowest_constituent[i]);
00997   }
00998 
00999   // establish an array for what we have and have not extracted so far
01000   valarray<bool> extracted(_history.size()); extracted = false;
01001   vector<int> unique_tree;
01002   unique_tree.reserve(_history.size());
01003 
01004   // now work our way through the tree
01005   for (unsigned i = 0; i < n_particles(); i++) {
01006     if (!extracted[i]) {
01007       unique_tree.push_back(i);
01008       extracted[i] = true;
01009       _extract_tree_children(i, extracted, lowest_constituent, unique_tree);
01010     }
01011   }
01012 
01013   return unique_tree;
01014 }
01015 
01016 //======================================================================
01017 // helper for unique_history_order
01018 void ClusterSequence::_extract_tree_children(
01019        int position, 
01020        valarray<bool> & extracted, 
01021        const valarray<int> & lowest_constituent,
01022        vector<int> & unique_tree) const {
01023   if (!extracted[position]) {
01024     // that means we may have unidentified parents around, so go and
01025     // collect them (extracted[position]) will then be made true)
01026     _extract_tree_parents(position,extracted,lowest_constituent,unique_tree);
01027   } 
01028   
01029   // now look after the children...
01030   int child = _history[position].child;
01031   if (child  >= 0) _extract_tree_children(child,extracted,lowest_constituent,unique_tree);
01032 }
01033 
01034 
01035 //======================================================================
01036 // return the list of unclustered particles
01037 vector<PseudoJet> ClusterSequence::unclustered_particles() const {
01038   vector<PseudoJet> unclustered;
01039   for (unsigned i = 0; i < n_particles() ; i++) {
01040     if (_history[i].child == Invalid) 
01041       unclustered.push_back(_jets[_history[i].jetp_index]);
01042   }
01043   return unclustered;
01044 }
01045 
01046 
01047 
01048 //======================================================================
01049 // helper for unique_history_order
01050 void ClusterSequence::_extract_tree_parents(
01051        int position, 
01052        valarray<bool> & extracted, 
01053        const valarray<int> & lowest_constituent,
01054        vector<int> & unique_tree) const {
01055 
01056   if (!extracted[position]) {
01057     int parent1 = _history[position].parent1;
01058     int parent2 = _history[position].parent2;
01059     // where relevant order parents so that we will first treat the
01060     // one containing the smaller "lowest_constituent"
01061     if (parent1 >= 0 && parent2 >= 0) {
01062       if (lowest_constituent[parent1] > lowest_constituent[parent2]) 
01063         swap(parent1, parent2);
01064     }
01065     // then actually run through the parents to extract the constituents...
01066     if (parent1 >= 0 && !extracted[parent1]) 
01067       _extract_tree_parents(parent1,extracted,lowest_constituent,unique_tree);
01068     if (parent2 >= 0 && !extracted[parent2]) 
01069       _extract_tree_parents(parent2,extracted,lowest_constituent,unique_tree);
01070     // finally declare this position to be accounted for and push it
01071     // onto our list.
01072     unique_tree.push_back(position);
01073     extracted[position] = true;
01074   }
01075 }
01076 
01077 
01078 //======================================================================
01082 void ClusterSequence::_do_ij_recombination_step(
01083                                const int & jet_i, const int & jet_j, 
01084                                const double & dij, 
01085                                int & newjet_k) {
01086 
01087   // create the new jet by recombining the first two
01088   PseudoJet newjet;
01089   _jet_def.recombiner()->recombine(_jets[jet_i], _jets[jet_j], newjet);
01090   _jets.push_back(newjet);
01091   // original version...
01092   //_jets.push_back(_jets[jet_i] + _jets[jet_j]);
01093 
01094   // get its index
01095   newjet_k = _jets.size()-1;
01096 
01097   // get history index
01098   int newstep_k = _history.size();
01099   // and provide jet with the info
01100   _jets[newjet_k].set_cluster_hist_index(newstep_k);
01101 
01102   // finally sort out the history 
01103   int hist_i = _jets[jet_i].cluster_hist_index();
01104   int hist_j = _jets[jet_j].cluster_hist_index();
01105 
01106   _add_step_to_history(newstep_k, min(hist_i, hist_j), max(hist_i,hist_j),
01107                        newjet_k, dij);
01108 
01109 }
01110 
01111 
01112 //======================================================================
01115 void ClusterSequence::_do_iB_recombination_step(
01116                                   const int & jet_i, const double & diB) {
01117   // get history index
01118   int newstep_k = _history.size();
01119 
01120   // recombine the jet with the beam
01121   _add_step_to_history(newstep_k,_jets[jet_i].cluster_hist_index(),BeamJet,
01122                        Invalid, diB);
01123 
01124 }
01125 
01126 FASTJET_END_NAMESPACE
01127 

Generated on 26 Feb 2010 for fastjet by  doxygen 1.6.1