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;
104 xmlHeader(0), xmlEvent(0), headerOk(
false), npLO(-99), npNLO(-99) {}
106 {
if (xmlHeader) xmlHeader->release();
107 if (xmlEvent) xmlEvent->release(); }
117 void reset() { headerOk =
false; weightsinevent.clear();}
122 void startElement(
const XMLCh *
const uri,
123 const XMLCh *
const localname,
124 const XMLCh *
const qname,
125 const Attributes &attributes)
override;
127 void endElement(
const XMLCh *
const uri,
128 const XMLCh *
const localname,
129 const XMLCh *
const qname)
override;
131 virtual void characters (
const XMLCh *
const chars,
const XMLSize_t length)
override;
132 virtual void comment (
const XMLCh *
const chars,
const XMLSize_t length)
override;
137 bool skipEvent =
false;
138 std::unique_ptr<DOMImplementation>
impl;
155 for(
unsigned int i = 0;
i < attributes.getLength();
i++) {
156 const XMLCh *
name = attributes.getQName(
i);
157 const XMLCh *
value = attributes.getValue(
i);
159 dom->setAttribute(name, value);
166 const char *
end = len >= 0 ? (data + len) : 0;
167 while(*data && (!end || data < end)) {
168 std::size_t len = std::strcspn(data,
"\r\n");
169 if (end && data + len > end)
171 if (data[len] ==
'\r' && data[len + 1] ==
'\n')
181 const XMLCh *
const localname,
182 const XMLCh *
const qname,
183 const Attributes &attributes)
188 if (name !=
"LesHouchesEvents")
190 <<
"LHE file has invalid header" << std::endl;
195 if (
mode == kHeader) {
196 DOMElement *
elem = xmlHeader->createElement(qname);
198 xmlNodes.back()->appendChild(elem);
199 xmlNodes.push_back(elem);
201 }
else if (
mode == kEvent ) {
203 if (skipEvent) {
return;}
205 DOMElement *
elem = xmlEvent->createElement(qname);
209 if( name ==
"rwgt" ) {
210 xmlEventNodes[0]->appendChild(elem);
211 }
else if (name ==
"wgt") {
212 xmlEventNodes[1]->appendChild(elem);
214 else if (name ==
"scales") {
215 for (XMLSize_t iscale=0; iscale<attributes.getLength(); ++iscale) {
217 const char *scalename =
XMLSimpleStr(attributes.getQName(iscale));
218 int nmatch = sscanf(scalename,
"pt_clust_%d",&ipart);
222 <<
"invalid attribute in <scales> tag" 227 const char *scalevalstr =
XMLSimpleStr(attributes.getValue(iscale));
228 sscanf(scalevalstr,
"%e",&scaleval);
230 scales.push_back(scaleval);
233 xmlEventNodes.push_back(elem);
235 }
else if (
mode == kInit) {
240 <<
"LHE file has invalid format" << std::endl;
243 if (name ==
"header") {
245 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
247 xmlHeader =
impl->createDocument(0, qname, 0);
249 xmlNodes[0] = xmlHeader->getDocumentElement();
251 }
if (name ==
"init") {
253 }
else if (name ==
"event") {
257 impl.reset(DOMImplementationRegistry::getDOMImplementation(
XMLUniStr(
"Core")));
259 if(xmlEvent) xmlEvent->release();
260 xmlEvent =
impl->createDocument(0, qname, 0);
261 weightsinevent.resize(0);
266 const XMLCh *npLOval = attributes.getValue(XMLString::transcode(
"npLO"));
269 sscanf(npLOs,
"%d",&npLO);
271 const XMLCh *npNLOval = attributes.getValue(XMLString::transcode(
"npNLO"));
274 sscanf(npNLOs,
"%d",&npNLO);
277 xmlEventNodes.resize(1);
278 xmlEventNodes[0] = xmlEvent->getDocumentElement();
285 <<
"LHE file has invalid format" << std::endl;
291 const XMLCh *
const localname,
292 const XMLCh *
const qname)
298 if (
mode == kHeader && xmlNodes.size() > 1) {
299 xmlNodes.resize(xmlNodes.size() - 1);
301 }
else if (
mode == kHeader) {
302 std::unique_ptr<DOMLSSerializer>
writer(
impl->createLSSerializer());
303 std::unique_ptr<DOMLSOutput> outputDesc(
impl->createLSOutput());
304 assert(outputDesc.get());
305 outputDesc->setEncoding(
XMLUniStr(
"UTF-8"));
307 for(DOMNode *node = xmlNodes[0]->getFirstChild();
308 node; node = node->getNextSibling()) {
315 switch(node->getNodeType()) {
316 case DOMNode::ELEMENT_NODE:
317 elem =
static_cast<DOMElement*
>(node);
320 p = std::strchr((
const char*)buffer,
322 q = std::strrchr(p,
'<');
324 case DOMNode::COMMENT_NODE:
327 q = buffer + strlen(buffer) - 3;
332 std::strspn(buffer,
" \t\r\n");
339 headers.push_back(header);
342 xmlHeader->release();
345 else if (name ==
"event" &&
347 (skipEvent || (xmlEventNodes.size() >= 1))) {
356 for(DOMNode *node = xmlEventNodes[0]->getFirstChild();
357 node; node = node->getNextSibling()) {
358 switch( node->getNodeType() ) {
359 case DOMNode::ELEMENT_NODE:
360 for(DOMNode *rwgt = xmlEventNodes[1]->getFirstChild();
361 rwgt; rwgt = rwgt->getNextSibling()) {
362 DOMNode* attr = rwgt->getAttributes()->item(0);
365 switch( rwgt->getNodeType() ) {
366 case DOMNode::ELEMENT_NODE:
367 weightsinevent.push_back(std::make_pair((
const char*)atname,
375 case DOMNode::TEXT_NODE:
386 else if (
mode == kEvent) {
391 if (gotObject !=
kNone)
393 <<
"Unexpected pileup in" 394 " LHEReader::XMLHandler::endElement" 403 const XMLSize_t length)
405 if (
mode == kHeader) {
406 DOMText *
text = xmlHeader->createTextNode(data_);
407 xmlNodes.back()->appendChild(text);
418 if(
mode == kEvent ) {
421 DOMText *
text = xmlEvent->createTextNode(data_+offset);
422 xmlEventNodes.back()->appendChild(text);
429 <<
"LHE file has invalid format" << std::endl;
436 const XMLSize_t length)
438 if (
mode == kHeader) {
439 DOMComment *
comment = xmlHeader->createComment(data_);
440 xmlNodes.back()->appendChild(comment);
448 headers.push_back(header);
454 firstEvent(params.getUntrackedParameter<unsigned
int>(
"skipEvents", 0)),
455 maxEvents(params.getUntrackedParameter<
int>(
"limitEvents", -1)),
493 if ( newFileOpened !=
nullptr ) *newFileOpened =
true;
513 return boost::shared_ptr<LHEEvent>();
522 std::istringstream
data;
528 std::for_each(
handler->headers.begin(),
543 <<
"Got LHE event without" 544 " initialization." << std::endl;
552 return boost::shared_ptr<LHEEvent>();
556 std::istringstream
data;
560 boost::shared_ptr<LHEEvent> lheevent;
563 for(
size_t i=0;
i< info.size(); ++
i ) {
568 lheevent->setNpLO(
handler->npLO);
569 lheevent->setNpNLO(
handler->npNLO);
571 if (
handler->scales.size()>0) {
572 lheevent->setScales(
handler->scales);
579 return boost::shared_ptr<LHEEvent>();
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)
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)
virtual 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)
static bool isAllSpaces(const XMLCh *str, unsigned int length)
const std::string strName
std::vector< std::pair< std::string, std::string > > wgt_info
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
std::auto_ptr< StorageWrap > fileStream
char data[epos_bytes_allocation]
virtual 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
std::auto_ptr< std::istream > fileStream
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