FastJet 3.0alpha3
PseudoJet.cc
00001 //STARTHEADER
00002 // $Id: PseudoJet.cc 2215 2011-06-02 21:32:36Z soyez $
00003 //
00004 // Copyright (c) 2005-2010, Matteo Cacciari, Gavin Salam and Gregory Soyez
00005 //
00006 //----------------------------------------------------------------------
00007 // This file is part of FastJet.
00008 //
00009 //  FastJet is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU General Public License as published by
00011 //  the Free Software Foundation; either version 2 of the License, or
00012 //  (at your option) any later version.
00013 //
00014 //  The algorithms that underlie FastJet have required considerable
00015 //  development and are described in hep-ph/0512210. If you use
00016 //  FastJet as part of work towards a scientific publication, please
00017 //  include a citation to the FastJet paper.
00018 //
00019 //  FastJet is distributed in the hope that it will be useful,
00020 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 //  GNU General Public License for more details.
00023 //
00024 //  You should have received a copy of the GNU General Public License
00025 //  along with FastJet; if not, write to the Free Software
00026 //  Foundation, Inc.:
00027 //      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 //----------------------------------------------------------------------
00029 //ENDHEADER
00030 
00031 
00032 #include "fastjet/Error.hh"
00033 #include "fastjet/PseudoJet.hh"
00034 #include "fastjet/ClusterSequence.hh"
00035 #include "fastjet/ClusterSequenceAreaBase.hh"
00036 #include "fastjet/CompositeJetStructure.hh"
00037 #include<valarray>
00038 #include<iostream>
00039 #include<sstream>
00040 #include<cmath>
00041 #include<algorithm>
00042 #include <cstdarg>
00043 
00044 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00045 
00046 using namespace std;
00047 
00048 
00049 //----------------------------------------------------------------------
00050 // another constructor...
00051 PseudoJet::PseudoJet(const double px, const double py, const double pz, const double E) {
00052   
00053   _E  = E ;
00054   _px = px;
00055   _py = py;
00056   _pz = pz;
00057 
00058   this->_finish_init();
00059 
00060   // some default values for the history and user indices
00061   _reset_indices();
00062 
00063 }
00064 
00065 
00066 //----------------------------------------------------------------------
00067 /// do standard end of initialisation
00068 void PseudoJet::_finish_init () {
00069   _kt2 = this->px()*this->px() + this->py()*this->py();
00070   _phi = pseudojet_invalid_phi;
00071 }
00072 
00073 //----------------------------------------------------------------------
00074 void PseudoJet::_set_rap_phi() const {
00075 
00076   if (_kt2 == 0.0) {
00077     _phi = 0.0; } 
00078   else {
00079     _phi = atan2(this->py(),this->px());
00080   }
00081   if (_phi < 0.0) {_phi += twopi;}
00082   if (_phi >= twopi) {_phi -= twopi;} // can happen if phi=-|eps<1e-15|?
00083   if (this->E() == abs(this->pz()) && _kt2 == 0) {
00084     // Point has infinite rapidity -- convert that into a very large
00085     // number, but in such a way that different 0-pt momenta will have
00086     // different rapidities (so as to lift the degeneracy between
00087     // them) [this can be relevant at parton-level]
00088     double MaxRapHere = MaxRap + abs(this->pz());
00089     if (this->pz() >= 0.0) {_rap = MaxRapHere;} else {_rap = -MaxRapHere;}
00090   } else {
00091     // get the rapidity in a way that's modestly insensitive to roundoff
00092     // error when things pz,E are large (actually the best we can do without
00093     // explicit knowledge of mass)
00094     double effective_m2 = max(0.0,m2()); // force non tachyonic mass
00095     double E_plus_pz    = _E + abs(_pz); // the safer of p+, p-
00096     // p+/p- = (p+ p-) / (p-)^2 = (kt^2+m^2)/(p-)^2
00097     _rap = 0.5*log((_kt2 + effective_m2)/(E_plus_pz*E_plus_pz));
00098     if (_pz > 0) {_rap = - _rap;}
00099   }
00100 
00101 }
00102 
00103 
00104 //----------------------------------------------------------------------
00105 // return a valarray four-momentum
00106 valarray<double> PseudoJet::four_mom() const {
00107   valarray<double> mom(4);
00108   mom[0] = _px;
00109   mom[1] = _py;
00110   mom[2] = _pz;
00111   mom[3] = _E ;
00112   return mom;
00113 }
00114 
00115 //----------------------------------------------------------------------
00116 // Return the component corresponding to the specified index.
00117 // taken from CLHEP
00118 double PseudoJet::operator () (int i) const {
00119   switch(i) {
00120   case X:
00121     return px();
00122   case Y:
00123     return py();
00124   case Z:
00125     return pz();
00126   case T:
00127     return e();
00128   default:
00129     ostringstream err;
00130     err << "PseudoJet subscripting: bad index (" << i << ")";
00131     throw Error(err.str());
00132   }
00133   return 0.;
00134 }  
00135 
00136 //----------------------------------------------------------------------
00137 // return the pseudorapidity
00138 double PseudoJet::pseudorapidity() const {
00139   if (px() == 0.0 && py() ==0.0) return MaxRap;
00140   if (pz() == 0.0) return 0.0;
00141 
00142   double theta = atan(perp()/pz());
00143   if (theta < 0) theta += pi;
00144   return -log(tan(theta/2));
00145 }
00146 
00147 //----------------------------------------------------------------------
00148 // return "sum" of two pseudojets
00149 PseudoJet operator+ (const PseudoJet & jet1, const PseudoJet & jet2) {
00150   //return PseudoJet(jet1.four_mom()+jet2.four_mom());
00151   return PseudoJet(jet1.px()+jet2.px(),
00152                    jet1.py()+jet2.py(),
00153                    jet1.pz()+jet2.pz(),
00154                    jet1.E() +jet2.E()  );
00155 } 
00156 
00157 //----------------------------------------------------------------------
00158 // return difference of two pseudojets
00159 PseudoJet operator- (const PseudoJet & jet1, const PseudoJet & jet2) {
00160   //return PseudoJet(jet1.four_mom()-jet2.four_mom());
00161   return PseudoJet(jet1.px()-jet2.px(),
00162                    jet1.py()-jet2.py(),
00163                    jet1.pz()-jet2.pz(),
00164                    jet1.E() -jet2.E()  );
00165 } 
00166 
00167 //----------------------------------------------------------------------
00168 // return the product, coeff * jet
00169 PseudoJet operator* (double coeff, const PseudoJet & jet) {
00170   //return PseudoJet(coeff*jet.four_mom());
00171   // the following code is hopefully more efficient
00172   PseudoJet coeff_times_jet(jet);
00173   coeff_times_jet *= coeff;
00174   return coeff_times_jet;
00175 } 
00176 
00177 //----------------------------------------------------------------------
00178 // return the product, coeff * jet
00179 PseudoJet operator* (const PseudoJet & jet, double coeff) {
00180   return coeff*jet;
00181 } 
00182 
00183 //----------------------------------------------------------------------
00184 // return the ratio, jet / coeff
00185 PseudoJet operator/ (const PseudoJet & jet, double coeff) {
00186   return (1.0/coeff)*jet;
00187 } 
00188 
00189 //----------------------------------------------------------------------
00190 /// multiply the jet's momentum by the coefficient
00191 void PseudoJet::operator*=(double coeff) {
00192   _px *= coeff;
00193   _py *= coeff;
00194   _pz *= coeff;
00195   _E  *= coeff;
00196   _kt2*= coeff*coeff;
00197   // phi and rap are unchanged
00198 }
00199 
00200 //----------------------------------------------------------------------
00201 /// divide the jet's momentum by the coefficient
00202 void PseudoJet::operator/=(double coeff) {
00203   (*this) *= 1.0/coeff;
00204 }
00205 
00206 
00207 //----------------------------------------------------------------------
00208 /// add the other jet's momentum to this jet
00209 void PseudoJet::operator+=(const PseudoJet & other_jet) {
00210   _px += other_jet._px;
00211   _py += other_jet._py;
00212   _pz += other_jet._pz;
00213   _E  += other_jet._E ;
00214   _finish_init(); // we need to recalculate phi,rap,kt2
00215 }
00216 
00217 
00218 //----------------------------------------------------------------------
00219 /// subtract the other jet's momentum from this jet
00220 void PseudoJet::operator-=(const PseudoJet & other_jet) {
00221   _px -= other_jet._px;
00222   _py -= other_jet._py;
00223   _pz -= other_jet._pz;
00224   _E  -= other_jet._E ;
00225   _finish_init(); // we need to recalculate phi,rap,kt2
00226 }
00227 
00228 //----------------------------------------------------------------------
00229 bool operator==(const PseudoJet & a, const PseudoJet & b) {
00230   if (a.px() != b.px()) return false;
00231   if (a.py() != b.py()) return false;
00232   if (a.pz() != b.pz()) return false;
00233   if (a.E () != b.E ()) return false;
00234   
00235   if (a.user_index()    != b.user_index()) return false;
00236   if (a.cluster_hist_index() != b.cluster_hist_index()) return false;
00237   if (a.user_info_ptr() != b.user_info_ptr()) return false;
00238   if (a.structure_ptr() != b.structure_ptr()) return false;
00239 
00240   return true;
00241 }
00242 
00243 //----------------------------------------------------------------------
00244 // check if the jet has zero momentum
00245 bool operator==(const PseudoJet & jet, const double val) {
00246   if (val != 0) 
00247     throw Error("comparing a PseudoJet with a non-zero constant (double) is not allowed.");
00248   return (jet.px() == 0 && jet.py() == 0 && 
00249           jet.pz() == 0 && jet.E() == 0);
00250 }
00251 
00252 
00253 
00254 //----------------------------------------------------------------------
00255 /// transform this jet (given in lab) into a jet in the rest
00256 /// frame of prest
00257 //
00258 // NB: code adapted from that in herwig f77 (checked how it worked
00259 // long ago)
00260 PseudoJet & PseudoJet::boost(const PseudoJet & prest) {
00261   
00262   if (prest.px() == 0.0 && prest.py() == 0.0 && prest.pz() == 0.0) 
00263     return *this;
00264 
00265   double m = prest.m();
00266   assert(m != 0);
00267 
00268   double pf4  = (  px()*prest.px() + py()*prest.py()
00269                  + pz()*prest.pz() + E()*prest.E() )/m;
00270   double fn   = (pf4 + E()) / (prest.E() + m);
00271   _px +=  fn*prest.px();
00272   _py +=  fn*prest.py();
00273   _pz +=  fn*prest.pz();
00274   _E = pf4;
00275 
00276   _finish_init(); // we need to recalculate phi,rap,kt2
00277   return *this;
00278 }
00279 
00280 
00281 //----------------------------------------------------------------------
00282 /// transform this jet (given in the rest frame of prest) into a jet
00283 /// in the lab frame;
00284 //
00285 // NB: code adapted from that in herwig f77 (checked how it worked
00286 // long ago)
00287 PseudoJet & PseudoJet::unboost(const PseudoJet & prest) {
00288   
00289   if (prest.px() == 0.0 && prest.py() == 0.0 && prest.pz() == 0.0) 
00290     return *this;
00291 
00292   double m = prest.m();
00293   assert(m != 0);
00294 
00295   double pf4  = ( -px()*prest.px() - py()*prest.py()
00296                  - pz()*prest.pz() + E()*prest.E() )/m;
00297   double fn   = (pf4 + E()) / (prest.E() + m);
00298   _px -=  fn*prest.px();
00299   _py -=  fn*prest.py();
00300   _pz -=  fn*prest.pz();
00301   _E = pf4;
00302 
00303   _finish_init(); // we need to recalculate phi,rap,kt2
00304   return *this;
00305 }
00306 
00307 
00308 //----------------------------------------------------------------------
00309 /// returns true if the momenta of the two input jets are identical
00310 bool have_same_momentum(const PseudoJet & jeta, const PseudoJet & jetb) {
00311   return jeta.px() == jetb.px()
00312     &&   jeta.py() == jetb.py()
00313     &&   jeta.pz() == jetb.pz()
00314     &&   jeta.E()  == jetb.E();
00315 }
00316 
00317 
00318 //----------------------------------------------------------------------
00319 /// return a pseudojet with the given pt, y, phi and mass
00320 PseudoJet PtYPhiM(double pt, double y, double phi, double m) {
00321   double ptm = (m == 0) ? pt : sqrt(pt*pt+m*m);
00322   double exprap = exp(y);
00323   double pminus = ptm/exprap;
00324   double pplus  = ptm*exprap;
00325   double px = pt*cos(phi);
00326   double py = pt*sin(phi);
00327   PseudoJet mom(px,py,0.5*(pplus-pminus),0.5*(pplus+pminus));
00328   mom.set_cached_rap_phi(y,phi);
00329   return mom;
00330   //return PseudoJet(pt*cos(phi), pt*sin(phi), ptm*sinh(y), ptm*cosh(y));
00331 }
00332 
00333 
00334 //----------------------------------------------------------------------
00335 // return kt-distance between this jet and another one
00336 double PseudoJet::kt_distance(const PseudoJet & other) const {
00337   //double distance = min(this->kt2(), other.kt2());
00338   double distance = min(_kt2, other._kt2);
00339   double dphi = abs(phi() - other.phi());
00340   if (dphi > pi) {dphi = twopi - dphi;}
00341   double drap = rap() - other.rap();
00342   distance = distance * (dphi*dphi + drap*drap);
00343   return distance;
00344 }
00345 
00346 
00347 //----------------------------------------------------------------------
00348 // return squared cylinder (eta-phi) distance between this jet and another one
00349 double PseudoJet::plain_distance(const PseudoJet & other) const {
00350   double dphi = abs(phi() - other.phi());
00351   if (dphi > pi) {dphi = twopi - dphi;}
00352   double drap = rap() - other.rap();
00353   return (dphi*dphi + drap*drap);
00354 }
00355 
00356 //----------------------------------------------------------------------
00357 /// returns other.phi() - this.phi(), i.e. the phi distance to
00358 /// other, constrained to be in range -pi .. pi
00359 double PseudoJet::delta_phi_to(const PseudoJet & other) const {
00360   double dphi = other.phi() - phi();
00361   if (dphi >  pi) dphi -= twopi;
00362   if (dphi < -pi) dphi += twopi;
00363   return dphi;
00364 }
00365 
00366 
00367 string PseudoJet::description() const{
00368   // the "default" case of a PJ which does not belong to any cluster sequence
00369   if (!_structure())
00370     return "standard PseudoJet (with no associated Clustering information)";
00371   
00372   // for all the other cases, the descition comes from the structure
00373   return _structure()->description();
00374 }
00375 
00376 
00377 
00378 //----------------------------------------------------------------------
00379 //
00380 // The following methods access the associated jet structure (if any)
00381 //
00382 //----------------------------------------------------------------------
00383 
00384 
00385 //----------------------------------------------------------------------
00386 // check whether this PseudoJet has an associated parent
00387 // ClusterSequence
00388 bool PseudoJet::has_associated_cluster_sequence() const{
00389   return (_structure()) && (_structure->has_associated_cluster_sequence());
00390 }
00391 
00392 //----------------------------------------------------------------------
00393 // get a (const) pointer to the associated ClusterSequence (NULL if
00394 // inexistent)
00395 const ClusterSequence* PseudoJet::associated_cluster_sequence() const{
00396   if (! has_associated_cluster_sequence()) return NULL;
00397 
00398   return _structure->associated_cluster_sequence();
00399 }
00400 
00401 
00402 //----------------------------------------------------------------------
00403 // check whether this PseudoJet has an associated parent
00404 // ClusterSequence that is still valid
00405 bool PseudoJet::has_valid_cluster_sequence() const{
00406   return (_structure()) && (_structure->has_valid_cluster_sequence());
00407 }
00408 
00409 //----------------------------------------------------------------------
00410 // If there is a valid cluster sequence associated with this jet,
00411 // returns a pointer to it; otherwise throws an Error.
00412 //
00413 // Open question: should these errors be upgraded to classes of their
00414 // own so that they can be caught? [Maybe, but later]
00415 const ClusterSequence * PseudoJet::validated_cs() const {
00416   return validated_structure_ptr()->validated_cs();
00417 }
00418 
00419 
00420 //----------------------------------------------------------------------
00421 // set the associated structure
00422 void PseudoJet::set_structure_shared_ptr(const SharedPtr<PseudoJetStructureBase> &structure){
00423   _structure = structure;
00424 }
00425 
00426 //----------------------------------------------------------------------
00427 // return true if there is some strusture associated with this PseudoJet
00428 bool PseudoJet::has_structure() const{
00429   return _structure();
00430 }
00431 
00432 //----------------------------------------------------------------------
00433 // return a pointer to the structure (of type
00434 // PseudoJetStructureBase*) associated with this PseudoJet.
00435 //
00436 // return NULL if there is no associated structure
00437 const PseudoJetStructureBase* PseudoJet::structure_ptr() const {
00438   if (!_structure()) return NULL;
00439   return _structure();
00440 }
00441   
00442 //----------------------------------------------------------------------
00443 // return a non-const pointer to the structure (of type
00444 // PseudoJetStructureBase*) associated with this PseudoJet.
00445 //
00446 // return NULL if there is no associated structure
00447 //
00448 // Only use this if you know what you are doing. In any case,
00449 // prefer the 'structure_ptr()' (the const version) to this method,
00450 // unless you really need a write access to the PseudoJet's
00451 // underlying structure.
00452 PseudoJetStructureBase* PseudoJet::structure_non_const_ptr(){
00453   if (!_structure()) return NULL;
00454   return _structure();
00455 }
00456   
00457 //----------------------------------------------------------------------
00458 // return a pointer to the structure (of type
00459 // PseudoJetStructureBase*) associated with this PseudoJet.
00460 //
00461 // throw an error if there is no associated structure
00462 const PseudoJetStructureBase* PseudoJet::validated_structure_ptr() const {
00463   if (!_structure()) 
00464     throw Error("Trying to access the structure of a PseudoJet which has no associated structure");
00465   return _structure();
00466 }
00467   
00468 //----------------------------------------------------------------------
00469 // return a reference to the shared pointer to the
00470 // PseudoJetStructureBase associated with this PseudoJet
00471 const SharedPtr<PseudoJetStructureBase> & PseudoJet::structure_shared_ptr() const {
00472   return _structure;
00473 }
00474 
00475 
00476 //----------------------------------------------------------------------
00477 // check if it has been recombined with another PseudoJet in which
00478 // case, return its partner through the argument. Otherwise,
00479 // 'partner' is set to 0.
00480 //
00481 // false is also returned if this PseudoJet has no associated
00482 // ClusterSequence
00483 bool PseudoJet::has_partner(PseudoJet &partner) const{
00484   return validated_structure_ptr()->has_partner(*this, partner);
00485 }
00486 
00487 //----------------------------------------------------------------------
00488 // check if it has been recombined with another PseudoJet in which
00489 // case, return its child through the argument. Otherwise, 'child'
00490 // is set to 0.
00491 // 
00492 // false is also returned if this PseudoJet has no associated
00493 // ClusterSequence, with the child set to 0
00494 bool PseudoJet::has_child(PseudoJet &child) const{
00495   return validated_structure_ptr()->has_child(*this, child);
00496 }
00497 
00498 //----------------------------------------------------------------------
00499 // check if it is the product of a recombination, in which case
00500 // return the 2 parents through the 'parent1' and 'parent2'
00501 // arguments. Otherwise, set these to 0.
00502 //
00503 // false is also returned if this PseudoJet has no parent
00504 // ClusterSequence
00505 bool PseudoJet::has_parents(PseudoJet &parent1, PseudoJet &parent2) const{
00506   return validated_structure_ptr()->has_parents(*this, parent1, parent2);
00507 }
00508 
00509 //----------------------------------------------------------------------
00510 // check if the current PseudoJet contains the one passed as
00511 // argument
00512 //
00513 // false is also returned if this PseudoJet has no associated
00514 // ClusterSequence.
00515 bool PseudoJet::contains(const PseudoJet &constituent) const{
00516   return validated_structure_ptr()->object_in_jet(constituent, *this);
00517 }
00518 
00519 //----------------------------------------------------------------------
00520 // check if the current PseudoJet is contained the one passed as
00521 // argument
00522 //
00523 // false is also returned if this PseudoJet has no associated
00524 // ClusterSequence
00525 bool PseudoJet::is_inside(const PseudoJet &jet) const{
00526   return validated_structure_ptr()->object_in_jet(*this, jet);
00527 }
00528 
00529 
00530 //----------------------------------------------------------------------
00531 // returns true if the PseudoJet has constituents
00532 bool PseudoJet::has_constituents() const{
00533   return (_structure()) && (_structure->has_constituents());
00534 }
00535 
00536 //----------------------------------------------------------------------
00537 // retrieve the constituents.
00538 vector<PseudoJet> PseudoJet::constituents() const{
00539   return validated_structure_ptr()->constituents(*this);
00540 }
00541 
00542 
00543 //----------------------------------------------------------------------
00544 // returns true if the PseudoJet has support for exclusive subjets
00545 bool PseudoJet::has_exclusive_subjets() const{
00546   return (_structure()) && (_structure->has_exclusive_subjets());
00547 }
00548 
00549 //----------------------------------------------------------------------
00550 // return a vector of all subjets of the current jet (in the sense
00551 // of the exclusive algorithm) that would be obtained when running
00552 // the algorithm with the given dcut. 
00553 //
00554 // Time taken is O(m ln m), where m is the number of subjets that
00555 // are found. If m gets to be of order of the total number of
00556 // constituents in the jet, this could be substantially slower than
00557 // just getting that list of constituents.
00558 //
00559 // an Error is thrown if this PseudoJet has no currently valid
00560 // associated ClusterSequence
00561 std::vector<PseudoJet> PseudoJet::exclusive_subjets (const double & dcut) const {
00562   return validated_structure_ptr()->exclusive_subjets(*this, dcut);
00563 }
00564 
00565 //----------------------------------------------------------------------
00566 // return the size of exclusive_subjets(...); still n ln n with same
00567 // coefficient, but marginally more efficient than manually taking
00568 // exclusive_subjets.size()
00569 //
00570 // an Error is thrown if this PseudoJet has no currently valid
00571 // associated ClusterSequence
00572 int PseudoJet::n_exclusive_subjets(const double & dcut) const {
00573   return validated_structure_ptr()->n_exclusive_subjets(*this, dcut);
00574 }
00575 
00576 //----------------------------------------------------------------------
00577 // return the list of subjets obtained by unclustering the supplied
00578 // jet down to n subjets (or all constituents if there are fewer
00579 // than n).
00580 //
00581 // requires n ln n time
00582 //
00583 // an Error is thrown if this PseudoJet has no currently valid
00584 // associated ClusterSequence
00585 std::vector<PseudoJet> PseudoJet::exclusive_subjets (int nsub) const {
00586   return validated_structure_ptr()->exclusive_subjets(*this, nsub);
00587 }
00588 
00589 //----------------------------------------------------------------------
00590 // return the dij that was present in the merging nsub+1 -> nsub 
00591 // subjets inside this jet.
00592 //
00593 // an Error is thrown if this PseudoJet has no currently valid
00594 // associated ClusterSequence
00595 double PseudoJet::exclusive_subdmerge(int nsub) const {
00596   return validated_structure_ptr()->exclusive_subdmerge(*this, nsub);
00597 }
00598 
00599 //----------------------------------------------------------------------
00600 // return the maximum dij that occurred in the whole event at the
00601 // stage that the nsub+1 -> nsub merge of subjets occurred inside 
00602 // this jet.
00603 //
00604 // an Error is thrown if this PseudoJet has no currently valid
00605 // associated ClusterSequence
00606 double PseudoJet::exclusive_subdmerge_max(int nsub) const {
00607   return validated_structure_ptr()->exclusive_subdmerge_max(*this, nsub);
00608 }
00609 
00610 
00611 // returns true if a jet has pieces
00612 //
00613 // By default a single particle or a jet coming from a
00614 // ClusterSequence have no pieces and this methos will return false.
00615 bool PseudoJet::has_pieces() const{
00616   return ((_structure()) && (_structure->has_pieces(*this)));
00617 }
00618 
00619 // retrieve the pieces that make up the jet. 
00620 //
00621 // By default a jet does not have pieces.
00622 // If the underlying interface supports "pieces" retrieve the
00623 // pieces from there.
00624 std::vector<PseudoJet> PseudoJet::pieces() const{
00625   return validated_structure_ptr()->pieces(*this);
00626   // if (!has_pieces())
00627   //   throw Error("Trying to retrieve the pieces of a PseudoJet that has no support for pieces.");
00628   //
00629   // return _structure->pieces(*this);
00630 }
00631 
00632 
00633 //----------------------------------------------------------------------
00634 // the following ones require a computation of the area in the
00635 // associated ClusterSequence (See ClusterSequenceAreaBase for details)
00636 //----------------------------------------------------------------------
00637 
00638 //----------------------------------------------------------------------
00639 // if possible, return a valid ClusterSequenceAreaBase pointer; otherwise
00640 // throw an error
00641 const ClusterSequenceAreaBase * PseudoJet::validated_csab() const {
00642   const ClusterSequenceAreaBase *csab = dynamic_cast<const ClusterSequenceAreaBase*>(validated_cs());
00643   if (csab == NULL) throw Error("you requested jet-area related information, but the PseudoJet does not have associated area information.");
00644   return csab;
00645 }
00646 
00647 
00648 //----------------------------------------------------------------------
00649 // check if it has a defined area
00650 bool PseudoJet::has_area() const{
00651   //if (! has_associated_cluster_sequence()) return false;
00652   if (! has_structure()) return false;
00653   return (validated_structure_ptr()->has_area() != 0);
00654 }
00655 
00656 //----------------------------------------------------------------------
00657 // return the jet (scalar) area.
00658 // throw an Error if there is no support for area in the associated CS
00659 double PseudoJet::area() const{
00660   return validated_structure_ptr()->area(*this);
00661 }
00662 
00663 //----------------------------------------------------------------------
00664 // return the error (uncertainty) associated with the determination
00665 // of the area of this jet.
00666 // throws an Error if there is no support for area in the associated CS
00667 double PseudoJet::area_error() const{
00668   return validated_structure_ptr()->area_error(*this);
00669 }
00670 
00671 //----------------------------------------------------------------------
00672 // return the jet 4-vector area
00673 // throws an Error if there is no support for area in the associated CS
00674 PseudoJet PseudoJet::area_4vector() const{
00675   return validated_structure_ptr()->area_4vector(*this);
00676 }
00677 
00678 //----------------------------------------------------------------------
00679 // true if this jet is made exclusively of ghosts
00680 // throws an Error if there is no support for area in the associated CS
00681 bool PseudoJet::is_pure_ghost() const{
00682   return validated_structure_ptr()->is_pure_ghost(*this);
00683 }
00684 
00685 
00686 //----------------------------------------------------------------------
00687 //
00688 // end of the methods accessing the information in the associated
00689 // Cluster Sequence
00690 //
00691 //----------------------------------------------------------------------
00692 
00693 //----------------------------------------------------------------------
00694 /// provide a meaningful error message for InexistentUserInfo
00695 PseudoJet::InexistentUserInfo::InexistentUserInfo() : Error("you attempted to perform a dynamic cast of a PseudoJet's extra info, but the extra info pointer was null")
00696 {}
00697 
00698 
00699 //----------------------------------------------------------------------
00700 // sort the indices so that values[indices[0..n-1]] is sorted
00701 // into increasing order 
00702 void sort_indices(vector<int> & indices, 
00703                          const vector<double> & values) {
00704   IndexedSortHelper index_sort_helper(&values);
00705   sort(indices.begin(), indices.end(), index_sort_helper);
00706 }
00707 
00708 
00709 
00710 //----------------------------------------------------------------------
00711 /// given a vector of values with a one-to-one correspondence with the
00712 /// vector of objects, sort objects into an order such that the
00713 /// associated values would be in increasing order
00714 template<class T> vector<T>  objects_sorted_by_values(
00715                        const vector<T> & objects, 
00716                        const vector<double> & values) {
00717 
00718   assert(objects.size() == values.size());
00719 
00720   // get a vector of indices
00721   vector<int> indices(values.size());
00722   for (size_t i = 0; i < indices.size(); i++) {indices[i] = i;}
00723   
00724   // sort the indices
00725   sort_indices(indices, values);
00726   
00727   // copy the objects 
00728   vector<T> objects_sorted(objects.size());
00729   
00730   // place the objects in the correct order
00731   for (size_t i = 0; i < indices.size(); i++) {
00732     objects_sorted[i] = objects[indices[i]];
00733   }
00734 
00735   return objects_sorted;
00736 }
00737 
00738 //----------------------------------------------------------------------
00739 /// return a vector of jets sorted into decreasing kt2
00740 vector<PseudoJet> sorted_by_pt(const vector<PseudoJet> & jets) {
00741   vector<double> minus_kt2(jets.size());
00742   for (size_t i = 0; i < jets.size(); i++) {minus_kt2[i] = -jets[i].kt2();}
00743   return objects_sorted_by_values(jets, minus_kt2);
00744 }
00745 
00746 //----------------------------------------------------------------------
00747 /// return a vector of jets sorted into increasing rapidity
00748 vector<PseudoJet> sorted_by_rapidity(const vector<PseudoJet> & jets) {
00749   vector<double> rapidities(jets.size());
00750   for (size_t i = 0; i < jets.size(); i++) {rapidities[i] = jets[i].rap();}
00751   return objects_sorted_by_values(jets, rapidities);
00752 }
00753 
00754 //----------------------------------------------------------------------
00755 /// return a vector of jets sorted into decreasing energy
00756 vector<PseudoJet> sorted_by_E(const vector<PseudoJet> & jets) {
00757   vector<double> energies(jets.size());
00758   for (size_t i = 0; i < jets.size(); i++) {energies[i] = -jets[i].E();}
00759   return objects_sorted_by_values(jets, energies);
00760 }
00761 
00762 //----------------------------------------------------------------------
00763 /// return a vector of jets sorted into increasing pz
00764 vector<PseudoJet> sorted_by_pz(const vector<PseudoJet> & jets) {
00765   vector<double> pz(jets.size());
00766   for (size_t i = 0; i < jets.size(); i++) {pz[i] = jets[i].pz();}
00767   return objects_sorted_by_values(jets, pz);
00768 }
00769 
00770 
00771 
00772 //-------------------------------------------------------------------------------
00773 // helper functions to build a jet made of pieces
00774 //-------------------------------------------------------------------------------
00775 
00776 // build a "CompositeJet" from the vector of its pieces
00777 //
00778 // In this case, E-scheme recombination is assumed to compute the
00779 // total momentum
00780 PseudoJet join(const vector<PseudoJet> & pieces){
00781   // compute the total momentum
00782   //--------------------------------------------------
00783   PseudoJet result;  // automatically initialised to 0
00784   for (unsigned int i=0; i<pieces.size(); i++)
00785     result += pieces[i];
00786 
00787   // attach a CompositeJetStructure to the result
00788   //--------------------------------------------------
00789   CompositeJetStructure *cj_struct = new CompositeJetStructure(pieces);
00790 
00791   result.set_structure_shared_ptr(SharedPtr<PseudoJetStructureBase>(cj_struct));
00792 
00793   return result;
00794 }
00795 
00796 // build a "CompositeJet" from a single PseudoJet
00797 PseudoJet join(const PseudoJet & j1){
00798   return join(vector<PseudoJet>(1,j1));
00799 }
00800 
00801 // build a "CompositeJet" from two PseudoJet
00802 PseudoJet join(const PseudoJet & j1, const PseudoJet & j2){
00803   vector<PseudoJet> pieces;
00804   pieces.push_back(j1);
00805   pieces.push_back(j2);
00806   return join(pieces);
00807 }
00808 
00809 // build a "CompositeJet" from 3 PseudoJet
00810 PseudoJet join(const PseudoJet & j1, const PseudoJet & j2, const PseudoJet & j3){
00811   vector<PseudoJet> pieces;
00812   pieces.push_back(j1);
00813   pieces.push_back(j2);
00814   pieces.push_back(j3);
00815   return join(pieces);
00816 }
00817 
00818 // build a "CompositeJet" from 4 PseudoJet
00819 PseudoJet join(const PseudoJet & j1, const PseudoJet & j2, const PseudoJet & j3, const PseudoJet & j4){
00820   vector<PseudoJet> pieces;
00821   pieces.push_back(j1);
00822   pieces.push_back(j2);
00823   pieces.push_back(j3);
00824   pieces.push_back(j4);
00825   return join(pieces);
00826 }
00827 
00828 
00829 
00830 
00831 FASTJET_END_NAMESPACE
00832 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends