11 #include <boost/bind.hpp>
13 #include <xercesc/sax2/Attributes.hpp>
14 #include <xercesc/dom/DOM.hpp>
31 #include "boost/lexical_cast.hpp"
56 <<
"Could not open LHE file \"" << fileURL <<
"\" for reading" << std::endl;
73 throw cms::Exception(
"StreamOpenError") <<
"Empty LHE file string name \"" << std::endl;
75 std::stringstream *tmpis =
new std::stringstream(
inputs);
89 typedef std::vector<std::pair<std::string, std::string> >
wgt_info;
119 const XMLCh *
const localname,
120 const XMLCh *
const qname,
121 const Attributes &attributes)
override;
123 void endElement(
const XMLCh *
const uri,
const XMLCh *
const localname,
const XMLCh *
const qname)
override;
125 void characters(
const XMLCh *
const chars,
const XMLSize_t length)
override;
126 void comment(
const XMLCh *
const chars,
const XMLSize_t length)
override;
132 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;
185 DOMElement *elem =
xmlHeader->createElement(qname);
195 DOMElement *elem =
xmlEvent->createElement(qname);
199 if (
name ==
"rwgt") {
201 }
else if (
name ==
"wgt") {
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);
226 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
229 if (
name ==
"header") {
231 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
238 if (
name ==
"init") {
240 }
else if (
name ==
"event") {
243 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
247 xmlEvent =
impl->createDocument(
nullptr, qname,
nullptr);
253 const XMLCh *npLOval = attributes.getValue(XMLString::transcode(
"npLO"));
256 sscanf(npLOs,
"%d", &
npLO);
258 const XMLCh *npNLOval = attributes.getValue(XMLString::transcode(
"npNLO"));
261 sscanf(npNLOs,
"%d", &
npNLO);
271 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
277 const XMLCh *
const localname,
278 const XMLCh *
const qname) {
282 if (
mode == kHeader && xmlNodes.size() > 1) {
283 xmlNodes.resize(xmlNodes.size() - 1);
285 }
else if (
mode == kHeader) {
286 std::unique_ptr<DOMLSSerializer>
writer(
impl->createLSSerializer());
287 std::unique_ptr<DOMLSOutput> outputDesc(
impl->createLSOutput());
289 outputDesc->setEncoding(
XMLUniStr(
"UTF-8"));
291 for (DOMNode *node = xmlNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
298 switch (node->getNodeType()) {
299 case DOMNode::ELEMENT_NODE:
300 elem = static_cast<DOMElement *>(node);
302 p = std::strchr((
const char *)
buffer,
'>') + 1;
303 q = std::strrchr(
p,
'<');
305 case DOMNode::COMMENT_NODE:
322 xmlHeader->release();
324 }
else if (
name ==
"event" &&
mode == kEvent &&
325 (skipEvent || (!xmlEventNodes.empty()))) {
333 for (DOMNode *node = xmlEventNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
334 switch (node->getNodeType()) {
335 case DOMNode::ELEMENT_NODE:
336 for (DOMNode *rwgt = xmlEventNodes[1]->getFirstChild(); rwgt; rwgt = rwgt->getNextSibling()) {
337 DOMNode *attr = rwgt->getAttributes()->item(0);
340 switch (rwgt->getNodeType()) {
341 case DOMNode::ELEMENT_NODE:
342 weightsinevent.push_back(std::make_pair((
const char *)atname, (
const char *)
weight));
349 case DOMNode::TEXT_NODE:
358 }
else if (
mode == kEvent) {
363 if (gotObject !=
kNone)
365 " LHEReader::XMLHandler::endElement"
374 if (
mode == kHeader) {
375 DOMText *
text = xmlHeader->createTextNode(data_);
376 xmlNodes.back()->appendChild(
text);
387 if (
mode == kEvent) {
389 DOMText *
text = xmlEvent->createTextNode(data_ +
offset);
390 xmlEventNodes.back()->appendChild(
text);
396 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
403 if (
mode == kHeader) {
404 DOMComment *
comment = xmlHeader->createComment(data_);
405 xmlNodes.back()->appendChild(
comment);
457 if (newFileOpened !=
nullptr)
458 *newFileOpened =
true;
477 return std::shared_ptr<LHEEvent>();
485 std::istringstream
data;
491 std::for_each(
handler->headers.begin(),
512 return std::shared_ptr<LHEEvent>();
516 std::istringstream
data;
520 std::shared_ptr<LHEEvent> lheevent;
523 for (
size_t i = 0;
i <
info.size(); ++
i) {
528 lheevent->setNpLO(
handler->npLO);
529 lheevent->setNpNLO(
handler->npNLO);
531 if (!
handler->scales.empty()) {
532 lheevent->setScales(
handler->scales);
539 return std::shared_ptr<LHEEvent>();