30 #include <boost/shared_ptr.hpp>
31 #include <boost/ptr_container/ptr_deque.hpp>
95 boost::shared_ptr<lhef::LHERunInfo>
runInfo;
126 scriptName_((iConfig.getParameter<edm::
FileInPath>(
"scriptName")).fullPath().c_str()),
127 outputFile_(iConfig.getParameter<std::string>(
"outputFile")),
128 args_(iConfig.getParameter<std::vector<std::string> >(
"args")),
129 nEvents_(iConfig.getParameter<uint32_t>(
"nEvents"))
131 produces<std::string, edm::InRun>(
"LHEScriptOutput");
133 produces<LHEEventProduct>();
134 produces<LHERunInfoProduct, edm::InRun>();
167 std::for_each(
runInfo->getHeaders().begin(),
171 std::for_each(
runInfo->getComments().begin(),
213 std::ostringstream eventStream;
215 args_.push_back(eventStream.str());
223 <<
"The ExternalLHEProducer module requires the RandomNumberGeneratorService\n"
224 "which is not present in the configuration file. You must add the service\n"
225 "in the configuration file if you want to run ExternalLHEProducer";
227 std::ostringstream randomStream;
228 randomStream << rng->
mySeed();
229 args_.push_back(randomStream.str());
231 for (
unsigned int iArg = 0; iArg <
args_.size() ; iArg++ ) {
232 LogDebug(
"LHEInputArgs") <<
"arg [" << iArg <<
"] = " <<
args_[iArg];
236 std::auto_ptr<std::string> localContents =
readOutput();
239 run.
put(localContents,
"LHEScriptOutput");
243 unsigned int skip = 0;
252 std::for_each(
runInfo->getHeaders().begin(),
256 std::for_each(
runInfo->getComments().begin(),
278 std::auto_ptr<LHERunInfoProduct> product(
runInfoProducts.pop_front().release());
294 if ((dir = opendir(
"/proc/self/fd"))) {
296 while ((dp = readdir (dir)) !=
NULL) {
297 if ((strcmp(dp->d_name,
".") == 0) || (strcmp(dp->d_name,
"..") == 0)) {
300 if (sscanf(dp->d_name,
"%d", &fd) != 1) {
316 for (fd=3; fd<maxfd+1; fd++) {
331 int filedes[2], fd_flags;
339 if ((fd_flags = fcntl(filedes[1], F_GETFD,
NULL)) == -1) {
340 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to get pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
342 if (fcntl(filedes[1], F_SETFD, fd_flags | FD_CLOEXEC) == -1) {
343 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to set pipe file descriptor flags (errno=" << rc <<
", " << strerror(rc) <<
")";
346 argc = 1 +
args_.size();
348 char **argv =
new char *[argc+1];
350 for (
unsigned int i=1;
i<argc;
i++) {
351 argv[
i] = strdup(
args_[
i-1].c_str());
359 execvp(argv[0], argv);
362 while ((write(filedes[1], &rc,
sizeof(
int)) == -1) && (errno == EINTR)) {}
367 for (
unsigned int i=0;
i<
args_.size()+1;
i++) {
373 throw cms::Exception(
"ForkException") <<
"Unable to fork a child (errno=" << errno <<
", " << strerror(errno) <<
")";
378 while (((rc2 =
read(filedes[0], &rc,
sizeof(
int))) == -1) && (errno == EINTR)) { rc2 = 0; }
379 if ((rc2 ==
sizeof(
int)) && rc) {
380 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to execute script (errno=" << rc <<
", " << strerror(rc) <<
")";
387 if (waitpid(pid, &status, 0) < 0) {
388 if (errno == EINTR) {
391 throw cms::Exception(
"ExternalLHEProducer") <<
"Failed to read child status (errno=" << errno <<
", " << strerror(errno) <<
")";
394 if (WIFSIGNALED(status)) {
395 throw cms::Exception(
"ExternalLHEProducer") <<
"Child exited due to signal " << WTERMSIG(status) <<
".";
397 if (WIFEXITED(status)) {
398 rc = WEXITSTATUS(status);
403 throw cms::Exception(
"ExternalLHEProducer") <<
"Child failed with exit code " << rc <<
".";
416 if ((fd = open(
outputFile_.c_str(), O_RDONLY)) == -1) {
417 throw cms::Exception(
"OutputOpenError") <<
"Unable to open script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
420 std::stringstream ss;
421 while ((n =
read(fd, buf,
BUFSIZE)) > 0 || (n == -1 && errno == EINTR)) {
426 throw cms::Exception(
"OutputOpenError") <<
"Unable to read from script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
430 throw cms::Exception(
"OutputDeleteError") <<
"Unable to delete original script output file " <<
outputFile_ <<
" (errno=" << errno <<
", " << strerror(errno) <<
").";
433 return std::auto_ptr<std::string>(
new std::string(ss.str()));
454 desc.
setComment(
"Executes an external script and places its output file into an EDM collection");
458 desc.
add<std::string>(
"outputFile",
"myoutput");
459 desc.
add<std::vector<std::string> >(
"args");
460 desc.
add<uint32_t>(
"nEvents");
475 boost::shared_ptr<lhef::LHERunInfo> runInfoThis =
partonLevel->getRunInfo();
boost::shared_ptr< lhef::LHEEvent > partonLevel
std::auto_ptr< std::string > readOutput()
virtual void produce(edm::Event &, const edm::EventSetup &)
void addHeader(const Header &header)
void addComment(const std::string &line)
#define DEFINE_FWK_MODULE(type)
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)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
virtual void beginRun(edm::Run &run, edm::EventSetup const &es)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::string outputContents_
boost::shared_ptr< lhef::LHERunInfo > runInfo
void addComment(const std::string &line)
virtual void beginLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
void put(std::auto_ptr< PROD > product)
Put a new product.
boost::ptr_deque< LHERunInfoProduct > runInfoProducts
virtual uint32_t mySeed() const =0
Exists for backward compatibility.
ExternalLHEProducer(const edm::ParameterSet &iConfig)
virtual void endLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
virtual void endRun(edm::Run &, edm::EventSetup const &)