CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LH5Reader.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iomanip>
3 #include <iostream>
4 #include <memory>
5 
6 #include <cstdio>
7 #include <cstring>
8 #include <fstream>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 
17 
21 //#include "GeneratorInterface/LHEInterface/code/HighFive/include/highfive/H5File.hpp"
23 
27 
28 namespace lhef {
29  using namespace lheh5;
30 
31  static void logFileAction(char const *msg, std::string const &fileName) {
32  edm::LogAbsolute("fileAction") << std::setprecision(0) << edm::TimeOfDay() << msg << fileName;
34  }
35 
37  public:
38  Source() {}
39  virtual ~Source() {}
40  std::unique_ptr<H5Handler> handler;
41 
42  private:
43  };
44 
46  public:
47  FileSource(const std::string &fileURL) {
48  const std::string fileName = fileURL.substr(5, fileURL.length());
49 
50  H5Handler *tmph = new H5Handler(fileName);
51  handler.reset(tmph);
52  }
53 
54  ~FileSource() override {}
55  };
56 
58  public:
60  if (inputs.empty())
61  throw cms::Exception("StreamOpenError") << "Empty LHE file string name \"" << std::endl;
62 
63  H5Handler *tmph = new H5Handler(inputs);
64  handler.reset(tmph);
65  }
66 
67  ~StringSource() override {}
68  };
69 
70  H5Handler::H5Handler(const std::string &fileNameIn)
71  : h5file(new HighFive::File(fileNameIn)),
72  indexStatus(h5file->exist("/index")),
73  _index(h5file->getGroup(indexStatus ? "index" : "event")),
74  _particle(h5file->getGroup("particle")),
75  _event(h5file->getGroup("event")),
76  _init(h5file->getGroup("init")),
77  _procInfo(h5file->getGroup("procInfo")) {
78  hid_t dspace;
79  _formatType = 1;
80 
81  if (indexStatus) {
82  dspace = H5Dget_space(h5file->getDataSet("index/start").getId());
83  _formatType = 2;
84  } else {
85  _index = h5file->getGroup("event");
86  dspace = H5Dget_space(h5file->getDataSet("event/start").getId());
87  _formatType = 1;
88  }
89 
90  _eventsTotal = H5Sget_simple_extent_npoints(dspace);
91  _eventsRead = 0;
92  _eventsInBlock = 6400 / 4; // configurable parameter??
93  if (_eventsTotal % _eventsInBlock != 0)
94  throw cms::Exception("ReadError") << "block size does not match HDF5 file" << std::endl;
95  _blocksRead = 0;
96 
97  // Check if the file contains the npLO, npNLO information
98  bool status = h5file->exist("/procInfo/npLO");
99  if (status) {
100  HighFive::DataSet _npLO = _procInfo.getDataSet("npLO");
101  _npLO.read(npLO);
102  HighFive::DataSet _npNLO = _procInfo.getDataSet("npNLO");
103  _npNLO.read(npNLO);
104  } else {
105  npLO = 1;
106  npNLO = 0;
107  }
108  };
109 
110  void H5Handler::counter(const int firstEventIn, const int maxEventsIn) {
111  if (maxEventsIn > 0 && firstEventIn > maxEventsIn)
112  throw cms::Exception("ConfigurationError") << "\" firstEvent > maxEvents \"" << std::endl;
113  // Reset maximum number of events to read
114  if (_blocksRead == 0 && maxEventsIn >= 0 && (unsigned long int)maxEventsIn < _eventsTotal)
115  _eventsTotal = (unsigned long int)maxEventsIn;
116  // If there are multiple files, assume you only want to jump through the first file
117  if (firstEventIn > 0 && _blocksRead > 0)
118  _eventsRead = firstEventIn - 1;
119  // Set blocks read to be in the correct place
121  }
122 
124  // Start counting at 0
125  size_t iStart = _blocksRead * _eventsInBlock;
126  size_t nEvents = _eventsInBlock;
127  if ((unsigned long int)(iStart + nEvents) > _eventsTotal)
128  return;
129  _events1 = readEvents(_index, _particle, _event, iStart, nEvents);
130  _blocksRead++;
131  }
132 
133  std::vector<lheh5::Particle> H5Handler::getEvent() {
134  std::vector<lheh5::Particle> _vE;
135  if (_eventsRead > _eventsTotal - 1)
136  return std::vector<lheh5::Particle>();
137  int checkEvents = (_blocksRead)*_eventsInBlock - 1;
139  _eventsRead++;
140  if (checkEvents >= 0 && _eventsRead > (unsigned long int)checkEvents)
141  readBlock();
142  return _vE;
143  }
144 
146  // fragile, must be called before getEvent
148  }
149 
150  std::pair<lheh5::EventHeader, std::vector<lheh5::Particle> > H5Handler::getEventProperties() {
151  std::vector<lheh5::Particle> _vE;
153  _h.nparticles = -1;
154  if (_eventsRead > _eventsTotal - 1)
155  return std::make_pair(_h, _vE);
156  int checkEvents = (_blocksRead)*_eventsInBlock - 1;
158  _h = _events1.mkEventHeader(_eventsRead % _eventsInBlock);
159  _eventsRead++;
160  if (checkEvents >= 0 && _eventsRead > (unsigned long int)checkEvents)
161  readBlock();
162  return std::make_pair(_h, _vE);
163  }
164 
166  : fileURLs(params.getUntrackedParameter<std::vector<std::string> >("fileNames")),
167  strName(""),
168  firstEvent(params.getUntrackedParameter<unsigned int>("skipEvents", 0)),
169  maxEvents(params.getUntrackedParameter<int>("limitEvents", -1)) {}
170 
171  LH5Reader::LH5Reader(const std::vector<std::string> &fileNames, unsigned int firstEvent, int maxEvents)
172  : fileURLs(fileNames), strName(""), firstEvent(firstEvent), maxEvents(maxEvents), curIndex(0), curDoc(false) {}
173 
175  : strName(inputs), firstEvent(firstEvent), maxEvents(maxEvents), curIndex(0) {}
176 
178 
179  std::shared_ptr<LHEEvent> LH5Reader::next(bool *newFileOpened) {
180  while (curDoc || curIndex < fileURLs.size() || (fileURLs.empty() && !strName.empty())) {
181  if (!curDoc) {
182  if (!fileURLs.empty()) {
183  logFileAction(" Initiating request to open LHE file ", fileURLs[curIndex]);
184  curSource = std::make_unique<FileSource>(fileURLs[curIndex]);
185  logFileAction(" Successfully opened LHE file ", fileURLs[curIndex]);
186  if (newFileOpened != nullptr)
187  *newFileOpened = true;
188  ++curIndex;
189  } else if (!strName.empty()) {
190  curSource = std::make_unique<StringSource>(strName);
191  }
192  // New "doc" has been opened. This is the same as a new source.
193  curDoc = true;
194  // Set maxEvents and firstEvent
195  curSource->handler->counter(firstEvent, maxEvents);
196  curSource->handler->readBlock();
197 
198  curRunInfo.reset();
199  HEPRUP tmprup;
200  int beamA, beamB;
201  curSource->handler->_init.getDataSet("beamA").read(beamA);
202  curSource->handler->_init.getDataSet("beamB").read(beamB);
203  tmprup.IDBMUP = std::make_pair(beamA, beamB);
204  double energyA, energyB;
205  curSource->handler->_init.getDataSet("energyA").read(energyA);
206  curSource->handler->_init.getDataSet("energyB").read(energyB);
207  tmprup.EBMUP = std::make_pair(energyA, energyB);
208  int PDFsetA, PDFsetB;
209  curSource->handler->_init.getDataSet("PDFsetA").read(PDFsetA);
210  curSource->handler->_init.getDataSet("PDFsetB").read(PDFsetB);
211  tmprup.PDFSUP = std::make_pair(PDFsetA, PDFsetB);
212  int PDFgroupA, PDFgroupB;
213  curSource->handler->_init.getDataSet("PDFgroupA").read(PDFgroupA);
214  curSource->handler->_init.getDataSet("PDFgroupB").read(PDFgroupB);
215  tmprup.PDFGUP = std::make_pair(PDFgroupA, PDFgroupB);
216  std::vector<int> procId; // NOTE: C++17 allows int[numProcesses]
217  std::vector<double> xSection; // NOTE: C++17 allows double[numProcesses]
218  std::vector<double> error; // NOTE: C++17 allows double[numProcesses]
219  std::vector<double> unitWeight; // NOTE: C++17 allows double[numProcesses]
220 
221  curSource->handler->_procInfo.getDataSet("procId").read(procId);
222  curSource->handler->_procInfo.getDataSet("xSection").read(xSection);
223  curSource->handler->_procInfo.getDataSet("error").read(error);
224  curSource->handler->_procInfo.getDataSet("unitWeight").read(unitWeight);
225 
226  tmprup.LPRUP = procId;
227  tmprup.XSECUP = xSection;
228  tmprup.XERRUP = error;
229  tmprup.XMAXUP = unitWeight;
230  tmprup.IDWTUP = 3;
231  size_t numProcesses = procId.size();
232  tmprup.NPRUP = numProcesses;
233  // Use temporary process info block to define const HEPRUP
234  const HEPRUP heprup(tmprup);
235 
236  curRunInfo.reset(new LHERunInfo(heprup));
237  // Run info has now been set when a new file is encountered
238  }
239  // Handler should be modified to have these capabilities
240  // Maybe this is set event by event??
241  int npLO = curSource->handler->npLO;
242  int npNLO = curSource->handler->npNLO;
243 
244  // Event-loop here
245  std::pair<EventHeader, std::vector<Particle> > evp = curSource->handler->getEventProperties();
246  EventHeader hd = evp.first;
247  if (hd.nparticles < 0) {
248  curDoc = false;
249  logFileAction(" Closed LHE file ", fileURLs[curIndex - 1]);
250  return std::shared_ptr<LHEEvent>();
251  }
252  HEPEUP tmp;
253  tmp.resize(hd.nparticles);
254  //Particle loop
255  unsigned int ip = 0;
256  // for (auto part: curSource->handler->_events1.mkEvent(i)) {
257  for (auto part : evp.second) {
258  tmp.IDUP[ip] = part.id;
259  tmp.ISTUP[ip] = part.status;
260  tmp.MOTHUP[ip] = std::make_pair(part.mother1, part.mother2);
261  tmp.ICOLUP[ip] = std::make_pair(part.color1, part.color2);
262  tmp.VTIMUP[ip] = part.lifetime;
263  tmp.SPINUP[ip] = part.spin;
264  tmp.PUP[ip][0] = part.px;
265  tmp.PUP[ip][1] = part.py;
266  tmp.PUP[ip][2] = part.pz;
267  tmp.PUP[ip][3] = part.e;
268  tmp.PUP[ip][4] = part.m;
269  ip++;
270  }
271  tmp.IDPRUP = hd.pid;
272  tmp.XWGTUP = hd.weight;
273  tmp.SCALUP = hd.scale;
274  tmp.AQEDUP = hd.aqed;
275  tmp.AQCDUP = hd.aqcd;
276  std::shared_ptr<LHEEvent> lheevent;
277  // Use temporary event to construct const HEPEUP;
278  const HEPEUP hepeup(tmp);
279 
280  lheevent.reset(new LHEEvent(curRunInfo, hepeup));
281  // Might have to add this capability later
282  /* const XMLHandler::wgt_info &info = handler->weightsinevent;
283  for (size_t i = 0; i < info.size(); ++i) {
284  double num = -1.0;
285  sscanf(info[i].second.c_str(), "%le", &num);
286  lheevent->addWeight(gen::WeightsInfo(info[i].first, num));
287  }*/
288  // Currently these are set just at the beginning?
289  // might be an event property
290  lheevent->setNpLO(npLO);
291  lheevent->setNpNLO(npNLO);
292  //fill scales
293  /* if (!handler->scales.empty()) {
294  lheevent->setScales(handler->scales);
295  }*/
296  return lheevent;
297  }
298  return std::shared_ptr<LHEEvent>();
299  }
300 
301 } // namespace lhef
unsigned int long _eventsTotal
Definition: LH5Reader.h:34
LH5Reader(const edm::ParameterSet &params)
Definition: LH5Reader.cc:165
EventHeader mkEventHeader(int ievent) const
Definition: lheh5.cc:51
static void logFileAction(char const *msg, std::string const &fileName)
Definition: LH5Reader.cc:31
HighFive::Group _particle
Definition: LH5Reader.h:24
std::unique_ptr< Source > curSource
Definition: LH5Reader.h:61
unsigned int firstEvent
Definition: LH5Reader.h:56
void FlushMessageLog()
FileSource(const std::string &fileURL)
Definition: LH5Reader.cc:47
std::vector< std::pair< int, int > > ICOLUP
Definition: LesHouches.h:240
std::vector< Particle > mkEvent(size_t ievent) const
Definition: lheh5.cc:35
std::pair< double, double > EBMUP
Definition: LesHouches.h:82
Events readEvents(HighFive::Group &g_index, HighFive::Group &g_particle, HighFive::Group &g_event, size_t first_event, size_t n_events)
Definition: lheh5.cc:68
std::vector< lheh5::Particle > getEvent()
Definition: LH5Reader.cc:133
double scale
Definition: lheh5.h:34
list status
Definition: mps_update.py:107
int _eventsInBlock
Definition: LH5Reader.h:35
bool indexStatus
Definition: LH5Reader.h:23
std::vector< double > VTIMUP
Definition: LesHouches.h:252
std::pair< int, int > IDBMUP
Definition: LesHouches.h:77
void counter(int, int)
Definition: LH5Reader.cc:110
double weight
Definition: lheh5.h:32
std::unique_ptr< H5Handler > handler
Definition: LH5Reader.cc:40
std::shared_ptr< LHEEvent > next(bool *newFileOpened=nullptr)
Definition: LH5Reader.cc:179
double aqed
Definition: lheh5.h:37
std::pair< int, int > PDFGUP
Definition: LesHouches.h:88
lheh5::EventHeader getHeader()
Definition: LH5Reader.cc:145
void resize(int nup)
Definition: LesHouches.h:161
unsigned int curIndex
Definition: LH5Reader.h:58
std::vector< FiveVector > PUP
Definition: LesHouches.h:246
std::pair< lheh5::EventHeader, std::vector< lheh5::Particle > > getEventProperties()
Definition: LH5Reader.cc:150
std::vector< double > SPINUP
Definition: LesHouches.h:259
HighFive::Group _procInfo
Definition: LH5Reader.h:24
std::vector< int > ISTUP
Definition: LesHouches.h:228
const std::string strName
Definition: LH5Reader.h:55
HighFive::Group _event
Definition: LH5Reader.h:24
std::vector< std::pair< int, int > > MOTHUP
Definition: LesHouches.h:234
StringSource(const std::string &inputs)
Definition: LH5Reader.cc:59
std::vector< int > IDUP
Definition: LesHouches.h:223
std::vector< double > XERRUP
Definition: LesHouches.h:118
HighFive::Group _index
Definition: LH5Reader.h:24
std::unique_ptr< HighFive::File > h5file
Definition: LH5Reader.h:22
std::vector< double > XMAXUP
Definition: LesHouches.h:123
double AQCDUP
Definition: LesHouches.h:218
unsigned int long _eventsRead
Definition: LH5Reader.h:26
void readBlock()
Definition: LH5Reader.cc:123
part
Definition: HCALResponse.h:20
tuple msg
Definition: mps_check.py:285
std::pair< int, int > PDFSUP
Definition: LesHouches.h:94
H5Handler(const std::string &fileNameIn)
Definition: LH5Reader.cc:70
lheh5::Events _events1
Definition: LH5Reader.h:28
std::shared_ptr< LHERunInfo > curRunInfo
Definition: LH5Reader.h:63
Log< level::System, true > LogAbsolute
double AQEDUP
Definition: LesHouches.h:213
double aqcd
Definition: lheh5.h:38
tuple fileNames
Definition: LaserDQM_cfg.py:34
std::vector< double > XSECUP
Definition: LesHouches.h:112
const std::vector< std::string > fileURLs
Definition: LH5Reader.h:52
tmp
align.sh
Definition: createJobs.py:716
double XWGTUP
Definition: LesHouches.h:194
std::vector< int > LPRUP
Definition: LesHouches.h:128
double SCALUP
Definition: LesHouches.h:208