CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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

void characters (const XMLCh *const data, const unsigned int length) override
 
void comment (const XMLCh *const data, const unsigned int 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
 
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 108 of file LHEReader.cc.

Constructor & Destructor Documentation

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

Definition at line 104 of file LHEReader.cc.

References xmlEvent, and xmlHeader.

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

Member Function Documentation

void lhef::LHEReader::XMLHandler::characters ( const XMLCh *const  data,
const unsigned int  length 
)
overrideprotected

Definition at line 401 of file LHEReader.cc.

References AlCaHLTBitMon_QueryRunRegistry::data, Exception, lhef::XMLSimpleStr::isAllSpaces(), lhef::XMLSimpleStr::isSpace(), WDecay::kNone, alignBH_cfg::mode, hltrates_dqm_sourceclient-live_cfg::offset, and runonSM::text.

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

Definition at line 434 of file LHEReader.cc.

References comment, AlCaHLTBitMon_QueryRunRegistry::data, lhef::fillHeader(), getRunAppsInfo::headers, and alignBH_cfg::mode.

436 {
437  if (mode == kHeader) {
438  DOMComment *comment = xmlHeader->createComment(data_);
439  xmlNodes.back()->appendChild(comment);
440  return;
441  }
442 
443  XMLSimpleStr data(data_);
444 
445  LHERunInfo::Header header;
446  fillHeader(header, data);
447  headers.push_back(header);
448 }
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:162
void comment(const XMLCh *const data, const unsigned int length) override
Definition: LHEReader.cc:434
std::vector< LHERunInfo::Header > headers
Definition: LHEReader.cc:145
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:143
void lhef::LHEReader::XMLHandler::endElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const  qname 
)
overrideprotected

Definition at line 289 of file LHEReader.cc.

References AlCaHLTBitMon_QueryRunRegistry::data, HTMLExport::elem(), Exception, lhef::fillHeader(), getRunAppsInfo::headers, WDecay::kNone, alignBH_cfg::mode, mergeVDriftHistosByStation::name, AlCaHLTBitMon_ParallelJobs::p, lumiQueryAPI::q, AlCaHLTBitMon_QueryRunRegistry::string, and puppiForMET_cff::weight.

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

Definition at line 116 of file LHEReader.cc.

References headerOk, and weightsinevent.

116 { 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 179 of file LHEReader.cc.

References lhef::attributesToDom(), buffer, HTMLExport::elem(), Exception, headerOk, impl, kEvent, kHeader, kInit, kNone, mode, mergeVDriftHistosByStation::name, npLO, npNLO, scales, skipEvent, AlCaHLTBitMon_QueryRunRegistry::string, XERCES_CPP_NAMESPACE_USE::transcode(), weightsinevent, xmlEvent, xmlEventNodes, xmlHeader, and xmlNodes.

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

Definition at line 118 of file LHEReader.cc.

References weightsinevent.

118 {return weightsinevent;}

Friends And Related Function Documentation

friend class LHEReader
friend

Definition at line 134 of file LHEReader.cc.

Member Data Documentation

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

Definition at line 138 of file LHEReader.cc.

Referenced by startElement().

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

Definition at line 139 of file LHEReader.cc.

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

Definition at line 144 of file LHEReader.cc.

Referenced by reset(), and startElement().

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

Definition at line 137 of file LHEReader.cc.

Referenced by startElement().

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

Definition at line 140 of file LHEReader.cc.

Referenced by startElement().

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

Definition at line 147 of file LHEReader.cc.

Referenced by startElement().

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

Definition at line 148 of file LHEReader.cc.

Referenced by startElement().

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

Definition at line 149 of file LHEReader.cc.

Referenced by startElement().

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

Definition at line 136 of file LHEReader.cc.

Referenced by startElement().

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

Definition at line 146 of file LHEReader.cc.

Referenced by reset(), startElement(), and weightInfo().

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

Definition at line 142 of file LHEReader.cc.

Referenced by startElement(), and ~XMLHandler().

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

Definition at line 143 of file LHEReader.cc.

Referenced by startElement().

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

Definition at line 141 of file LHEReader.cc.

Referenced by startElement(), and ~XMLHandler().

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

Definition at line 143 of file LHEReader.cc.

Referenced by startElement().