CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/GeneratorInterface/LHEInterface/src/JetClustering.cc

Go to the documentation of this file.
00001 #include <vector>
00002 #include <string>
00003 
00004 #include <fastjet/JetDefinition.hh>
00005 #include <fastjet/PseudoJet.hh>
00006 #include <fastjet/ClusterSequence.hh>
00007 #include <fastjet/SISConePlugin.hh>
00008 
00009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00010 #include "FWCore/Utilities/interface/Exception.h"
00011 
00012 #include "GeneratorInterface/LHEInterface/interface/JetInput.h"
00013 #include "GeneratorInterface/LHEInterface/interface/JetClustering.h"
00014 
00015 namespace lhef {
00016 
00017 class JetClustering::Algorithm {
00018     public:
00019         typedef JetClustering::Jet              Jet;
00020         typedef JetClustering::ParticleVector   ParticleVector;
00021 
00022         Algorithm(const edm::ParameterSet &params, double jetPtMin) :
00023                 jetPtMin(jetPtMin) {}
00024         virtual ~Algorithm() {}
00025 
00026         virtual std::vector<Jet> operator () (
00027                                 const ParticleVector &input) const = 0;
00028 
00029         double getJetPtMin() const { return jetPtMin; }
00030 
00031     private:
00032         double  jetPtMin;
00033 };
00034 
00035 namespace {
00036         class FastJetAlgorithmWrapper : public JetClustering::Algorithm {
00037             public:
00038                 FastJetAlgorithmWrapper(const edm::ParameterSet &params,
00039                                         double jetPtMin);
00040                 ~FastJetAlgorithmWrapper() {}
00041 
00042             protected:
00043                 std::vector<Jet> operator () (
00044                                 const ParticleVector &input) const;
00045 
00046                 std::auto_ptr<fastjet::JetDefinition::Plugin>   plugin;
00047                 std::auto_ptr<fastjet::JetDefinition>           jetDefinition;
00048         };
00049 
00050         class KtAlgorithm : public FastJetAlgorithmWrapper {
00051             public:
00052                 KtAlgorithm(const edm::ParameterSet &params,
00053                             double jetPtMin);
00054                 ~KtAlgorithm() {}
00055         };
00056 
00057         class SISConeAlgorithm : public FastJetAlgorithmWrapper {
00058             public:
00059                 SISConeAlgorithm(const edm::ParameterSet &params,
00060                                  double jetPtMin);
00061                 ~SISConeAlgorithm() {}
00062 
00063             private:
00064                 static fastjet::SISConePlugin::SplitMergeScale
00065                                         getScale(const std::string &name);
00066         };
00067 } // anonymous namespace
00068 
00069 FastJetAlgorithmWrapper::FastJetAlgorithmWrapper(
00070                         const edm::ParameterSet &params, double jetPtMin) :
00071         JetClustering::Algorithm(params, jetPtMin)
00072 {
00073 }
00074 
00075 std::vector<JetClustering::Jet> FastJetAlgorithmWrapper::operator () (
00076                                         const ParticleVector &input) const
00077 {
00078         if (input.empty())
00079                 return std::vector<JetClustering::Jet>();
00080 
00081         std::vector<fastjet::PseudoJet> jfInput;
00082         jfInput.reserve(input.size());
00083         for(ParticleVector::const_iterator iter = input.begin();
00084             iter != input.end(); ++iter) {
00085                 jfInput.push_back(fastjet::PseudoJet(
00086                         (*iter)->momentum().px(), (*iter)->momentum().py(),
00087                         (*iter)->momentum().pz(), (*iter)->momentum().e()));
00088                 jfInput.back().set_user_index(iter - input.begin());
00089         }
00090 
00091         fastjet::ClusterSequence sequence(jfInput, *jetDefinition);
00092         std::vector<fastjet::PseudoJet> jets =
00093                                 sequence.inclusive_jets(getJetPtMin());
00094 
00095         std::vector<Jet> result;
00096         result.reserve(jets.size());
00097         ParticleVector constituents;
00098         for(std::vector<fastjet::PseudoJet>::const_iterator iter = jets.begin();
00099             iter != jets.end(); ++iter) {
00100                 std::vector<fastjet::PseudoJet> fjConstituents =
00101                                         sequence.constituents(*iter);
00102                 unsigned int size = fjConstituents.size();
00103                 constituents.resize(size);
00104                 for(unsigned int i = 0; i < size; i++)
00105                         constituents[i] =
00106                                 input[fjConstituents[i].user_index()];
00107 
00108                 result.push_back(
00109                         Jet(iter->px(), iter->py(), iter->pz(), iter->E(),
00110                             constituents));
00111         }
00112 
00113         return result;
00114 }
00115 
00116 KtAlgorithm::KtAlgorithm(const edm::ParameterSet &params, double jetPtMin) :
00117         FastJetAlgorithmWrapper(params, jetPtMin)
00118 {
00119         jetDefinition.reset(new fastjet::JetDefinition(
00120                                 fastjet::kt_algorithm,
00121                                 params.getParameter<double>("ktRParam"),
00122                                 fastjet::Best));
00123 }
00124 
00125 SISConeAlgorithm::SISConeAlgorithm(
00126                         const edm::ParameterSet &params, double jetPtMin) :
00127         FastJetAlgorithmWrapper(params, jetPtMin)
00128 {
00129         std::string splitMergeScale = 
00130                         params.getParameter<std::string>("splitMergeScale");
00131         fastjet::SISConePlugin::SplitMergeScale scale;
00132 
00133         if (splitMergeScale == "pt")
00134                 scale = fastjet::SISConePlugin::SM_pt;
00135         else if (splitMergeScale == "Et")
00136                 scale = fastjet::SISConePlugin::SM_Et;
00137         else if (splitMergeScale == "mt")
00138                 scale = fastjet::SISConePlugin::SM_mt;
00139         else if (splitMergeScale == "pttilde")
00140                 scale = fastjet::SISConePlugin::SM_pttilde;
00141         else
00142                 throw cms::Exception("Configuration") 
00143                         << "JetClustering SISCone scale '" << splitMergeScale
00144                         << "' unknown." << std::endl;
00145 
00146         plugin.reset(new fastjet::SISConePlugin(
00147                         params.getParameter<double>("coneRadius"), 
00148                         params.getParameter<double>("coneOverlapThreshold"),
00149                         params.getParameter<int>("maxPasses"),
00150                         params.getParameter<double>("protojetPtMin"),
00151                         params.getParameter<bool>("caching"),
00152                         scale));
00153         jetDefinition.reset(new fastjet::JetDefinition(plugin.get()));
00154 }
00155 
00156 JetClustering::JetClustering(const edm::ParameterSet &params)
00157 {
00158         double jetPtMin = params.getParameter<double>("jetPtMin");
00159         init(params, jetPtMin);
00160 }
00161 
00162 JetClustering::JetClustering(const edm::ParameterSet &params,
00163                              double jetPtMin)
00164 {
00165         init(params, jetPtMin);
00166 }
00167 
00168 JetClustering::~JetClustering()
00169 {
00170 }
00171 
00172 void JetClustering::init(const edm::ParameterSet &params, double jetPtMin)
00173 {
00174         edm::ParameterSet algoParams =
00175                         params.getParameter<edm::ParameterSet>("algorithm");
00176         std::string algoName = algoParams.getParameter<std::string>("name");
00177 
00178         if (algoName == "KT")
00179                 jetAlgo.reset(new KtAlgorithm(algoParams, jetPtMin));
00180         else if (algoName == "SISCone")
00181                 jetAlgo.reset(new SISConeAlgorithm(algoParams, jetPtMin));
00182         else
00183                 throw cms::Exception("Configuration")
00184                         << "JetClustering algorithm \"" << algoName
00185                         << "\" unknown." << std::endl;
00186 }
00187 
00188 double JetClustering::getJetPtMin() const
00189 {
00190         return jetAlgo->getJetPtMin();
00191 }
00192 
00193 std::vector<JetClustering::Jet> JetClustering::operator () (
00194                                         const ParticleVector &input) const
00195 {
00196         return (*jetAlgo)(input);
00197 }
00198 
00199 } // namespace lhef