13 #include <xercesc/sax2/Attributes.hpp> 14 #include <xercesc/dom/DOM.hpp> 55 <<
"Could not open LHE file \"" << fileURL <<
"\" for reading" << std::endl;
72 throw cms::Exception(
"StreamOpenError") <<
"Empty LHE file string name \"" << std::endl;
74 std::stringstream *tmpis =
new std::stringstream(
inputs);
88 typedef std::vector<std::pair<std::string, std::string> >
wgt_info;
100 xmlHeader->release();
109 weightsinevent.clear();
117 void startElement(
const XMLCh *
const uri,
118 const XMLCh *
const localname,
119 const XMLCh *
const qname,
120 const Attributes &attributes)
override;
122 void endElement(
const XMLCh *
const uri,
const XMLCh *
const localname,
const XMLCh *
const qname)
override;
124 void characters(
const XMLCh *
const chars,
const XMLSize_t length)
override;
125 void comment(
const XMLCh *
const chars,
const XMLSize_t length)
override;
130 bool skipEvent =
false;
131 std::unique_ptr<DOMImplementation>
impl;
148 for (
unsigned int i = 0;
i < attributes.getLength();
i++) {
149 const XMLCh *
name = attributes.getQName(
i);
150 const XMLCh *
value = attributes.getValue(
i);
157 const char *
end = len >= 0 ? (
data + len) :
nullptr;
159 std::size_t len = std::strcspn(
data,
"\r\n");
162 if (
data[len] ==
'\r' &&
data[len + 1] ==
'\n')
172 const XMLCh *
const localname,
173 const XMLCh *
const qname,
174 const Attributes &attributes) {
178 if (
name !=
"LesHouchesEvents")
179 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid header" << std::endl;
184 if (
mode == kHeader) {
185 DOMElement *elem = xmlHeader->createElement(qname);
187 xmlNodes.back()->appendChild(elem);
188 xmlNodes.push_back(elem);
190 }
else if (
mode == kEvent) {
195 DOMElement *elem = xmlEvent->createElement(qname);
199 if (
name ==
"rwgt") {
200 xmlEventNodes[0]->appendChild(elem);
201 }
else if (
name ==
"wgt") {
202 xmlEventNodes[1]->appendChild(elem);
203 }
else if (
name ==
"scales") {
204 for (XMLSize_t iscale = 0; iscale < attributes.getLength(); ++iscale) {
206 const char *scalename =
XMLSimpleStr(attributes.getQName(iscale));
207 int nmatch = sscanf(scalename,
"pt_clust_%d", &ipart);
210 edm::LogError(
"Generator|LHEInterface") <<
"invalid attribute in <scales> tag" << std::endl;
214 const char *scalevalstr =
XMLSimpleStr(attributes.getValue(iscale));
215 sscanf(scalevalstr,
"%e", &scaleval);
217 scales.push_back(scaleval);
219 }
else if (
name ==
"event_num") {
220 const char *evtnumstr =
XMLSimpleStr(attributes.getValue(XMLString::transcode(
"num")));
221 sscanf(evtnumstr,
"%d", &evtnum);
223 xmlEventNodes.push_back(elem);
225 }
else if (
mode == kInit) {
229 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
232 if (
name ==
"header") {
234 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
236 xmlHeader =
impl->createDocument(
nullptr, qname,
nullptr);
238 xmlNodes[0] = xmlHeader->getDocumentElement();
241 if (
name ==
"init") {
243 }
else if (
name ==
"event") {
246 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
250 xmlEvent =
impl->createDocument(
nullptr, qname,
nullptr);
251 weightsinevent.resize(0);
256 const XMLCh *npLOval = attributes.getValue(XMLString::transcode(
"npLO"));
259 sscanf(npLOs,
"%d", &npLO);
261 const XMLCh *npNLOval = attributes.getValue(XMLString::transcode(
"npNLO"));
264 sscanf(npNLOs,
"%d", &npNLO);
267 xmlEventNodes.resize(1);
268 xmlEventNodes[0] = xmlEvent->getDocumentElement();
274 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
280 const XMLCh *
const localname,
281 const XMLCh *
const qname) {
285 if (
mode == kHeader && xmlNodes.size() > 1) {
286 xmlNodes.resize(xmlNodes.size() - 1);
288 }
else if (
mode == kHeader) {
289 std::unique_ptr<DOMLSSerializer>
writer(
impl->createLSSerializer());
290 std::unique_ptr<DOMLSOutput> outputDesc(
impl->createLSOutput());
292 outputDesc->setEncoding(
XMLUniStr(
"UTF-8"));
294 for (DOMNode *node = xmlNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
301 switch (node->getNodeType()) {
302 case DOMNode::ELEMENT_NODE:
303 elem =
static_cast<DOMElement *
>(node);
305 p = std::strchr((
const char *)
buffer,
'>') + 1;
306 q = std::strrchr(
p,
'<');
308 case DOMNode::COMMENT_NODE:
325 xmlHeader->release();
327 }
else if (
name ==
"event" &&
mode == kEvent &&
328 (skipEvent || (!xmlEventNodes.empty()))) {
336 for (DOMNode *node = xmlEventNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
337 switch (node->getNodeType()) {
338 case DOMNode::ELEMENT_NODE:
339 for (DOMNode *rwgt = xmlEventNodes[1]->getFirstChild(); rwgt; rwgt = rwgt->getNextSibling()) {
340 DOMNode *attr = rwgt->getAttributes()->item(0);
343 switch (rwgt->getNodeType()) {
344 case DOMNode::ELEMENT_NODE:
345 weightsinevent.push_back(std::make_pair((
const char *)atname, (
const char *)
weight));
352 case DOMNode::TEXT_NODE:
361 }
else if (
mode == kEvent) {
366 if (gotObject !=
kNone)
368 " LHEReader::XMLHandler::endElement" 377 if (
mode == kHeader) {
378 DOMText *
text = xmlHeader->createTextNode(data_);
379 xmlNodes.back()->appendChild(
text);
390 if (
mode == kEvent) {
392 DOMText *
text = xmlEvent->createTextNode(data_ +
offset);
393 xmlEventNodes.back()->appendChild(
text);
399 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
406 if (
mode == kHeader) {
407 DOMComment *comment = xmlHeader->createComment(data_);
408 xmlNodes.back()->appendChild(comment);
460 if (newFileOpened !=
nullptr)
461 *newFileOpened =
true;
480 return std::shared_ptr<LHEEvent>();
488 std::istringstream
data;
494 std::for_each(
handler->headers.begin(),
515 return std::shared_ptr<LHEEvent>();
519 std::istringstream
data;
523 std::shared_ptr<LHEEvent> lheevent;
526 for (
size_t i = 0;
i <
info.size(); ++
i) {
531 lheevent->setNpLO(
handler->npLO);
532 lheevent->setNpNLO(
handler->npNLO);
533 lheevent->setEvtNum(
handler->evtnum);
536 if (!
handler->scales.empty()) {
537 lheevent->setScales(
handler->scales);
544 return std::shared_ptr<LHEEvent>();
std::shared_ptr< void > platform
XMLDocument * createReader(XMLDocument::Handler &handler) override
LHEReader(const edm::ParameterSet ¶ms)
static void logFileAction(char const *msg, std::string const &fileName)
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
std::unique_ptr< std::istream > fileStream
StringSource(const std::string &inputs)
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname) override
std::vector< std::pair< std::string, std::string > > wgt_info
const std::vector< std::string > fileURLs
static bool isSpace(XMLCh ch)
void characters(const XMLCh *const chars, const XMLSize_t length) override
Log< level::Error, false > LogError
std::vector< LHERunInfo::Header > headers
U second(std::pair< T, U > const &p)
ROOT::VecOps::RVec< UChar_t > chars
void addHeader(const Header &header)
std::unique_ptr< StorageWrap > fileStream
static const char *const kComment
static bool isAllSpaces(const XMLCh *str, unsigned int length)
const std::string strName
static std::shared_ptr< void > platformHandle()
std::unique_ptr< DOMImplementation > impl
std::vector< DOMElement * > xmlNodes
std::unique_ptr< XMLDocument > curDoc
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attributes) override
const wgt_info & weightInfo() const
std::unique_ptr< Source > curSource
XMLDocument * createReader(XMLDocument::Handler &handler) override
char data[epos_bytes_allocation]
void comment(const XMLCh *const chars, const XMLSize_t length) override
virtual XMLDocument * createReader(XMLDocument::Handler &handler)=0
std::shared_ptr< LHERunInfo > curRunInfo
Log< level::System, true > LogAbsolute
std::vector< float > scales
std::unique_ptr< XMLHandler > handler
std::shared_ptr< LHEEvent > next(bool *newFileOpened=nullptr)
FileSource(const std::string &fileURL)
static void attributesToDom(DOMElement *dom, const Attributes &attributes)