CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/GeneratorInterface/LHEInterface/plugins/LHESource.cc

Go to the documentation of this file.
00001 #include <algorithm>
00002 #include <functional>
00003 #include <iostream>
00004 #include <string>
00005 #include <memory>
00006 
00007 #include <boost/bind.hpp>
00008 #include <boost/ptr_container/ptr_deque.hpp>
00009 
00010 #include "FWCore/Sources/interface/ExternalInputSource.h"
00011 #include "FWCore/Framework/interface/InputSourceMacros.h"
00012 #include "FWCore/Framework/interface/MakerMacros.h"
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/Framework/interface/Run.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017 
00018 #include "DataFormats/Common/interface/OrphanHandle.h"
00019 
00020 #include "SimDataFormats/GeneratorProducts/interface/LesHouches.h"
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/LHEReader.h"
00027 
00028 #include "LHESource.h"
00029 
00030 using namespace lhef;
00031 
00032 LHESource::LHESource(const edm::ParameterSet &params,
00033                      const edm::InputSourceDescription &desc) :
00034         ExternalInputSource(params, desc, false),
00035         reader(new LHEReader(fileNames(), params.getUntrackedParameter<unsigned int>("skipEvents", 0))),
00036         wasMerged(false)
00037 {
00038         produces<LHEEventProduct>();
00039         produces<LHERunInfoProduct, edm::InRun>();
00040 }
00041 
00042 LHESource::~LHESource()
00043 {
00044 }
00045 
00046 void LHESource::endJob()
00047 {
00048         reader.reset();
00049 }
00050 
00051 void LHESource::nextEvent()
00052 {
00053         if (partonLevel)
00054                 return;
00055 
00056         partonLevel = reader->next();
00057         if (!partonLevel)
00058                         return;
00059 
00060         boost::shared_ptr<LHERunInfo> runInfoThis = partonLevel->getRunInfo();
00061         if (runInfoThis != runInfoLast) {
00062                 runInfo = runInfoThis;
00063                 runInfoLast = runInfoThis;
00064         }
00065 }
00066 
00067 void LHESource::beginRun(edm::Run &run)
00068 {
00069         nextEvent();
00070         if (runInfoLast) {
00071                 runInfo = runInfoLast;
00072 
00073                 std::auto_ptr<LHERunInfoProduct> product(
00074                                 new LHERunInfoProduct(*runInfo->getHEPRUP()));
00075                 std::for_each(runInfo->getHeaders().begin(),
00076                               runInfo->getHeaders().end(),
00077                               boost::bind(
00078                                 &LHERunInfoProduct::addHeader,
00079                                 product.get(), _1));
00080                 std::for_each(runInfo->getComments().begin(),
00081                               runInfo->getComments().end(),
00082                               boost::bind(&LHERunInfoProduct::addComment,
00083                                 product.get(), _1));
00084 
00085                 // keep a copy around in case of merging
00086                 runInfoProducts.push_back(new LHERunInfoProduct(*product));
00087                 wasMerged = false;
00088 
00089                 run.put(product);
00090 
00091                 runInfo.reset();
00092         }
00093 }
00094 
00095 void LHESource::endRun(edm::Run &run)
00096 {
00097         if (!runInfoProducts.empty()) {
00098                 std::auto_ptr<LHERunInfoProduct> product(
00099                                         runInfoProducts.pop_front().release());
00100                 run.put(product);
00101         }
00102 }
00103 
00104 bool LHESource::produce(edm::Event &event)
00105 {
00106         nextEvent();
00107         if (!partonLevel)
00108                 return false;
00109 
00110         std::auto_ptr<LHEEventProduct> product(
00111                         new LHEEventProduct(*partonLevel->getHEPEUP()));
00112         if (partonLevel->getPDF())
00113                 product->setPDF(*partonLevel->getPDF());
00114         std::for_each(partonLevel->getComments().begin(),
00115                       partonLevel->getComments().end(),
00116                       boost::bind(&LHEEventProduct::addComment,
00117                                   product.get(), _1));
00118         event.put(product);
00119 
00120         if (runInfo) {
00121                 std::auto_ptr<LHERunInfoProduct> product(
00122                                 new LHERunInfoProduct(*runInfo->getHEPRUP()));
00123                 std::for_each(runInfo->getHeaders().begin(),
00124                               runInfo->getHeaders().end(),
00125                               boost::bind(
00126                                 &LHERunInfoProduct::addHeader,
00127                                 product.get(), _1));
00128                 std::for_each(runInfo->getComments().begin(),
00129                               runInfo->getComments().end(),
00130                               boost::bind(&LHERunInfoProduct::addComment,
00131                                 product.get(), _1));
00132 
00133                 if (!runInfoProducts.empty()) {
00134                         runInfoProducts.front().mergeProduct(*product);
00135                         if (!wasMerged) {
00136                                 runInfoProducts.pop_front();
00137                                 runInfoProducts.push_front(product);
00138                                 wasMerged = true;
00139                         }
00140                 }
00141 
00142                 runInfo.reset();
00143         }
00144 
00145         partonLevel.reset();
00146         return true;
00147 }
00148 
00149 DEFINE_FWK_INPUT_SOURCE(LHESource);