CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Protected Member Functions | Private Attributes | Friends
lhef::LHEReader::XMLHandler Class Reference
Inheritance diagram for lhef::LHEReader::XMLHandler:
lhef::XMLDocument::Handler

Public Types

enum  Object {
  kNone = 0, kHeader, kInit, kComment,
  kEvent
}
 
typedef std::vector< std::pair< std::string, std::string > > wgt_info
 

Public Member Functions

void reset ()
 
const wgt_infoweightInfo () const
 
 XMLHandler ()
 
 ~XMLHandler () override
 

Protected Member Functions

void characters (const XMLCh *const chars, const XMLSize_t length) override
 
void comment (const XMLCh *const chars, const XMLSize_t length) override
 
void endElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname) override
 
void startElement (const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const Attributes &attributes) override
 

Private Attributes

std::string buffer
 
Object gotObject
 
bool headerOk
 
std::vector< LHERunInfo::Headerheaders
 
std::unique_ptr< DOMImplementation > impl
 
Object mode
 
int npLO
 
int npNLO
 
std::vector< float > scales
 
bool skipEvent = false
 
wgt_info weightsinevent
 
DOMDocument * xmlEvent
 
std::vector< DOMElement * > xmlEventNodes
 
DOMDocument * xmlHeader
 
std::vector< DOMElement * > xmlNodes
 

Friends

class LHEReader
 

Detailed Description

Definition at line 98 of file LHEReader.cc.

Member Typedef Documentation

typedef std::vector<std::pair<std::string,std::string> > lhef::LHEReader::XMLHandler::wgt_info

Definition at line 100 of file LHEReader.cc.

Member Enumeration Documentation

Enumerator
kNone 
kHeader 
kInit 
kComment 
kEvent 

Definition at line 109 of file LHEReader.cc.

Constructor & Destructor Documentation

lhef::LHEReader::XMLHandler::XMLHandler ( )
inline

Definition at line 101 of file LHEReader.cc.

101  :
102  impl(nullptr),
104  xmlHeader(nullptr), xmlEvent(nullptr), headerOk(false), npLO(-99), npNLO(-99) {}
std::unique_ptr< DOMImplementation > impl
Definition: LHEReader.cc:143
lhef::LHEReader::XMLHandler::~XMLHandler ( )
inlineoverride

Definition at line 105 of file LHEReader.cc.

106  { if (xmlHeader) xmlHeader->release();
107  if (xmlEvent) xmlEvent->release(); }

Member Function Documentation

void lhef::LHEReader::XMLHandler::characters ( const XMLCh *const  chars,
const XMLSize_t  length 
)
overrideprotected

Definition at line 407 of file LHEReader.cc.

References edmScanValgrind::buffer, data, Exception, lhef::XMLSimpleStr::isAllSpaces(), lhef::XMLSimpleStr::isSpace(), WDecay::kNone, ALCARECOPromptCalibProdSiPixelAli0T_cff::mode, PFRecoTauDiscriminationByIsolation_cfi::offset, and RecoTauValidation_cfi::text.

409 {
410  if (mode == kHeader) {
411  DOMText *text = xmlHeader->createTextNode(data_);
412  xmlNodes.back()->appendChild(text);
413  return;
414  }
415 
416  if (XMLSimpleStr::isAllSpaces(data_, length))
417  return;
418 
419  unsigned int offset = 0;
420  while(offset < length && XMLSimpleStr::isSpace(data_[offset]))
421  offset++;
422 
423  if( mode == kEvent ) {
424  if (!skipEvent)
425  {
426  DOMText *text = xmlEvent->createTextNode(data_+offset);
427  xmlEventNodes.back()->appendChild(text);
428  }
429  return;
430  }
431 
432  if (mode == kNone)
433  throw cms::Exception("InvalidFormat")
434  << "LHE file has invalid format" << std::endl;
435 
436  XMLSimpleStr data(data_ + offset);
437  buffer.append(data);
438 }
static bool isSpace(XMLCh ch)
Definition: XMLUtils.h:89
std::vector< DOMElement * > xmlEventNodes
Definition: LHEReader.cc:149
static bool isAllSpaces(const XMLCh *str, unsigned int length)
Definition: XMLUtils.h:85
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:149
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void lhef::LHEReader::XMLHandler::comment ( const XMLCh *const  chars,
const XMLSize_t  length 
)
overrideprotected

Definition at line 440 of file LHEReader.cc.

References comment, data, lhef::fillHeader(), RecoTauValidation_cfi::header, and ALCARECOPromptCalibProdSiPixelAli0T_cff::mode.

442 {
443  if (mode == kHeader) {
444  DOMComment *comment = xmlHeader->createComment(data_);
445  xmlNodes.back()->appendChild(comment);
446  return;
447  }
448 
449  XMLSimpleStr data(data_);
450 
451  LHERunInfo::Header header;
452  fillHeader(header, data);
453  headers.push_back(header);
454 }
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:168
std::vector< LHERunInfo::Header > headers
Definition: LHEReader.cc:151
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:149
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void comment(const XMLCh *const chars, const XMLSize_t length) override
Definition: LHEReader.cc:440
void lhef::LHEReader::XMLHandler::endElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const  qname 
)
overrideprotected

Definition at line 295 of file LHEReader.cc.

References edmScanValgrind::buffer, data, HTMLExport::elem(), Exception, lhef::fillHeader(), RecoTauValidation_cfi::header, WDecay::kNone, ALCARECOPromptCalibProdSiPixelAli0T_cff::mode, dataset::name, AlCaHLTBitMon_ParallelJobs::p, lumiQueryAPI::q, AlCaHLTBitMon_QueryRunRegistry::string, mps_merge::weight, and cscNeutronWriter_cfi::writer.

298 {
299  std::string name((const char*)XMLSimpleStr(qname));
300 
301  if (mode) {
302 
303  if (mode == kHeader && xmlNodes.size() > 1) {
304  xmlNodes.resize(xmlNodes.size() - 1);
305  return;
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"));
311 
312  for(DOMNode *node = xmlNodes[0]->getFirstChild();
313  node; node = node->getNextSibling()) {
314  XMLSimpleStr buffer(writer->writeToString(node));
315 
317  const char *p, *q;
318  DOMElement *elem;
319 
320  switch(node->getNodeType()) {
321  case DOMNode::ELEMENT_NODE:
322  elem = static_cast<DOMElement*>(node);
323  type = (const char*)XMLSimpleStr(
324  elem->getTagName());
325  p = std::strchr((const char*)buffer,
326  '>') + 1;
327  q = std::strrchr(p, '<');
328  break;
329  case DOMNode::COMMENT_NODE:
330  type = "";
331  p = buffer + 4;
332  q = buffer + strlen(buffer) - 3;
333  break;
334  default:
335  type = "<>";
336  p = buffer +
337  std::strspn(buffer, " \t\r\n");
338  if (!*p)
339  continue;
340  q = p + strlen(p);
341  }
342  LHERunInfo::Header header(type);
343  fillHeader(header, p, q - p);
344  headers.push_back(header);
345  }
346 
347  xmlHeader->release();
348  xmlHeader = nullptr;
349  }
350  else if (name == "event" &&
351  mode == kEvent &&
352  (skipEvent || (!xmlEventNodes.empty()))) { // handling of weights in LHE file
353 
354  if (skipEvent)
355  {
356  gotObject = mode;
357  mode = kNone;
358  return;
359  }
360 
361  for(DOMNode *node = xmlEventNodes[0]->getFirstChild();
362  node; node = node->getNextSibling()) {
363  switch( node->getNodeType() ) {
364  case DOMNode::ELEMENT_NODE: // rwgt
365  for(DOMNode *rwgt = xmlEventNodes[1]->getFirstChild();
366  rwgt; rwgt = rwgt->getNextSibling()) {
367  DOMNode* attr = rwgt->getAttributes()->item(0);
368  XMLSimpleStr atname(attr->getNodeValue());
369  XMLSimpleStr weight(rwgt->getFirstChild()->getNodeValue());
370  switch( rwgt->getNodeType() ) {
371  case DOMNode::ELEMENT_NODE:
372  weightsinevent.push_back(std::make_pair((const char*)atname,
373  (const char*)weight));
374  break;
375  default:
376  break;
377  }
378  }
379  break;
380  case DOMNode::TEXT_NODE: // event information
381  {
382  XMLSimpleStr data(node->getNodeValue());
383  buffer.append(data);
384  }
385  break;
386  default:
387  break;
388  }
389  }
390  }
391  else if (mode == kEvent) {
392  //skip unknown tags
393  return;
394  }
395 
396  if (gotObject != kNone)
397  throw cms::Exception("InvalidState")
398  << "Unexpected pileup in"
399  " LHEReader::XMLHandler::endElement"
400  << std::endl;
401 
402  gotObject = mode;
403  mode = kNone;
404  }
405 }
type
Definition: HCALResponse.h:21
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:168
Definition: weight.py:1
std::vector< LHERunInfo::Header > headers
Definition: LHEReader.cc:151
std::vector< DOMElement * > xmlEventNodes
Definition: LHEReader.cc:149
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:149
def elem(elemtype, innerHTML='', html_class='', kwargs)
Definition: HTMLExport.py:18
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void lhef::LHEReader::XMLHandler::reset ( void  )
inline
void lhef::LHEReader::XMLHandler::startElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const  qname,
const Attributes &  attributes 
)
overrideprotected

Definition at line 185 of file LHEReader.cc.

References lhef::attributesToDom(), edmScanValgrind::buffer, HTMLExport::elem(), Exception, WDecay::kNone, ALCARECOPromptCalibProdSiPixelAli0T_cff::mode, dataset::name, and AlCaHLTBitMon_QueryRunRegistry::string.

189 {
190  std::string name((const char*)XMLSimpleStr(qname));
191 
192  if (!headerOk) {
193  if (name != "LesHouchesEvents")
194  throw cms::Exception("InvalidFormat")
195  << "LHE file has invalid header" << std::endl;
196  headerOk = true;
197  return;
198  }
199 
200  if (mode == kHeader) {
201  DOMElement *elem = xmlHeader->createElement(qname);
202  attributesToDom(elem, attributes);
203  xmlNodes.back()->appendChild(elem);
204  xmlNodes.push_back(elem);
205  return;
206  } else if ( mode == kEvent ) {
207 
208  if (skipEvent) {return;}
209 
210  DOMElement *elem = xmlEvent->createElement(qname);
211  attributesToDom(elem, attributes);
212 
213  //TODO this is a hack (even more than the rest of this class)
214  if( name == "rwgt" ) {
215  xmlEventNodes[0]->appendChild(elem);
216  } else if (name == "wgt") {
217  xmlEventNodes[1]->appendChild(elem);
218  }
219  else if (name == "scales") {
220  for (XMLSize_t iscale=0; iscale<attributes.getLength(); ++iscale) {
221  int ipart = 0;
222  const char *scalename = XMLSimpleStr(attributes.getQName(iscale));
223  int nmatch = sscanf(scalename,"pt_clust_%d",&ipart);
224 
225  if (nmatch!=1) {
226  edm::LogError("Generator|LHEInterface")
227  << "invalid attribute in <scales> tag"
228  << std::endl;
229  }
230 
231  float scaleval;
232  const char *scalevalstr = XMLSimpleStr(attributes.getValue(iscale));
233  sscanf(scalevalstr,"%e",&scaleval);
234 
235  scales.push_back(scaleval);
236  }
237  }
238  xmlEventNodes.push_back(elem);
239  return;
240  } else if (mode == kInit) {
241  //skip unknown tags in init block as well
242  return;
243  } else if (mode != kNone) {
244  throw cms::Exception("InvalidFormat")
245  << "LHE file has invalid format" << std::endl;
246  }
247 
248  if (name == "header") {
249  if (!impl)
250  impl.reset(DOMImplementationRegistry::getDOMImplementation(XMLUniStr("Core")));
251 
252  xmlHeader = impl->createDocument(nullptr, qname, nullptr);
253  xmlNodes.resize(1);
254  xmlNodes[0] = xmlHeader->getDocumentElement();
255  mode = kHeader;
256  } if (name == "init") {
257  mode = kInit;
258  } else if (name == "event") {
259  if (!skipEvent)
260  {
261  if (!impl)
262  impl.reset(DOMImplementationRegistry::getDOMImplementation(XMLUniStr("Core")));
263 
264  if(xmlEvent) xmlEvent->release();
265  xmlEvent = impl->createDocument(nullptr, qname, nullptr);
266  weightsinevent.resize(0);
267  scales.clear();
268 
269  npLO = -99;
270  npNLO = -99;
271  const XMLCh *npLOval = attributes.getValue(XMLString::transcode("npLO"));
272  if (npLOval) {
273  const char *npLOs = XMLSimpleStr(npLOval);
274  sscanf(npLOs,"%d",&npLO);
275  }
276  const XMLCh *npNLOval = attributes.getValue(XMLString::transcode("npNLO"));
277  if (npNLOval) {
278  const char *npNLOs = XMLSimpleStr(npNLOval);
279  sscanf(npNLOs,"%d",&npNLO);
280  }
281 
282  xmlEventNodes.resize(1);
283  xmlEventNodes[0] = xmlEvent->getDocumentElement();
284  }
285  mode = kEvent;
286  }
287 
288  if (mode == kNone)
289  throw cms::Exception("InvalidFormat")
290  << "LHE file has invalid format" << std::endl;
291 
292  buffer.clear();
293 }
std::vector< DOMElement * > xmlEventNodes
Definition: LHEReader.cc:149
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:149
def elem(elemtype, innerHTML='', html_class='', kwargs)
Definition: HTMLExport.py:18
std::vector< float > scales
Definition: LHEReader.cc:155
static void attributesToDom(DOMElement *dom, const Attributes &attributes)
Definition: LHEReader.cc:158
const wgt_info& lhef::LHEReader::XMLHandler::weightInfo ( ) const
inline

Definition at line 124 of file LHEReader.cc.

References comment.

124 {return weightsinevent;}

Friends And Related Function Documentation

friend class LHEReader
friend

Definition at line 140 of file LHEReader.cc.

Member Data Documentation

std::string lhef::LHEReader::XMLHandler::buffer
private

Definition at line 144 of file LHEReader.cc.

Object lhef::LHEReader::XMLHandler::gotObject
private

Definition at line 145 of file LHEReader.cc.

bool lhef::LHEReader::XMLHandler::headerOk
private

Definition at line 150 of file LHEReader.cc.

std::vector<LHERunInfo::Header> lhef::LHEReader::XMLHandler::headers
private
std::unique_ptr<DOMImplementation> lhef::LHEReader::XMLHandler::impl
private

Definition at line 143 of file LHEReader.cc.

Object lhef::LHEReader::XMLHandler::mode
private

Definition at line 146 of file LHEReader.cc.

int lhef::LHEReader::XMLHandler::npLO
private

Definition at line 153 of file LHEReader.cc.

int lhef::LHEReader::XMLHandler::npNLO
private

Definition at line 154 of file LHEReader.cc.

std::vector<float> lhef::LHEReader::XMLHandler::scales
private

Definition at line 155 of file LHEReader.cc.

bool lhef::LHEReader::XMLHandler::skipEvent = false
private

Definition at line 142 of file LHEReader.cc.

wgt_info lhef::LHEReader::XMLHandler::weightsinevent
private

Definition at line 152 of file LHEReader.cc.

DOMDocument* lhef::LHEReader::XMLHandler::xmlEvent
private

Definition at line 148 of file LHEReader.cc.

std::vector<DOMElement*> lhef::LHEReader::XMLHandler::xmlEventNodes
private

Definition at line 149 of file LHEReader.cc.

DOMDocument* lhef::LHEReader::XMLHandler::xmlHeader
private

Definition at line 147 of file LHEReader.cc.

std::vector<DOMElement*> lhef::LHEReader::XMLHandler::xmlNodes
private

Definition at line 149 of file LHEReader.cc.