test
CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | 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 ()
 
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)
 
edm::EventgetEDMEvent () const
 
HepMC::GenEvent * getGenEvent ()
 
GenEventInfoProductgetGenEventInfo ()
 
GenRunInfoProductgetGenRunInfo ()
 
const boost::shared_ptr
< lhef::LHERunInfo > & 
getLHERunInfo () const
 
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 ()
 

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
 
bool rearrangeWeights
 
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 ()
 

Detailed Description

Definition at line 37 of file SherpaHadronizer.cc.

Constructor & Destructor Documentation

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

Definition at line 112 of file SherpaHadronizer.cc.

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

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

198 {
199 }

Member Function Documentation

const char* SherpaHadronizer::classname ( ) const
inline

Definition at line 51 of file SherpaHadronizer.cc.

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

Definition at line 346 of file SherpaHadronizer.cc.

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

Definition at line 45 of file SherpaHadronizer.cc.

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

Definition at line 224 of file SherpaHadronizer.cc.

References getDQMSummary::iter.

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

Reimplemented from gen::BaseHadronizer.

Definition at line 93 of file SherpaHadronizer.cc.

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

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

Definition at line 356 of file SherpaHadronizer.cc.

References gen::BaseHadronizer::event(), getDQMSummary::iter, and maxEventsToPrint.

357 {
358 #if 0
359  for(HepMC::GenEvent::particle_iterator iter = event->particles_begin();
360  iter != event->particles_end(); iter++)
361  (*iter)->set_status(getStatus(*iter));
362 #endif
363  //******** Verbosity *******
364  if (maxEventsToPrint > 0) {
366  event()->print();
367  }
368 }
unsigned int maxEventsToPrint
std::auto_ptr< HepMC::GenEvent > & event()
bool SherpaHadronizer::generatePartonsAndHadronize ( )

Definition at line 264 of file SherpaHadronizer.cc.

References dtNoiseDBValidation_cfg::cerr, HTMLExport::elem(), edm::hlt::Exception, relval_steps::gen(), i, rearrangeWeights, gen::BaseHadronizer::resetEvent(), variationweightlist, and weightlist.

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

Definition at line 201 of file SherpaHadronizer.cc.

References dir2webdir::argc, arguments, getDQMSummary::argv, isInitialized, and prof2calltree::l.

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

Definition at line 42 of file SherpaHadronizer.cc.

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

Definition at line 351 of file SherpaHadronizer.cc.

352 {
353  return true;
354 }
void SherpaHadronizer::statistics ( )

Definition at line 239 of file SherpaHadronizer.cc.

References i, rearrangeWeights, gen::BaseHadronizer::runInfo(), GenRunInfoProduct::setInternalXSec(), variationweightlist, and weightlist.

240 {
241  //calculate statistics
242  Generator.SummarizeRun();
243 
244  //get the xsec & err
245  double xsec_val = Generator.TotalXS();
246  double xsec_err = Generator.TotalErr();
247 
248  //set the internal cross section in pb in GenRunInfoProduct
249  runInfo().setInternalXSec(GenRunInfoProduct::XSec(xsec_val,xsec_err));
250 
251  if(rearrangeWeights){
252  edm::LogPrint("SherpaHadronizer") << "The order of event weights was changed!" ;
253  for(auto &i: weightlist){
254  edm::LogVerbatim("SherpaHadronizer") << i;
255  }
256  for(auto &i: variationweightlist) {
257  edm::LogVerbatim("SherpaHadronizer") << i;
258  }
259  }
260 
261 }
int i
Definition: DBlmapReader.cc:9
std::vector< std::string > weightlist
void setInternalXSec(const XSec &xsec)
GenRunInfoProduct & runInfo()
std::vector< std::string > variationweightlist

Member Data Documentation

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

Definition at line 66 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

SHERPA::Sherpa SherpaHadronizer::Generator
private

Definition at line 67 of file SherpaHadronizer.cc.

bool SherpaHadronizer::isInitialized
private

Definition at line 68 of file SherpaHadronizer.cc.

Referenced by initializeForInternalPartons(), and SherpaHadronizer().

bool SherpaHadronizer::isRNGinitialized
private

Definition at line 69 of file SherpaHadronizer.cc.

Referenced by doSetRandomEngine().

unsigned int SherpaHadronizer::maxEventsToPrint
private

Definition at line 65 of file SherpaHadronizer.cc.

Referenced by finalizeEvent(), and SherpaHadronizer().

bool SherpaHadronizer::rearrangeWeights
private

Definition at line 70 of file SherpaHadronizer.cc.

Referenced by generatePartonsAndHadronize(), SherpaHadronizer(), and statistics().

std::string SherpaHadronizer::SherpaChecksum
private

Definition at line 59 of file SherpaHadronizer.cc.

double SherpaHadronizer::SherpaDefaultWeight
private

Definition at line 63 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

edm::ParameterSet SherpaHadronizer::SherpaParameterSet
private

Definition at line 64 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPath
private

Definition at line 60 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaPathPiece
private

Definition at line 61 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaProcess
private

Definition at line 58 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

std::string SherpaHadronizer::SherpaResultDir
private

Definition at line 62 of file SherpaHadronizer.cc.

Referenced by SherpaHadronizer().

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

Definition at line 72 of file SherpaHadronizer.cc.

Referenced by generatePartonsAndHadronize(), SherpaHadronizer(), and statistics().

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

Definition at line 71 of file SherpaHadronizer.cc.

Referenced by generatePartonsAndHadronize(), SherpaHadronizer(), and statistics().