30 #include <sys/resource.h> 32 #include "boost/bind.hpp" 34 #include "boost/ptr_container/ptr_deque.hpp" 123 npars_(iConfig.getParameter<uint32_t>(
"numberOfParameters")),
124 nEvents_(iConfig.getUntrackedParameter<uint32_t>(
"nEvents")),
128 <<
"Problem with configuration: " <<
args_.size() <<
" script arguments given, expected " <<
npars_;
130 if (iConfig.
exists(
"nPartonMapping")) {
132 for (
auto&
cfg : processMap) {
133 unsigned processId(
cfg.getParameter<
unsigned>(
"idprup"));
137 if (orderStr ==
"LO")
139 else if (orderStr ==
"NLO")
143 <<
"Invalid order specification for process " << processId <<
": " << orderStr;
145 unsigned np(
cfg.getParameter<
unsigned>(
"np"));
151 produces<LHEXMLStringProduct, edm::Transition::BeginRun>(
"LHEScriptOutput");
153 produces<LHEEventProduct>();
154 produces<LHERunInfoProduct, edm::Transition::BeginRun>();
155 produces<LHERunInfoProduct, edm::Transition::EndRun>();
172 <<
"No lhe event found in ExternalLHEProducer::produce(). " 173 <<
"The likely cause is that the lhe file contains fewer events than were requested, which is possible " 174 <<
"in case of phase space integration or uneweighting efficiency problems.";
177 std::unique_ptr<LHEEventProduct> product(
191 unsigned processId(
partonLevel->getHEPEUP()->IDPRUP);
196 order = procDef.first;
198 }
catch (std::out_of_range&) {
200 <<
"Unexpected IDPRUP encountered: " <<
partonLevel->getHEPEUP()->IDPRUP;
205 product->setNpLO(np);
206 product->setNpNLO(-1);
209 product->setNpLO(-1);
210 product->setNpNLO(np);
225 std::for_each(
runInfo->getHeaders().begin(),
228 std::for_each(
runInfo->getComments().begin(),
252 std::ostringstream eventStream;
255 args_.insert(
args_.begin() + 1, eventStream.str());
263 <<
"The ExternalLHEProducer module requires the RandomNumberGeneratorService\n" 264 "which is not present in the configuration file. You must add the service\n" 265 "in the configuration file if you want to run ExternalLHEProducer";
267 std::ostringstream randomStream;
268 randomStream << rng->
mySeed();
270 args_.insert(
args_.begin() + 2, randomStream.str());
275 for (
unsigned int iArg = 0; iArg <
args_.size(); iArg++) {
276 LogDebug(
"LHEInputArgs") <<
"arg [" << iArg <<
"] = " <<
args_[iArg];
290 instream.seekg(0, instream.end);
291 int insize = instream.tellg();
292 instream.seekg(0, instream.beg);
293 p->fillCompressedContent(instream, 0.25 * insize);
302 unsigned int skip = 0;
303 reader_ = std::make_unique<lhef::LHEReader>(infiles,
skip);
310 std::for_each(
runInfo->getHeaders().begin(),
313 std::for_each(
runInfo->getComments().begin(),
330 std::unique_ptr<LHERunInfoProduct> product(
runInfoProducts.pop_front().release());
337 <<
"Error in ExternalLHEProducer::endRunProduce(). " 338 <<
"Event loop is over, but there are still lhe events to process." 339 <<
"This could happen if lhe file contains more events than requested. This is never expected to happen.";
346 <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
358 if ((dir = opendir(
"/proc/self/fd"))) {
360 while ((dp = readdir(dir)) !=
nullptr) {
361 if ((strcmp(dp->d_name,
".") == 0) || (strcmp(dp->d_name,
"..") == 0)) {
364 if (sscanf(dp->d_name,
"%d", &fd) != 1) {
380 for (fd = 3; fd < maxfd + 1; fd++) {
392 int filedes[2], fd_flags;
400 if ((fd_flags = fcntl(filedes[1], F_GETFD,
NULL)) == -1) {
402 <<
"Failed to get pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
404 if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
406 <<
"Failed to set pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
409 argc = 1 +
args_.size();
411 char**
argv =
new char*[argc + 1];
413 for (
unsigned int i = 1;
i <
argc;
i++) {
414 argv[
i] = strdup(
args_[
i - 1].c_str());
416 argv[
argc] =
nullptr;
422 execvp(argv[0], argv);
425 while ((
write(filedes[1], &rc,
sizeof(
int)) == -1) && (errno == EINTR)) {
431 for (
unsigned int i = 0;
i <
args_.size() + 1;
i++) {
438 <<
"Unable to fork a child (errno=" << errno <<
", " << strerror(errno) <<
")";
443 while (((rc2 =
read(filedes[0], &rc,
sizeof(
int))) == -1) && (errno == EINTR)) {
446 if ((rc2 ==
sizeof(
int)) && rc) {
448 <<
"Failed to execute script (errno=" << rc <<
", " << strerror(rc) <<
")";
455 if (waitpid(pid, &status, 0) < 0) {
456 if (errno == EINTR) {
460 <<
"Failed to read child status (errno=" << errno <<
", " << strerror(errno) <<
")";
463 if (WIFSIGNALED(status)) {
464 throw cms::Exception(
"ExternalLHEProducer") <<
"Child exited due to signal " << WTERMSIG(status) <<
".";
466 if (WIFEXITED(status)) {
467 rc = WEXITSTATUS(status);
472 throw cms::Exception(
"ExternalLHEProducer") <<
"Child failed with exit code " << rc <<
".";
483 if ((fd = open(
outputFile_.c_str(), O_RDONLY)) == -1) {
485 <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
488 std::stringstream
ss;
489 while ((n =
read(fd, buf,
BUFSIZE)) > 0 || (n == -1 && errno == EINTR)) {
495 <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
500 <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
503 return std::unique_ptr<std::string>(
new std::string(ss.str()));
511 desc.
setComment(
"Executes an external script and places its output file into an EDM collection");
516 desc.
add<std::vector<std::string>>(
"args");
517 desc.
add<uint32_t>(
"numberOfParameters");
522 nPartonMappingDesc.
add<
unsigned>(
"idprup");
524 nPartonMappingDesc.add<
unsigned>(
"np");
541 std::shared_ptr<lhef::LHERunInfo> runInfoThis =
partonLevel->getRunInfo();
void beginRunProduce(edm::Run &run, edm::EventSetup const &es) override
VParameterSet const & getParameterSetVector(std::string const &name) const
void endRunProduce(edm::Run &, edm::EventSetup const &) override
std::shared_ptr< lhef::LHEEvent > partonLevel
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
std::unique_ptr< std::string > readOutput()
std::shared_ptr< lhef::LHERunInfo > runInfoLast
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void addHeader(const Header &header)
void addComment(const std::string &line)
ParameterDescriptionBase * addVPSetOptional(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
void produce(edm::Event &, const edm::EventSetup &) override
void addWeight(const WGT &wgt)
bool exists(std::string const ¶meterName) const
checks if a parameter exists
std::vector< std::string > args_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void setComment(std::string const &value)
int closeDescriptors(int preserve)
#define DEFINE_FWK_MODULE(type)
void addDefault(ParameterSetDescription const &psetDescription)
std::unique_ptr< lhef::LHEReader > reader_
std::map< unsigned, std::pair< unsigned, unsigned > > nPartonMapping_
def pipe(cmdline, input=None)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual std::uint32_t mySeed() const =0
std::string outputContents_
std::shared_ptr< lhef::LHERunInfo > runInfo
void addComment(const std::string &line)
~ExternalLHEProducer() override
void preallocThreads(unsigned int) override
void put(std::unique_ptr< PROD > product)
Put a new product.
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
ExternalLHEProducer(const edm::ParameterSet &iConfig)