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 ()
 
std::unique_ptr< 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
 
std::unique_ptr< HepMC::GenEventgetGenEvent ()
 
std::unique_ptr< GenEventInfoProductgetGenEventInfo ()
 
GenRunInfoProductgetGenRunInfo ()
 
std::unique_ptr< lhef::LHEEventgetLHEEvent ()
 
const std::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 (std::unique_ptr< HepMC::GenEvent > event)
 
void resetEventInfo (std::unique_ptr< GenEventInfoProduct > eventInfo)
 
virtual bool select (HepMC::GenEvent *) const
 
void setEDMEvent (edm::Event &event)
 
void setLHEEvent (std::unique_ptr< lhef::LHEEvent > event)
 
void setLHERunInfo (std::unique_ptr< 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::unique_ptr< HepMC::GenEvent > & event ()
 
std::unique_ptr< GenEventInfoProduct > & eventInfo ()
 
lhef::LHEEventlheEvent ()
 
lhef::LHERunInfolheRunInfo ()
 
GenRunInfoProductrunInfo ()
 
- Protected Attributes inherited from gen::BaseHadronizer
std::string lheFile_
 
int randomIndex_
 

Detailed Description

Definition at line 41 of file SherpaHadronizer.cc.

Constructor & Destructor Documentation

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

Definition at line 119 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.

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

208 {
209  Generator->~Sherpa();
210  #ifdef USING__MPI
211  MPI::Finalize();
212  #endif
213 }

Member Function Documentation

const char* SherpaHadronizer::classname ( ) const
inline

Definition at line 57 of file SherpaHadronizer.cc.

References doSetRandomEngine(), and findQualityFiles::v.

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

Definition at line 333 of file SherpaHadronizer.cc.

Referenced by declareSpecialSettings().

334 {
335  return true;
336 }
bool SherpaHadronizer::declareSpecialSettings ( const std::vector< std::string > &  )
inline

Definition at line 49 of file SherpaHadronizer.cc.

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

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

Definition at line 231 of file SherpaHadronizer.cc.

Referenced by readSettings().

232 {
233  return false;
234 }
void SherpaHadronizer::doSetRandomEngine ( CLHEP::HepRandomEngine *  v)
overrideprivatevirtual

Reimplemented from gen::BaseHadronizer.

Definition at line 98 of file SherpaHadronizer.cc.

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

Referenced by classname().

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

Definition at line 343 of file SherpaHadronizer.cc.

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

344 {
345  //******** Verbosity *******
346  if (maxEventsToPrint > 0) {
348  event()->print();
349  }
350 }
unsigned int maxEventsToPrint
std::unique_ptr< HepMC::GenEvent > & event()
bool SherpaHadronizer::generatePartonsAndHadronize ( )

Definition at line 251 of file SherpaHadronizer.cc.

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

Referenced by declareSpecialSettings().

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

Reimplemented from gen::BaseHadronizer.

Definition at line 373 of file SherpaHadronizer.cc.

References genWeightsTable_cfi::genLumiInfoHeader, mps_fire::i, rearrangeWeights, variationweightlist, and weightlist.

373  {
374  auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
375 
376  if(rearrangeWeights){
377  edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!" ;
378  for(auto &i: weightlist){
379  genLumiInfoHeader->weightNames().push_back(i);
380  edm::LogVerbatim("SherpaHadronizer") << i;
381  }
382  for(auto &i: variationweightlist) {
383  genLumiInfoHeader->weightNames().push_back(i);
384  edm::LogVerbatim("SherpaHadronizer") << i;
385  }
386  }
387  return genLumiInfoHeader;
388 }
std::vector< std::string > weightlist
std::vector< std::string > variationweightlist
bool SherpaHadronizer::initializeForInternalPartons ( )

Definition at line 215 of file SherpaHadronizer.cc.

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

Referenced by readSettings().

216 {
217  //initialize Sherpa but only once
218  if (!isInitialized) {
219  int argc = arguments.size();
220  char *argv[argc];
221  for (int l = 0; l < argc; l++)
222  argv[l] = (char *)arguments[l].c_str();
223  Generator->InitializeTheRun(argc, argv);
224  Generator->InitializeTheEventHandler();
225  isInitialized = true;
226  }
227  return true;
228 }
std::vector< std::string > arguments
bool SherpaHadronizer::readSettings ( int  )
inline

Definition at line 46 of file SherpaHadronizer.cc.

References declareStableParticles(), and initializeForInternalPartons().

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

Definition at line 338 of file SherpaHadronizer.cc.

339 {
340  return true;
341 }
void SherpaHadronizer::statistics ( )

Definition at line 237 of file SherpaHadronizer.cc.

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

Referenced by declareSpecialSettings().

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

Member Data Documentation

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

Definition at line 72 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

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

Definition at line 73 of file SherpaHadronizer.cc.

bool SherpaHadronizer::isInitialized
private
bool SherpaHadronizer::isRNGinitialized
private

Definition at line 75 of file SherpaHadronizer.cc.

Referenced by doSetRandomEngine().

unsigned int SherpaHadronizer::maxEventsToPrint
private

Definition at line 71 of file SherpaHadronizer.cc.

Referenced by finalizeEvent(), and SherpaHadronizer().

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

Definition at line 65 of file SherpaHadronizer.cc.

double SherpaHadronizer::SherpaDefaultWeight
private

Definition at line 69 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

edm::ParameterSet SherpaHadronizer::SherpaParameterSet
private

Definition at line 70 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPath
private

Definition at line 66 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPathPiece
private

Definition at line 67 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaProcess
private

Definition at line 64 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaResultDir
private

Definition at line 68 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

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