CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/GeneratorInterface/SherpaInterface/src/SherpaHadronizer.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <sstream>
00003 #include <string>
00004 #include <memory>
00005 #include <stdint.h>
00006 
00007 
00008 #include "HepMC/GenEvent.h"
00009 
00010 #include "SHERPA/Main/Sherpa.H"
00011 #include "ATOOLS/Org/Message.H"
00012 #include "ATOOLS/Math/Random.H"
00013 #include "ATOOLS/Org/Exception.H"
00014 #include "ATOOLS/Org/Run_Parameter.H"
00015 #include "ATOOLS/Org/My_Root.H"
00016 #include "SHERPA/Tools/Input_Output_Handler.H"
00017 #include "SHERPA/Tools/HepMC2_Interface.H"
00018 
00019 #include "GeneratorInterface/Core/interface/ParameterCollector.h"
00020 #include "GeneratorInterface/Core/interface/BaseHadronizer.h"
00021 #include "GeneratorInterface/Core/interface/GeneratorFilter.h"
00022 #include "GeneratorInterface/Core/interface/HadronizerFilter.h"
00023 #include "GeneratorInterface/Core/interface/RNDMEngineAccess.h"
00024 
00025 
00026 class SherpaHadronizer : public gen::BaseHadronizer {
00027 public:
00028   SherpaHadronizer(const edm::ParameterSet &params);
00029   ~SherpaHadronizer();
00030   
00031   bool initializeForInternalPartons();
00032   bool declareStableParticles(const std::vector<int> &pdgIds);
00033   bool declareSpecialSettings( const std::vector<std::string> ) { return true; }
00034   void statistics();
00035   bool generatePartonsAndHadronize();
00036   bool decay();
00037   bool residualDecay();
00038   void finalizeEvent();
00039   const char *classname() const { return "SherpaHadronizer"; }
00040   
00041 private:
00042   
00043   std::string SherpaLibDir;
00044   std::string SherpaResultDir;
00045   edm::ParameterSet     SherpaParameter;
00046   unsigned int  maxEventsToPrint;
00047   
00048   SHERPA::Sherpa Generator;
00049   CLHEP::HepRandomEngine* randomEngine;
00050   
00051 };
00052 
00053 class CMS_SHERPA_RNG: public ATOOLS::External_RNG {
00054 public:
00055   CMS_SHERPA_RNG(){randomEngine = &gen::getEngineReference();};
00056 private: 
00057   CLHEP::HepRandomEngine* randomEngine;
00058   double Get();
00059 };
00060 
00061 
00062 
00063 SherpaHadronizer::SherpaHadronizer(const edm::ParameterSet &params) :
00064   BaseHadronizer(params),
00065   SherpaLibDir(params.getUntrackedParameter<std::string>("libDir","Sherpa_Process")),
00066   SherpaResultDir(params.getUntrackedParameter<std::string>("resultDir","Result")),
00067   SherpaParameter(params.getParameter<edm::ParameterSet>("SherpaParameters")),
00068   maxEventsToPrint(params.getUntrackedParameter<int>("maxEventsToPrint", 0))
00069 {
00070 
00071   // The ids (names) of parameter sets to be read (Analysis,Run) to create Analysis.dat, Run.dat
00072   //They are given as a vstring.  
00073   std::vector<std::string> setNames = SherpaParameter.getParameter<std::vector<std::string> >("parameterSets");
00074   //Loop all set names...
00075   for ( unsigned i=0; i<setNames.size(); ++i ) {
00076     // ...and read the parameters for each set given in vstrings
00077     std::vector<std::string> pars = SherpaParameter.getParameter<std::vector<std::string> >(setNames[i]);
00078     std::cout << "Write Sherpa parameter set " << setNames[i] <<" to "<<setNames[i]<<".dat "<<std::endl;
00079     std::string datfile =  SherpaLibDir + "/" + setNames[i] +".dat";
00080     std::ofstream os(datfile.c_str());  
00081     // Loop over all strings and write the according *.dat
00082     for(std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
00083       os<<(*itPar)<<std::endl;
00084     } 
00085   }
00086 
00087   //To be conform to the default Sherpa usage create a command line:
00088   //name of executable  (only for demonstration, could also be empty)
00089   std::string shRun  = "./Sherpa";
00090   //Path where the Sherpa libraries are stored
00091   std::string shPath = "PATH=" + SherpaLibDir;
00092   //Path where results are stored 
00093   // std::string shRes  = "RESULT_DIRECTORY=" + SherpaLibDir + "/" + SherpaResultDir; // for Sherpa <=1.1.3
00094   std::string shRes  = "RESULT_DIRECTORY=" + SherpaResultDir; // from Sherpa 1.2.0 on
00095   //Name of the external random number class
00096   std::string shRng  = "EXTERNAL_RNG=CMS_SHERPA_RNG";
00097   
00098   //create the command line
00099   char* argv[4];
00100   argv[0]=(char*)shRun.c_str();
00101   argv[1]=(char*)shPath.c_str();
00102   argv[2]=(char*)shRes.c_str();
00103   argv[3]=(char*)shRng.c_str();
00104   
00105   //initialize Sherpa with the command line
00106   Generator.InitializeTheRun(4,argv);
00107 }
00108 
00109 SherpaHadronizer::~SherpaHadronizer()
00110 {
00111 }
00112 
00113 bool SherpaHadronizer::initializeForInternalPartons()
00114 {
00115   
00116   //initialize Sherpa
00117   Generator.InitializeTheEventHandler();
00118   
00119   return true;
00120 }
00121 
00122 #if 0
00123 // naive Sherpa HepMC status fixup //FIXME 
00124 static int getStatus(const HepMC::GenParticle *p)
00125 {
00126   return status;
00127 }
00128 #endif
00129 
00130 //FIXME
00131 bool SherpaHadronizer::declareStableParticles(const std::vector<int> &pdgIds)
00132 {
00133 #if 0
00134   for(std::vector<int>::const_iterator iter = pdgIds.begin();
00135       iter != pdgIds.end(); ++iter)
00136     if (!markStable(*iter))
00137       return false;
00138   
00139   return true;
00140 #else
00141   return false;
00142 #endif
00143 }
00144 
00145 
00146 void SherpaHadronizer::statistics()
00147 {
00148   //calculate statistics
00149   Generator.SummarizeRun(); 
00150 
00151   //get the xsec from EventHandler
00152   SHERPA::Event_Handler* theEventHandler = Generator.GetEventHandler();
00153   double xsec_val = theEventHandler->TotalXS();
00154   double xsec_err = theEventHandler->TotalErr();
00155   
00156   //set the internal cross section in pb in GenRunInfoProduct 
00157   runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec_val,xsec_err));
00158 
00159 }
00160 
00161 
00162 bool SherpaHadronizer::generatePartonsAndHadronize()
00163 {
00164   //get the next event and check if it produced
00165   if (Generator.GenerateOneEvent()) { 
00166     //convert it to HepMC2
00167     SHERPA::Input_Output_Handler* ioh = Generator.GetIOHandler();
00168     SHERPA::HepMC2_Interface* hm2i = ioh->GetHepMC2Interface();
00169     HepMC::GenEvent* evt = hm2i->GenEvent();
00170     //ugly!! a hard copy, since sherpa deletes the GenEvent internal
00171     resetEvent(new HepMC::GenEvent (*evt));         
00172     return true;
00173   }
00174   else {
00175     return false;
00176   }  
00177 }
00178 
00179 bool SherpaHadronizer::decay()
00180 {
00181         return true;
00182 }
00183 
00184 bool SherpaHadronizer::residualDecay()
00185 {
00186         return true;
00187 }
00188 
00189 void SherpaHadronizer::finalizeEvent()
00190 {
00191 #if 0
00192         for(HepMC::GenEvent::particle_iterator iter = event->particles_begin();
00193             iter != event->particles_end(); iter++)
00194                 (*iter)->set_status(getStatus(*iter));
00195 #endif
00196         //******** Verbosity *******
00197         if (maxEventsToPrint > 0) {
00198                 maxEventsToPrint--;
00199                 event()->print();               
00200         }
00201 }
00202 
00203 //GETTER for the external random numbers
00204 DECLARE_GETTER(CMS_SHERPA_RNG_Getter,"CMS_SHERPA_RNG",ATOOLS::External_RNG,ATOOLS::RNG_Key);
00205 
00206 ATOOLS::External_RNG *CMS_SHERPA_RNG_Getter::operator()(const ATOOLS::RNG_Key &) const
00207 { return new CMS_SHERPA_RNG(); }
00208 
00209 void CMS_SHERPA_RNG_Getter::PrintInfo(std::ostream &str,const size_t) const
00210 { str<<"CMS_SHERPA_RNG interface"; }
00211 
00212 double CMS_SHERPA_RNG::Get(){
00213    return randomEngine->flat();
00214    }
00215    
00216 #include "GeneratorInterface/ExternalDecays/interface/ExternalDecayDriver.h"
00217 
00218 typedef edm::GeneratorFilter<SherpaHadronizer, gen::ExternalDecayDriver> SherpaGeneratorFilter;
00219 DEFINE_FWK_MODULE(SherpaGeneratorFilter);