CMS 3D CMS Logo

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