CMS 3D CMS Logo

List of all members | Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
ExternalLHEProducer Class Reference

#include <Example/ExternalLHEProducer/src/ExternalLHEProducer.cc>

Inheritance diagram for ExternalLHEProducer:
edm::one::EDProducer< edm::BeginRunProducer, edm::one::WatchRuns > edm::one::EDProducerBase edm::ProducerBase edm::EDConsumerBase edm::ProductRegistryHelper

Classes

class  FileCloseSentry
 

Public Member Functions

 ExternalLHEProducer (const edm::ParameterSet &iConfig)
 
- Public Member Functions inherited from edm::one::EDProducer< edm::BeginRunProducer, edm::one::WatchRuns >
 EDProducer ()=default
 
SerialTaskQueueglobalLuminosityBlocksQueue () final
 
SerialTaskQueueglobalRunsQueue () final
 
bool hasAbilityToProduceInBeginLumis () const final
 
bool hasAbilityToProduceInBeginProcessBlocks () const final
 
bool hasAbilityToProduceInBeginRuns () const final
 
bool hasAbilityToProduceInEndLumis () const final
 
bool hasAbilityToProduceInEndProcessBlocks () const final
 
bool hasAbilityToProduceInEndRuns () const final
 
bool wantsGlobalLuminosityBlocks () const final
 
bool wantsGlobalRuns () const final
 
bool wantsInputProcessBlocks () const final
 
bool wantsProcessBlocks () const final
 
- Public Member Functions inherited from edm::one::EDProducerBase
 EDProducerBase ()
 
ModuleDescription const & moduleDescription () const
 
bool wantsStreamLuminosityBlocks () const
 
bool wantsStreamRuns () const
 
 ~EDProducerBase () override
 
- Public Member Functions inherited from edm::ProducerBase
void callWhenNewProductsRegistered (std::function< void(BranchDescription const &)> const &func)
 
std::vector< edm::ProductResolverIndex > const & indiciesForPutProducts (BranchType iBranchType) const
 
 ProducerBase ()
 
std::vector< edm::ProductResolverIndex > const & putTokenIndexToProductResolverIndex () const
 
std::vector< bool > const & recordProvenanceList () const
 
void registerProducts (ProducerBase *, ProductRegistry *, ModuleDescription const &)
 
std::function< void(BranchDescription const &)> registrationCallback () const
 used by the fwk to register list of products More...
 
void resolvePutIndicies (BranchType iBranchType, ModuleToResolverIndicies const &iIndicies, std::string const &moduleLabel)
 
TypeLabelList const & typeLabelList () const
 used by the fwk to register the list of products of this module More...
 
 ~ProducerBase () noexcept(false) override
 
- Public Member Functions inherited from edm::EDConsumerBase
std::vector< ConsumesInfoconsumesInfo () const
 
void convertCurrentProcessAlias (std::string const &processName)
 Convert "@currentProcess" in InputTag process names to the actual current process name. More...
 
 EDConsumerBase ()
 
 EDConsumerBase (EDConsumerBase &&)=default
 
 EDConsumerBase (EDConsumerBase const &)=delete
 
ESProxyIndex const * esGetTokenIndices (edm::Transition iTrans) const
 
std::vector< ESProxyIndex > const & esGetTokenIndicesVector (edm::Transition iTrans) const
 
std::vector< ESRecordIndex > const & esGetTokenRecordIndicesVector (edm::Transition iTrans) const
 
ProductResolverIndexAndSkipBit indexFrom (EDGetToken, BranchType, TypeID const &) const
 
void itemsMayGet (BranchType, std::vector< ProductResolverIndexAndSkipBit > &) const
 
void itemsToGet (BranchType, std::vector< ProductResolverIndexAndSkipBit > &) const
 
std::vector< ProductResolverIndexAndSkipBit > const & itemsToGetFrom (BranchType iType) const
 
void labelsForToken (EDGetToken iToken, Labels &oLabels) const
 
void modulesWhoseProductsAreConsumed (std::array< std::vector< ModuleDescription const * > *, NumBranchTypes > &modulesAll, std::vector< ModuleProcessName > &modulesInPreviousProcesses, ProductRegistry const &preg, std::map< std::string, ModuleDescription const * > const &labelsToDesc, std::string const &processName) const
 
EDConsumerBaseoperator= (EDConsumerBase &&)=default
 
EDConsumerBase const & operator= (EDConsumerBase const &)=delete
 
bool registeredToConsume (ProductResolverIndex, bool, BranchType) const
 
bool registeredToConsumeMany (TypeID const &, BranchType) const
 
ProductResolverIndexAndSkipBit uncheckedIndexFrom (EDGetToken) const
 
void updateLookup (BranchType iBranchType, ProductResolverIndexHelper const &, bool iPrefetchMayGet)
 
void updateLookup (eventsetup::ESRecordsToProxyIndices const &)
 
virtual ~EDConsumerBase () noexcept(false)
 

Static Public Member Functions

static void fillDescriptions (edm::ConfigurationDescriptions &descriptions)
 
- Static Public Member Functions inherited from edm::one::EDProducerBase
static const std::string & baseType ()
 
static void fillDescriptions (ConfigurationDescriptions &descriptions)
 
static void prevalidate (ConfigurationDescriptions &descriptions)
 

Private Member Functions

void beginRun (edm::Run const &, edm::EventSetup const &) override
 
void beginRunProduce (edm::Run &run, edm::EventSetup const &es) override
 
int closeDescriptors (int preserve) const
 
void endRun (edm::Run const &, edm::EventSetup const &) override
 
void executeScript (std::vector< std::string > const &args, int id) const
 
std::unique_ptr< LHERunInfoProductgenerateRunInfo (std::vector< std::string > const &files) const
 
std::vector< std::string > makeArgs (uint32_t nEvents, unsigned int nThreads, std::uint32_t seed) const
 
void nextEvent ()
 
void preallocThreads (unsigned int) override
 
void produce (edm::Event &, const edm::EventSetup &) override
 

Private Attributes

const std::vector< std::string > args_
 
edm::EDPutTokenT< LHERunInfoProductbeginRunPutToken_
 
edm::EDPutTokenT< LHEEventProducteventPutToken_
 
bool generateConcurrently_ {false}
 
uint32_t nEvents_
 
uint32_t npars_
 
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_ {}
 
unsigned int nThreads_ {1}
 
std::string outputContents_
 
std::string outputFile_
 
std::shared_ptr< lhef::LHEEventpartonLevel_
 
std::unique_ptr< lhef::LHEReaderreader_
 
std::string scriptName_
 
bool storeXML_
 
bool wasMerged_
 
edm::EDPutTokenT< LHEXMLStringProductxmlPutToken_
 

Additional Inherited Members

- Public Types inherited from edm::one::EDProducerBase
typedef EDProducerBase ModuleType
 
- Public Types inherited from edm::ProducerBase
using ModuleToResolverIndicies = std::unordered_multimap< std::string, std::tuple< edm::TypeID const *, const char *, edm::ProductResolverIndex > >
 
typedef ProductRegistryHelper::TypeLabelList TypeLabelList
 
- Public Types inherited from edm::EDConsumerBase
typedef ProductLabels Labels
 
- Protected Member Functions inherited from edm::ProducerBase
template<class ProductType >
BranchAliasSetterT< ProductType > produces ()
 declare what type of product will make and with which optional label More...
 
template<typename ProductType , BranchType B>
BranchAliasSetterT< ProductType > produces ()
 
template<typename ProductType , Transition B>
BranchAliasSetterT< ProductType > produces ()
 
BranchAliasSetter produces (const TypeID &id, std::string instanceName=std::string(), bool recordProvenance=true)
 
template<BranchType B>
BranchAliasSetter produces (const TypeID &id, std::string instanceName=std::string(), bool recordProvenance=true)
 
template<Transition B>
BranchAliasSetter produces (const TypeID &id, std::string instanceName=std::string(), bool recordProvenance=true)
 
template<typename ProductType , Transition B>
BranchAliasSetterT< ProductType > produces (std::string instanceName)
 
template<class ProductType >
BranchAliasSetterT< ProductType > produces (std::string instanceName)
 
template<typename ProductType , BranchType B>
BranchAliasSetterT< ProductType > produces (std::string instanceName)
 
ProducesCollector producesCollector ()
 
- Protected Member Functions inherited from edm::EDConsumerBase
EDGetToken consumes (const TypeToGet &id, edm::InputTag const &tag)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > consumes (edm::InputTag const &tag)
 
template<BranchType B = InEvent>
EDConsumerBaseAdaptor< B > consumes (edm::InputTag tag) noexcept
 
template<BranchType B>
EDGetToken consumes (TypeToGet const &id, edm::InputTag const &tag)
 
ConsumesCollector consumesCollector ()
 Use a ConsumesCollector to gather consumes information from helper functions. More...
 
template<typename ProductType , BranchType B = InEvent>
void consumesMany ()
 
void consumesMany (const TypeToGet &id)
 
template<BranchType B>
void consumesMany (const TypeToGet &id)
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes ()
 
template<Transition Tr = Transition::Event>
constexpr auto esConsumes () noexcept
 
template<typename ESProduct , typename ESRecord , Transition Tr = Transition::Event>
auto esConsumes (ESInputTag const &tag)
 
template<Transition Tr = Transition::Event>
auto esConsumes (ESInputTag tag) noexcept
 
template<Transition Tr = Transition::Event>
ESGetTokenGeneric esConsumes (eventsetup::EventSetupRecordKey const &iRecord, eventsetup::DataKey const &iKey)
 Used with EventSetupRecord::doGet. More...
 
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<BranchType B>
EDGetToken mayConsume (const TypeToGet &id, edm::InputTag const &tag)
 
template<typename ProductType , BranchType B = InEvent>
EDGetTokenT< ProductType > mayConsume (edm::InputTag const &tag)
 

Detailed Description

Description: [one line class summary]

Implementation: [Notes on implementation]

Definition at line 68 of file ExternalLHEProducer.cc.

Constructor & Destructor Documentation

◆ ExternalLHEProducer()

ExternalLHEProducer::ExternalLHEProducer ( const edm::ParameterSet iConfig)
explicit

Definition at line 127 of file ExternalLHEProducer.cc.

128  : scriptName_((iConfig.getParameter<edm::FileInPath>("scriptName")).fullPath()),
129  outputFile_(iConfig.getParameter<std::string>("outputFile")),
130  args_(iConfig.getParameter<std::vector<std::string>>("args")),
131  npars_(iConfig.getParameter<uint32_t>("numberOfParameters")),
132  nEvents_(iConfig.getUntrackedParameter<uint32_t>("nEvents")),
133  storeXML_(iConfig.getUntrackedParameter<bool>("storeXML")),
134  generateConcurrently_(iConfig.getUntrackedParameter<bool>("generateConcurrently")) {
135  if (npars_ != args_.size())
136  throw cms::Exception("ExternalLHEProducer")
137  << "Problem with configuration: " << args_.size() << " script arguments given, expected " << npars_;
138 
139  if (iConfig.exists("nPartonMapping")) {
140  auto& processMap(iConfig.getParameterSetVector("nPartonMapping"));
141  for (auto& cfg : processMap) {
142  unsigned processId(cfg.getParameter<unsigned>("idprup"));
143 
144  auto orderStr(cfg.getParameter<std::string>("order"));
145  unsigned order(0);
146  if (orderStr == "LO")
147  order = 0;
148  else if (orderStr == "NLO")
149  order = 1;
150  else
151  throw cms::Exception("ExternalLHEProducer")
152  << "Invalid order specification for process " << processId << ": " << orderStr;
153 
154  unsigned np(cfg.getParameter<unsigned>("np"));
155 
156  nPartonMapping_.emplace(processId, std::make_pair(order, np));
157  }
158  }
159 
160  xmlPutToken_ = produces<LHEXMLStringProduct, edm::Transition::BeginRun>("LHEScriptOutput");
161 
162  eventPutToken_ = produces<LHEEventProduct>();
163  beginRunPutToken_ = produces<LHERunInfoProduct, edm::Transition::BeginRun>();
164 }

References args_, beginRunPutToken_, looper::cfg, eventPutToken_, Exception, edm::ParameterSet::exists(), edm::ParameterSet::getParameterSetVector(), np, npars_, nPartonMapping_, eventshapeDQM_cfi::order, AlCaHLTBitMon_QueryRunRegistry::string, and xmlPutToken_.

Member Function Documentation

◆ beginRun()

void ExternalLHEProducer::beginRun ( edm::Run const &  run,
edm::EventSetup const &  es 
)
overrideprivate

Definition at line 328 of file ExternalLHEProducer.cc.

328 {}

◆ beginRunProduce()

void ExternalLHEProducer::beginRunProduce ( edm::Run run,
edm::EventSetup const &  es 
)
overrideprivate

Definition at line 234 of file ExternalLHEProducer.cc.

234  {
235  // pass the number of events as previous to last argument
236 
237  // pass the random number generator seed as last argument
238 
240 
241  if (!rng.isAvailable()) {
242  throw cms::Exception("Configuration")
243  << "The ExternalLHEProducer module requires the RandomNumberGeneratorService\n"
244  "which is not present in the configuration file. You must add the service\n"
245  "in the configuration file if you want to run ExternalLHEProducer";
246  }
247 
248  std::vector<std::string> infiles;
249  auto const seed = rng->mySeed();
250  if (generateConcurrently_) {
251  infiles.resize(nThreads_);
252  auto const nEventsAve = nEvents_ / nThreads_;
253  unsigned int const overflow = nThreads_ - (nEvents_ % nThreads_);
254  std::exception_ptr except;
255  std::atomic<char> exceptSet{0};
256 
257  tbb::this_task_arena::isolate([this, &except, &infiles, &exceptSet, nEventsAve, overflow, seed]() {
258  tbb::task_group group;
259  for (unsigned int t = 0; t < nThreads_; ++t) {
260  uint32_t nEvents = nEventsAve;
261  if (nEvents_ % nThreads_ != 0 and t >= overflow) {
262  nEvents += 1;
263  }
264  group.run([t, this, &infiles, seed, nEvents, &except, &exceptSet]() {
265  CMS_SA_ALLOW try {
266  using namespace std::filesystem;
267  using namespace std::string_literals;
268  auto out = path("thread"s + std::to_string(t)) / path(outputFile_);
269  infiles[t] = out.native();
271  } catch (...) {
272  char expected = 0;
273  if (exceptSet.compare_exchange_strong(expected, 1)) {
274  except = std::current_exception();
275  exceptSet.store(2);
276  }
277  }
278  });
279  }
280  group.wait();
281  });
282  if (exceptSet != 0) {
283  std::rethrow_exception(except);
284  }
285  } else {
286  infiles = std::vector<std::string>(1, outputFile_);
288  }
289 
290  //fill LHEXMLProduct (streaming read directly into compressed buffer to save memory)
291  std::unique_ptr<LHEXMLStringProduct> p(new LHEXMLStringProduct);
292 
293  //store the XML file only if explictly requested
294  if (storeXML_) {
296  if (generateConcurrently_) {
297  using namespace std::filesystem;
298  file = (path("thread0") / path(outputFile_)).native();
299  } else {
300  file = outputFile_;
301  }
302  std::ifstream instream(file);
303  if (!instream) {
304  throw cms::Exception("OutputOpenError") << "Unable to open script output file " << outputFile_ << ".";
305  }
306  instream.seekg(0, instream.end);
307  int insize = instream.tellg();
308  instream.seekg(0, instream.beg);
309  p->fillCompressedContent(instream, 0.25 * insize);
310  instream.close();
311  }
312  run.put(xmlPutToken_, std::move(p));
313 
314  //Read the beginning of each file to get the run info in order to do the merge
315  auto runInfo = generateRunInfo(infiles);
316  if (runInfo) {
318  }
319 
320  // LHE C++ classes translation
321  // (read back uncompressed file from disk in streaming mode again to save memory)
322  unsigned int skip = 0;
323  reader_ = std::make_unique<lhef::LHEReader>(infiles, skip);
324 
325  nextEvent();
326 }

References beginRunPutToken_, CMS_SA_ALLOW, Exception, executeScript(), FrontierConditions_GlobalTag_cff::file, generateConcurrently_, generateRunInfo(), watchdog::group, edm::Service< T >::isAvailable(), makeArgs(), eostools::move(), edm::RandomNumberGenerator::mySeed(), nEvents, nEvents_, nextEvent(), nThreads_, MillePedeFileConverter_cfg::out, outputFile_, AlCaHLTBitMon_ParallelJobs::p, castor_dqm_sourceclient_file_cfg::path, reader_, writedatasetfile::run, submitPVValidationJobs::runInfo, alignCSCRings::s, fileCollector::seed, optionsL1T::skip, storeXML_, AlCaHLTBitMon_QueryRunRegistry::string, submitPVValidationJobs::t, and xmlPutToken_.

◆ closeDescriptors()

int ExternalLHEProducer::closeDescriptors ( int  preserve) const
private

Definition at line 380 of file ExternalLHEProducer.cc.

380  {
381  int maxfd = 1024;
382  int fd;
383 #ifdef __linux__
384  DIR* dir;
385  struct dirent* dp;
386  maxfd = preserve;
387  if ((dir = opendir("/proc/self/fd"))) {
388  errno = 0;
389  while ((dp = readdir(dir)) != nullptr) {
390  if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) {
391  continue;
392  }
393  if (sscanf(dp->d_name, "%d", &fd) != 1) {
394  //throw cms::Exception("closeDescriptors") << "Found unexpected filename in /proc/self/fd: " << dp->d_name;
395  return -1;
396  }
397  if (fd > maxfd) {
398  maxfd = fd;
399  }
400  }
401  if (errno) {
402  //throw cms::Exception("closeDescriptors") << "Unable to determine the number of fd (errno=" << errno << ", " << strerror(errno) << ").";
403  return errno;
404  }
405  closedir(dir);
406  }
407 #endif
408  // TODO: assert for an unreasonable number of fds?
409  for (fd = 3; fd < maxfd + 1; fd++) {
410  if (fd != preserve)
411  close(fd);
412  }
413  return 0;
414 }

References dqmPostProcessing_online::DIR, DeadROC_duringRun::dir, Calorimetry_cff::dp, and ztee::fd.

Referenced by executeScript().

◆ endRun()

void ExternalLHEProducer::endRun ( edm::Run const &  run,
edm::EventSetup const &  es 
)
overrideprivate

Definition at line 330 of file ExternalLHEProducer.cc.

330  {
331  nextEvent();
332  if (partonLevel_) {
334  << "Error in ExternalLHEProducer::endRunProduce(). "
335  << "Event loop is over, but there are still lhe events to process."
336  << "This could happen if lhe file contains more events than requested. This is never expected to happen.";
337  }
338 
339  reader_.reset();
340  if (generateConcurrently_) {
341  for (unsigned int t = 0; t < nThreads_; ++t) {
342  using namespace std::filesystem;
343  using namespace std::string_literals;
344  auto out = path("thread"s + std::to_string(t)) / path(outputFile_);
345  if (unlink(out.c_str())) {
346  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << out
347  << " (errno=" << errno << ", " << strerror(errno) << ").";
348  }
349  }
350  } else {
351  if (unlink(outputFile_.c_str())) {
352  throw cms::Exception("OutputDeleteError") << "Unable to delete original script output file " << outputFile_
353  << " (errno=" << errno << ", " << strerror(errno) << ").";
354  }
355  }
356 }

References edm::errors::EventGenerationFailure, Exception, generateConcurrently_, nextEvent(), nThreads_, MillePedeFileConverter_cfg::out, outputFile_, partonLevel_, castor_dqm_sourceclient_file_cfg::path, reader_, alignCSCRings::s, and submitPVValidationJobs::t.

◆ executeScript()

void ExternalLHEProducer::executeScript ( std::vector< std::string > const &  args,
int  id 
) const
private

Definition at line 417 of file ExternalLHEProducer.cc.

417  {
418  // Fork a script, wait until it finishes.
419 
420  int rc = 0, rc2 = 0;
421  int filedes[2], fd_flags;
422 
423  if (pipe(filedes)) {
424  throw cms::Exception("Unable to create a new pipe");
425  }
426  FileCloseSentry sentry1(filedes[0]), sentry2(filedes[1]);
427 
428  if ((fd_flags = fcntl(filedes[1], F_GETFD, NULL)) == -1) {
429  throw cms::Exception("ExternalLHEProducer")
430  << "Failed to get pipe file descriptor flags (errno=" << rc << ", " << strerror(rc) << ")";
431  }
432  if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
433  throw cms::Exception("ExternalLHEProducer")
434  << "Failed to set pipe file descriptor flags (errno=" << rc << ", " << strerror(rc) << ")";
435  }
436 
437  unsigned int argc = 1 + args.size();
438  // TODO: assert that we have a reasonable number of arguments
439  char** argv = new char*[argc + 1];
440  argv[0] = strdup(scriptName_.c_str());
441  for (unsigned int i = 1; i < argc; i++) {
442  argv[i] = strdup(args[i - 1].c_str());
443  }
444  argv[argc] = nullptr;
445 
446  pid_t pid = fork();
447  if (pid == 0) {
448  // The child process
449  if (!(rc = closeDescriptors(filedes[1]))) {
450  if (generateConcurrently_) {
451  using namespace std::filesystem;
452  using namespace std::string_literals;
453  std::error_code ec;
454  auto newDir = path("thread"s + std::to_string(id));
455  create_directory(newDir, ec);
456  current_path(newDir, ec);
457  }
458  execvp(argv[0], argv); // If execv returns, we have an error.
459  rc = errno;
460  }
461  while ((write(filedes[1], &rc, sizeof(int)) == -1) && (errno == EINTR)) {
462  }
463  _exit(1);
464  }
465 
466  // Free the arg vector ASAP
467  for (unsigned int i = 0; i < args.size() + 1; i++) {
468  free(argv[i]);
469  }
470  delete[] argv;
471 
472  if (pid == -1) {
473  throw cms::Exception("ForkException")
474  << "Unable to fork a child (errno=" << errno << ", " << strerror(errno) << ")";
475  }
476 
477  close(filedes[1]);
478  // If the exec succeeds, the read will fail.
479  while (((rc2 = read(filedes[0], &rc, sizeof(int))) == -1) && (errno == EINTR)) {
480  rc2 = 0;
481  }
482  if ((rc2 == sizeof(int)) && rc) {
483  throw cms::Exception("ExternalLHEProducer")
484  << "Failed to execute script (errno=" << rc << ", " << strerror(rc) << ")";
485  }
486  close(filedes[0]);
487 
488  int status = 0;
489  errno = 0;
490  do {
491  if (waitpid(pid, &status, 0) < 0) {
492  if (errno == EINTR) {
493  continue;
494  } else {
495  throw cms::Exception("ExternalLHEProducer")
496  << "Failed to read child status (errno=" << errno << ", " << strerror(errno) << ")";
497  }
498  }
499  if (WIFSIGNALED(status)) {
500  throw cms::Exception("ExternalLHEProducer") << "Child exited due to signal " << WTERMSIG(status) << ".";
501  }
502  if (WIFEXITED(status)) {
503  rc = WEXITSTATUS(status);
504  break;
505  }
506  } while (true);
507  if (rc) {
508  throw cms::Exception("ExternalLHEProducer") << "Child failed with exit code " << rc << ".";
509  }
510 }

References dir2webdir::argc, writedatasetfile::args, cmsBatch::argv, closeDescriptors(), Exception, generateConcurrently_, mps_fire::i, NULL, castor_dqm_sourceclient_file_cfg::path, pipe::pipe(), readEcalDQMStatus::read, alignCSCRings::s, scriptName_, mps_update::status, and writeEcalDQMStatus::write.

Referenced by beginRunProduce().

◆ fillDescriptions()

void ExternalLHEProducer::fillDescriptions ( edm::ConfigurationDescriptions descriptions)
static

Definition at line 513 of file ExternalLHEProducer.cc.

513  {
514  //The following says we do not know what parameters are allowed so do no validation
515  // Please change this to state exactly what you do use, even if it is no parameters
517  desc.setComment("Executes an external script and places its output file into an EDM collection");
518 
519  edm::FileInPath thePath;
520  desc.add<edm::FileInPath>("scriptName", thePath);
521  desc.add<std::string>("outputFile", "myoutput");
522  desc.add<std::vector<std::string>>("args");
523  desc.add<uint32_t>("numberOfParameters");
524  desc.addUntracked<uint32_t>("nEvents");
525  desc.addUntracked<bool>("storeXML", false);
526  desc.addUntracked<bool>("generateConcurrently", false)
527  ->setComment("If true, run the script concurrently in separate processes.");
528 
529  edm::ParameterSetDescription nPartonMappingDesc;
530  nPartonMappingDesc.add<unsigned>("idprup");
531  nPartonMappingDesc.add<std::string>("order");
532  nPartonMappingDesc.add<unsigned>("np");
533  desc.addVPSetOptional("nPartonMapping", nPartonMappingDesc);
534 
535  descriptions.addDefault(desc);
536 }

References edm::ParameterSetDescription::add(), edm::ConfigurationDescriptions::addDefault(), submitPVResolutionJobs::desc, and AlCaHLTBitMon_QueryRunRegistry::string.

◆ generateRunInfo()

std::unique_ptr< LHERunInfoProduct > ExternalLHEProducer::generateRunInfo ( std::vector< std::string > const &  files) const
private

Definition at line 538 of file ExternalLHEProducer.cc.

538  {
539  std::unique_ptr<LHERunInfoProduct> retValue;
540  //read each file in turn and only get the header info
541  for (auto const& file : iFiles) {
542  unsigned int skip = 0;
543  std::vector<std::string> infiles(1, file);
544  auto reader = std::make_unique<lhef::LHEReader>(infiles, skip);
545  auto parton = reader->next();
546  if (!parton) {
547  break;
548  }
549  auto runInfo = parton->getRunInfo();
550  LHERunInfoProduct product(*runInfo->getHEPRUP());
551 
552  std::for_each(runInfo->getHeaders().begin(),
553  runInfo->getHeaders().end(),
554  std::bind(&LHERunInfoProduct::addHeader, &product, std::placeholders::_1));
555  std::for_each(runInfo->getComments().begin(),
556  runInfo->getComments().end(),
557  std::bind(&LHERunInfoProduct::addComment, &product, std::placeholders::_1));
558  if (not retValue) {
559  retValue = std::make_unique<LHERunInfoProduct>(std::move(product));
560  } else {
561  retValue->mergeProduct(product);
562  }
563  }
564 
565  return retValue;
566 }

References LHERunInfoProduct::addComment(), LHERunInfoProduct::addHeader(), FrontierConditions_GlobalTag_cff::file, eostools::move(), DQM::reader, submitPVValidationJobs::runInfo, and optionsL1T::skip.

Referenced by beginRunProduce().

◆ makeArgs()

std::vector< std::string > ExternalLHEProducer::makeArgs ( uint32_t  nEvents,
unsigned int  nThreads,
std::uint32_t  seed 
) const
private

Definition at line 358 of file ExternalLHEProducer.cc.

360  {
361  std::vector<std::string> args;
362  args.reserve(3 + args_.size());
363 
364  args.push_back(args_.front());
365  args.push_back(std::to_string(nEvents));
366 
367  args.push_back(std::to_string(seed));
368 
369  args.push_back(std::to_string(nThreads));
370  std::copy(args_.begin() + 1, args_.end(), std::back_inserter(args));
371 
372  for (unsigned int iArg = 0; iArg < args.size(); iArg++) {
373  LogDebug("LHEInputArgs") << "arg [" << iArg << "] = " << args[iArg];
374  }
375 
376  return args;
377 }

References writedatasetfile::args, args_, filterCSVwithJSON::copy, LogDebug, nEvents, runTheMatrix::nThreads, and fileCollector::seed.

Referenced by beginRunProduce().

◆ nextEvent()

void ExternalLHEProducer::nextEvent ( )
private

Definition at line 568 of file ExternalLHEProducer.cc.

568  {
569  if (partonLevel_)
570  return;
571 
572  if (not reader_) {
573  return;
574  }
575 
576  partonLevel_ = reader_->next();
577  if (!partonLevel_) {
578  //see if we have another file to read;
579  bool newFileOpened;
580  do {
581  newFileOpened = false;
582  partonLevel_ = reader_->next(&newFileOpened);
583  } while (newFileOpened && !partonLevel_);
584  }
585 }

References partonLevel_, and reader_.

Referenced by beginRunProduce(), endRun(), and produce().

◆ preallocThreads()

void ExternalLHEProducer::preallocThreads ( unsigned int  iThreads)
overrideprivatevirtual

Reimplemented from edm::one::EDProducerBase.

Definition at line 171 of file ExternalLHEProducer.cc.

171 { nThreads_ = iThreads; }

References nThreads_.

◆ produce()

void ExternalLHEProducer::produce ( edm::Event iEvent,
const edm::EventSetup iSetup 
)
overrideprivatevirtual

Implements edm::one::EDProducerBase.

Definition at line 174 of file ExternalLHEProducer.cc.

174  {
175  nextEvent();
176  if (!partonLevel_) {
178  << "No lhe event found in ExternalLHEProducer::produce(). "
179  << "The likely cause is that the lhe file contains fewer events than were requested, which is possible "
180  << "in case of phase space integration or uneweighting efficiency problems.";
181  }
182 
183  std::unique_ptr<LHEEventProduct> product(
184  new LHEEventProduct(*partonLevel_->getHEPEUP(), partonLevel_->originalXWGTUP()));
185  if (partonLevel_->getPDF()) {
186  product->setPDF(*partonLevel_->getPDF());
187  }
188  std::for_each(partonLevel_->weights().begin(),
189  partonLevel_->weights().end(),
190  std::bind(&LHEEventProduct::addWeight, product.get(), std::placeholders::_1));
191  product->setScales(partonLevel_->scales());
192  if (nPartonMapping_.empty()) {
193  product->setNpLO(partonLevel_->npLO());
194  product->setNpNLO(partonLevel_->npNLO());
195  } else {
196  // overwrite npLO and npNLO values by user-specified mapping
197  unsigned processId(partonLevel_->getHEPEUP()->IDPRUP);
198  unsigned order(0);
199  unsigned np(0);
200  try {
201  auto procDef(nPartonMapping_.at(processId));
202  order = procDef.first;
203  np = procDef.second;
204  } catch (std::out_of_range&) {
205  throw cms::Exception("ExternalLHEProducer")
206  << "Unexpected IDPRUP encountered: " << partonLevel_->getHEPEUP()->IDPRUP;
207  }
208 
209  switch (order) {
210  case 0:
211  product->setNpLO(np);
212  product->setNpNLO(-1);
213  break;
214  case 1:
215  product->setNpLO(-1);
216  product->setNpNLO(np);
217  break;
218  default:
219  break;
220  }
221  }
222 
223  std::for_each(partonLevel_->getComments().begin(),
224  partonLevel_->getComments().end(),
225  std::bind(&LHEEventProduct::addComment, product.get(), std::placeholders::_1));
226 
227  iEvent.put(eventPutToken_, std::move(product));
228 
229  partonLevel_.reset();
230  return;
231 }

References LHEEventProduct::addComment(), LHEEventProduct::addWeight(), edm::errors::EventGenerationFailure, eventPutToken_, Exception, iEvent, lhe2HepMCConverter_cff::LHEEventProduct, eostools::move(), nextEvent(), np, nPartonMapping_, eventshapeDQM_cfi::order, and partonLevel_.

Member Data Documentation

◆ args_

const std::vector<std::string> ExternalLHEProducer::args_
private

◆ beginRunPutToken_

edm::EDPutTokenT<LHERunInfoProduct> ExternalLHEProducer::beginRunPutToken_
private

Definition at line 108 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and ExternalLHEProducer().

◆ eventPutToken_

edm::EDPutTokenT<LHEEventProduct> ExternalLHEProducer::eventPutToken_
private

Definition at line 107 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer(), and produce().

◆ generateConcurrently_

bool ExternalLHEProducer::generateConcurrently_ {false}
private

Definition at line 97 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), endRun(), and executeScript().

◆ nEvents_

uint32_t ExternalLHEProducer::nEvents_
private

Definition at line 93 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

◆ npars_

uint32_t ExternalLHEProducer::npars_
private

Definition at line 92 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer().

◆ nPartonMapping_

std::map<unsigned, std::pair<unsigned, unsigned> > ExternalLHEProducer::nPartonMapping_ {}
private

Definition at line 100 of file ExternalLHEProducer.cc.

Referenced by ExternalLHEProducer(), and produce().

◆ nThreads_

unsigned int ExternalLHEProducer::nThreads_ {1}
private

Definition at line 95 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), endRun(), and preallocThreads().

◆ outputContents_

std::string ExternalLHEProducer::outputContents_
private

Definition at line 96 of file ExternalLHEProducer.cc.

◆ outputFile_

std::string ExternalLHEProducer::outputFile_
private

Definition at line 90 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and endRun().

◆ partonLevel_

std::shared_ptr<lhef::LHEEvent> ExternalLHEProducer::partonLevel_
private

Definition at line 103 of file ExternalLHEProducer.cc.

Referenced by endRun(), nextEvent(), and produce().

◆ reader_

std::unique_ptr<lhef::LHEReader> ExternalLHEProducer::reader_
private

Definition at line 102 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), endRun(), and nextEvent().

◆ scriptName_

std::string ExternalLHEProducer::scriptName_
private

Definition at line 89 of file ExternalLHEProducer.cc.

Referenced by executeScript().

◆ storeXML_

bool ExternalLHEProducer::storeXML_
private

Definition at line 94 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce().

◆ wasMerged_

bool ExternalLHEProducer::wasMerged_
private

Definition at line 104 of file ExternalLHEProducer.cc.

◆ xmlPutToken_

edm::EDPutTokenT<LHEXMLStringProduct> ExternalLHEProducer::xmlPutToken_
private

Definition at line 106 of file ExternalLHEProducer.cc.

Referenced by beginRunProduce(), and ExternalLHEProducer().

writedatasetfile.args
args
Definition: writedatasetfile.py:18
dqmPostProcessing_online.DIR
DIR
Definition: dqmPostProcessing_online.py:10
cmsBatch.argv
argv
Definition: cmsBatch.py:279
mps_fire.i
i
Definition: mps_fire.py:428
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
ExternalLHEProducer::nEvents_
uint32_t nEvents_
Definition: ExternalLHEProducer.cc:93
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
dir2webdir.argc
argc
Definition: dir2webdir.py:39
ExternalLHEProducer::xmlPutToken_
edm::EDPutTokenT< LHEXMLStringProduct > xmlPutToken_
Definition: ExternalLHEProducer.cc:106
mps_update.status
status
Definition: mps_update.py:69
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
np
int np
Definition: AMPTWrapper.h:43
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
ExternalLHEProducer::beginRunPutToken_
edm::EDPutTokenT< LHERunInfoProduct > beginRunPutToken_
Definition: ExternalLHEProducer.cc:108
edm::errors::EventGenerationFailure
Definition: EDMException.h:70
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
edm::RandomNumberGenerator::mySeed
virtual std::uint32_t mySeed() const =0
ExternalLHEProducer::closeDescriptors
int closeDescriptors(int preserve) const
Definition: ExternalLHEProducer.cc:380
LHERunInfoProduct::addHeader
void addHeader(const Header &header)
Definition: LHERunInfoProduct.h:52
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
ExternalLHEProducer::generateRunInfo
std::unique_ptr< LHERunInfoProduct > generateRunInfo(std::vector< std::string > const &files) const
Definition: ExternalLHEProducer.cc:538
lhe2HepMCConverter_cff.LHEEventProduct
LHEEventProduct
Definition: lhe2HepMCConverter_cff.py:4
fileCollector.seed
seed
Definition: fileCollector.py:127
edm::FileInPath
Definition: FileInPath.h:64
optionsL1T.skip
skip
Definition: optionsL1T.py:30
ztee.fd
fd
Definition: ztee.py:136
alignCSCRings.s
s
Definition: alignCSCRings.py:92
ExternalLHEProducer::makeArgs
std::vector< std::string > makeArgs(uint32_t nEvents, unsigned int nThreads, std::uint32_t seed) const
Definition: ExternalLHEProducer.cc:358
LHERunInfoProduct
Definition: LHERunInfoProduct.h:13
Calorimetry_cff.dp
dp
Definition: Calorimetry_cff.py:158
DQM.reader
reader
Definition: DQM.py:105
LHERunInfoProduct::addComment
void addComment(const std::string &line)
Definition: LHERunInfoProduct.h:53
ExternalLHEProducer::executeScript
void executeScript(std::vector< std::string > const &args, int id) const
Definition: ExternalLHEProducer.cc:417
runTheMatrix.nThreads
nThreads
Definition: runTheMatrix.py:370
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
ExternalLHEProducer::args_
const std::vector< std::string > args_
Definition: ExternalLHEProducer.cc:91
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:233
ExternalLHEProducer::scriptName_
std::string scriptName_
Definition: ExternalLHEProducer.cc:89
eventshapeDQM_cfi.order
order
Definition: eventshapeDQM_cfi.py:8
ExternalLHEProducer::outputFile_
std::string outputFile_
Definition: ExternalLHEProducer.cc:90
edm::Service< edm::RandomNumberGenerator >
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
iEvent
int iEvent
Definition: GenABIO.cc:224
ExternalLHEProducer::nThreads_
unsigned int nThreads_
Definition: ExternalLHEProducer.cc:95
NULL
#define NULL
Definition: scimark2.h:8
ExternalLHEProducer::nPartonMapping_
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_
Definition: ExternalLHEProducer.cc:100
looper.cfg
cfg
Definition: looper.py:297
readEcalDQMStatus.read
read
Definition: readEcalDQMStatus.py:38
writeEcalDQMStatus.write
write
Definition: writeEcalDQMStatus.py:48
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
eostools.move
def move(src, dest)
Definition: eostools.py:511
writedatasetfile.run
run
Definition: writedatasetfile.py:27
ExternalLHEProducer::generateConcurrently_
bool generateConcurrently_
Definition: ExternalLHEProducer.cc:97
Exception
Definition: hltDiff.cc:245
ExternalLHEProducer::reader_
std::unique_ptr< lhef::LHEReader > reader_
Definition: ExternalLHEProducer.cc:102
submitPVValidationJobs.runInfo
dictionary runInfo
Definition: submitPVValidationJobs.py:1013
ExternalLHEProducer::eventPutToken_
edm::EDPutTokenT< LHEEventProduct > eventPutToken_
Definition: ExternalLHEProducer.cc:107
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ExternalLHEProducer::npars_
uint32_t npars_
Definition: ExternalLHEProducer.cc:92
edm::ParameterSet::getParameterSetVector
VParameterSet const & getParameterSetVector(std::string const &name) const
Definition: ParameterSet.cc:2160
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
cms::Exception
Definition: Exception.h:70
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
ExternalLHEProducer::nextEvent
void nextEvent()
Definition: ExternalLHEProducer.cc:568
pipe.pipe
def pipe(cmdline, input=None)
Definition: pipe.py:5
LHEXMLStringProduct
Definition: LHEXMLStringProduct.h:12
nEvents
UInt_t nEvents
Definition: hcalCalib.cc:40
ExternalLHEProducer::storeXML_
bool storeXML_
Definition: ExternalLHEProducer.cc:94
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
ExternalLHEProducer::partonLevel_
std::shared_ptr< lhef::LHEEvent > partonLevel_
Definition: ExternalLHEProducer.cc:103
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23
watchdog.group
group
Definition: watchdog.py:82
LHEEventProduct::addComment
void addComment(const std::string &line)
Definition: LHEEventProduct.h:32
LHEEventProduct::addWeight
void addWeight(const WGT &wgt)
Definition: LHEEventProduct.h:31