11 #include <boost/bind.hpp> 13 #include <xercesc/sax2/Attributes.hpp> 14 #include <xercesc/dom/DOM.hpp> 31 #include "boost/lexical_cast.hpp" 60 <<
"Could not open LHE file \"" 61 << fileURL <<
"\" for reading" 82 <<
"Empty LHE file string name \"" 85 std::stringstream * tmpis =
new std::stringstream(inputs);
86 fileStream.reset(tmpis);
100 typedef std::vector<std::pair<std::string,std::string> >
wgt_info;
106 {
if (xmlHeader) xmlHeader->release();
107 if (xmlEvent) xmlEvent->release(); }
119 weightsinevent.clear();
127 void startElement(
const XMLCh *
const uri,
128 const XMLCh *
const localname,
129 const XMLCh *
const qname,
130 const Attributes &attributes)
override;
132 void endElement(
const XMLCh *
const uri,
133 const XMLCh *
const localname,
134 const XMLCh *
const qname)
override;
136 void characters (
const XMLCh *
const chars,
const XMLSize_t length)
override;
137 void comment (
const XMLCh *
const chars,
const XMLSize_t length)
override;
142 bool skipEvent =
false;
143 std::unique_ptr<DOMImplementation>
impl;
160 for(
unsigned int i = 0;
i < attributes.getLength();
i++) {
161 const XMLCh *
name = attributes.getQName(
i);
162 const XMLCh *
value = attributes.getValue(
i);
164 dom->setAttribute(name, value);
171 const char *
end = len >= 0 ? (data + len) :
nullptr;
172 while(*data && (!end || data < end)) {
173 std::size_t len = std::strcspn(data,
"\r\n");
174 if (end && data + len > end)
176 if (data[len] ==
'\r' && data[len + 1] ==
'\n')
186 const XMLCh *
const localname,
187 const XMLCh *
const qname,
188 const Attributes &attributes)
193 if (name !=
"LesHouchesEvents")
195 <<
"LHE file has invalid header" << std::endl;
200 if (
mode == kHeader) {
201 DOMElement *
elem = xmlHeader->createElement(qname);
203 xmlNodes.back()->appendChild(elem);
204 xmlNodes.push_back(elem);
206 }
else if (
mode == kEvent ) {
208 if (skipEvent) {
return;}
210 DOMElement *
elem = xmlEvent->createElement(qname);
214 if( name ==
"rwgt" ) {
215 xmlEventNodes[0]->appendChild(elem);
216 }
else if (name ==
"wgt") {
217 xmlEventNodes[1]->appendChild(elem);
219 else if (name ==
"scales") {
220 for (XMLSize_t iscale=0; iscale<attributes.getLength(); ++iscale) {
222 const char *scalename =
XMLSimpleStr(attributes.getQName(iscale));
223 int nmatch = sscanf(scalename,
"pt_clust_%d",&ipart);
227 <<
"invalid attribute in <scales> tag" 232 const char *scalevalstr =
XMLSimpleStr(attributes.getValue(iscale));
233 sscanf(scalevalstr,
"%e",&scaleval);
235 scales.push_back(scaleval);
238 xmlEventNodes.push_back(elem);
240 }
else if (
mode == kInit) {
245 <<
"LHE file has invalid format" << std::endl;
248 if (name ==
"header") {
250 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
252 xmlHeader =
impl->createDocument(
nullptr, qname,
nullptr);
254 xmlNodes[0] = xmlHeader->getDocumentElement();
256 }
if (name ==
"init") {
258 }
else if (name ==
"event") {
262 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
264 if(xmlEvent) xmlEvent->release();
265 xmlEvent =
impl->createDocument(
nullptr, qname,
nullptr);
266 weightsinevent.resize(0);
271 const XMLCh *npLOval = attributes.getValue(XMLString::transcode(
"npLO"));
274 sscanf(npLOs,
"%d",&npLO);
276 const XMLCh *npNLOval = attributes.getValue(XMLString::transcode(
"npNLO"));
279 sscanf(npNLOs,
"%d",&npNLO);
282 xmlEventNodes.resize(1);
283 xmlEventNodes[0] = xmlEvent->getDocumentElement();
290 <<
"LHE file has invalid format" << std::endl;
296 const XMLCh *
const localname,
297 const XMLCh *
const qname)
303 if (
mode == kHeader && xmlNodes.size() > 1) {
304 xmlNodes.resize(xmlNodes.size() - 1);
306 }
else if (
mode == kHeader) {
307 std::unique_ptr<DOMLSSerializer>
writer(
impl->createLSSerializer());
308 std::unique_ptr<DOMLSOutput> outputDesc(
impl->createLSOutput());
309 assert(outputDesc.get());
310 outputDesc->setEncoding(
XMLUniStr(
"UTF-8"));
312 for(DOMNode *node = xmlNodes[0]->getFirstChild();
313 node; node = node->getNextSibling()) {
320 switch(node->getNodeType()) {
321 case DOMNode::ELEMENT_NODE:
322 elem =
static_cast<DOMElement*
>(node);
325 p = std::strchr((
const char*)
buffer,
327 q = std::strrchr(p,
'<');
329 case DOMNode::COMMENT_NODE:
337 std::strspn(
buffer,
" \t\r\n");
344 headers.push_back(header);
347 xmlHeader->release();
350 else if (name ==
"event" &&
352 (skipEvent || (!xmlEventNodes.empty()))) {
361 for(DOMNode *node = xmlEventNodes[0]->getFirstChild();
362 node; node = node->getNextSibling()) {
363 switch( node->getNodeType() ) {
364 case DOMNode::ELEMENT_NODE:
365 for(DOMNode *rwgt = xmlEventNodes[1]->getFirstChild();
366 rwgt; rwgt = rwgt->getNextSibling()) {
367 DOMNode* attr = rwgt->getAttributes()->item(0);
370 switch( rwgt->getNodeType() ) {
371 case DOMNode::ELEMENT_NODE:
372 weightsinevent.push_back(std::make_pair((
const char*)atname,
380 case DOMNode::TEXT_NODE:
391 else if (
mode == kEvent) {
396 if (gotObject !=
kNone)
398 <<
"Unexpected pileup in" 399 " LHEReader::XMLHandler::endElement" 408 const XMLSize_t length)
410 if (
mode == kHeader) {
411 DOMText *
text = xmlHeader->createTextNode(data_);
412 xmlNodes.back()->appendChild(text);
423 if(
mode == kEvent ) {
426 DOMText *
text = xmlEvent->createTextNode(data_+offset);
427 xmlEventNodes.back()->appendChild(text);
434 <<
"LHE file has invalid format" << std::endl;
441 const XMLSize_t length)
443 if (
mode == kHeader) {
444 DOMComment *
comment = xmlHeader->createComment(data_);
445 xmlNodes.back()->appendChild(comment);
453 headers.push_back(header);
459 firstEvent(params.getUntrackedParameter<unsigned
int>(
"skipEvents", 0)),
460 maxEvents(params.getUntrackedParameter<
int>(
"limitEvents", -1)),
503 if ( newFileOpened !=
nullptr ) *newFileOpened =
true;
505 }
else if ( !
strName.empty() ) {
523 return boost::shared_ptr<LHEEvent>();
532 std::istringstream
data;
538 std::for_each(
handler->headers.begin(),
553 <<
"Got LHE event without" 554 " initialization." << std::endl;
562 return boost::shared_ptr<LHEEvent>();
566 std::istringstream
data;
570 boost::shared_ptr<LHEEvent> lheevent;
573 for(
size_t i=0;
i< info.size(); ++
i ) {
578 lheevent->setNpLO(
handler->npLO);
579 lheevent->setNpNLO(
handler->npNLO);
581 if (!
handler->scales.empty()) {
582 lheevent->setScales(
handler->scales);
589 return boost::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
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)
boost::shared_ptr< LHERunInfo > curRunInfo
static const StorageFactory * get(void)
void addHeader(const Header &header)
std::unique_ptr< StorageWrap > fileStream
static bool isAllSpaces(const XMLCh *str, unsigned int length)
const std::string strName
std::vector< std::pair< std::string, std::string > > wgt_info
static std::shared_ptr< void > platformHandle()
std::unique_ptr< DOMImplementation > impl
std::unique_ptr< XMLDocument > curDoc
std::vector< DOMElement * > xmlNodes
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
boost::shared_ptr< LHEEvent > next(bool *newFileOpened=nullptr)
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::vector< float > scales
std::unique_ptr< XMLHandler > handler
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