CMS 3D CMS Logo

BaseHadronizer.cc
Go to the documentation of this file.
1 
4 
5 #include <random>
6 #include <sys/wait.h>
7 
8 namespace gen {
9 
10  const std::vector<std::string> BaseHadronizer::theSharedResources;
11 
12  BaseHadronizer::BaseHadronizer(edm::ParameterSet const& ps) : randomIndex_(-1), gridpackPaths_(1) {
13  if (ps.exists("RandomizedParameters")) {
14  std::vector<edm::ParameterSet> randomizedParameters =
15  ps.getParameter<std::vector<edm::ParameterSet> >("RandomizedParameters");
16  randomInitWeights_.resize(randomizedParameters.size());
17  randomInitConfigDescriptions_.resize(randomizedParameters.size());
18  gridpackPaths_.resize(randomizedParameters.size());
19  for (unsigned int irand = 0; irand < randomizedParameters.size(); ++irand) {
20  randomInitWeights_[irand] = randomizedParameters[irand].getParameter<double>("ConfigWeight");
21  if (randomizedParameters[irand].exists("ConfigDescription")) {
23  randomizedParameters[irand].getParameter<std::string>("ConfigDescription");
24  }
25  if (randomizedParameters[irand].exists("GridpackPath")) {
26  gridpackPaths_[irand] = randomizedParameters[irand].getParameter<std::string>("GridpackPath");
27  }
28  }
29  } else {
30  if (ps.exists("GridpackPath")) {
31  gridpackPaths_[0] = ps.getParameter<std::string>("GridpackPath");
32  }
33  }
34 
35  runInfo().setFilterEfficiency(ps.getUntrackedParameter<double>("filterEfficiency", -1.));
36  runInfo().setExternalXSecLO(GenRunInfoProduct::XSec(ps.getUntrackedParameter<double>("crossSection", -1.)));
37  runInfo().setExternalXSecNLO(GenRunInfoProduct::XSec(ps.getUntrackedParameter<double>("crossSectionNLO", -1.)));
38  }
39 
40  std::unique_ptr<GenLumiInfoHeader> BaseHadronizer::getGenLumiInfoHeader() const {
41  auto genLumiInfoHeader = std::make_unique<GenLumiInfoHeader>();
42 
43  //fill information on randomized configs for parameter scans
44  genLumiInfoHeader->setRandomConfigIndex(randomIndex_);
45  if (randomIndex_ >= 0) {
46  genLumiInfoHeader->setConfigDescription(randomInitConfigDescription());
47  }
48 
49  return genLumiInfoHeader;
50  }
51 
52  void BaseHadronizer::randomizeIndex(edm::LuminosityBlock const& lumi, CLHEP::HepRandomEngine* rengine) {
53  if (!randomInitWeights_.empty()) {
54  //randomly select from a list of provided configuration sets (for parameter scans)
55 
56  //seeds std 32-bit mersene twister with HepRandomEngine state plus run and lumi section numbers
57  //(random engine state will be the same for every lumi section in a job)
58  std::vector<long unsigned int> seeds = rengine->put();
59  seeds.push_back(lumi.id().run());
60  seeds.push_back(lumi.id().luminosityBlock());
61  std::seed_seq seedseq(seeds.begin(), seeds.end());
62  std::mt19937 randgen(seedseq);
63  std::discrete_distribution<int> randdist(randomInitWeights_.begin(), randomInitWeights_.end());
64 
65  randomIndex_ = randdist(randgen);
66  }
67  }
68 
70  CLHEP::HepRandomEngine* rengine,
71  unsigned int ncpu) {
72  if (gridpackPath().empty()) {
73  return;
74  }
75 
76  //get random seed from HepRandomEngine state plus run and lumi section numbers
77  //(random engine state will be the same for every lumi section in a job)
78  std::vector<long unsigned int> seeds = rengine->put();
79  seeds.push_back(lumi.id().run());
80  seeds.push_back(lumi.id().luminosityBlock());
81  std::seed_seq seedseq(seeds.begin(), seeds.end());
82  std::array<unsigned int, 1> lheseed;
83  seedseq.generate(lheseed.begin(), lheseed.end());
84 
85  constexpr unsigned int maxseed = 30081 * 30081; //madgraph cannot handle seeds larger than this
86  unsigned int seedval = lheseed[0] % (maxseed + 1);
87 
88  unsigned int nevents = edm::pset::Registry::instance()
89  ->getMapped(lumi.processHistory().rbegin()->parameterSetID())
90  ->getParameter<edm::ParameterSet>("@main_input")
91  .getUntrackedParameter<unsigned int>("numberEventsInLuminosityBlock");
92 
93  std::ostringstream nevStream;
94  nevStream << nevents;
95 
96  std::ostringstream randomStream;
97  randomStream << seedval;
98 
99  edm::FileInPath script("GeneratorInterface/LHEInterface/data/run_generic_tarball_cvmfs.sh");
100  const char* outfilename = "cmsgrid_final.lhe";
101 
102  std::array<std::string, 5> argStrs;
103  argStrs[0] = script.fullPath();
104  argStrs[1] = gridpackPath();
105  argStrs[2] = nevStream.str();
106  argStrs[3] = randomStream.str();
107  argStrs[4] = std::to_string(ncpu);
108  std::array<char*, 6> args{
109  {&argStrs[0][0], &argStrs[1][0], &argStrs[2][0], &argStrs[3][0], &argStrs[4][0], nullptr}};
110 
111  pid_t pid = fork();
112 
113  if (pid == -1) {
114  // error, failed to fork()
115  throw cms::Exception("BaseHadronizer::generateLHE") << "Unable to fork a child";
116  } else if (pid == 0) {
117  //child
118  execvp(args[0], std::begin(args));
119  _exit(1); // exec never returns
120  } else {
121  //parent
122  int status;
123  waitpid(pid, &status, 0);
124  if (status) {
125  throw cms::Exception("BaseHadronizer::generateLHE") << "Failed to execute script";
126  }
127  }
128  FILE* lhef = std::fopen(outfilename, "r");
129  if (!lhef) {
130  throw cms::Exception("BaseHadronizer::generateLHE") << "Output file " << outfilename << " not found.";
131  }
132  std::fclose(lhef);
133 
135  }
136 
138  if (lheFile_.empty()) {
139  return;
140  }
141 
142  std::remove(lheFile_.c_str());
143  }
144 
145 } // namespace gen
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::vector< double > randomInitWeights_
void generateLHE(edm::LuminosityBlock const &lumi, CLHEP::HepRandomEngine *rengine, unsigned int ncpu)
bool getMapped(key_type const &k, value_type &result) const
Definition: Registry.cc:17
const std::string & randomInitConfigDescription() const
virtual std::unique_ptr< GenLumiInfoHeader > getGenLumiInfoHeader() const
void setFilterEfficiency(double effic)
BaseHadronizer(edm::ParameterSet const &ps)
const std::string & gridpackPath() const
bool exists(std::string const &parameterName) const
checks if a parameter exists
static std::string to_string(const XMLCh *ch)
T getUntrackedParameter(std::string const &, T const &) const
void randomizeIndex(edm::LuminosityBlock const &lumi, CLHEP::HepRandomEngine *rengine)
GenRunInfoProduct & runInfo()
std::vector< std::string > gridpackPaths_
std::vector< std::string > randomInitConfigDescriptions_
def remove(d, key, TELL=False)
Definition: MatrixUtil.py:223
void setExternalXSecNLO(const XSec &xsec)
void setExternalXSecLO(const XSec &xsec)
static const std::vector< std::string > theSharedResources
static Registry * instance()
Definition: Registry.cc:12