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
 
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 85 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 87 of file LHEReader.cc.

Member Enumeration Documentation

◆ Object

Enumerator
kNone 
kHeader 
kInit 
kComment 
kEvent 

Definition at line 104 of file LHEReader.cc.

104 { kNone = 0, kHeader, kInit, kComment, kEvent };

Constructor & Destructor Documentation

◆ XMLHandler()

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

Definition at line 88 of file LHEReader.cc.

89  : impl(nullptr),
91  mode(kNone),
92  xmlHeader(nullptr),
93  xmlEvent(nullptr),
94  headerOk(false),
95  npLO(-99),
96  npNLO(-99) {}

◆ ~XMLHandler()

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

Definition at line 97 of file LHEReader.cc.

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

References xmlEvent, and xmlHeader.

Member Function Documentation

◆ characters()

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

Definition at line 371 of file LHEReader.cc.

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

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

◆ comment()

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

Definition at line 400 of file LHEReader.cc.

400  {
401  if (mode == kHeader) {
402  DOMComment *comment = xmlHeader->createComment(data_);
403  xmlNodes.back()->appendChild(comment);
404  return;
405  }
406 
407  XMLSimpleStr data(data_);
408 
409  LHERunInfo::Header header;
411  headers.push_back(header);
412  }

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

◆ endElement()

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

Definition at line 274 of file LHEReader.cc.

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

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 cscNeutronWriter_cfi::writer.

◆ reset()

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

Definition at line 106 of file LHEReader.cc.

106  {
107  headerOk = false;
108  weightsinevent.clear();
109  gotObject = kNone;
110  mode = kNone;
111  }

References gotObject, headerOk, kNone, mode, and weightsinevent.

◆ 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 169 of file LHEReader.cc.

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

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

◆ weightInfo()

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

Definition at line 113 of file LHEReader.cc.

113 { return weightsinevent; }

References weightsinevent.

Friends And Related Function Documentation

◆ LHEReader

friend class LHEReader
friend

Definition at line 127 of file LHEReader.cc.

Member Data Documentation

◆ buffer

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

Definition at line 131 of file LHEReader.cc.

Referenced by startElement().

◆ gotObject

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

Definition at line 132 of file LHEReader.cc.

Referenced by reset().

◆ headerOk

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

Definition at line 137 of file LHEReader.cc.

Referenced by reset(), and startElement().

◆ headers

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

◆ impl

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

Definition at line 130 of file LHEReader.cc.

◆ mode

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

Definition at line 133 of file LHEReader.cc.

Referenced by reset(), and startElement().

◆ npLO

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

Definition at line 140 of file LHEReader.cc.

Referenced by startElement().

◆ npNLO

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

Definition at line 141 of file LHEReader.cc.

Referenced by startElement().

◆ scales

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

Definition at line 142 of file LHEReader.cc.

Referenced by startElement().

◆ skipEvent

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

Definition at line 129 of file LHEReader.cc.

Referenced by startElement().

◆ weightsinevent

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

Definition at line 139 of file LHEReader.cc.

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

◆ xmlEvent

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

Definition at line 135 of file LHEReader.cc.

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

◆ xmlEventNodes

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

Definition at line 136 of file LHEReader.cc.

Referenced by startElement().

◆ xmlHeader

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

Definition at line 134 of file LHEReader.cc.

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

◆ xmlNodes

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

Definition at line 136 of file LHEReader.cc.

Referenced by startElement().

lhef::XMLSimpleStr::isAllSpaces
static bool isAllSpaces(const XMLCh *str, unsigned int length)
Definition: XMLUtils.h:81
lhef::LHEReader::XMLHandler::impl
std::unique_ptr< DOMImplementation > impl
Definition: LHEReader.cc:130
lhef::fillHeader
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:154
mps_merge.weight
weight
Definition: mps_merge.py:88
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
lhef::LHEReader::XMLHandler::weightsinevent
wgt_info weightsinevent
Definition: LHEReader.cc:139
lhef::LHEReader::XMLHandler::xmlHeader
DOMDocument * xmlHeader
Definition: LHEReader.cc:134
lhef::LHEReader::XMLHandler::scales
std::vector< float > scales
Definition: LHEReader.cc:142
cms::cuda::assert
assert(be >=bs)
cscNeutronWriter_cfi.writer
writer
Definition: cscNeutronWriter_cfi.py:6
lhef::LHEReader::XMLHandler::headers
std::vector< LHERunInfo::Header > headers
Definition: LHEReader.cc:138
lhef::LHEReader::XMLHandler::kComment
Definition: LHEReader.cc:104
lhef::LHEReader::XMLHandler::buffer
std::string buffer
Definition: LHEReader.cc:131
lhef::LHEReader::XMLHandler::gotObject
Object gotObject
Definition: LHEReader.cc:132
lhef::LHEReader::XMLHandler::comment
void comment(const XMLCh *const chars, const XMLSize_t length) override
Definition: LHEReader.cc:400
lhef::LHEReader::XMLHandler::npLO
int npLO
Definition: LHEReader.cc:140
lhef::LHEReader::XMLHandler::kInit
Definition: LHEReader.cc:104
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
lhef::LHEReader::XMLHandler::xmlEvent
DOMDocument * xmlEvent
Definition: LHEReader.cc:135
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
gainCalibHelper::gainCalibPI::type
type
Definition: SiPixelGainCalibHelper.h:39
lhef::LHEReader::XMLHandler::mode
Object mode
Definition: LHEReader.cc:133
lhef::LHEReader::XMLHandler::kNone
Definition: LHEReader.cc:104
submitPVResolutionJobs.q
q
Definition: submitPVResolutionJobs.py:84
lhef::XMLSimpleStr::isSpace
static bool isSpace(XMLCh ch)
Definition: XMLUtils.h:85
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
lhef::LHEReader::XMLHandler::skipEvent
bool skipEvent
Definition: LHEReader.cc:129
lhef::LHEReader::XMLHandler::npNLO
int npNLO
Definition: LHEReader.cc:141
impl
Definition: trackAlgoPriorityOrder.h:18
lhef::LHEReader::XMLHandler::xmlNodes
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:136
lhef::LHEReader::XMLHandler::kHeader
Definition: LHEReader.cc:104
Exception
Definition: hltDiff.cc:246
lhef::attributesToDom
static void attributesToDom(DOMElement *dom, const Attributes &attributes)
Definition: LHEReader.cc:145
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
lhef::LHEReader::XMLHandler::xmlEventNodes
std::vector< DOMElement * > xmlEventNodes
Definition: LHEReader.cc:136
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
RecoTauValidation_cfi.header
header
Definition: RecoTauValidation_cfi.py:292
lhef::LHEReader::XMLHandler::headerOk
bool headerOk
Definition: LHEReader.cc:137
runonSM.text
text
Definition: runonSM.py:43
lhef::LHEReader::XMLHandler::kEvent
Definition: LHEReader.cc:104
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
weight
Definition: weight.py:1