31 #include "boost/bind.hpp" 32 #include "boost/shared_ptr.hpp" 33 #include "boost/ptr_container/ptr_deque.hpp" 66 edm::EndRunProducer> {
97 boost::shared_ptr<lhef::LHERunInfo>
runInfo;
131 npars_(iConfig.getParameter<uint32_t>(
"numberOfParameters")),
132 nEvents_(iConfig.getUntrackedParameter<uint32_t>(
"nEvents"))
135 throw cms::Exception(
"ExternalLHEProducer") <<
"Problem with configuration: " <<
args_.size() <<
" script arguments given, expected " <<
npars_;
136 produces<LHEXMLStringProduct, edm::Transition::BeginRun>(
"LHEScriptOutput");
138 produces<LHEEventProduct>();
139 produces<LHERunInfoProduct, edm::Transition::BeginRun>();
166 throw cms::Exception(
"ExternalLHEProducer") <<
"No lhe event found in ExternalLHEProducer::produce(). " 167 <<
"The likely cause is that the lhe file contains fewer events than were requested, which is possible " 168 <<
"in case of phase space integration or uneweighting efficiency problems.";
171 std::unique_ptr<LHEEventProduct> product(
194 std::for_each(
runInfo->getHeaders().begin(),
198 std::for_each(
runInfo->getComments().begin(),
226 std::ostringstream eventStream;
228 args_.push_back(eventStream.str());
236 <<
"The ExternalLHEProducer module requires the RandomNumberGeneratorService\n" 237 "which is not present in the configuration file. You must add the service\n" 238 "in the configuration file if you want to run ExternalLHEProducer";
240 std::ostringstream randomStream;
241 randomStream << rng->
mySeed();
242 args_.push_back(randomStream.str());
246 for (
unsigned int iArg = 0; iArg <
args_.size() ; iArg++ ) {
247 LogDebug(
"LHEInputArgs") <<
"arg [" << iArg <<
"] = " <<
args_[iArg];
258 instream.seekg (0, instream.end);
259 int insize = instream.tellg();
260 instream.seekg (0, instream.beg);
261 p->fillCompressedContent(instream, 0.25*insize);
269 unsigned int skip = 0;
270 std::auto_ptr<lhef::LHEReader> thisRead(
new lhef::LHEReader(infiles, skip));
278 std::for_each(
runInfo->getHeaders().begin(),
282 std::for_each(
runInfo->getComments().begin(),
304 std::unique_ptr<LHERunInfoProduct> product(
runInfoProducts.pop_front().release());
310 throw cms::Exception(
"ExternalLHEProducer") <<
"Error in ExternalLHEProducer::endRunProduce(). " 311 <<
"Event loop is over, but there are still lhe events to process." 312 <<
"This could happen if lhe file contains more events than requested. This is never expected to happen.";
318 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
333 if ((dir = opendir(
"/proc/self/fd"))) {
335 while ((dp = readdir (dir)) !=
NULL) {
336 if ((strcmp(dp->d_name,
".") == 0) || (strcmp(dp->d_name,
"..") == 0)) {
339 if (sscanf(dp->d_name,
"%d", &fd) != 1) {
355 for (fd=3; fd<maxfd+1; fd++) {
370 int filedes[2], fd_flags;
378 if ((fd_flags = fcntl(filedes[1], F_GETFD,
NULL)) == -1) {
379 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to get pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
381 if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
382 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to set pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
385 argc = 1 +
args_.size();
387 char **
argv =
new char *[argc+1];
389 for (
unsigned int i=1;
i<
argc;
i++) {
390 argv[
i] = strdup(
args_[
i-1].c_str());
398 execvp(argv[0], argv);
401 while ((
write(filedes[1], &rc,
sizeof(
int)) == -1) && (errno == EINTR)) {}
406 for (
unsigned int i=0;
i<
args_.size()+1;
i++) {
412 throw cms::Exception(
"ForkException") <<
"Unable to fork a child (errno=" << errno <<
", " << strerror(errno) <<
")";
417 while (((rc2 = read(filedes[0], &rc,
sizeof(
int))) == -1) && (errno == EINTR)) { rc2 = 0; }
418 if ((rc2 ==
sizeof(
int)) && rc) {
419 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to execute script (errno=" << rc <<
", " << strerror(rc) <<
")";
426 if (waitpid(pid, &status, 0) < 0) {
427 if (errno == EINTR) {
430 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to read child status (errno=" << errno <<
", " << strerror(errno) <<
")";
433 if (WIFSIGNALED(status)) {
434 throw cms::Exception(
"ExternalLHEProducer") <<
"Child exited due to signal " << WTERMSIG(status) <<
".";
436 if (WIFEXITED(status)) {
437 rc = WEXITSTATUS(status);
442 throw cms::Exception(
"ExternalLHEProducer") <<
"Child failed with exit code " << rc <<
".";
455 if ((fd = open(
outputFile_.c_str(), O_RDONLY)) == -1) {
456 throw cms::Exception(
"OutputOpenError") <<
"Unable to open script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
459 std::stringstream ss;
460 while ((n = read(fd, buf,
BUFSIZE)) > 0 || (n == -1 && errno == EINTR)) {
465 throw cms::Exception(
"OutputOpenError") <<
"Unable to read from script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
469 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
472 return std::unique_ptr<std::string>(
new std::string(ss.str()));
481 desc.
setComment(
"Executes an external script and places its output file into an EDM collection");
486 desc.
add<std::vector<std::string> >(
"args");
487 desc.
add<uint32_t>(
"numberOfParameters");
503 boost::shared_ptr<lhef::LHERunInfo> runInfoThis =
partonLevel->getRunInfo();
boost::shared_ptr< lhef::LHEEvent > partonLevel
virtual void beginRunProduce(edm::Run &run, edm::EventSetup const &es) override
virtual void endRunProduce(edm::Run &, edm::EventSetup const &) override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
std::unique_ptr< std::string > readOutput()
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void addHeader(const Header &header)
void addComment(const std::string &line)
#define DEFINE_FWK_MODULE(type)
virtual void produce(edm::Event &, const edm::EventSetup &) override
void addWeight(const WGT &wgt)
std::vector< std::string > args_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::auto_ptr< lhef::LHEReader > reader_
boost::shared_ptr< lhef::LHERunInfo > runInfoLast
void setComment(std::string const &value)
int closeDescriptors(int preserve)
void addDefault(ParameterSetDescription const &psetDescription)
def pipe(cmdline, input=None)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual std::uint32_t mySeed() const =0
std::string outputContents_
boost::shared_ptr< lhef::LHERunInfo > runInfo
void addComment(const std::string &line)
virtual void preallocThreads(unsigned int) override
void put(std::unique_ptr< PROD > product)
Put a new product.
virtual ~ExternalLHEProducer()
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
ExternalLHEProducer(const edm::ParameterSet &iConfig)