CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 () 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 86 of file LHEReader.cc.

Member Typedef Documentation

◆ wgt_info

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

Definition at line 88 of file LHEReader.cc.

Member Enumeration Documentation

◆ Object

Constructor & Destructor Documentation

◆ XMLHandler()

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

Definition at line 89 of file LHEReader.cc.

90  : impl(nullptr),
92  mode(kNone),
93  xmlHeader(nullptr),
94  xmlEvent(nullptr),
95  headerOk(false),
96  npLO(-99),
97  npNLO(-99) {}
std::unique_ptr< DOMImplementation > impl
Definition: LHEReader.cc:131

◆ ~XMLHandler()

lhef::LHEReader::XMLHandler::~XMLHandler ( )
inlineoverride

Definition at line 98 of file LHEReader.cc.

98  {
99  if (xmlHeader)
100  xmlHeader->release();
101  if (xmlEvent)
102  xmlEvent->release();
103  }

Member Function Documentation

◆ characters()

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

Definition at line 372 of file LHEReader.cc.

References edmScanValgrind::buffer, data, Exception, lhef::XMLSimpleStr::isAllSpaces(), lhef::XMLSimpleStr::isSpace(), WDecay::kNone, ALCARECOPromptCalibProdSiPixelAli0T_cff::mode, hltrates_dqm_sourceclient-live_cfg::offset, and submitPVValidationJobs::text.

372  {
373  if (mode == kHeader) {
374  DOMText *text = xmlHeader->createTextNode(data_);
375  xmlNodes.back()->appendChild(text);
376  return;
377  }
378 
379  if (XMLSimpleStr::isAllSpaces(data_, length))
380  return;
381 
382  unsigned int offset = 0;
383  while (offset < length && XMLSimpleStr::isSpace(data_[offset]))
384  offset++;
385 
386  if (mode == kEvent) {
387  if (!skipEvent) {
388  DOMText *text = xmlEvent->createTextNode(data_ + offset);
389  xmlEventNodes.back()->appendChild(text);
390  }
391  return;
392  }
393 
394  if (mode == kNone)
395  throw cms::Exception("InvalidFormat") << "LHE file has invalid format" << std::endl;
396 
397  XMLSimpleStr data(data_ + offset);
398  buffer.append(data);
399  }
static bool isSpace(XMLCh ch)
Definition: XMLUtils.h:88
static bool isAllSpaces(const XMLCh *str, unsigned int length)
Definition: XMLUtils.h:84
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:137
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
std::vector< DOMElement * > xmlEventNodes
Definition: LHEReader.cc:137

◆ comment()

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

Definition at line 401 of file LHEReader.cc.

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

401  {
402  if (mode == kHeader) {
403  DOMComment *comment = xmlHeader->createComment(data_);
404  xmlNodes.back()->appendChild(comment);
405  return;
406  }
407 
408  XMLSimpleStr data(data_);
409 
410  LHERunInfo::Header header;
412  headers.push_back(header);
413  }
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:155
std::vector< LHERunInfo::Header > headers
Definition: LHEReader.cc:139
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:137
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
void comment(const XMLCh *const chars, const XMLSize_t length) override
Definition: LHEReader.cc:401

◆ endElement()

void lhef::LHEReader::XMLHandler::endElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const  qname 
)
overrideprotected

Definition at line 275 of file LHEReader.cc.

References cms::cuda::assert(), edmScanValgrind::buffer, data, Exception, lhef::fillHeader(), RecoTauValidation_cfi::header, getRunAppsInfo::headers, WDecay::kNone, ALCARECOPromptCalibProdSiPixelAli0T_cff::mode, Skims_PA_cff::name, AlCaHLTBitMon_ParallelJobs::p, submitPVResolutionJobs::q, AlCaHLTBitMon_QueryRunRegistry::string, mps_merge::weight, and convertToRaw::writer.

277  {
278  std::string name((const char *)XMLSimpleStr(qname));
279 
280  if (mode) {
281  if (mode == kHeader && xmlNodes.size() > 1) {
282  xmlNodes.resize(xmlNodes.size() - 1);
283  return;
284  } else if (mode == kHeader) {
285  std::unique_ptr<DOMLSSerializer> writer(impl->createLSSerializer());
286  std::unique_ptr<DOMLSOutput> outputDesc(impl->createLSOutput());
287  assert(outputDesc.get());
288  outputDesc->setEncoding(XMLUniStr("UTF-8"));
289 
290  for (DOMNode *node = xmlNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
291  XMLSimpleStr buffer(writer->writeToString(node));
292 
294  const char *p, *q;
295  DOMElement *elem;
296 
297  switch (node->getNodeType()) {
298  case DOMNode::ELEMENT_NODE:
299  elem = static_cast<DOMElement *>(node);
300  type = (const char *)XMLSimpleStr(elem->getTagName());
301  p = std::strchr((const char *)buffer, '>') + 1;
302  q = std::strrchr(p, '<');
303  break;
304  case DOMNode::COMMENT_NODE:
305  type = "";
306  p = buffer + 4;
307  q = buffer + strlen(buffer) - 3;
308  break;
309  default:
310  type = "<>";
311  p = buffer + std::strspn(buffer, " \t\r\n");
312  if (!*p)
313  continue;
314  q = p + strlen(p);
315  }
316  LHERunInfo::Header header(type);
317  fillHeader(header, p, q - p);
318  headers.push_back(header);
319  }
320 
321  xmlHeader->release();
322  xmlHeader = nullptr;
323  } else if (name == "event" && mode == kEvent &&
324  (skipEvent || (!xmlEventNodes.empty()))) { // handling of weights in LHE file
325 
326  if (skipEvent) {
327  gotObject = mode;
328  mode = kNone;
329  return;
330  }
331 
332  for (DOMNode *node = xmlEventNodes[0]->getFirstChild(); node; node = node->getNextSibling()) {
333  switch (node->getNodeType()) {
334  case DOMNode::ELEMENT_NODE: // rwgt
335  for (DOMNode *rwgt = xmlEventNodes[1]->getFirstChild(); rwgt; rwgt = rwgt->getNextSibling()) {
336  DOMNode *attr = rwgt->getAttributes()->item(0);
337  XMLSimpleStr atname(attr->getNodeValue());
338  XMLSimpleStr weight(rwgt->getFirstChild()->getNodeValue());
339  switch (rwgt->getNodeType()) {
340  case DOMNode::ELEMENT_NODE:
341  weightsinevent.push_back(std::make_pair((const char *)atname, (const char *)weight));
342  break;
343  default:
344  break;
345  }
346  }
347  break;
348  case DOMNode::TEXT_NODE: // event information
349  {
350  XMLSimpleStr data(node->getNodeValue());
351  buffer.append(data);
352  } break;
353  default:
354  break;
355  }
356  }
357  } else if (mode == kEvent) {
358  //skip unknown tags
359  return;
360  }
361 
362  if (gotObject != kNone)
363  throw cms::Exception("InvalidState") << "Unexpected pileup in"
364  " LHEReader::XMLHandler::endElement"
365  << std::endl;
366 
367  gotObject = mode;
368  mode = kNone;
369  }
370  }
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:155
Definition: weight.py:1
assert(be >=bs)
std::vector< LHERunInfo::Header > headers
Definition: LHEReader.cc:139
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:137
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
std::vector< DOMElement * > xmlEventNodes
Definition: LHEReader.cc:137

◆ reset()

void lhef::LHEReader::XMLHandler::reset ( void  )
inline

◆ startElement()

void lhef::LHEReader::XMLHandler::startElement ( const XMLCh *const  uri,
const XMLCh *const  localname,
const XMLCh *const  qname,
const Attributes &  attributes 
)
overrideprotected

Definition at line 170 of file LHEReader.cc.

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

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

◆ weightInfo()

const wgt_info& lhef::LHEReader::XMLHandler::weightInfo ( ) const
inline

Definition at line 114 of file LHEReader.cc.

114 { return weightsinevent; }

Friends And Related Function Documentation

◆ LHEReader

friend class LHEReader
friend

Definition at line 128 of file LHEReader.cc.

Member Data Documentation

◆ buffer

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

Definition at line 132 of file LHEReader.cc.

◆ gotObject

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

Definition at line 133 of file LHEReader.cc.

◆ headerOk

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

Definition at line 138 of file LHEReader.cc.

◆ headers

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

◆ impl

std::unique_ptr<DOMImplementation> lhef::LHEReader::XMLHandler::impl
private

Definition at line 131 of file LHEReader.cc.

◆ mode

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

Definition at line 134 of file LHEReader.cc.

◆ npLO

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

Definition at line 141 of file LHEReader.cc.

◆ npNLO

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

Definition at line 142 of file LHEReader.cc.

◆ scales

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

Definition at line 143 of file LHEReader.cc.

◆ skipEvent

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

Definition at line 130 of file LHEReader.cc.

◆ weightsinevent

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

Definition at line 140 of file LHEReader.cc.

◆ xmlEvent

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

Definition at line 136 of file LHEReader.cc.

◆ xmlEventNodes

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

Definition at line 137 of file LHEReader.cc.

◆ xmlHeader

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

Definition at line 135 of file LHEReader.cc.

◆ xmlNodes

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

Definition at line 137 of file LHEReader.cc.