CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/GeneratorInterface/MCatNLOInterface/plugins/MCatNLOSource.cc

Go to the documentation of this file.
00001 #include <algorithm>
00002 #include <functional>
00003 #include <iostream>
00004 #include <sstream>
00005 #include <string>
00006 #include <memory>
00007 
00008 #include <boost/bind.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 "GeneratorInterface/LHEInterface/interface/LHERunInfo.h"
00019 #include "GeneratorInterface/LHEInterface/interface/LHEEvent.h"
00020 
00021 #include "GeneratorInterface/Herwig6Interface/interface/Herwig6Instance.h"
00022 
00023 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
00024 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
00025 
00026 #include "SimDataFormats/GeneratorProducts/interface/LesHouches.h"
00027 #include "SimDataFormats/GeneratorProducts/interface/LHECommonBlocks.h"
00028 
00029 #include "MCatNLOSource.h"
00030 
00031 
00032 // MC@NLO v3.4 LHE file rountines (mcatnlo_hwlhin.f)
00033 extern "C" {
00034   void mcatnloupinit_(int*, const char*, int);
00035   void mcatnloupevnt_(int*, int*, int*);
00036 }
00037 
00038 
00039 using namespace lhef;
00040 
00041 MCatNLOSource::MCatNLOSource(const edm::ParameterSet &params,
00042                      const edm::InputSourceDescription &desc) :
00043         ExternalInputSource(params, desc, false),
00044         gen::Herwig6Instance(0),
00045         skipEvents(params.getUntrackedParameter<unsigned int>("skipEvents", 0)),
00046         nEvents(0),
00047         processCode(params.getParameter<int>("processCode"))
00048 {
00049         std::vector<std::string> allFileNames = fileNames();
00050 
00051         // Only one filename
00052         if (allFileNames.size() != 1)
00053           throw cms::Exception("Generator|MCatNLOInterface")
00054             << "MCatNLOSource needs exactly one file specified. " <<std::endl;
00055 
00056         fileName = allFileNames[0];
00057 
00058         // Strip the "file:" prefix 
00059         if (fileName.find("file:") != 0)
00060           throw cms::Exception("Generator|MCatNLOInterface") << "MCatNLOSource only supports the file: scheme. "<<std::endl;
00061         fileName.erase(0, 5);
00062 
00063         // open input file
00064         reader.reset(new std::ifstream(fileName.c_str()));
00065 
00066         produces<LHEEventProduct>();
00067         produces<LHERunInfoProduct, edm::InRun>();
00068 }
00069 
00070 MCatNLOSource::~MCatNLOSource()
00071 {
00072 }
00073 
00074 void MCatNLOSource::endJob()
00075 {
00076   reader.reset();
00077 }
00078 
00079 void MCatNLOSource::nextEvent()
00080 {
00081   return;
00082 }
00083 
00084 template<typename T>
00085 static std::string makeConfigLine(const char *var, T value)
00086 {
00087   std::ostringstream ss;
00088   ss << var << " = " << value << "\n";
00089   return ss.str();
00090 }
00091 
00092 template<typename T>
00093 static std::string makeConfigLine(const char *var, unsigned int index, T value)
00094 {
00095   std::ostringstream ss;
00096   ss << var << "(" << index << ") = " << value << "\n";
00097   return ss.str();
00098 }
00099 
00100 void MCatNLOSource::beginRun(edm::Run &run)
00101 {
00102   InstanceWrapper wrapper(this);
00103 
00104   // call UPINIT privided by MC@NLO (v3.4)
00105   mcatnloupinit_(&processCode, fileName.c_str(), fileName.length());
00106 
00107   // fill HEPRUP common block and store in edm::Run
00108   lhef::HEPRUP heprup;
00109   lhef::CommonBlocks::readHEPRUP(&heprup);
00110 
00111   // make sure we write a valid LHE header, Herwig6Hadronizer
00112   // will interpret it correctly and set up LHAPDF
00113   heprup.PDFGUP.first = 0;
00114   heprup.PDFGUP.second = 0;
00115 
00116   std::auto_ptr<LHERunInfoProduct> runInfo(new LHERunInfoProduct(heprup));
00117 
00118   LHERunInfoProduct::Header hw6header("herwig6header");
00119   hw6header.addLine("\n");
00120   hw6header.addLine("# Herwig6 parameters\n");
00121   hw6header.addLine(makeConfigLine("IPROC", processCode));
00122   // add lines for parameter that have been touched by UPINIT
00123   if(mcpars_.emmins) 
00124     hw6header.addLine(makeConfigLine("EMMIN", mcpars_.emmin));
00125   if(mcpars_.emmaxs) 
00126     hw6header.addLine(makeConfigLine("EMMAX", mcpars_.emmax));
00127   if(mcpars_.gammaxs) 
00128     hw6header.addLine(makeConfigLine("GAMMAX",mcpars_.gammax));
00129   if(mcpars_.gamzs) 
00130     hw6header.addLine(makeConfigLine("GAMZ",mcpars_.gamz));
00131   if(mcpars_.gamws) 
00132     hw6header.addLine(makeConfigLine("GAMW",mcpars_.gamw));
00133   for(unsigned int i=0; i<1000; ++i) {
00134     if(mcpars_.rmasss[i])
00135       hw6header.addLine(makeConfigLine("RMASS",i+1,mcpars_.rmass[i]));
00136   }
00137 
00138   // other needed MC@NLO defaults (from mcatnlo_hwdriver.f v3.4)
00139   hw6header.addLine(makeConfigLine("SOFTME", false));
00140   hw6header.addLine(makeConfigLine("NOWGT", false));
00141   hw6header.addLine(makeConfigLine("NEGWTS", true));
00142   if(abs(processCode)==1705 || abs(processCode)==11705)
00143     hw6header.addLine(makeConfigLine("PSPLT",2,0.5));
00144   double wgtmax_=1.000001;
00145   hw6header.addLine(makeConfigLine("WGTMAX", wgtmax_));
00146   hw6header.addLine(makeConfigLine("AVABW", wgtmax_));
00147   hw6header.addLine(makeConfigLine("RLTIM",6, 1.0E-23));
00148   hw6header.addLine(makeConfigLine("RLTIM",12, 1.0E-23));
00149   
00150 
00151   runInfo->addHeader(hw6header);
00152 
00153   run.put(runInfo);
00154 
00155   return;
00156 }
00157 
00158 bool MCatNLOSource::produce(edm::Event &event)
00159 {
00160   InstanceWrapper wrapper(this);
00161 
00162   int lastEventDone=0;
00163   int ihpro=0;
00164   // skip events if asked to...
00165 
00166   while(skipEvents>0) {
00167     skipEvents--;
00168     mcatnloupevnt_(&processCode,&lastEventDone,&ihpro);
00169     if(lastEventDone) return false;
00170   }
00171 
00172   // call UPINIT privided by MC@NLO (v3.4)
00173   mcatnloupevnt_(&processCode,&lastEventDone,&ihpro);
00174 
00175   if(lastEventDone) return false;
00176 
00177   // fill HEPRUP common block and store in edm::Run
00178   lhef::HEPRUP heprup;
00179   lhef::HEPEUP hepeup;
00180   lhef::CommonBlocks::readHEPRUP(&heprup);
00181   lhef::CommonBlocks::readHEPEUP(&hepeup);
00182   hepeup.IDPRUP = heprup.LPRUP[0];
00183   std::auto_ptr<LHEEventProduct> lhEvent(new LHEEventProduct(hepeup));
00184   lhEvent->addComment(makeConfigLine("#IHPRO", ihpro));
00185   event.put(lhEvent);
00186 
00187   return true;
00188 }
00189 
00190 bool MCatNLOSource::hwwarn(const std::string &fn, int code)
00191 {
00192   // dummy ignoring useless HERWIG warnings
00193   return true;
00194 }
00195 
00196 DEFINE_FWK_INPUT_SOURCE(MCatNLOSource);