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
 
int evtnum =-1
 
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 412 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.

414 {
415  if (mode == kHeader) {
416  DOMText *text = xmlHeader->createTextNode(data_);
417  xmlNodes.back()->appendChild(text);
418  return;
419  }
420 
421  if (XMLSimpleStr::isAllSpaces(data_, length))
422  return;
423 
424  unsigned int offset = 0;
425  while(offset < length && XMLSimpleStr::isSpace(data_[offset]))
426  offset++;
427 
428  if( mode == kEvent ) {
429  if (!skipEvent)
430  {
431  DOMText *text = xmlEvent->createTextNode(data_+offset);
432  xmlEventNodes.back()->appendChild(text);
433  }
434  return;
435  }
436 
437  if (mode == kNone)
438  throw cms::Exception("InvalidFormat")
439  << "LHE file has invalid format" << std::endl;
440 
441  XMLSimpleStr data(data_ + offset);
442  buffer.append(data);
443 }
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 445 of file LHEReader.cc.

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

447 {
448  if (mode == kHeader) {
449  DOMComment *comment = xmlHeader->createComment(data_);
450  xmlNodes.back()->appendChild(comment);
451  return;
452  }
453 
454  XMLSimpleStr data(data_);
455 
456  LHERunInfo::Header header;
457  fillHeader(header, data);
458  headers.push_back(header);
459 }
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:169
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:445
void lhef::LHEReader::XMLHandler::endElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const  qname 
)
overrideprotected

Definition at line 300 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.

303 {
304  std::string name((const char*)XMLSimpleStr(qname));
305 
306  if (mode) {
307 
308  if (mode == kHeader && xmlNodes.size() > 1) {
309  xmlNodes.resize(xmlNodes.size() - 1);
310  return;
311  } else if (mode == kHeader) {
312  std::unique_ptr<DOMLSSerializer> writer(impl->createLSSerializer());
313  std::unique_ptr<DOMLSOutput> outputDesc(impl->createLSOutput());
314  assert(outputDesc.get());
315  outputDesc->setEncoding(XMLUniStr("UTF-8"));
316 
317  for(DOMNode *node = xmlNodes[0]->getFirstChild();
318  node; node = node->getNextSibling()) {
319  XMLSimpleStr buffer(writer->writeToString(node));
320 
322  const char *p, *q;
323  DOMElement *elem;
324 
325  switch(node->getNodeType()) {
326  case DOMNode::ELEMENT_NODE:
327  elem = static_cast<DOMElement*>(node);
328  type = (const char*)XMLSimpleStr(
329  elem->getTagName());
330  p = std::strchr((const char*)buffer,
331  '>') + 1;
332  q = std::strrchr(p, '<');
333  break;
334  case DOMNode::COMMENT_NODE:
335  type = "";
336  p = buffer + 4;
337  q = buffer + strlen(buffer) - 3;
338  break;
339  default:
340  type = "<>";
341  p = buffer +
342  std::strspn(buffer, " \t\r\n");
343  if (!*p)
344  continue;
345  q = p + strlen(p);
346  }
347  LHERunInfo::Header header(type);
348  fillHeader(header, p, q - p);
349  headers.push_back(header);
350  }
351 
352  xmlHeader->release();
353  xmlHeader = nullptr;
354  }
355  else if (name == "event" &&
356  mode == kEvent &&
357  (skipEvent || (!xmlEventNodes.empty()))) { // handling of weights in LHE file
358 
359  if (skipEvent)
360  {
361  gotObject = mode;
362  mode = kNone;
363  return;
364  }
365 
366  for(DOMNode *node = xmlEventNodes[0]->getFirstChild();
367  node; node = node->getNextSibling()) {
368  switch( node->getNodeType() ) {
369  case DOMNode::ELEMENT_NODE: // rwgt
370  for(DOMNode *rwgt = xmlEventNodes[1]->getFirstChild();
371  rwgt; rwgt = rwgt->getNextSibling()) {
372  DOMNode* attr = rwgt->getAttributes()->item(0);
373  XMLSimpleStr atname(attr->getNodeValue());
374  XMLSimpleStr weight(rwgt->getFirstChild()->getNodeValue());
375  switch( rwgt->getNodeType() ) {
376  case DOMNode::ELEMENT_NODE:
377  weightsinevent.push_back(std::make_pair((const char*)atname,
378  (const char*)weight));
379  break;
380  default:
381  break;
382  }
383  }
384  break;
385  case DOMNode::TEXT_NODE: // event information
386  {
387  XMLSimpleStr data(node->getNodeValue());
388  buffer.append(data);
389  }
390  break;
391  default:
392  break;
393  }
394  }
395  }
396  else if (mode == kEvent) {
397  //skip unknown tags
398  return;
399  }
400 
401  if (gotObject != kNone)
402  throw cms::Exception("InvalidState")
403  << "Unexpected pileup in"
404  " LHEReader::XMLHandler::endElement"
405  << std::endl;
406 
407  gotObject = mode;
408  mode = kNone;
409  }
410 }
type
Definition: HCALResponse.h:21
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:169
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:19
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 186 of file LHEReader.cc.

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

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

int lhef::LHEReader::XMLHandler::evtnum =-1
private

Definition at line 156 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.