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/MyStrStream.H"
16 #include "SHERPA/Tools/Input_Output_Handler.H"
17 #include "SHERPA/Tools/HepMC2_Interface.H"
18 
25 
27 public:
28  SherpaHadronizer(const edm::ParameterSet &params);
30 
31  bool readSettings( int ) { return true; }
33  bool declareStableParticles(const std::vector<int> &pdgIds);
34  bool declareSpecialSettings( const std::vector<std::string>& ) { return true; }
35  void statistics();
37  bool decay();
38  bool residualDecay();
39  void finalizeEvent();
40  const char *classname() const { return "SherpaHadronizer"; }
41 
42 private:
43 
51  unsigned int maxEventsToPrint;
52 
54  CLHEP::HepRandomEngine* randomEngine;
55 
56 };
57 
58 class CMS_SHERPA_RNG: public ATOOLS::External_RNG {
59 public:
61 private:
62  CLHEP::HepRandomEngine* randomEngine;
63  double Get() override;
64 };
65 
66 
67 
69  BaseHadronizer(params),
70  SherpaParameterSet(params.getParameter<edm::ParameterSet>("SherpaParameters"))
71 {
72  if (!params.exists("SherpaProcess")) SherpaProcess="";
73  else SherpaProcess=params.getParameter<std::string>("SherpaProcess");
74  if (!params.exists("SherpaPath")) SherpaPath="";
75  else SherpaPath=params.getParameter<std::string>("SherpaPath");
76  if (!params.exists("SherpaPathPiece")) SherpaPathPiece="";
77  else SherpaPathPiece=params.getParameter<std::string>("SherpaPathPiece");
78  if (!params.exists("SherpaResultDir")) SherpaResultDir="Result";
79  else SherpaResultDir=params.getParameter<std::string>("SherpaResultDir");
80  if (!params.exists("SherpaDefaultWeight")) SherpaDefaultWeight=1.;
81  else SherpaDefaultWeight=params.getParameter<double>("SherpaDefaultWeight");
82  if (!params.exists("maxEventsToPrint")) maxEventsToPrint=0;
83  else maxEventsToPrint=params.getParameter<int>("maxEventsToPrint");
84 
85 
86  spf::SherpackFetcher Fetcher(params);
87  int retval=Fetcher.Fetch();
88  if (retval != 0) {
89  std::cout << "SherpaHadronizer: Preparation of Sherpack failed ... " << std::endl;
90  std::cout << "SherpaHadronizer: Error code: " << retval << std::endl;
91  std::terminate();
92 
93  }
94  // The ids (names) of parameter sets to be read (Analysis,Run) to create Analysis.dat, Run.dat
95  //They are given as a vstring.
96  std::vector<std::string> setNames = SherpaParameterSet.getParameter<std::vector<std::string> >("parameterSets");
97  //Loop all set names...
98  for ( unsigned i=0; i<setNames.size(); ++i ) {
99  // ...and read the parameters for each set given in vstrings
100  std::vector<std::string> pars = SherpaParameterSet.getParameter<std::vector<std::string> >(setNames[i]);
101  std::cout << "Write Sherpa parameter set " << setNames[i] <<" to "<<setNames[i]<<".dat "<<std::endl;
102  std::string datfile = SherpaPath + "/" + setNames[i] +".dat";
103  std::ofstream os(datfile.c_str());
104  // Loop over all strings and write the according *.dat
105  for(std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
106  os<<(*itPar)<<std::endl;
107  }
108  }
109 
110  //To be conform to the default Sherpa usage create a command line:
111  //name of executable (only for demonstration, could also be empty)
112  std::string shRun = "./Sherpa";
113  //Path where the Sherpa libraries are stored
114  std::string shPath = "PATH=" + SherpaPath;
115  // new for Sherpa 1.3.0, suggested by authors
116  std::string shPathPiece = "PATH_PIECE=" + SherpaPathPiece;
117  //Path where results are stored
118  std::string shRes = "RESULT_DIRECTORY=" + SherpaResultDir; // from Sherpa 1.2.0 on
119  //Name of the external random number class
120  std::string shRng = "EXTERNAL_RNG=CMS_SHERPA_RNG";
121 
122  //create the command line
123  char* argv[5];
124  argv[0]=(char*)shRun.c_str();
125  argv[1]=(char*)shPath.c_str();
126  argv[2]=(char*)shPathPiece.c_str();
127  argv[3]=(char*)shRes.c_str();
128  argv[4]=(char*)shRng.c_str();
129 
130  //initialize Sherpa with the command line
131  Generator.InitializeTheRun(5,argv);
132 }
133 
135 {
136 }
137 
139 {
140 
141  //initialize Sherpa
142  Generator.InitializeTheEventHandler();
143 
144  return true;
145 }
146 
147 #if 0
148 // naive Sherpa HepMC status fixup //FIXME
149 static int getStatus(const HepMC::GenParticle *p)
150 {
151  return status;
152 }
153 #endif
154 
155 //FIXME
156 bool SherpaHadronizer::declareStableParticles(const std::vector<int> &pdgIds)
157 {
158 #if 0
159  for(std::vector<int>::const_iterator iter = pdgIds.begin();
160  iter != pdgIds.end(); ++iter)
161  if (!markStable(*iter))
162  return false;
163 
164  return true;
165 #else
166  return false;
167 #endif
168 }
169 
170 
172 {
173  //calculate statistics
174  Generator.SummarizeRun();
175 
176  //get the xsec from EventHandler
177  SHERPA::Event_Handler* theEventHandler = Generator.GetEventHandler();
178  double xsec_val = theEventHandler->TotalXS();
179  double xsec_err = theEventHandler->TotalErr();
180 
181  //set the internal cross section in pb in GenRunInfoProduct
182  runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec_val,xsec_err));
183 
184 }
185 
186 
188 {
189  //get the next event and check if it produced
190  if (Generator.GenerateOneEvent()) {
191  //convert it to HepMC2
192  SHERPA::Input_Output_Handler* ioh = Generator.GetIOHandler();
193  SHERPA::HepMC2_Interface* hm2i = ioh->GetHepMC2Interface();
194  //get the event weight from blobs
195  ATOOLS::Blob_List* blobs = Generator.GetEventHandler()-> GetBlobs();
196  ATOOLS::Blob* sp(blobs->FindFirst(ATOOLS::btp::Signal_Process));
197  double weight((*sp)["Weight"]->Get<double>());
198  double ef((*sp)["Enhance"]->Get<double>());
199  // in case of unweighted events sherpa puts the max weight as event weight.
200  // this is not optimal, we want 1 for unweighted events, so we check
201  // whether we are producing unweighted events ("EVENT_GENERATION_MODE" == "1")
202  if ( ATOOLS::ToType<int>( ATOOLS::rpa->gen.Variable("EVENT_GENERATION_MODE") ) == 1 ) {
203  if (ef > 0.) {
204  weight = SherpaDefaultWeight/ef;
205  } else {
206  weight = -1234.;
207  }
208  }
209  //create and empty event and then hand it to SherpaIOHandler to fill it
210  HepMC::GenEvent* evt = new HepMC::GenEvent();
211  hm2i->Sherpa2HepMC(blobs, *evt, weight);
212  resetEvent(evt);
213  return true;
214  }
215  else {
216  return false;
217  }
218 }
219 
221 {
222  return true;
223 }
224 
226 {
227  return true;
228 }
229 
231 {
232 #if 0
233  for(HepMC::GenEvent::particle_iterator iter = event->particles_begin();
234  iter != event->particles_end(); iter++)
235  (*iter)->set_status(getStatus(*iter));
236 #endif
237  //******** Verbosity *******
238  if (maxEventsToPrint > 0) {
240  event()->print();
241  }
242 }
243 
244 //GETTER for the external random numbers
245 DECLARE_GETTER(CMS_SHERPA_RNG_Getter,"CMS_SHERPA_RNG",ATOOLS::External_RNG,ATOOLS::RNG_Key);
246 
247 ATOOLS::External_RNG *CMS_SHERPA_RNG_Getter::operator()(const ATOOLS::RNG_Key &) const
248 { return new CMS_SHERPA_RNG(); }
249 
250 void CMS_SHERPA_RNG_Getter::PrintInfo(std::ostream &str,const size_t) const
251 { str<<"CMS_SHERPA_RNG interface"; }
252 
254  return randomEngine->flat();
255  }
256 
258 
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
CLHEP::HepRandomEngine * randomEngine
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
CLHEP::HepRandomEngine * randomEngine
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::string SherpaPathPiece
unsigned int maxEventsToPrint
edm::GeneratorFilter< SherpaHadronizer, gen::ExternalDecayDriver > SherpaGeneratorFilter
void setInternalXSec(const XSec &xsec)
bool declareStableParticles(const std::vector< int > &pdgIds)
std::auto_ptr< HepMC::GenEvent > & event()
CLHEP::HepRandomEngine & getEngineReference()
GenRunInfoProduct & runInfo()
edm::ParameterSet SherpaParameterSet
bool initializeForInternalPartons()
DECLARE_GETTER(CMS_SHERPA_RNG_Getter,"CMS_SHERPA_RNG", ATOOLS::External_RNG, ATOOLS::RNG_Key)
SHERPA::Sherpa Generator
bool generatePartonsAndHadronize()
std::string SherpaPath
bool declareSpecialSettings(const std::vector< std::string > &)
std::string SherpaProcess
std::string SherpaResultDir
SherpaHadronizer(const edm::ParameterSet &params)
tuple cout
Definition: gather_cfg.py:121
void resetEvent(HepMC::GenEvent *event)
int weight
Definition: histoStyle.py:50
tuple status
Definition: ntuplemaker.py:245
const char * classname() const
std::string SherpaChecksum
double Get() override