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 ()
 

Protected Member Functions

virtual void characters (const XMLCh *const chars, const XMLSize_t length) override
 
virtual 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.

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

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 
)
overrideprotectedvirtual

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

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

Definition at line 435 of file LHEReader.cc.

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

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

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

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

Definition at line 117 of file LHEReader.cc.

117 { headerOk = false; weightsinevent.clear();}
void lhef::LHEReader::XMLHandler::startElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const  qname,
const Attributes &  attributes 
)
overrideprotected

Definition at line 180 of file LHEReader.cc.

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

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

Definition at line 119 of file LHEReader.cc.

References comment.

119 {return weightsinevent;}

Friends And Related Function Documentation

friend class LHEReader
friend

Definition at line 135 of file LHEReader.cc.

Member Data Documentation

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

Definition at line 139 of file LHEReader.cc.

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

Definition at line 140 of file LHEReader.cc.

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

Definition at line 145 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 138 of file LHEReader.cc.

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

Definition at line 141 of file LHEReader.cc.

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

Definition at line 148 of file LHEReader.cc.

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

Definition at line 149 of file LHEReader.cc.

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

Definition at line 150 of file LHEReader.cc.

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

Definition at line 137 of file LHEReader.cc.

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

Definition at line 147 of file LHEReader.cc.

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

Definition at line 143 of file LHEReader.cc.

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

Definition at line 144 of file LHEReader.cc.

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

Definition at line 142 of file LHEReader.cc.

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

Definition at line 144 of file LHEReader.cc.