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

Member Enumeration Documentation

◆ Object

Enumerator
kNone 
kHeader 
kInit 
kComment 
kEvent 

Definition at line 106 of file LHEReader.cc.

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

Constructor & Destructor Documentation

◆ XMLHandler()

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

Definition at line 90 of file LHEReader.cc.

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

◆ ~XMLHandler()

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

Definition at line 99 of file LHEReader.cc.

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

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

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

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

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

References comment, 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 276 of file LHEReader.cc.

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

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, data-class-funcs::q, AlCaHLTBitMon_QueryRunRegistry::string, mps_merge::weight, and cscNeutronWriter_cfi::writer.

◆ reset()

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

Definition at line 108 of file LHEReader.cc.

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

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

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  }
220  xmlEventNodes.push_back(elem);
221  return;
222  } else if (mode == kInit) {
223  //skip unknown tags in init block as well
224  return;
225  } else if (mode != kNone) {
226  throw cms::Exception("InvalidFormat") << "LHE file has invalid format" << std::endl;
227  }
228 
229  if (name == "header") {
230  if (!impl)
231  impl.reset(DOMImplementationRegistry::getDOMImplementation(XMLUniStr("Core")));
232 
233  xmlHeader = impl->createDocument(nullptr, qname, nullptr);
234  xmlNodes.resize(1);
235  xmlNodes[0] = xmlHeader->getDocumentElement();
236  mode = kHeader;
237  }
238  if (name == "init") {
239  mode = kInit;
240  } else if (name == "event") {
241  if (!skipEvent) {
242  if (!impl)
243  impl.reset(DOMImplementationRegistry::getDOMImplementation(XMLUniStr("Core")));
244 
245  if (xmlEvent)
246  xmlEvent->release();
247  xmlEvent = impl->createDocument(nullptr, qname, nullptr);
248  weightsinevent.resize(0);
249  scales.clear();
250 
251  npLO = -99;
252  npNLO = -99;
253  const XMLCh *npLOval = attributes.getValue(XMLString::transcode("npLO"));
254  if (npLOval) {
255  const char *npLOs = XMLSimpleStr(npLOval);
256  sscanf(npLOs, "%d", &npLO);
257  }
258  const XMLCh *npNLOval = attributes.getValue(XMLString::transcode("npNLO"));
259  if (npNLOval) {
260  const char *npNLOs = XMLSimpleStr(npNLOval);
261  sscanf(npNLOs, "%d", &npNLO);
262  }
263 
264  xmlEventNodes.resize(1);
265  xmlEventNodes[0] = xmlEvent->getDocumentElement();
266  }
267  mode = kEvent;
268  }
269 
270  if (mode == kNone)
271  throw cms::Exception("InvalidFormat") << "LHE file has invalid format" << std::endl;
272 
273  buffer.clear();
274  }

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

115 { return weightsinevent; }

References weightsinevent.

Friends And Related Function Documentation

◆ LHEReader

friend class LHEReader
friend

Definition at line 129 of file LHEReader.cc.

Member Data Documentation

◆ buffer

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

Definition at line 133 of file LHEReader.cc.

Referenced by startElement().

◆ gotObject

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

Definition at line 134 of file LHEReader.cc.

Referenced by reset().

◆ headerOk

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

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

◆ mode

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

Definition at line 135 of file LHEReader.cc.

Referenced by reset(), and startElement().

◆ npLO

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

Definition at line 142 of file LHEReader.cc.

Referenced by startElement().

◆ npNLO

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

Definition at line 143 of file LHEReader.cc.

Referenced by startElement().

◆ scales

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

Definition at line 144 of file LHEReader.cc.

Referenced by startElement().

◆ skipEvent

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

Definition at line 131 of file LHEReader.cc.

Referenced by startElement().

◆ weightsinevent

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

Definition at line 141 of file LHEReader.cc.

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

◆ xmlEvent

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

Definition at line 137 of file LHEReader.cc.

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

◆ xmlEventNodes

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

Definition at line 138 of file LHEReader.cc.

Referenced by startElement().

◆ xmlHeader

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

Definition at line 136 of file LHEReader.cc.

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

◆ xmlNodes

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

Definition at line 138 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:132
lhef::fillHeader
static void fillHeader(LHERunInfo::Header &header, const char *data, int len=-1)
Definition: LHEReader.cc:156
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:141
lhef::LHEReader::XMLHandler::xmlHeader
DOMDocument * xmlHeader
Definition: LHEReader.cc:136
lhef::LHEReader::XMLHandler::scales
std::vector< float > scales
Definition: LHEReader.cc:144
data-class-funcs.q
q
Definition: data-class-funcs.py:169
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:140
lhef::LHEReader::XMLHandler::kComment
Definition: LHEReader.cc:106
lhef::LHEReader::XMLHandler::buffer
std::string buffer
Definition: LHEReader.cc:133
lhef::LHEReader::XMLHandler::gotObject
Object gotObject
Definition: LHEReader.cc:134
lhef::LHEReader::XMLHandler::comment
void comment(const XMLCh *const chars, const XMLSize_t length) override
Definition: LHEReader.cc:402
lhef::LHEReader::XMLHandler::npLO
int npLO
Definition: LHEReader.cc:142
lhef::LHEReader::XMLHandler::kInit
Definition: LHEReader.cc:106
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::LogError
Definition: MessageLogger.h:183
lhef::LHEReader::XMLHandler::xmlEvent
DOMDocument * xmlEvent
Definition: LHEReader.cc:137
lhef::LHEReader::XMLHandler::mode
Object mode
Definition: LHEReader.cc:135
lhef::LHEReader::XMLHandler::kNone
Definition: LHEReader.cc:106
lhef::XMLSimpleStr::isSpace
static bool isSpace(XMLCh ch)
Definition: XMLUtils.h:85
lhef::LHEReader::XMLHandler::skipEvent
bool skipEvent
Definition: LHEReader.cc:131
lhef::LHEReader::XMLHandler::npNLO
int npNLO
Definition: LHEReader.cc:143
type
type
Definition: HCALResponse.h:21
impl
Definition: trackAlgoPriorityOrder.h:18
lhef::LHEReader::XMLHandler::xmlNodes
std::vector< DOMElement * > xmlNodes
Definition: LHEReader.cc:138
lhef::LHEReader::XMLHandler::kHeader
Definition: LHEReader.cc:106
Exception
Definition: hltDiff.cc:246
lhef::attributesToDom
static void attributesToDom(DOMElement *dom, const Attributes &attributes)
Definition: LHEReader.cc:147
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
lhef::LHEReader::XMLHandler::xmlEventNodes
std::vector< DOMElement * > xmlEventNodes
Definition: LHEReader.cc:138
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:139
runonSM.text
text
Definition: runonSM.py:43
lhef::LHEReader::XMLHandler::kEvent
Definition: LHEReader.cc:106
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
weight
Definition: weight.py:1