CMS 3D CMS Logo

List of all members | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
SherpaHadronizer Class Reference
Inheritance diagram for SherpaHadronizer:
gen::BaseHadronizer

Public Member Functions

const char * classname () const
 
bool decay ()
 
bool declareSpecialSettings (const std::vector< std::string > &)
 
bool declareStableParticles (const std::vector< int > &pdgIds)
 
void finalizeEvent ()
 
bool generatePartonsAndHadronize ()
 
GenLumiInfoHeadergetGenLumiInfoHeader () const override
 
bool initializeForInternalPartons ()
 
bool readSettings (int)
 
bool residualDecay ()
 
 SherpaHadronizer (const edm::ParameterSet &params)
 
void statistics ()
 
 ~SherpaHadronizer () override
 
- Public Member Functions inherited from gen::BaseHadronizer
 BaseHadronizer (edm::ParameterSet const &ps)
 
void cleanLHE ()
 
void generateLHE (edm::LuminosityBlock const &lumi, CLHEP::HepRandomEngine *rengine, unsigned int ncpu)
 
edm::EventgetEDMEvent () const
 
HepMC::GenEventgetGenEvent ()
 
GenEventInfoProductgetGenEventInfo ()
 
GenRunInfoProductgetGenRunInfo ()
 
const boost::shared_ptr< lhef::LHERunInfo > & getLHERunInfo () const
 
const std::string & gridpackPath () const
 
int randomIndex () const
 
const std::string & randomInitConfigDescription () const
 
void randomizeIndex (edm::LuminosityBlock const &lumi, CLHEP::HepRandomEngine *rengine)
 
void resetEvent (HepMC::GenEvent *event)
 
void resetEventInfo (GenEventInfoProduct *eventInfo)
 
virtual bool select (HepMC::GenEvent *) const
 
void setEDMEvent (edm::Event &event)
 
void setLHEEvent (lhef::LHEEvent *event)
 
void setLHERunInfo (lhef::LHERunInfo *runInfo)
 
void setRandomEngine (CLHEP::HepRandomEngine *v)
 
std::vector< std::string > const & sharedResources () const
 
virtual ~BaseHadronizer ()(false)
 

Public Attributes

bool rearrangeWeights
 

Private Member Functions

void doSetRandomEngine (CLHEP::HepRandomEngine *v) override
 

Private Attributes

std::vector< std::string > arguments
 
SHERPA::Sherpa * Generator = new SHERPA::Sherpa()
 
bool isInitialized
 
bool isRNGinitialized
 
unsigned int maxEventsToPrint
 
std::string SherpaChecksum
 
double SherpaDefaultWeight
 
edm::ParameterSet SherpaParameterSet
 
std::string SherpaPath
 
std::string SherpaPathPiece
 
std::string SherpaProcess
 
std::string SherpaResultDir
 
std::vector< std::string > variationweightlist
 
std::vector< std::string > weightlist
 

Additional Inherited Members

- Protected Member Functions inherited from gen::BaseHadronizer
std::auto_ptr< HepMC::GenEvent > & event ()
 
std::auto_ptr< GenEventInfoProduct > & eventInfo ()
 
lhef::LHEEventlheEvent ()
 
lhef::LHERunInfolheRunInfo ()
 
GenRunInfoProductrunInfo ()
 
- Protected Attributes inherited from gen::BaseHadronizer
std::string lheFile_
 
int randomIndex_
 

Detailed Description

Definition at line 40 of file SherpaHadronizer.cc.

Constructor & Destructor Documentation

SherpaHadronizer::SherpaHadronizer ( const edm::ParameterSet params)

Definition at line 115 of file SherpaHadronizer.cc.

References arguments, Exception, edm::ParameterSet::exists(), spf::SherpackFetcher::Fetch(), edm::ParameterSet::getParameter(), mps_fire::i, isInitialized, maxEventsToPrint, rearrangeWeights, SherpaDefaultWeight, SherpaParameterSet, SherpaPath, SherpaPathPiece, SherpaProcess, SherpaResultDir, AlCaHLTBitMon_QueryRunRegistry::string, variationweightlist, and weightlist.

115  :
116  BaseHadronizer(params),
117  SherpaParameterSet(params.getParameter<edm::ParameterSet>("SherpaParameters")),
118  isRNGinitialized(false)
119 {
120  if (!params.exists("SherpaProcess")) SherpaProcess="";
121  else SherpaProcess=params.getParameter<std::string>("SherpaProcess");
122  if (!params.exists("SherpaPath")) SherpaPath="";
123  else SherpaPath=params.getParameter<std::string>("SherpaPath");
124  if (!params.exists("SherpaPathPiece")) SherpaPathPiece="";
125  else SherpaPathPiece=params.getParameter<std::string>("SherpaPathPiece");
126  if (!params.exists("SherpaResultDir")) SherpaResultDir="Result";
127  else SherpaResultDir=params.getParameter<std::string>("SherpaResultDir");
128  if (!params.exists("SherpaDefaultWeight")) SherpaDefaultWeight=1.;
129  else SherpaDefaultWeight=params.getParameter<double>("SherpaDefaultWeight");
130  if (!params.exists("maxEventsToPrint")) maxEventsToPrint=0;
131  else maxEventsToPrint=params.getParameter<int>("maxEventsToPrint");
132 // if hepmcextendedweights is used the event weights have to be reordered ( unordered list can be accessed via event->weights().write() )
133 // two lists have to be provided:
134 // 1) SherpaWeights
135 // - containing nominal event weight, combined matrix element and phase space weight, event normalization, and possibly other sherpa weights
136 // 2) SherpaVariationsWeights
137 // - containing weights from scale and PDF variations ( have to be defined in the runcard )
138 // - in case of unweighted events these weights are also divided by the event normalization (see list 1 )
139 // Sherpa Documentation: http://sherpa.hepforge.org/doc/SHERPA-MC-2.2.0.html#Scale-and-PDF-variations
140  if (!params.exists("SherpaWeightsBlock")) {
141  rearrangeWeights=false;
142  } else {
143  rearrangeWeights=true;
144  edm::ParameterSet WeightsBlock = params.getParameter<edm::ParameterSet>("SherpaWeightsBlock");
145  if (WeightsBlock.exists("SherpaWeights"))
146  weightlist=WeightsBlock.getParameter< std::vector<std::string> >("SherpaWeights");
147  else
148  throw cms::Exception("SherpaInterface") <<"SherpaWeights does not exists in SherpaWeightsBlock" << std::endl;
149  if (WeightsBlock.exists("SherpaVariationWeights"))
150  variationweightlist=WeightsBlock.getParameter< std::vector<std::string> >("SherpaVariationWeights");
151  else
152  throw cms::Exception("SherpaInterface") <<"SherpaVariationWeights does not exists in SherpaWeightsBlock" << std::endl;
153  edm::LogVerbatim("SherpaHadronizer") << "SherpaHadronizer will try rearrange the event weights according to SherpaWeights and SherpaVariationWeights";
154  }
155 
156 
157  spf::SherpackFetcher Fetcher(params);
158  int retval=Fetcher.Fetch();
159  if (retval != 0) {
160  throw cms::Exception("SherpaInterface") <<"SherpaHadronizer: Preparation of Sherpack failed ... ";
161  }
162  // The ids (names) of parameter sets to be read (Analysis,Run) to create Analysis.dat, Run.dat
163  //They are given as a vstring.
164  std::vector<std::string> setNames = SherpaParameterSet.getParameter<std::vector<std::string> >("parameterSets");
165  //Loop all set names...
166  for ( unsigned i=0; i<setNames.size(); ++i ) {
167  // ...and read the parameters for each set given in vstrings
168  std::vector<std::string> pars = SherpaParameterSet.getParameter<std::vector<std::string> >(setNames[i]);
169  edm::LogVerbatim("SherpaHadronizer") << "Write Sherpa parameter set " << setNames[i] <<" to "<<setNames[i]<<".dat ";
170  std::string datfile = SherpaPath + "/" + setNames[i] +".dat";
171  std::ofstream os(datfile.c_str());
172  // Loop over all strings and write the according *.dat
173  for(std::vector<std::string>::const_iterator itPar = pars.begin(); itPar != pars.end(); ++itPar ) {
174  os<<(*itPar)<<std::endl;
175  }
176  }
177 
178  //To be conform to the default Sherpa usage create a command line:
179  //name of executable (only for demonstration, could also be empty)
180  std::string shRun = "./Sherpa";
181  //Path where the Sherpa libraries are stored
182  std::string shPath = "PATH=" + SherpaPath;
183  // new for Sherpa 1.3.0, suggested by authors
184  std::string shPathPiece = "PATH_PIECE=" + SherpaPathPiece;
185  //Path where results are stored
186  std::string shRes = "RESULT_DIRECTORY=" + SherpaResultDir; // from Sherpa 1.2.0 on
187  //Name of the external random number class
188  std::string shRng = "EXTERNAL_RNG=CMS_SHERPA_RNG";
189 
190  //create the command line
191  arguments.push_back(shRun);
192  arguments.push_back(shPath);
193  arguments.push_back(shPathPiece);
194  arguments.push_back(shRes);
195  arguments.push_back(shRng);
196  isInitialized=false;
197  //initialization of Sherpa moved to initializeForInternalPartons
198 }
T getParameter(std::string const &) const
BaseHadronizer(edm::ParameterSet const &ps)
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::vector< std::string > weightlist
std::string SherpaPathPiece
unsigned int maxEventsToPrint
edm::ParameterSet SherpaParameterSet
std::vector< std::string > variationweightlist
std::string SherpaPath
std::string SherpaProcess
std::string SherpaResultDir
std::vector< std::string > arguments
SherpaHadronizer::~SherpaHadronizer ( )
override

Definition at line 200 of file SherpaHadronizer.cc.

201 {
202  Generator->~Sherpa();
203  #ifdef USING__MPI
204  MPI::Finalize();
205  #endif
206 }

Member Function Documentation

const char* SherpaHadronizer::classname ( ) const
inline

Definition at line 56 of file SherpaHadronizer.cc.

References doSetRandomEngine(), and findQualityFiles::v.

56 { return "SherpaHadronizer"; }
bool SherpaHadronizer::decay ( )

Definition at line 328 of file SherpaHadronizer.cc.

Referenced by declareSpecialSettings().

329 {
330  return true;
331 }
bool SherpaHadronizer::declareSpecialSettings ( const std::vector< std::string > &  )
inline

Definition at line 48 of file SherpaHadronizer.cc.

References decay(), generatePartonsAndHadronize(), and statistics().

48 { return true; }
bool SherpaHadronizer::declareStableParticles ( const std::vector< int > &  pdgIds)

Definition at line 226 of file SherpaHadronizer.cc.

Referenced by readSettings().

227 {
228  return false;
229 }
void SherpaHadronizer::doSetRandomEngine ( CLHEP::HepRandomEngine *  v)
overrideprivatevirtual

Reimplemented from gen::BaseHadronizer.

Definition at line 97 of file SherpaHadronizer.cc.

References Exception, isRNGinitialized, edm::errors::LogicError, summarizeEdmComparisonLogfiles::ran, and CMS_SHERPA_RNG::setRandomEngine().

Referenced by classname().

97  {
98  CMS_SHERPA_RNG* cmsSherpaRng = dynamic_cast<CMS_SHERPA_RNG*>(ATOOLS::ran->GetExternalRng());
99  if (cmsSherpaRng ==nullptr) {
100  //First time call to this function makes the interface store the reference in the unnamed namespace
101  if (!isRNGinitialized){
102  isRNGinitialized=true;
103  edm::LogVerbatim("SherpaHadronizer") << "Store assigned reference of the randomEngine";
104  SetExternalEngine(v);
105  // Throw exception if there is no reference to an external RNG and it is not the first call!
106  } else {
108  << "The Sherpa interface got a randomEngine reference but there is no reference to the external RNG to hand it over to\n";
109  }
110  } else {
111  cmsSherpaRng->setRandomEngine(v);
112  }
113 }
void setRandomEngine(CLHEP::HepRandomEngine *v)
void SherpaHadronizer::finalizeEvent ( )

Definition at line 338 of file SherpaHadronizer.cc.

References DECLARE_GETTER(), gen::BaseHadronizer::event(), maxEventsToPrint, and str.

339 {
340  //******** Verbosity *******
341  if (maxEventsToPrint > 0) {
343  event()->print();
344  }
345 }
unsigned int maxEventsToPrint
std::auto_ptr< HepMC::GenEvent > & event()
bool SherpaHadronizer::generatePartonsAndHadronize ( )

Definition at line 246 of file SherpaHadronizer.cc.

References MessageLogger_cfi::cerr, HTMLExport::elem(), Exception, mps_fire::i, rearrangeWeights, gen::BaseHadronizer::resetEvent(), variationweightlist, and weightlist.

Referenced by declareSpecialSettings().

247 {
248  //get the next event and check if it produced
249  bool rc = false;
250  int itry = 0;
251  bool gen_event = true;
252  while((itry < 3) && gen_event){
253  try{
254  rc = Generator->GenerateOneEvent();
255  gen_event = false;
256  } catch(...){
257  ++itry;
258  std::cerr << "Exception from Generator->GenerateOneEvent() catch. Call # "
259  << itry << " for this event\n";
260  }
261  }
262  if (rc) {
263  //convert it to HepMC2
264  HepMC::GenEvent* evt = new HepMC::GenEvent();
265  Generator->FillHepMCEvent(*evt);
266 
267  // in case of unweighted events sherpa puts the max weight as event weight.
268  // this is not optimal, we want 1 for unweighted events, so we check
269  // whether we are producing unweighted events ("EVENT_GENERATION_MODE" == "1")
270  // the information about the weights to the HepMC weight vector:
271  // [0] event weight
272  // [1] combined matrix element and phase space weight (missing only PDF information, thus directly suitable for PDF reweighting)
273  // [2] event weight normalisation (in case of unweighted events event weights of ~ +/-1 can be obtained by (event weight)/(event weight normalisation))
274  // [3] number of trials.
275  // see also: https://sherpa.hepforge.org/doc/SHERPA-MC-2.1.0.html#Event-output-formats
276  bool unweighted = false;
277  double weight_normalization = -1;
278  if(ATOOLS::ToType<int>(ATOOLS::rpa->gen.Variable("EVENT_GENERATION_MODE")) == 1){
279  if (evt->weights().size()>2) {
280  unweighted = true;
281  weight_normalization = evt->weights()[2];
282  }else{
283  throw cms::Exception("SherpaInterface") <<"Requested unweighted production. Missing normalization weight." << std::endl;
284  }
285  }
286 
287  // vector to fill new weights in correct order
288  std::vector<double> newWeights;
289  if (rearrangeWeights){
290  for ( auto &i : weightlist ) {
291  if (evt->weights().has_key(i)) {
292  newWeights.push_back(evt->weights()[i]);
293  } else {
294  throw cms::Exception("SherpaInterface") <<"Missing weights! Key " << i << " not found, please check the weight definition!" << std::endl;
295  }
296  }
297  for ( auto &i : variationweightlist ) {
298  if (evt->weights().has_key(i)) {
299  if(unweighted){
300  newWeights.push_back(evt->weights()[i]/weight_normalization);
301  }else{
302  newWeights.push_back(evt->weights()[i]);
303  }
304  } else {
305  throw cms::Exception("SherpaInterface") <<"Missing weights! Key " << i << " not found, please check the weight definition!" << std::endl;
306  }
307 
308  }
309 
310  //Change original weights for reordered ones
311  evt->weights().clear();
312  for (auto& elem: newWeights) {
313  evt->weights().push_back(elem);
314  }
315  }
316 
317  if(unweighted){
318  evt->weights()[0]/=weight_normalization;
319  }
320  resetEvent(evt);
321  return true;
322  }
323  else {
324  return false;
325  }
326 }
std::vector< std::string > weightlist
std::vector< std::string > variationweightlist
def elem(elemtype, innerHTML='', html_class='', kwargs)
Definition: HTMLExport.py:18
void resetEvent(HepMC::GenEvent *event)
GenLumiInfoHeader * SherpaHadronizer::getGenLumiInfoHeader ( ) const
overridevirtual

Reimplemented from gen::BaseHadronizer.

Definition at line 368 of file SherpaHadronizer.cc.

References nano_cff::genLumiInfoHeader, mps_fire::i, rearrangeWeights, variationweightlist, weightlist, and GenLumiInfoHeader::weightNames().

368  {
369  GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
370 
371  if(rearrangeWeights){
372  edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!" ;
373  for(auto &i: weightlist){
374  genLumiInfoHeader->weightNames().push_back(i);
375  edm::LogVerbatim("SherpaHadronizer") << i;
376  }
377  for(auto &i: variationweightlist) {
378  genLumiInfoHeader->weightNames().push_back(i);
379  edm::LogVerbatim("SherpaHadronizer") << i;
380  }
381  }
382  return genLumiInfoHeader;
383 }
const std::vector< std::string > & weightNames() const
std::vector< std::string > weightlist
genLumiInfoHeader
Definition: nano_cff.py:97
std::vector< std::string > variationweightlist
bool SherpaHadronizer::initializeForInternalPartons ( )

Definition at line 208 of file SherpaHadronizer.cc.

References dir2webdir::argc, arguments, cmsBatch::argv, isInitialized, and checklumidiff::l.

Referenced by readSettings().

209 {
210  //initialize Sherpa but only once
211  if (!isInitialized){
212  int argc=arguments.size();
213  char* argv[argc];
214  for (int l=0; l<argc; l++) argv[l]=(char*)arguments[l].c_str();
215  #ifdef USING__MPI
216  MPI::Init();
217  #endif
218  Generator->InitializeTheRun(argc,argv);
219  Generator->InitializeTheEventHandler();
220  isInitialized=true;
221  }
222  return true;
223 }
std::vector< std::string > arguments
bool SherpaHadronizer::readSettings ( int  )
inline

Definition at line 45 of file SherpaHadronizer.cc.

References declareStableParticles(), and initializeForInternalPartons().

45 { return true; }
bool SherpaHadronizer::residualDecay ( )

Definition at line 333 of file SherpaHadronizer.cc.

334 {
335  return true;
336 }
void SherpaHadronizer::statistics ( )

Definition at line 232 of file SherpaHadronizer.cc.

References gen::BaseHadronizer::runInfo(), and GenRunInfoProduct::setInternalXSec().

Referenced by declareSpecialSettings().

233 {
234  //calculate statistics
235  Generator->SummarizeRun();
236 
237  //get the xsec & err
238  double xsec_val = Generator->TotalXS();
239  double xsec_err = Generator->TotalErr();
240 
241  //set the internal cross section in pb in GenRunInfoProduct
242  runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec_val,xsec_err));
243 }
void setInternalXSec(const XSec &xsec)
GenRunInfoProduct & runInfo()

Member Data Documentation

std::vector<std::string> SherpaHadronizer::arguments
private

Definition at line 71 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

SHERPA::Sherpa* SherpaHadronizer::Generator = new SHERPA::Sherpa()
private

Definition at line 72 of file SherpaHadronizer.cc.

bool SherpaHadronizer::isInitialized
private

Definition at line 73 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

bool SherpaHadronizer::isRNGinitialized
private

Definition at line 74 of file SherpaHadronizer.cc.

Referenced by doSetRandomEngine().

unsigned int SherpaHadronizer::maxEventsToPrint
private

Definition at line 70 of file SherpaHadronizer.cc.

Referenced by finalizeEvent(), and SherpaHadronizer().

bool SherpaHadronizer::rearrangeWeights
std::string SherpaHadronizer::SherpaChecksum
private

Definition at line 64 of file SherpaHadronizer.cc.

double SherpaHadronizer::SherpaDefaultWeight
private

Definition at line 68 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

edm::ParameterSet SherpaHadronizer::SherpaParameterSet
private

Definition at line 69 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPath
private

Definition at line 65 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPathPiece
private

Definition at line 66 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaProcess
private

Definition at line 63 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaResultDir
private

Definition at line 67 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::vector<std::string> SherpaHadronizer::variationweightlist
private
std::vector<std::string> SherpaHadronizer::weightlist
private