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