CMS 3D CMS Logo

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;
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;
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
lhef::HEPRUP::XSECUP
std::vector< double > XSECUP
Definition: LesHouches.h:112
lheh5
Definition: lheh5.h:14
lhef::LH5Reader::maxEvents
int maxEvents
Definition: LH5Reader.h:57
lhef::H5Handler::_formatType
int _formatType
Definition: LH5Reader.h:36
MessageLogger.h
funct::false
false
Definition: Factorize.h:29
edm::TimeOfDay
Definition: TimeOfDay.h:9
lheh5::Events::mkEventHeader
EventHeader mkEventHeader(int ievent) const
Definition: lheh5.cc:51
lhef::LH5Reader::curIndex
unsigned int curIndex
Definition: LH5Reader.h:58
mps_update.status
status
Definition: mps_update.py:68
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
lhef::LH5Reader::Source
Definition: LH5Reader.cc:36
lhef::H5Handler::readBlock
void readBlock()
Definition: LH5Reader.cc:123
lhef::H5Handler::_particle
HighFive::Group _particle
Definition: LH5Reader.h:24
lhef::H5Handler::_index
HighFive::Group _index
Definition: LH5Reader.h:24
lhef::H5Handler::H5Handler
H5Handler(const std::string &fileNameIn)
Definition: LH5Reader.cc:70
lhef::LH5Reader::LH5Reader
LH5Reader(const edm::ParameterSet &params)
Definition: LH5Reader.cc:165
mps_check.msg
tuple msg
Definition: mps_check.py:285
lheh5::Events::mkEvent
std::vector< Particle > mkEvent(size_t ievent) const
Definition: lheh5.cc:35
particlelevel_cff.LHERunInfo
LHERunInfo
Definition: particlelevel_cff.py:57
lhef::H5Handler::getEventProperties
std::pair< lheh5::EventHeader, std::vector< lheh5::Particle > > getEventProperties()
Definition: LH5Reader.cc:150
lheh5::EventHeader::weight
double weight
Definition: lheh5.h:32
Storage.h
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
lhef::LH5Reader::Source::~Source
virtual ~Source()
Definition: LH5Reader.cc:39
lheh5.h
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
lhef::H5Handler::indexStatus
bool indexStatus
Definition: LH5Reader.h:23
relativeConstraints.error
error
Definition: relativeConstraints.py:53
lhef::H5Handler::npLO
int npLO
Definition: LH5Reader.h:25
lhef::LH5Reader::Source::handler
std::unique_ptr< H5Handler > handler
Definition: LH5Reader.cc:40
lhef::HEPRUP::EBMUP
std::pair< double, double > EBMUP
Definition: LesHouches.h:82
lhef::LH5Reader::curSource
std::unique_ptr< Source > curSource
Definition: LH5Reader.h:61
lhef::LH5Reader::curRunInfo
std::shared_ptr< LHERunInfo > curRunInfo
Definition: LH5Reader.h:63
part
part
Definition: HCALResponse.h:20
File
Definition: File.h:11
lheh5::EventHeader
Definition: lheh5.h:28
lhef::LH5Reader::FileSource::FileSource
FileSource(const std::string &fileURL)
Definition: LH5Reader.cc:47
lhef::HEPRUP::LPRUP
std::vector< int > LPRUP
Definition: LesHouches.h:128
lhef::H5Handler::h5file
std::unique_ptr< HighFive::File > h5file
Definition: LH5Reader.h:22
LH5Reader.h
lhef::LH5Reader::next
std::shared_ptr< LHEEvent > next(bool *newFileOpened=nullptr)
Definition: LH5Reader.cc:179
LaserDQM_cfg.fileNames
fileNames
Definition: LaserDQM_cfg.py:34
lhef::LH5Reader::Source::Source
Source()
Definition: LH5Reader.cc:38
lhef::logFileAction
static void logFileAction(char const *msg, std::string const &fileName)
Definition: LH5Reader.cc:31
edm::FlushMessageLog
void FlushMessageLog()
Definition: MessageLogger.cc:34
lhef::HEPRUP::PDFGUP
std::pair< int, int > PDFGUP
Definition: LesHouches.h:88
edm::ParameterSet
Definition: ParameterSet.h:47
lhef::LH5Reader::curDoc
bool curDoc
Definition: LH5Reader.h:62
lhef::H5Handler::_eventsInBlock
int _eventsInBlock
Definition: LH5Reader.h:35
lhef::H5Handler::_procInfo
HighFive::Group _procInfo
Definition: LH5Reader.h:24
lhef::H5Handler::_events1
lheh5::Events _events1
Definition: LH5Reader.h:28
LHERunInfo.h
lhef::LH5Reader::fileURLs
const std::vector< std::string > fileURLs
Definition: LH5Reader.h:52
lhef
Definition: ExhumeHadronizer.h:12
lhef::LH5Reader::FileSource
Definition: LH5Reader.cc:45
lhef::LHEEvent
Definition: LHEEvent.h:23
lhef::HEPRUP::NPRUP
int NPRUP
Definition: LesHouches.h:107
lhef::LH5Reader::FileSource::~FileSource
~FileSource() override
Definition: LH5Reader.cc:54
StorageFactory.h
createfilelist.int
int
Definition: createfilelist.py:10
lhef::LH5Reader::StringSource::~StringSource
~StringSource() override
Definition: LH5Reader.cc:67
lhef::H5Handler
Definition: LH5Reader.h:16
PixelMapPlotter.inputs
inputs
Definition: PixelMapPlotter.py:490
lhef::H5Handler::getHeader
lheh5::EventHeader getHeader()
Definition: LH5Reader.cc:145
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::LogAbsolute
Log< level::System, true > LogAbsolute
Definition: MessageLogger.h:134
lhef::HEPRUP
Definition: LesHouches.h:22
IOTypes.h
lhef::LH5Reader::StringSource::StringSource
StringSource(const std::string &inputs)
Definition: LH5Reader.cc:59
lhef::HEPRUP::IDWTUP
int IDWTUP
Definition: LesHouches.h:101
lhef::HEPEUP
Definition: LesHouches.h:138
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
lheh5::EventHeader::pid
int pid
Definition: lheh5.h:31
lhef::H5Handler::_event
HighFive::Group _event
Definition: LH5Reader.h:24
lhef::H5Handler::_blocksRead
int _blocksRead
Definition: LH5Reader.h:37
lheh5::readEvents
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
lhef::H5Handler::_eventsRead
unsigned int long _eventsRead
Definition: LH5Reader.h:26
lhef::HEPRUP::XERRUP
std::vector< double > XERRUP
Definition: LesHouches.h:118
lhef::HEPRUP::PDFSUP
std::pair< int, int > PDFSUP
Definition: LesHouches.h:94
std
Definition: JetResolutionObject.h:76
lheh5::EventHeader::nparticles
int nparticles
Definition: lheh5.h:30
lhef::H5Handler::getEvent
std::vector< lheh5::Particle > getEvent()
Definition: LH5Reader.cc:133
Exception
Definition: hltDiff.cc:245
lhef::LH5Reader::StringSource
Definition: LH5Reader.cc:57
Data_TkAlMinBias_Run2018C_PromptReco_v3_cff.maxEvents
maxEvents
Definition: Data_TkAlMinBias_Run2018C_PromptReco_v3_cff.py:3
lhef::H5Handler::counter
void counter(int, int)
Definition: LH5Reader.cc:110
Exception.h
LHEEvent.h
lheh5::EventHeader::aqed
double aqed
Definition: lheh5.h:37
lhef::LH5Reader::firstEvent
unsigned int firstEvent
Definition: LH5Reader.h:56
lheh5::EventHeader::scale
double scale
Definition: lheh5.h:34
cms::Exception
Definition: Exception.h:70
lhef::HEPRUP::XMAXUP
std::vector< double > XMAXUP
Definition: LesHouches.h:123
lhef::LH5Reader::strName
const std::string strName
Definition: LH5Reader.h:55
ParameterSet.h
lhef::H5Handler::npNLO
int npNLO
Definition: LH5Reader.h:25
TimeOfDay.h
lhef::H5Handler::_eventsTotal
unsigned int long _eventsTotal
Definition: LH5Reader.h:34
nEvents
UInt_t nEvents
Definition: hcalCalib.cc:40
lheh5::EventHeader::aqcd
double aqcd
Definition: lheh5.h:38
lhef::LH5Reader::~LH5Reader
~LH5Reader()
Definition: LH5Reader.cc:177
amptDefault_cfi.firstEvent
firstEvent
Definition: amptDefault_cfi.py:8
lhef::HEPRUP::IDBMUP
std::pair< int, int > IDBMUP
Definition: LesHouches.h:77