CMS 3D CMS Logo

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

Go to the documentation of this file.
00001 #include <string>
00002 #include <memory>
00003 
00004 #include <boost/shared_ptr.hpp>
00005 #include <sigc++/signal.h>
00006 
00007 #include <HepMC/GenEvent.h>
00008 #include <HepMC/SimpleVector.h>
00009 
00010 #include "FWCore/Framework/interface/one/EDFilter.h"
00011 #include "FWCore/Framework/interface/MakerMacros.h"
00012 #include "FWCore/Framework/interface/Event.h"
00013 #include "FWCore/Framework/interface/Run.h"
00014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 
00017 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00018 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
00019 #include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
00020 
00021 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
00022 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
00023 
00024 #include "GeneratorInterface/LHEInterface/interface/LHERunInfo.h"
00025 #include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
00026 #include "GeneratorInterface/LHEInterface/interface/Hadronisation.h"
00027 #include "GeneratorInterface/LHEInterface/interface/JetMatching.h"
00028 #include "GeneratorInterface/LHEInterface/interface/JetMatchingMLM.h"
00029 #include "GeneratorInterface/LHEInterface/interface/BranchingRatios.h"
00030 
00031 using namespace lhef;
00032 
00033 class LHEProducer : public edm::one::EDFilter<edm::EndRunProducer,
00034                                               edm::one::WatchRuns> {
00035     public:
00036         explicit LHEProducer(const edm::ParameterSet &params);
00037         virtual ~LHEProducer();
00038 
00039     protected:
00040         virtual void beginJob() override;
00041         virtual void endJob() override;
00042         virtual void beginRun(edm::Run const& run, const edm::EventSetup &es) override;
00043         virtual void endRun(edm::Run const&run, const edm::EventSetup &es) override;
00044         virtual void endRunProduce(edm::Run &run, const edm::EventSetup &es) override;
00045         virtual bool filter(edm::Event &event, const edm::EventSetup &es) override;
00046 
00047     private:
00048         double matching(const HepMC::GenEvent *event, bool shower) const;
00049 
00050         bool showeredEvent(const boost::shared_ptr<HepMC::GenEvent> &event);
00051         void onInit();
00052         void onBeforeHadronisation();
00053 
00054         unsigned int                    eventsToPrint;
00055         std::vector<int>                removeResonances;
00056         std::auto_ptr<Hadronisation>    hadronisation;
00057         std::auto_ptr<JetMatching>      jetMatching;
00058 
00059         double                          extCrossSect;
00060         double                          extFilterEff;
00061         bool                            matchSummary;
00062 
00063         boost::shared_ptr<LHEEvent>     partonLevel;
00064         boost::shared_ptr<LHERunInfo>   runInfo;
00065         unsigned int                    index;
00066         bool                            matchingDone;
00067         double                          weight;
00068         BranchingRatios                 branchingRatios;
00069 };
00070 
00071 LHEProducer::LHEProducer(const edm::ParameterSet &params) :
00072         eventsToPrint(params.getUntrackedParameter<unsigned int>("eventsToPrint", 0)),
00073         extCrossSect(params.getUntrackedParameter<double>("crossSection", -1.0)),
00074         extFilterEff(params.getUntrackedParameter<double>("filterEfficiency", -1.0)),
00075         matchSummary(false)
00076 {
00077         hadronisation = Hadronisation::create(
00078                 params.getParameter<edm::ParameterSet>("hadronisation"));
00079 
00080         if (params.exists("removeResonances"))
00081                 removeResonances =
00082                         params.getParameter<std::vector<int> >(
00083                                                         "removeResonances");
00084 
00085         std::set<std::string> matchingCapabilities;
00086         if (params.exists("jetMatching")) {
00087                 edm::ParameterSet jetParams =
00088                         params.getUntrackedParameter<edm::ParameterSet>(
00089                                                                 "jetMatching");
00090                 jetMatching = JetMatching::create(jetParams);
00091 
00092                 matchingCapabilities = jetMatching->capabilities();
00093                 hadronisation->matchingCapabilities(matchingCapabilities);
00094         }
00095 
00096         produces<edm::HepMCProduct>();
00097         produces<GenEventInfoProduct>();
00098         produces<GenRunInfoProduct, edm::InRun>();
00099 
00100         if (jetMatching.get()) {
00101                 if (params.getUntrackedParameter<bool>(
00102                                         "preferShowerVetoCallback", true))
00103                         hadronisation->onShoweredEvent().connect(
00104                                 sigc::mem_fun(*this,
00105                                               &LHEProducer::showeredEvent));
00106                 hadronisation->onInit().connect(
00107                                 sigc::mem_fun(*this, &LHEProducer::onInit));
00108                 hadronisation->onBeforeHadronisation().connect(
00109                         sigc::mem_fun(*this,
00110                                       &LHEProducer::onBeforeHadronisation));
00111 
00112                 matchSummary = matchingCapabilities.count("matchSummary");
00113                 if (matchSummary) {
00114                         produces< std::vector<double> >("matchDeltaR");
00115                         produces< std::vector<double> >("matchDeltaPRel");
00116                 }
00117         }
00118 
00119         // force total branching ratio for QCD/QED to 1
00120         for(int i = 0; i < 6; i++)
00121                 branchingRatios.set(i);
00122         for(int i = 9; i < 23; i++)
00123                 branchingRatios.set(i);
00124 }
00125 
00126 LHEProducer::~LHEProducer()
00127 {
00128 }
00129 
00130 void LHEProducer::beginJob()
00131 {
00132         hadronisation->init();
00133 }
00134 
00135 void LHEProducer::endJob()
00136 {
00137         hadronisation.reset();
00138         jetMatching.reset();
00139 }
00140 
00141 void LHEProducer::beginRun(edm::Run const& run, const edm::EventSetup &es)
00142 {
00143         edm::Handle<LHERunInfoProduct> product;
00144         run.getByLabel("source", product);
00145 
00146         runInfo.reset(new LHERunInfo(*product));
00147         index = 0;
00148 }
00149 void LHEProducer::endRun(edm::Run const& run, const edm::EventSetup &es)
00150 {
00151 }
00152 
00153 void LHEProducer::endRunProduce(edm::Run &run, const edm::EventSetup &es)
00154 {
00155         hadronisation->statistics();
00156 
00157         LHERunInfo::XSec crossSection;
00158         if (runInfo) {
00159                 crossSection = runInfo->xsec();
00160                 runInfo->statistics();
00161         }
00162 
00163         std::auto_ptr<GenRunInfoProduct> runInfo(new GenRunInfoProduct);
00164 
00165         runInfo->setInternalXSec(
00166                         GenRunInfoProduct::XSec(crossSection.value,
00167                                                 crossSection.error));
00168         runInfo->setExternalXSecLO(extCrossSect);
00169         runInfo->setFilterEfficiency(extFilterEff);
00170 
00171         run.put(runInfo);
00172 
00173         runInfo.reset();
00174 }
00175 
00176 bool LHEProducer::filter(edm::Event &event, const edm::EventSetup &es)
00177 {
00178         std::auto_ptr<edm::HepMCProduct> result(new edm::HepMCProduct);
00179 
00180         edm::Handle<LHEEventProduct> product;
00181         event.getByLabel("source", product);
00182 
00183         partonLevel.reset(new LHEEvent(runInfo, *product));
00184         if (!removeResonances.empty())
00185                 partonLevel->removeResonances(removeResonances);
00186 
00187         if (product->pdf())
00188                 partonLevel->setPDF(
00189                         std::auto_ptr<LHEEvent::PDF>(
00190                                 new LHEEvent::PDF(*product->pdf())));
00191 
00192         hadronisation->setEvent(partonLevel);
00193 
00194         double br = branchingRatios.getFactor(hadronisation.get(),
00195                                               partonLevel);
00196 
00197         matchingDone = false;
00198         weight = 1.0;
00199         std::auto_ptr<HepMC::GenEvent> hadronLevel(hadronisation->hadronize());
00200 
00201         if (!hadronLevel.get()) {
00202                 if (matchingDone) {
00203                         if (weight == 0.0)
00204                                 partonLevel->count(LHERunInfo::kSelected, br);
00205                         else
00206                                 partonLevel->count(LHERunInfo::kKilled,
00207                                                    br, weight);
00208                 } else
00209                         partonLevel->count(LHERunInfo::kTried, br);
00210         }
00211 
00212         if (!matchingDone && jetMatching.get() && hadronLevel.get())
00213                 weight = matching(hadronLevel.get(), false);
00214 
00215         if (weight == 0.0) {
00216                 edm::LogInfo("Generator|LHEInterface")
00217                         << "Event got rejected by the "
00218                            "jet matching." << std::endl;
00219 
00220                 if (hadronLevel.get()) {
00221                         partonLevel->count(LHERunInfo::kSelected);
00222                         hadronLevel.reset();
00223                 }
00224         }
00225 
00226         if (!hadronLevel.get()) {
00227                 event.put(result);
00228                 std::auto_ptr<GenEventInfoProduct> info(
00229                                                 new GenEventInfoProduct);
00230                 event.put(info);
00231                 return false;
00232         }
00233 
00234         partonLevel->count(LHERunInfo::kAccepted, br, weight);
00235 
00236         hadronLevel->set_event_number(++index);
00237 
00238         if (eventsToPrint) {
00239                 eventsToPrint--;
00240                 hadronLevel->print();
00241         }
00242 
00243         std::auto_ptr<GenEventInfoProduct> info(
00244                                 new GenEventInfoProduct(hadronLevel.get()));
00245         result->addHepMCData(hadronLevel.release());
00246         event.put(result);
00247         event.put(info);
00248 
00249         if (jetMatching.get() && matchSummary) {
00250                 std::auto_ptr< std::vector<double> > matchDeltaR(
00251                                                 new std::vector<double>);
00252                 std::auto_ptr< std::vector<double> > matchDeltaPRel(
00253                                                 new std::vector<double>);
00254 
00255                 typedef std::vector<JetMatching::JetPartonMatch> Matches;
00256                 Matches matches = jetMatching->getMatchSummary();
00257 
00258                 for(Matches::const_iterator iter = matches.begin();
00259                     iter != matches.end(); ++iter) {
00260                         if (!iter->isMatch())
00261                                 continue;
00262 
00263                         matchDeltaR->push_back(iter->delta);
00264                         matchDeltaPRel->push_back(iter->jet.rho() /
00265                                                   iter->parton.rho() - 1.0);
00266                 }
00267 
00268                 event.put(matchDeltaR, "matchDeltaR");
00269                 event.put(matchDeltaPRel, "matchDeltaPRel");
00270         }
00271 
00272         return true;
00273 }
00274 
00275 double LHEProducer::matching(const HepMC::GenEvent *event, bool shower) const
00276 {
00277         if (!jetMatching.get())
00278                 return 1.0;
00279 
00280         return jetMatching->match(partonLevel->asHepMCEvent().get(),
00281                                   event, shower);
00282 }
00283 
00284 bool LHEProducer::showeredEvent(const boost::shared_ptr<HepMC::GenEvent> &event)
00285 {
00286         weight = matching(event.get(), true);
00287         matchingDone = true;
00288         return weight == 0.0;
00289 }
00290 
00291 void LHEProducer::onInit()
00292 {
00293         jetMatching->init(runInfo);
00294 }
00295 
00296 void LHEProducer::onBeforeHadronisation()
00297 {
00298         jetMatching->beforeHadronisation(partonLevel);
00299 }
00300 
00301 DEFINE_FWK_MODULE(LHEProducer);
00302 
00303 DEFINE_LHE_JETMATCHING_PLUGIN(JetMatchingMLM);