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 38 of file SherpaHadronizer.cc.

Constructor & Destructor Documentation

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

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

107  : BaseHadronizer(params),
108  SherpaParameterSet(params.getParameter<edm::ParameterSet>("SherpaParameters")),
109  isRNGinitialized(false) {
110  if (!params.exists("SherpaProcess"))
111  SherpaProcess = "";
112  else
113  SherpaProcess = params.getParameter<std::string>("SherpaProcess");
114  if (!params.exists("SherpaPath"))
115  SherpaPath = "";
116  else
117  SherpaPath = params.getParameter<std::string>("SherpaPath");
118  if (!params.exists("SherpaPathPiece"))
119  SherpaPathPiece = "";
120  else
121  SherpaPathPiece = params.getParameter<std::string>("SherpaPathPiece");
122  if (!params.exists("SherpaResultDir"))
123  SherpaResultDir = "Result";
124  else
125  SherpaResultDir = params.getParameter<std::string>("SherpaResultDir");
126  if (!params.exists("SherpaDefaultWeight"))
127  SherpaDefaultWeight = 1.;
128  else
129  SherpaDefaultWeight = params.getParameter<double>("SherpaDefaultWeight");
130  if (!params.exists("maxEventsToPrint"))
131  maxEventsToPrint = 0;
132  else
133  maxEventsToPrint = params.getParameter<int>("maxEventsToPrint");
134  // if hepmcextendedweights is used the event weights have to be reordered ( unordered list can be accessed via event->weights().write() )
135  // two lists have to be provided:
136  // 1) SherpaWeights
137  // - containing nominal event weight, combined matrix element and phase space weight, event normalization, and possibly other sherpa weights
138  // 2) SherpaVariationsWeights
139  // - containing weights from scale and PDF variations ( have to be defined in the runcard )
140  // - in case of unweighted events these weights are also divided by the event normalization (see list 1 )
141  // Sherpa Documentation: http://sherpa.hepforge.org/doc/SHERPA-MC-2.2.0.html#Scale-and-PDF-variations
142  if (!params.exists("SherpaWeightsBlock")) {
143  rearrangeWeights = false;
144  } else {
145  rearrangeWeights = true;
146  edm::ParameterSet WeightsBlock = params.getParameter<edm::ParameterSet>("SherpaWeightsBlock");
147  if (WeightsBlock.exists("SherpaWeights"))
148  weightlist = WeightsBlock.getParameter<std::vector<std::string> >("SherpaWeights");
149  else
150  throw cms::Exception("SherpaInterface") << "SherpaWeights does not exists in SherpaWeightsBlock" << std::endl;
151  if (WeightsBlock.exists("SherpaVariationWeights"))
152  variationweightlist = WeightsBlock.getParameter<std::vector<std::string> >("SherpaVariationWeights");
153  else
154  throw cms::Exception("SherpaInterface")
155  << "SherpaVariationWeights does not exists in SherpaWeightsBlock" << std::endl;
156  edm::LogVerbatim("SherpaHadronizer") << "SherpaHadronizer will try rearrange the event weights according to "
157  "SherpaWeights and SherpaVariationWeights";
158  }
159 
160  spf::SherpackFetcher Fetcher(params);
161  int retval = Fetcher.Fetch();
162  if (retval != 0) {
163  throw cms::Exception("SherpaInterface") << "SherpaHadronizer: Preparation of Sherpack failed ... ";
164  }
165  // The ids (names) of parameter sets to be read (Analysis,Run) to create Analysis.dat, Run.dat
166  //They are given as a vstring.
167  std::vector<std::string> setNames = SherpaParameterSet.getParameter<std::vector<std::string> >("parameterSets");
168  //Loop all set names...
169  for (unsigned i = 0; i < setNames.size(); ++i) {
170  // ...and read the parameters for each set given in vstrings
171  std::vector<std::string> pars = SherpaParameterSet.getParameter<std::vector<std::string> >(setNames[i]);
172  edm::LogVerbatim("SherpaHadronizer") << "Write Sherpa parameter set " << setNames[i] << " to " << setNames[i]
173  << ".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 }
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 204 of file SherpaHadronizer.cc.

204  {
205  Generator->~Sherpa();
206 #ifdef USING__MPI
207  MPI::Finalize();
208 #endif
209 }

Member Function Documentation

const char* SherpaHadronizer::classname ( ) const
inline

Definition at line 54 of file SherpaHadronizer.cc.

References doSetRandomEngine(), and findQualityFiles::v.

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

Definition at line 323 of file SherpaHadronizer.cc.

Referenced by declareSpecialSettings().

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

Definition at line 46 of file SherpaHadronizer.cc.

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

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

Definition at line 228 of file SherpaHadronizer.cc.

Referenced by readSettings().

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

Reimplemented from gen::BaseHadronizer.

Definition at line 88 of file SherpaHadronizer.cc.

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

Referenced by classname().

88  {
89  CMS_SHERPA_RNG *cmsSherpaRng = dynamic_cast<CMS_SHERPA_RNG *>(ATOOLS::ran->GetExternalRng());
90  if (cmsSherpaRng == nullptr) {
91  //First time call to this function makes the interface store the reference in the unnamed namespace
92  if (!isRNGinitialized) {
93  isRNGinitialized = true;
94  edm::LogVerbatim("SherpaHadronizer") << "Store assigned reference of the randomEngine";
95  SetExternalEngine(v);
96  // Throw exception if there is no reference to an external RNG and it is not the first call!
97  } else {
98  throw edm::Exception(edm::errors::LogicError) << "The Sherpa interface got a randomEngine reference but there is "
99  "no reference to the external RNG to hand it over to\n";
100  }
101  } else {
102  cmsSherpaRng->setRandomEngine(v);
103  }
104 }
void setRandomEngine(CLHEP::HepRandomEngine *v)
void SherpaHadronizer::finalizeEvent ( )

Definition at line 327 of file SherpaHadronizer.cc.

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

327  {
328  //******** Verbosity *******
329  if (maxEventsToPrint > 0) {
331  event()->print();
332  }
333 }
unsigned int maxEventsToPrint
std::unique_ptr< HepMC::GenEvent > & event()
bool SherpaHadronizer::generatePartonsAndHadronize ( )

Definition at line 242 of file SherpaHadronizer.cc.

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

Referenced by declareSpecialSettings().

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

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

357  {
358  auto genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
359 
360  if (rearrangeWeights) {
361  edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!";
362  for (auto &i : weightlist) {
363  genLumiInfoHeader->weightNames().push_back(i);
364  edm::LogVerbatim("SherpaHadronizer") << i;
365  }
366  for (auto &i : variationweightlist) {
367  genLumiInfoHeader->weightNames().push_back(i);
368  edm::LogVerbatim("SherpaHadronizer") << i;
369  }
370  }
371  return genLumiInfoHeader;
372 }
std::vector< std::string > weightlist
genLumiInfoHeader
Definition: nano_cff.py:98
std::vector< std::string > variationweightlist
bool SherpaHadronizer::initializeForInternalPartons ( )

Definition at line 211 of file SherpaHadronizer.cc.

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

Referenced by readSettings().

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

Definition at line 43 of file SherpaHadronizer.cc.

References declareStableParticles(), initializeForInternalPartons(), and CosmicGenFilterHelix_cfi::pdgIds.

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

Definition at line 325 of file SherpaHadronizer.cc.

325 { return true; }
void SherpaHadronizer::statistics ( )

Definition at line 230 of file SherpaHadronizer.cc.

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

Referenced by declareSpecialSettings().

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

Member Data Documentation

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

Definition at line 67 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

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

Definition at line 68 of file SherpaHadronizer.cc.

bool SherpaHadronizer::isInitialized
private

Definition at line 69 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

bool SherpaHadronizer::isRNGinitialized
private

Definition at line 70 of file SherpaHadronizer.cc.

Referenced by doSetRandomEngine().

unsigned int SherpaHadronizer::maxEventsToPrint
private

Definition at line 66 of file SherpaHadronizer.cc.

Referenced by finalizeEvent(), and SherpaHadronizer().

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

Definition at line 60 of file SherpaHadronizer.cc.

double SherpaHadronizer::SherpaDefaultWeight
private

Definition at line 64 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

edm::ParameterSet SherpaHadronizer::SherpaParameterSet
private

Definition at line 65 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPath
private

Definition at line 61 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPathPiece
private

Definition at line 62 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaProcess
private

Definition at line 59 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaResultDir
private

Definition at line 63 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

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