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 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 376 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.

376  {
377  if (mode == kHeader) {
378  DOMText *text = xmlHeader->createTextNode(data_);
379  xmlNodes.back()->appendChild(text);
380  return;
381  }
382 
383  if (XMLSimpleStr::isAllSpaces(data_, length))
384  return;
385 
386  unsigned int offset = 0;
387  while (offset < length && XMLSimpleStr::isSpace(data_[offset]))
388  offset++;
389 
390  if (mode == kEvent) {
391  if (!skipEvent) {
392  DOMText *text = xmlEvent->createTextNode(data_ + offset);
393  xmlEventNodes.back()->appendChild(text);
394  }
395  return;
396  }
397 
398  if (mode == kNone)
399  throw cms::Exception("InvalidFormat") << "LHE file has invalid format" << std::endl;
400 
401  XMLSimpleStr data(data_ + offset);
402  buffer.append(data);
403  }
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 405 of file LHEReader.cc.

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

405  {
406  if (mode == kHeader) {
407  DOMComment *comment = xmlHeader->createComment(data_);
408  xmlNodes.back()->appendChild(comment);
409  return;
410  }
411 
412  XMLSimpleStr data(data_);
413 
414  LHERunInfo::Header header;
416  headers.push_back(header);
417  }
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:156
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:405

◆ endElement()

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

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

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

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

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

◆ 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.

◆ evtnum

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

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