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;
147 for (
unsigned int i = 0;
i < attributes.getLength();
i++) {
148 const XMLCh *
name = attributes.getQName(
i);
149 const XMLCh *
value = attributes.getValue(
i);
156 const char *
end = len >= 0 ? (
data + len) :
nullptr;
158 std::size_t len = std::strcspn(
data,
"\r\n");
161 if (
data[len] ==
'\r' &&
data[len + 1] ==
'\n')
171 const XMLCh *
const localname,
172 const XMLCh *
const qname,
173 const Attributes &attributes) {
177 if (
name !=
"LesHouchesEvents")
178 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid header" << std::endl;
183 if (
mode == kHeader) {
184 DOMElement *elem = xmlHeader->createElement(qname);
186 xmlNodes.back()->appendChild(elem);
187 xmlNodes.push_back(elem);
189 }
else if (
mode == kEvent) {
194 DOMElement *elem = xmlEvent->createElement(qname);
198 if (
name ==
"rwgt") {
199 xmlEventNodes[0]->appendChild(elem);
200 }
else if (
name ==
"wgt") {
201 xmlEventNodes[1]->appendChild(elem);
202 }
else if (
name ==
"scales") {
203 for (XMLSize_t iscale = 0; iscale < attributes.getLength(); ++iscale) {
205 const char *scalename =
XMLSimpleStr(attributes.getQName(iscale));
206 int nmatch = sscanf(scalename,
"pt_clust_%d", &ipart);
209 edm::LogError(
"Generator|LHEInterface") <<
"invalid attribute in <scales> tag" << std::endl;
213 const char *scalevalstr =
XMLSimpleStr(attributes.getValue(iscale));
214 sscanf(scalevalstr,
"%e", &scaleval);
216 scales.push_back(scaleval);
219 xmlEventNodes.push_back(elem);
221 }
else if (
mode == kInit) {
225 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
228 if (
name ==
"header") {
230 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
232 xmlHeader =
impl->createDocument(
nullptr, qname,
nullptr);
234 xmlNodes[0] = xmlHeader->getDocumentElement();
237 if (
name ==
"init") {
239 }
else if (
name ==
"event") {
242 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
246 xmlEvent =
impl->createDocument(
nullptr, qname,
nullptr);
247 weightsinevent.resize(0);
252 const XMLCh *npLOval = attributes.getValue(XMLString::transcode(
"npLO"));
255 sscanf(npLOs,
"%d", &npLO);
257 const XMLCh *npNLOval = attributes.getValue(XMLString::transcode(
"npNLO"));
260 sscanf(npNLOs,
"%d", &npNLO);
263 xmlEventNodes.resize(1);
264 xmlEventNodes[0] = xmlEvent->getDocumentElement();
270 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
276 const XMLCh *
const localname,
277 const XMLCh *
const qname) {
281 if (
mode == kHeader && xmlNodes.size() > 1) {
282 xmlNodes.resize(xmlNodes.size() - 1);
284 }
else if (
mode == kHeader) {
285 std::unique_ptr<DOMLSSerializer>
writer(
impl->createLSSerializer());
286 std::unique_ptr<DOMLSOutput> outputDesc(
impl->createLSOutput());
288 outputDesc->setEncoding(
XMLUniStr(
"UTF-8"));
290 for (DOMNode *node = xmlNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
297 switch (node->getNodeType()) {
298 case DOMNode::ELEMENT_NODE:
299 elem =
static_cast<DOMElement *
>(node);
301 p = std::strchr((
const char *)
buffer,
'>') + 1;
302 q = std::strrchr(
p,
'<');
304 case DOMNode::COMMENT_NODE:
321 xmlHeader->release();
323 }
else if (
name ==
"event" &&
mode == kEvent &&
324 (skipEvent || (!xmlEventNodes.empty()))) {
332 for (DOMNode *node = xmlEventNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
333 switch (node->getNodeType()) {
334 case DOMNode::ELEMENT_NODE:
335 for (DOMNode *rwgt = xmlEventNodes[1]->getFirstChild(); rwgt; rwgt = rwgt->getNextSibling()) {
336 DOMNode *attr = rwgt->getAttributes()->item(0);
339 switch (rwgt->getNodeType()) {
340 case DOMNode::ELEMENT_NODE:
341 weightsinevent.push_back(std::make_pair((
const char *)atname, (
const char *)
weight));
348 case DOMNode::TEXT_NODE:
357 }
else if (
mode == kEvent) {
362 if (gotObject !=
kNone)
364 " LHEReader::XMLHandler::endElement" 373 if (
mode == kHeader) {
374 DOMText *
text = xmlHeader->createTextNode(data_);
375 xmlNodes.back()->appendChild(
text);
386 if (
mode == kEvent) {
388 DOMText *
text = xmlEvent->createTextNode(data_ +
offset);
389 xmlEventNodes.back()->appendChild(
text);
395 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
402 if (
mode == kHeader) {
403 DOMComment *comment = xmlHeader->createComment(data_);
404 xmlNodes.back()->appendChild(comment);
456 if (newFileOpened !=
nullptr)
457 *newFileOpened =
true;
476 return std::shared_ptr<LHEEvent>();
484 std::istringstream
data;
490 std::for_each(
handler->headers.begin(),
511 return std::shared_ptr<LHEEvent>();
515 std::istringstream
data;
519 std::shared_ptr<LHEEvent> lheevent;
522 for (
size_t i = 0;
i <
info.size(); ++
i) {
527 lheevent->setNpLO(
handler->npLO);
528 lheevent->setNpNLO(
handler->npNLO);
530 if (!
handler->scales.empty()) {
531 lheevent->setScales(
handler->scales);
538 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)
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)