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 ()
 
- 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::GenEvent * getGenEvent ()
 
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
 
 ~BaseHadronizer ()
 

Public Attributes

bool rearrangeWeights
 

Private Member Functions

virtual void doSetRandomEngine (CLHEP::HepRandomEngine *v) override
 

Private Attributes

std::vector< std::string > arguments
 
SHERPA::Sherpa Generator
 
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 37 of file SherpaHadronizer.cc.

Constructor & Destructor Documentation

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

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

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

Definition at line 198 of file SherpaHadronizer.cc.

199 {
200 }

Member Function Documentation

const char* SherpaHadronizer::classname ( ) const
inline

Definition at line 53 of file SherpaHadronizer.cc.

References doSetRandomEngine(), and findQualityFiles::v.

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

Definition at line 339 of file SherpaHadronizer.cc.

Referenced by declareSpecialSettings().

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

Definition at line 45 of file SherpaHadronizer.cc.

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

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

Definition at line 225 of file SherpaHadronizer.cc.

Referenced by readSettings().

226 {
227 #if 0
228  for(std::vector<int>::const_iterator iter = pdgIds.begin();
229  iter != pdgIds.end(); ++iter)
230  if (!markStable(*iter))
231  return false;
232 
233  return true;
234 #else
235  return false;
236 #endif
237 }
void SherpaHadronizer::doSetRandomEngine ( CLHEP::HepRandomEngine *  v)
overrideprivatevirtual

Reimplemented from gen::BaseHadronizer.

Definition at line 94 of file SherpaHadronizer.cc.

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

Referenced by classname().

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

Definition at line 349 of file SherpaHadronizer.cc.

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

350 {
351 #if 0
352  for(HepMC::GenEvent::particle_iterator iter = event->particles_begin();
353  iter != event->particles_end(); iter++)
354  (*iter)->set_status(getStatus(*iter));
355 #endif
356  //******** Verbosity *******
357  if (maxEventsToPrint > 0) {
359  event()->print();
360  }
361 }
unsigned int maxEventsToPrint
std::auto_ptr< HepMC::GenEvent > & event()
Definition: event.py:1
bool SherpaHadronizer::generatePartonsAndHadronize ( )

Definition at line 257 of file SherpaHadronizer.cc.

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

Referenced by declareSpecialSettings().

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

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

384  {
385  GenLumiInfoHeader *genLumiInfoHeader = BaseHadronizer::getGenLumiInfoHeader();
386 
387  if(rearrangeWeights){
388  edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!" ;
389  for(auto &i: weightlist){
390  genLumiInfoHeader->weightNames().push_back(i);
391  edm::LogVerbatim("SherpaHadronizer") << i;
392  }
393  for(auto &i: variationweightlist) {
394  genLumiInfoHeader->weightNames().push_back(i);
395  edm::LogVerbatim("SherpaHadronizer") << i;
396  }
397  }
398  return genLumiInfoHeader;
399 }
const std::vector< std::string > & weightNames() const
std::vector< std::string > weightlist
std::vector< std::string > variationweightlist
bool SherpaHadronizer::initializeForInternalPartons ( )

Definition at line 202 of file SherpaHadronizer.cc.

References dir2webdir::argc, arguments, cmsBatch::argv, GenParticle::GenParticle, isInitialized, checklumidiff::l, AlCaHLTBitMon_ParallelJobs::p, and mps_update::status.

Referenced by readSettings().

203 {
204  //initialize Sherpa but only once
205  if (!isInitialized){
206  int argc=arguments.size();
207  char* argv[argc];
208  for (int l=0; l<argc; l++) argv[l]=(char*)arguments[l].c_str();
209  Generator.InitializeTheRun(argc,argv);
210  Generator.InitializeTheEventHandler();
211  isInitialized=true;
212  }
213  return true;
214 }
std::vector< std::string > arguments
bool SherpaHadronizer::readSettings ( int  )
inline

Definition at line 42 of file SherpaHadronizer.cc.

References declareStableParticles(), and initializeForInternalPartons().

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

Definition at line 344 of file SherpaHadronizer.cc.

345 {
346  return true;
347 }
void SherpaHadronizer::statistics ( )

Definition at line 240 of file SherpaHadronizer.cc.

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

Referenced by declareSpecialSettings().

241 {
242  //calculate statistics
243  Generator.SummarizeRun();
244 
245  //get the xsec & err
246  double xsec_val = Generator.TotalXS();
247  double xsec_err = Generator.TotalErr();
248 
249  //set the internal cross section in pb in GenRunInfoProduct
250  runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec_val,xsec_err));
251 
252 
253  //~ runInfo().setWeightList(newWeightList);
254 }
void setInternalXSec(const XSec &xsec)
GenRunInfoProduct & runInfo()

Member Data Documentation

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

Definition at line 68 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

SHERPA::Sherpa SherpaHadronizer::Generator
private

Definition at line 69 of file SherpaHadronizer.cc.

bool SherpaHadronizer::isInitialized
private

Definition at line 70 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

bool SherpaHadronizer::isRNGinitialized
private

Definition at line 71 of file SherpaHadronizer.cc.

Referenced by doSetRandomEngine().

unsigned int SherpaHadronizer::maxEventsToPrint
private

Definition at line 67 of file SherpaHadronizer.cc.

Referenced by finalizeEvent(), and SherpaHadronizer().

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

Definition at line 61 of file SherpaHadronizer.cc.

double SherpaHadronizer::SherpaDefaultWeight
private

Definition at line 65 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

edm::ParameterSet SherpaHadronizer::SherpaParameterSet
private

Definition at line 66 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPath
private

Definition at line 62 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPathPiece
private

Definition at line 63 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaProcess
private

Definition at line 60 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaResultDir
private

Definition at line 64 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

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