13 #include <xercesc/sax2/Attributes.hpp>
14 #include <xercesc/dom/DOM.hpp>
54 <<
"Could not open LHE file \"" << fileURL <<
"\" for reading" << std::endl;
71 throw cms::Exception(
"StreamOpenError") <<
"Empty LHE file string name \"" << std::endl;
73 std::stringstream *tmpis =
new std::stringstream(
inputs);
87 typedef std::vector<std::pair<std::string, std::string> >
wgt_info;
117 const XMLCh *
const localname,
118 const XMLCh *
const qname,
119 const Attributes &attributes)
override;
121 void endElement(
const XMLCh *
const uri,
const XMLCh *
const localname,
const XMLCh *
const qname)
override;
123 void characters(
const XMLCh *
const chars,
const XMLSize_t length)
override;
124 void comment(
const XMLCh *
const chars,
const XMLSize_t length)
override;
130 std::unique_ptr<DOMImplementation>
impl;
146 for (
unsigned int i = 0;
i < attributes.getLength();
i++) {
147 const XMLCh *
name = attributes.getQName(
i);
148 const XMLCh *
value = attributes.getValue(
i);
155 const char *
end = len >= 0 ? (
data + len) :
nullptr;
157 std::size_t len = std::strcspn(
data,
"\r\n");
160 if (
data[len] ==
'\r' &&
data[len + 1] ==
'\n')
170 const XMLCh *
const localname,
171 const XMLCh *
const qname,
172 const Attributes &attributes) {
176 if (
name !=
"LesHouchesEvents")
177 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid header" << std::endl;
183 DOMElement *elem =
xmlHeader->createElement(qname);
193 DOMElement *elem =
xmlEvent->createElement(qname);
197 if (
name ==
"rwgt") {
199 }
else if (
name ==
"wgt") {
201 }
else if (
name ==
"scales") {
202 for (XMLSize_t iscale = 0; iscale < attributes.getLength(); ++iscale) {
204 const char *scalename =
XMLSimpleStr(attributes.getQName(iscale));
205 int nmatch = sscanf(scalename,
"pt_clust_%d", &ipart);
208 edm::LogError(
"Generator|LHEInterface") <<
"invalid attribute in <scales> tag" << std::endl;
212 const char *scalevalstr =
XMLSimpleStr(attributes.getValue(iscale));
213 sscanf(scalevalstr,
"%e", &scaleval);
215 scales.push_back(scaleval);
224 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
227 if (
name ==
"header") {
229 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
236 if (
name ==
"init") {
238 }
else if (
name ==
"event") {
241 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
245 xmlEvent =
impl->createDocument(
nullptr, qname,
nullptr);
251 const XMLCh *npLOval = attributes.getValue(XMLString::transcode(
"npLO"));
254 sscanf(npLOs,
"%d", &
npLO);
256 const XMLCh *npNLOval = attributes.getValue(XMLString::transcode(
"npNLO"));
259 sscanf(npNLOs,
"%d", &
npNLO);
269 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
275 const XMLCh *
const localname,
276 const XMLCh *
const qname) {
280 if (
mode == kHeader && xmlNodes.size() > 1) {
281 xmlNodes.resize(xmlNodes.size() - 1);
283 }
else if (
mode == kHeader) {
284 std::unique_ptr<DOMLSSerializer>
writer(
impl->createLSSerializer());
285 std::unique_ptr<DOMLSOutput> outputDesc(
impl->createLSOutput());
287 outputDesc->setEncoding(
XMLUniStr(
"UTF-8"));
289 for (DOMNode *node = xmlNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
296 switch (node->getNodeType()) {
297 case DOMNode::ELEMENT_NODE:
298 elem = static_cast<DOMElement *>(node);
300 p = std::strchr((
const char *)
buffer,
'>') + 1;
301 q = std::strrchr(
p,
'<');
303 case DOMNode::COMMENT_NODE:
320 xmlHeader->release();
322 }
else if (
name ==
"event" &&
mode == kEvent &&
323 (skipEvent || (!xmlEventNodes.empty()))) {
331 for (DOMNode *node = xmlEventNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
332 switch (node->getNodeType()) {
333 case DOMNode::ELEMENT_NODE:
334 for (DOMNode *rwgt = xmlEventNodes[1]->getFirstChild(); rwgt; rwgt = rwgt->getNextSibling()) {
335 DOMNode *attr = rwgt->getAttributes()->item(0);
338 switch (rwgt->getNodeType()) {
339 case DOMNode::ELEMENT_NODE:
340 weightsinevent.push_back(std::make_pair((
const char *)atname, (
const char *)
weight));
347 case DOMNode::TEXT_NODE:
356 }
else if (
mode == kEvent) {
361 if (gotObject !=
kNone)
363 " LHEReader::XMLHandler::endElement"
372 if (
mode == kHeader) {
373 DOMText *
text = xmlHeader->createTextNode(data_);
374 xmlNodes.back()->appendChild(
text);
385 if (
mode == kEvent) {
387 DOMText *
text = xmlEvent->createTextNode(data_ +
offset);
388 xmlEventNodes.back()->appendChild(
text);
394 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
401 if (
mode == kHeader) {
402 DOMComment *comment = xmlHeader->createComment(data_);
403 xmlNodes.back()->appendChild(comment);
455 if (newFileOpened !=
nullptr)
456 *newFileOpened =
true;
475 return std::shared_ptr<LHEEvent>();
483 std::istringstream
data;
489 std::for_each(
handler->headers.begin(),
510 return std::shared_ptr<LHEEvent>();
514 std::istringstream
data;
518 std::shared_ptr<LHEEvent> lheevent;
521 for (
size_t i = 0;
i <
info.size(); ++
i) {
526 lheevent->setNpLO(
handler->npLO);
527 lheevent->setNpNLO(
handler->npNLO);
529 if (!
handler->scales.empty()) {
530 lheevent->setScales(
handler->scales);
537 return std::shared_ptr<LHEEvent>();