CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SherpaHadronizer.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 #include <memory>
5 #include <stdint.h>
6 
7 
8 #include "HepMC/GenEvent.h"
9 
10 #include "SHERPA/Main/Sherpa.H"
11 #include "ATOOLS/Org/Message.H"
12 #include "ATOOLS/Math/Random.H"
13 #include "ATOOLS/Org/Exception.H"
14 #include "ATOOLS/Org/Run_Parameter.H"
15 #include "ATOOLS/Org/My_Root.H"
16 #include "SHERPA/Tools/Input_Output_Handler.H"
17 #include "SHERPA/Tools/HepMC2_Interface.H"
18 
24 
25 
27 public:
28  SherpaHadronizer(const edm::ParameterSet &params);
30 
32  bool declareStableParticles(const std::vector<int> &pdgIds);
33  bool declareSpecialSettings( const std::vector<std::string> ) { return true; }
34  void statistics();
36  bool decay();
37  bool residualDecay();
38  void finalizeEvent();
39  const char *classname() const { return "SherpaHadronizer"; }
40 
41 private:
42 
43  std::string SherpaLibDir;
44  std::string SherpaResultDir;
46  unsigned int maxEventsToPrint;
47 
49  CLHEP::HepRandomEngine* randomEngine;
50 
51 };
52 
53 class CMS_SHERPA_RNG: public ATOOLS::External_RNG {
54 public:
56 private:
57  CLHEP::HepRandomEngine* randomEngine;
58  double Get();
59 };
60 
61 
62 
64  BaseHadronizer(params),
65  SherpaLibDir(params.getUntrackedParameter<std::string>("libDir","Sherpa_Process")),
66  SherpaResultDir(params.getUntrackedParameter<std::string>("resultDir","Result")),
67  SherpaParameter(params.getParameter<edm::ParameterSet>("SherpaParameters")),
68  maxEventsToPrint(params.getUntrackedParameter<int>("maxEventsToPrint", 0))
69 {
70 
71  // The ids (names) of parameter sets to be read (Analysis,Run) to create Analysis.dat, Run.dat
72  //They are given as a vstring.
73  std::vector<std::string> setNames = SherpaParameter.getParameter<std::vector<std::string> >("parameterSets");
74  //Loop all set names...
75  for ( unsigned i=0; i<setNames.size(); ++i ) {
76  // ...and read the parameters for each set given in vstrings
77  std::vector<std::string> pars = SherpaParameter.getParameter<std::vector<std::string> >(setNames[i]);
78  std::cout << "Write Sherpa parameter set " << setNames[i] <<" to "<<setNames[i]<<".dat "<<std::endl;
79  std::string datfile = SherpaLibDir + "/" + setNames[i] +".dat";
80  std::ofstream os(datfile.c_str());
81  // Loop over all strings and write the according *.dat
82  for(std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
83  os<<(*itPar)<<std::endl;
84  }
85  }
86 
87  //To be conform to the default Sherpa usage create a command line:
88  //name of executable (only for demonstration, could also be empty)
89  std::string shRun = "./Sherpa";
90  //Path where the Sherpa libraries are stored
91  std::string shPath = "PATH=" + SherpaLibDir;
92  //Path where results are stored
93  // std::string shRes = "RESULT_DIRECTORY=" + SherpaLibDir + "/" + SherpaResultDir; // for Sherpa <=1.1.3
94  std::string shRes = "RESULT_DIRECTORY=" + SherpaResultDir; // from Sherpa 1.2.0 on
95  //Name of the external random number class
96  std::string shRng = "EXTERNAL_RNG=CMS_SHERPA_RNG";
97 
98  //create the command line
99  char* argv[4];
100  argv[0]=(char*)shRun.c_str();
101  argv[1]=(char*)shPath.c_str();
102  argv[2]=(char*)shRes.c_str();
103  argv[3]=(char*)shRng.c_str();
104 
105  //initialize Sherpa with the command line
106  Generator.InitializeTheRun(4,argv);
107 }
108 
110 {
111 }
112 
114 {
115 
116  //initialize Sherpa
117  Generator.InitializeTheEventHandler();
118 
119  return true;
120 }
121 
122 #if 0
123 // naive Sherpa HepMC status fixup //FIXME
124 static int getStatus(const HepMC::GenParticle *p)
125 {
126  return status;
127 }
128 #endif
129 
130 //FIXME
131 bool SherpaHadronizer::declareStableParticles(const std::vector<int> &pdgIds)
132 {
133 #if 0
134  for(std::vector<int>::const_iterator iter = pdgIds.begin();
135  iter != pdgIds.end(); ++iter)
136  if (!markStable(*iter))
137  return false;
138 
139  return true;
140 #else
141  return false;
142 #endif
143 }
144 
145 
147 {
148  //calculate statistics
149  Generator.SummarizeRun();
150 
151  //get the xsec from EventHandler
152  SHERPA::Event_Handler* theEventHandler = Generator.GetEventHandler();
153  double xsec_val = theEventHandler->TotalXS();
154  double xsec_err = theEventHandler->TotalErr();
155 
156  //set the internal cross section in pb in GenRunInfoProduct
157  runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec_val,xsec_err));
158 
159 }
160 
161 
163 {
164  //get the next event and check if it produced
165  if (Generator.GenerateOneEvent()) {
166  //convert it to HepMC2
167  SHERPA::Input_Output_Handler* ioh = Generator.GetIOHandler();
168  SHERPA::HepMC2_Interface* hm2i = ioh->GetHepMC2Interface();
169  HepMC::GenEvent* evt = hm2i->GenEvent();
170  //ugly!! a hard copy, since sherpa deletes the GenEvent internal
171  resetEvent(new HepMC::GenEvent (*evt));
172  return true;
173  }
174  else {
175  return false;
176  }
177 }
178 
180 {
181  return true;
182 }
183 
185 {
186  return true;
187 }
188 
190 {
191 #if 0
192  for(HepMC::GenEvent::particle_iterator iter = event->particles_begin();
193  iter != event->particles_end(); iter++)
194  (*iter)->set_status(getStatus(*iter));
195 #endif
196  //******** Verbosity *******
197  if (maxEventsToPrint > 0) {
199  event()->print();
200  }
201 }
202 
203 //GETTER for the external random numbers
204 DECLARE_GETTER(CMS_SHERPA_RNG_Getter,"CMS_SHERPA_RNG",ATOOLS::External_RNG,ATOOLS::RNG_Key);
205 
206 ATOOLS::External_RNG *CMS_SHERPA_RNG_Getter::operator()(const ATOOLS::RNG_Key &) const
207 { return new CMS_SHERPA_RNG(); }
208 
209 void CMS_SHERPA_RNG_Getter::PrintInfo(std::ostream &str,const size_t) const
210 { str<<"CMS_SHERPA_RNG interface"; }
211 
213  return randomEngine->flat();
214  }
215 
217 
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
CLHEP::HepRandomEngine * randomEngine
bool declareSpecialSettings(const std::vector< std::string >)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
CLHEP::HepRandomEngine * randomEngine
unsigned int maxEventsToPrint
edm::GeneratorFilter< SherpaHadronizer, gen::ExternalDecayDriver > SherpaGeneratorFilter
static int getStatus(const HepMC::GenParticle *p)
void setInternalXSec(const XSec &xsec)
bool declareStableParticles(const std::vector< int > &pdgIds)
std::auto_ptr< HepMC::GenEvent > & event()
CLHEP::HepRandomEngine & getEngineReference()
GenRunInfoProduct & runInfo()
std::string SherpaLibDir
bool initializeForInternalPartons()
DECLARE_GETTER(CMS_SHERPA_RNG_Getter,"CMS_SHERPA_RNG", ATOOLS::External_RNG, ATOOLS::RNG_Key)
SHERPA::Sherpa Generator
bool generatePartonsAndHadronize()
std::string SherpaResultDir
SherpaHadronizer(const edm::ParameterSet &params)
tuple cout
Definition: gather_cfg.py:41
void resetEvent(HepMC::GenEvent *event)
tuple status
Definition: ntuplemaker.py:245
const char * classname() const
edm::ParameterSet SherpaParameter