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);
76 fileStream.reset(tmpis);
89 typedef std::vector<std::pair<std::string, std::string> >
wgt_info;
101 xmlHeader->release();
110 weightsinevent.clear();
115 const wgt_info &
weightInfo()
const {
return weightsinevent; }
118 void startElement(
const XMLCh *
const uri,
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;
131 bool skipEvent =
false;
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);
152 dom->setAttribute(name, value);
157 const char *
end = len >= 0 ? (data + len) :
nullptr;
158 while (*data && (!end || data < end)) {
159 std::size_t len = std::strcspn(data,
"\r\n");
160 if (end && data + len > end)
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);
220 xmlEventNodes.push_back(elem);
222 }
else if (
mode == kInit) {
226 throw cms::Exception(
"InvalidFormat") <<
"LHE file has invalid format" << std::endl;
229 if (name ==
"header") {
231 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
233 xmlHeader =
impl->createDocument(
nullptr, qname,
nullptr);
235 xmlNodes[0] = xmlHeader->getDocumentElement();
238 if (name ==
"init") {
240 }
else if (name ==
"event") {
243 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
247 xmlEvent =
impl->createDocument(
nullptr, qname,
nullptr);
248 weightsinevent.resize(0);
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);
264 xmlEventNodes.resize(1);
265 xmlEventNodes[0] = xmlEvent->getDocumentElement();
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());
288 assert(outputDesc.get());
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);
419 firstEvent(params.getUntrackedParameter<unsigned
int>(
"skipEvents", 0)),
420 maxEvents(params.getUntrackedParameter<
int>(
"limitEvents", -1)),
427 firstEvent(firstEvent),
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>();
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 wgt_info & weightInfo() const
const std::vector< std::string > fileURLs
static bool isSpace(XMLCh ch)
void characters(const XMLCh *const chars, const XMLSize_t length) override
std::vector< LHERunInfo::Header > headers
U second(std::pair< T, U > const &p)
static const StorageFactory * get(void)
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
def elem(elemtype, innerHTML='', html_class='', kwargs)
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attributes) override
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
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)
std::unique_ptr< Storage > open(const std::string &url, int mode=IOFlags::OpenRead) const