CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/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         bool newFileOpened = false;
00057         partonLevel = reader->next(&newFileOpened);
00058         if(newFileOpened) incrementFileIndex();
00059         if (!partonLevel)
00060                         return;
00061 
00062         boost::shared_ptr<LHERunInfo> runInfoThis = partonLevel->getRunInfo();
00063         if (runInfoThis != runInfoLast) {
00064                 runInfo = runInfoThis;
00065                 runInfoLast = runInfoThis;
00066         }
00067 }
00068 
00069 void LHESource::beginRun(edm::Run &run)
00070 {
00071         nextEvent();
00072         if (runInfoLast) {
00073                 runInfo = runInfoLast;
00074 
00075                 std::auto_ptr<LHERunInfoProduct> product(
00076                                 new LHERunInfoProduct(*runInfo->getHEPRUP()));
00077                 std::for_each(runInfo->getHeaders().begin(),
00078                               runInfo->getHeaders().end(),
00079                               boost::bind(
00080                                 &LHERunInfoProduct::addHeader,
00081                                 product.get(), _1));
00082                 std::for_each(runInfo->getComments().begin(),
00083                               runInfo->getComments().end(),
00084                               boost::bind(&LHERunInfoProduct::addComment,
00085                                 product.get(), _1));
00086 
00087                 // keep a copy around in case of merging
00088                 runInfoProducts.push_back(new LHERunInfoProduct(*product));
00089                 wasMerged = false;
00090 
00091                 run.put(product);
00092 
00093                 runInfo.reset();
00094         }
00095 }
00096 
00097 void LHESource::endRun(edm::Run &run)
00098 {
00099         if (!runInfoProducts.empty()) {
00100                 std::auto_ptr<LHERunInfoProduct> product(
00101                                         runInfoProducts.pop_front().release());
00102                 run.put(product);
00103         }
00104 }
00105 
00106 bool LHESource::produce(edm::Event &event)
00107 {
00108         if (!partonLevel)
00109                 return false;
00110 
00111         std::auto_ptr<LHEEventProduct> product(
00112                         new LHEEventProduct(*partonLevel->getHEPEUP()));
00113         if (partonLevel->getPDF())
00114                 product->setPDF(*partonLevel->getPDF());
00115         std::for_each(partonLevel->getComments().begin(),
00116                       partonLevel->getComments().end(),
00117                       boost::bind(&LHEEventProduct::addComment,
00118                                   product.get(), _1));
00119         event.put(product);
00120 
00121         if (runInfo) {
00122                 std::auto_ptr<LHERunInfoProduct> product(
00123                                 new LHERunInfoProduct(*runInfo->getHEPRUP()));
00124                 std::for_each(runInfo->getHeaders().begin(),
00125                               runInfo->getHeaders().end(),
00126                               boost::bind(
00127                                 &LHERunInfoProduct::addHeader,
00128                                 product.get(), _1));
00129                 std::for_each(runInfo->getComments().begin(),
00130                               runInfo->getComments().end(),
00131                               boost::bind(&LHERunInfoProduct::addComment,
00132                                 product.get(), _1));
00133 
00134                 if (!runInfoProducts.empty()) {
00135                         runInfoProducts.front().mergeProduct(*product);
00136                         if (!wasMerged) {
00137                                 runInfoProducts.pop_front();
00138                                 runInfoProducts.push_front(product);
00139                                 wasMerged = true;
00140                         }
00141                 }
00142 
00143                 runInfo.reset();
00144         }
00145 
00146         partonLevel.reset();
00147         return true;
00148 }
00149 
00150 DEFINE_FWK_INPUT_SOURCE(LHESource);