CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/GeneratorInterface/LHEInterface/plugins/MCDBSource.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <utility>
00003 #include <string>
00004 
00005 #include <boost/regex.hpp>
00006 
00007 #include "mcdb.hpp"
00008 
00009 #include "FWCore/Framework/interface/InputSourceMacros.h"
00010 #include "FWCore/Framework/interface/MakerMacros.h"
00011 #include "FWCore/Framework/interface/Event.h"
00012 #include "FWCore/Framework/interface/Run.h"
00013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 
00016 #include "SimDataFormats/GeneratorProducts/interface/LesHouches.h"
00017 #include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
00018 #include "GeneratorInterface/LHEInterface/interface/LHEReader.h"
00019 
00020 #include "LHESource.h"
00021 
00022 using namespace lhef;
00023 
00024 class MCDBSource : public LHESource {
00025     public:
00026         explicit MCDBSource(const edm::ParameterSet &params,
00027                             const edm::InputSourceDescription &desc);
00028         virtual ~MCDBSource();
00029 };
00030 
00031 static std::pair<std::vector<std::string>, unsigned int>
00032                         getFileURLs(const edm::ParameterSet &params)
00033 {
00034         unsigned int articleId = params.getParameter<unsigned int>("articleID");
00035 
00036         edm::LogInfo("Generator|LHEInterface")
00037                 << "Reading article id " << articleId << " from MCDB."
00038                 << std::endl;
00039 
00040         mcdb::MCDB mcdb;
00041 
00042         mcdb::Article article = mcdb.getArticle(articleId);
00043 
00044         edm::LogInfo("Generator|LHEInterface")
00045                 << "Title: " << article.title() << std::endl
00046                 << "First author: " << article.authors()[0].firstName() << " "
00047                 << article.authors()[0].lastName() << std::endl
00048                 << "Number of authors: " << article.authors().size() << std::endl
00049                 << "Abstract: " << article.abstract() << std::endl
00050                 << "Generator: " << article.generator().name()
00051                 << ", " << article.generator().version() << std::endl
00052                 << "Number of files: " << article.files().size() << std::endl
00053                 << "Files: " << std::endl;
00054 
00055         std::vector<std::string> supportedProtocols = 
00056                 params.getUntrackedParameter< std::vector<std::string> >(
00057                                                         "supportedProtocols");
00058 
00059         boost::regex filter(params.getUntrackedParameter<std::string>(
00060                                                         "filter", "\\.lhef?$"),
00061                             boost::regex_constants::normal |
00062                             boost::regex_constants::icase);
00063 
00064         unsigned int firstEvent =
00065                 params.getUntrackedParameter<unsigned int>("skipEvents", 0);
00066 
00067     unsigned int fcount = 0;
00068         std::vector<std::string> fileURLs;
00069         for(std::vector<mcdb::File>::iterator file = article.files().begin();
00070             file != article.files().end(); ++file) {
00071                 std::string fileURL;
00072                 for(std::vector<std::string>::const_iterator prot =
00073                                                 supportedProtocols.begin();
00074                     prot != supportedProtocols.end(); ++prot) {
00075                         for(std::vector<std::string>::const_iterator path =
00076                                                         file->paths().begin();
00077                             path != file->paths().end(); ++path) {
00078                                 if (path->substr(0, prot->length() + 1) ==
00079                                     *prot + ":") {
00080                                         fileURL = *path;
00081                                         break;
00082                                 }
00083                         }
00084                         if (!fileURL.empty())
00085                                 break;
00086                 }
00087 
00088                 if (fileURL.empty())
00089                         throw cms::Exception("Generator|LHEInterface")
00090                                 << "MCDB did not contain any URLs with"
00091                                    " supported protocols for at least one"
00092                                    " file." << std::endl;
00093 
00094                 if (!boost::regex_search(fileURL, filter))
00095                         continue;
00096 
00097                 int nEvents = file->eventsNumber();
00098                 if (nEvents > 0 && (int)firstEvent >= nEvents) {
00099                         firstEvent -= nEvents;
00100                         continue;
00101                 }
00102 
00103                 fileURLs.push_back(fileURL);
00104         fcount++;
00105         edm::LogInfo("Generator|LHEInterface") << "Adding file n. " << fcount << " " << fileURL;
00106         }
00107 
00108         return std::make_pair(fileURLs, firstEvent);
00109 }
00110 
00111 static edm::ParameterSet augmentPSetFromMCDB(const edm::ParameterSet &params)
00112 {
00113         // note that this is inherently ugly, but the only way to
00114         // pass the file URLs to ExternalInputSource, as the MCDB client
00115         // is nothing but an MCDB to URL converter
00116         // all modified parameters are untracked, so the provenance is
00117         // unchanged
00118 
00119         std::pair<std::vector<std::string>, unsigned int> result =
00120                                                         getFileURLs(params);
00121 
00122         edm::ParameterSet newParams = params;
00123         newParams.addUntrackedParameter<std::vector<std::string> >(
00124                                                 "fileNames", result.first);
00125         newParams.addUntrackedParameter<unsigned int>(
00126                                                 "skipEvents", result.second);
00127 
00128         return newParams;
00129 }
00130 
00131 MCDBSource::MCDBSource(const edm::ParameterSet &params,
00132                        const edm::InputSourceDescription &desc) :
00133         LHESource(augmentPSetFromMCDB(params), desc)
00134 {
00135 }
00136 
00137 MCDBSource::~MCDBSource()
00138 {
00139 }
00140 
00141 DEFINE_FWK_INPUT_SOURCE(MCDBSource);