CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
LHERunInfo.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iostream>
3 #include <iomanip>
4 #include <string>
5 #include <cctype>
6 #include <vector>
7 #include <memory>
8 #include <cmath>
9 #include <cstring>
10 
11 #include <boost/bind.hpp>
12 
13 #include <xercesc/dom/DOM.hpp>
14 #include <xercesc/parsers/XercesDOMParser.hpp>
15 #include <xercesc/sax/HandlerBase.hpp>
16 
19 
21 
23 
24 #include "XMLUtils.h"
25 
26 XERCES_CPP_NAMESPACE_USE
27 
28 static int skipWhitespace(std::istream &in)
29 {
30  int ch;
31  do {
32  ch = in.get();
33  } while(std::isspace(ch));
34  if (ch != std::istream::traits_type::eof())
35  in.putback(ch);
36  return ch;
37 }
38 
39 namespace lhef {
40 
41 LHERunInfo::LHERunInfo(std::istream &in)
42 {
43  in >> heprup.IDBMUP.first >> heprup.IDBMUP.second
44  >> heprup.EBMUP.first >> heprup.EBMUP.second
45  >> heprup.PDFGUP.first >> heprup.PDFGUP.second
46  >> heprup.PDFSUP.first >> heprup.PDFSUP.second
47  >> heprup.IDWTUP >> heprup.NPRUP;
48  if (!in.good())
49  throw cms::Exception("InvalidFormat")
50  << "Les Houches file contained invalid"
51  " header in init section." << std::endl;
52 
53  heprup.resize();
54 
55  for(int i = 0; i < heprup.NPRUP; i++) {
56  in >> heprup.XSECUP[i] >> heprup.XERRUP[i]
57  >> heprup.XMAXUP[i] >> heprup.LPRUP[i];
58  if (!in.good())
59  throw cms::Exception("InvalidFormat")
60  << "Les Houches file contained invalid data"
61  " in header payload line " << (i + 1)
62  << "." << std::endl;
63  }
64 
65  while(skipWhitespace(in) == '#') {
67  std::getline(in, line);
68  comments.push_back(line + "\n");
69  }
70 
71  if (!in.eof())
72  edm::LogInfo("Generator|LHEInterface")
73  << "Les Houches file contained spurious"
74  " content after the regular data (this is normal for LHEv3 files for now)." << std::endl;
75 
76  init();
77 }
78 
80  heprup(heprup)
81 {
82  init();
83 }
84 
86  const std::vector<LHERunInfoProduct::Header> &headers,
87  const std::vector<std::string> &comments) :
88  heprup(heprup)
89 {
90  std::copy(headers.begin(), headers.end(),
91  std::back_inserter(this->headers));
92  std::copy(comments.begin(), comments.end(),
93  std::back_inserter(this->comments));
94 
95  init();
96 }
97 
99  heprup(product.heprup())
100 {
101  std::copy(product.headers_begin(), product.headers_end(),
102  std::back_inserter(headers));
103  std::copy(product.comments_begin(), product.comments_end(),
104  std::back_inserter(comments));
105 
106  init();
107 }
108 
110 {
111 }
112 
114 {
115  for(int i = 0; i < heprup.NPRUP; i++) {
116  Process proc;
117  proc.setProcess(heprup.LPRUP[i]);
118  proc.setHepRupIndex((unsigned int)i);
119  processes.push_back(proc);
120  }
121 
122  std::sort(processes.begin(), processes.end());
123 }
124 
126 {
127  processesLumi.clear();
128  for(int i = 0; i < heprup.NPRUP; i++) {
129 
130  Process proc;
131  proc.setProcess(heprup.LPRUP[i]);
132  proc.setHepRupIndex((unsigned int)i);
133  proc.setLHEXSec(heprup.XSECUP[i],heprup.XERRUP[i]);
134  processesLumi.push_back(proc);
135 
136  }
137 
138  std::sort(processesLumi.begin(), processesLumi.end());
139 }
140 
141 
142 bool LHERunInfo::operator == (const LHERunInfo &other) const
143 {
144  return heprup == other.heprup;
145 }
146 
147 void LHERunInfo::count(int process, CountMode mode, double eventWeight,
148  double brWeight, double matchWeight)
149 {
150  std::vector<Process>::iterator proc =
151  std::lower_bound(processes.begin(), processes.end(), process);
152  if (proc == processes.end() || proc->process() != process)
153  return;
154 
155  std::vector<Process>::iterator procLumi =
156  std::lower_bound(processesLumi.begin(), processesLumi.end(), process);
157  if (procLumi == processesLumi.end() || procLumi->process() != process)
158  return;
159 
160  switch(mode) {
161  case kAccepted:
162  proc->addAcceptedBr(eventWeight * brWeight * matchWeight);
163  proc->addAccepted(eventWeight * matchWeight);
164  procLumi->addAcceptedBr(eventWeight * brWeight * matchWeight);
165  procLumi->addAccepted(eventWeight * matchWeight);
166  case kKilled:
167  proc->addKilled(eventWeight * matchWeight);
168  procLumi->addKilled(eventWeight * matchWeight);
169  if(eventWeight>0)
170  {
171  proc->addNPassPos();
172  procLumi->addNPassPos();
173  }
174  else
175  {
176  proc->addNPassNeg();
177  procLumi->addNPassNeg();
178  }
179  case kSelected:
180  proc->addSelected(eventWeight);
181  procLumi->addSelected(eventWeight);
182  if(eventWeight>0)
183  {
184  proc->addNTotalPos();
185  procLumi->addNTotalPos();
186  }
187  else
188  {
189  proc->addNTotalNeg();
190  procLumi->addNTotalNeg();
191  }
192  case kTried:
193  proc->addTried(eventWeight);
194  procLumi->addTried(eventWeight);
195  }
196 }
197 
198 
200 {
201  double sigSelSum = 0.0;
202  double sigSum = 0.0;
203  double sigBrSum = 0.0;
204  double err2Sum = 0.0;
205  double errBr2Sum = 0.0;
206  int idwtup = std::abs(heprup.IDWTUP);
207  for(std::vector<Process>::const_iterator proc = processes.begin();
208  proc != processes.end(); ++proc) {
209  unsigned int idx = proc->heprupIndex();
210 
211  if (!proc->killed().n())
212  continue;
213 
214  double sigma2Sum, sigma2Err;
215  sigma2Sum = heprup.XSECUP[idx]* heprup.XSECUP[idx];
216  sigma2Err = heprup.XERRUP[idx]* heprup.XERRUP[idx];
217 
218  double sigmaAvg = heprup.XSECUP[idx];
219 
220  double fracAcc = 0;
221  double ntotal = proc->nTotalPos()-proc->nTotalNeg();
222  double npass = proc->nPassPos() -proc->nPassNeg();
223  switch(idwtup){
224  case 3: case -3:
225  fracAcc = ntotal > 1e-6? npass/ntotal: -1;
226  break;
227  default:
228  fracAcc = proc->selected().sum() > 1e-6? proc->killed().sum() / proc->selected().sum():-1;
229  break;
230  }
231 
232  if(fracAcc<1e-6)continue;
233 
234  double fracBr = proc->accepted().sum() > 0.0 ?
235  proc->acceptedBr().sum() / proc->accepted().sum() : 1;
236  double sigmaFin = sigmaAvg * fracAcc * fracBr;
237  double sigmaFinBr = sigmaFin * fracBr;
238 
239  double relErr = 1.0;
240  if (proc->killed().n() > 1) {
241 
242  double efferr2=0;
243  switch(idwtup) {
244  case 3: case -3:
245  {
246  double ntotal_pos = proc->nTotalPos();
247  double effp = ntotal_pos > 1e-6?
248  (double)proc->nPassPos()/ntotal_pos:0;
249  double effp_err2 = ntotal_pos > 1e-6?
250  (1-effp)*effp/ntotal_pos: 0;
251 
252  double ntotal_neg = proc->nTotalNeg();
253  double effn = ntotal_neg > 1e-6?
254  (double)proc->nPassNeg()/ntotal_neg:0;
255  double effn_err2 = ntotal_neg > 1e-6?
256  (1-effn)*effn/ntotal_neg: 0;
257 
258  efferr2 = ntotal > 0 ?
259  (ntotal_pos*ntotal_pos*effp_err2 +
260  ntotal_neg*ntotal_neg*effn_err2)/ntotal/ntotal:0;
261  break;
262  }
263  default:
264  {
265  double denominator = pow(proc->selected().sum(),4);
266  double passw = proc->killed().sum();
267  double passw2 = proc->killed().sum2();
268  double failw = proc->selected().sum() - passw;
269  double failw2 = proc->selected().sum2() - passw2;
270  double numerator = (passw2*failw*failw + failw2*passw*passw);
271 
272  efferr2 = denominator>1e-6?
273  numerator/denominator:0;
274  break;
275  }
276  }
277  double delta2Veto = efferr2/fracAcc/fracAcc;
278  double delta2Sum = delta2Veto
279  + sigma2Err / sigma2Sum;
280  relErr = (delta2Sum > 0.0 ?
281  std::sqrt(delta2Sum) : 0.0);
282  }
283  double deltaFin = sigmaFin * relErr;
284  double deltaFinBr = sigmaFinBr * relErr;
285 
286  sigSelSum += sigmaAvg;
287  sigSum += sigmaFin;
288  sigBrSum += sigmaFinBr;
289  err2Sum += deltaFin * deltaFin;
290  errBr2Sum += deltaFinBr * deltaFinBr;
291  }
292 
293  XSec result(sigBrSum,std::sqrt(errBr2Sum));
294 
295  return result;
296 }
297 
299 {
300  double sigSelSum = 0.0;
301  double sigSum = 0.0;
302  double sigBrSum = 0.0;
303  double err2Sum = 0.0;
304  double errBr2Sum = 0.0;
305  unsigned long nAccepted = 0;
306  unsigned long nTried = 0;
307  int idwtup = std::abs(heprup.IDWTUP);
308 
309  std::cout << std::endl;
310  std::cout << "Process and cross-section statistics" << std::endl;
311  std::cout << "------------------------------------" << std::endl;
312  std::cout << "Process\tevents\ttried\txsec [pb]\t\taccepted [%]"
313  << std::endl;
314 
315  for(std::vector<Process>::const_iterator proc = processes.begin();
316  proc != processes.end(); ++proc) {
317  unsigned int idx = proc->heprupIndex();
318 
319  if (!proc->selected().n()) {
320  std::cout << proc->process() << "\t0\t0\tn/a\t\t\tn/a"
321  << std::endl;
322  continue;
323  }
324 
325  double sigma2Sum, sigma2Err;
326  sigma2Sum = heprup.XSECUP[idx]* heprup.XSECUP[idx];
327  sigma2Err = heprup.XERRUP[idx]* heprup.XERRUP[idx];
328 
329  double sigmaAvg = heprup.XSECUP[idx];
330 
331  double fracAcc = 0;
332  double ntotal = proc->nTotalPos()-proc->nTotalNeg();
333  double npass = proc->nPassPos() -proc->nPassNeg();
334  switch(idwtup){
335  case 3: case -3:
336  fracAcc = ntotal > 1e-6? npass/ntotal: -1;
337  break;
338  default:
339  fracAcc = proc->selected().sum() > 1e-6? proc->killed().sum() / proc->selected().sum():-1;
340  break;
341  }
342 
343  if(fracAcc<1e-6)continue;
344 
345  double fracBr = proc->accepted().sum() > 0.0 ?
346  proc->acceptedBr().sum() / proc->accepted().sum() : 1;
347  double sigmaFin = sigmaAvg * fracAcc;
348  double sigmaFinBr = sigmaFin * fracBr;
349 
350  double relErr = 1.0;
351  if (proc->killed().n() > 1) {
352  double efferr2=0;
353  switch(idwtup) {
354  case 3: case -3:
355  {
356  double ntotal_pos = proc->nTotalPos();
357  double effp = ntotal_pos > 1e-6?
358  (double)proc->nPassPos()/ntotal_pos:0;
359  double effp_err2 = ntotal_pos > 1e-6?
360  (1-effp)*effp/ntotal_pos: 0;
361 
362  double ntotal_neg = proc->nTotalNeg();
363  double effn = ntotal_neg > 1e-6?
364  (double)proc->nPassNeg()/ntotal_neg:0;
365  double effn_err2 = ntotal_neg > 1e-6?
366  (1-effn)*effn/ntotal_neg: 0;
367 
368  efferr2 = ntotal > 0 ?
369  (ntotal_pos*ntotal_pos*effp_err2 +
370  ntotal_neg*ntotal_neg*effn_err2)/ntotal/ntotal:0;
371  break;
372  }
373  default:
374  {
375  double denominator = pow(proc->selected().sum(),4);
376  double passw = proc->killed().sum();
377  double passw2 = proc->killed().sum2();
378  double failw = proc->selected().sum() - passw;
379  double failw2 = proc->selected().sum2() - passw2;
380  double numerator = (passw2*failw*failw + failw2*passw*passw);
381 
382  efferr2 = denominator>1e-6?
383  numerator/denominator:0;
384  break;
385  }
386  }
387  double delta2Veto = efferr2/fracAcc/fracAcc;
388  double delta2Sum = delta2Veto
389  + sigma2Err / sigma2Sum;
390  relErr = (delta2Sum > 0.0 ?
391  std::sqrt(delta2Sum) : 0.0);
392  }
393  double deltaFin = sigmaFin * relErr;
394  double deltaFinBr = sigmaFinBr * relErr;
395 
396  std::cout << proc->process() << "\t"
397  << proc->accepted().n() << "\t"
398  << proc->tried().n() << "\t"
399  << std::scientific << std::setprecision(3)
400  << sigmaFinBr << " +/- "
401  << deltaFinBr << "\t"
402  << std::fixed << std::setprecision(1)
403  << (fracAcc * 100) << std::endl;
404 
405  nAccepted += proc->accepted().n();
406  nTried += proc->tried().n();
407  sigSelSum += sigmaAvg;
408  sigSum += sigmaFin;
409  sigBrSum += sigmaFinBr;
410  err2Sum += deltaFin * deltaFin;
411  errBr2Sum += deltaFinBr * deltaFinBr;
412  }
413 
414  std::cout << "Total\t"
415  << nAccepted << "\t"
416  << nTried << "\t"
417  << std::scientific << std::setprecision(3)
418  << sigBrSum << " +/- "
419  << std::sqrt(errBr2Sum) << "\t"
420  << std::fixed << std::setprecision(1)
421  << (sigSum / sigSelSum * 100) << std::endl;
422 }
423 
425  xmlDoc(0)
426 {
427 }
428 
430  LHERunInfoProduct::Header(tag), xmlDoc(0)
431 {
432 }
433 
435  LHERunInfoProduct::Header(orig), xmlDoc(0)
436 {
437 }
438 
440  LHERunInfoProduct::Header(orig), xmlDoc(0)
441 {
442 }
443 
445 {
446  if (xmlDoc)
447  xmlDoc->release();
448 }
449 
450 static void fillLines(std::vector<std::string> &lines, const char *data,
451  int len = -1)
452 {
453  const char *end = len >= 0 ? (data + len) : 0;
454  while(*data && (!end || data < end)) {
455  std::size_t len = std::strcspn(data, "\r\n");
456  if (end && data + len > end)
457  len = end - data;
458  if (data[len] == '\r' && data[len + 1] == '\n')
459  len += 2;
460  else if (data[len])
461  len++;
462  lines.push_back(std::string(data, len));
463  data += len;
464  }
465 }
466 
467 static std::vector<std::string> domToLines(const DOMNode *node)
468 {
469  std::vector<std::string> result;
470  DOMImplementation *impl =
471  DOMImplementationRegistry::getDOMImplementation(
472  XMLUniStr("Core"));
473  std::auto_ptr<DOMWriter> writer(
474  static_cast<DOMImplementationLS*>(impl)->createDOMWriter());
475 
476  writer->setEncoding(XMLUniStr("UTF-8"));
477  XMLSimpleStr buffer(writer->writeToString(*node));
478 
479  const char *p = std::strchr((const char*)buffer, '>') + 1;
480  const char *q = std::strrchr(p, '<');
481  fillLines(result, p, q - p);
482 
483  return result;
484 }
485 
486 std::vector<std::string> LHERunInfo::findHeader(const std::string &tag) const
487 {
488  const LHERunInfo::Header *header = 0;
489  for(std::vector<Header>::const_iterator iter = headers.begin();
490  iter != headers.end(); ++iter) {
491  if (iter->tag() == tag)
492  return std::vector<std::string>(iter->begin(),
493  iter->end());
494  if (iter->tag() == "header")
495  header = &*iter;
496  }
497 
498  if (!header)
499  return std::vector<std::string>();
500 
501  const DOMNode *root = header->getXMLNode();
502  if (!root)
503  return std::vector<std::string>();
504 
505  for(const DOMNode *iter = root->getFirstChild();
506  iter; iter = iter->getNextSibling()) {
507  if (iter->getNodeType() != DOMNode::ELEMENT_NODE)
508  continue;
509  if (tag == (const char*)XMLSimpleStr(iter->getNodeName()))
510  return domToLines(iter);
511  }
512 
513  return std::vector<std::string>();
514 }
515 
516 namespace {
517  class HeaderReader : public CBInputStream::Reader {
518  public:
519  HeaderReader(const LHERunInfo::Header *header) :
520  header(header), mode(kHeader),
521  iter(header->begin())
522  {
523  }
524 
525  const std::string &data() override
526  {
527  switch(mode) {
528  case kHeader:
529  tmp = "<" + header->tag() + ">";
530  mode = kBody;
531  break;
532  case kBody:
533  if (iter != header->end())
534  return *iter++;
535  tmp = "</" + header->tag() + ">";
536  mode = kFooter;
537  break;
538  case kFooter:
539  tmp.clear();
540  }
541 
542  return tmp;
543  }
544 
545  private:
546  enum Mode {
547  kHeader,
548  kBody,
549  kFooter
550  };
551 
552  const LHERunInfo::Header *header;
553  Mode mode;
556  };
557 } // anonymous namespace
558 
559 const DOMNode *LHERunInfo::Header::getXMLNode() const
560 {
561  if (tag().empty())
562  return 0;
563 
564  if (!xmlDoc) {
565  XercesDOMParser parser;
566  parser.setValidationScheme(XercesDOMParser::Val_Auto);
567  parser.setDoNamespaces(false);
568  parser.setDoSchema(false);
569  parser.setValidationSchemaFullChecking(false);
570 
571  HandlerBase errHandler;
572  parser.setErrorHandler(&errHandler);
573  parser.setCreateEntityReferenceNodes(false);
574 
575  try {
576  std::auto_ptr<CBInputStream::Reader> reader(
577  new HeaderReader(this));
579  parser.parse(source);
580  xmlDoc = parser.adoptDocument();
581  } catch(const XMLException &e) {
582  throw cms::Exception("Generator|LHEInterface")
583  << "XML parser reported DOM error no. "
584  << (unsigned long)e.getCode()
585  << ": " << XMLSimpleStr(e.getMessage()) << "."
586  << std::endl;
587  } catch(const SAXException &e) {
588  throw cms::Exception("Generator|LHEInterface")
589  << "XML parser reported: "
590  << XMLSimpleStr(e.getMessage()) << "."
591  << std::endl;
592  }
593  }
594 
595  return xmlDoc->getDocumentElement();
596 }
597 
598 std::pair<int, int> LHERunInfo::pdfSetTranslation() const
599 {
600  int pdfA = -1, pdfB = -1;
601 
602  if (heprup.PDFGUP.first >= 0) {
603  pdfA = heprup.PDFSUP.first;
604  }
605 
606  if (heprup.PDFGUP.second >= 0) {
607  pdfB = heprup.PDFSUP.second;
608  }
609 
610  return std::make_pair(pdfA, pdfB);
611 }
612 
613 const bool operator == (const LHERunInfo::Process& lhs, const LHERunInfo::Process &rhs)
614 { return (lhs.process() == rhs.process()); }
615 
616 const bool operator < (const LHERunInfo::Process& lhs, const LHERunInfo::Process &rhs)
617 { return (lhs.process() < rhs.process()); }
618 
619 } // namespace lhef
static XERCES_CPP_NAMESPACE_USE int skipWhitespace(std::istream &in)
Definition: LHERunInfo.cc:28
int i
Definition: DBlmapReader.cc:9
void resize(int nrup)
Definition: LesHouches.h:52
const bool operator<(const LHERunInfo::Process &lhs, const LHERunInfo::Process &rhs)
Definition: LHERunInfo.cc:616
list numerator
Definition: cuy.py:483
void setLHEXSec(double value, double error)
Definition: LHERunInfo.h:134
TrainProcessor *const proc
Definition: MVATrainer.cc:101
XMLInputSourceWrapper< CBInputStream > CBInputSource
Definition: XMLUtils.h:188
std::pair< double, double > EBMUP
Definition: LesHouches.h:78
comments_const_iterator comments_end() const
static std::vector< std::string > domToLines(const DOMNode *node)
Definition: LHERunInfo.cc:467
XSec xsec() const
Definition: LHERunInfo.cc:199
headers_const_iterator headers_end() const
std::pair< int, int > IDBMUP
Definition: LesHouches.h:73
std::vector< std::string >::const_iterator const_iterator
std::vector< Process > processes
Definition: LHERunInfo.h:166
tuple node
Definition: Node.py:50
std::pair< int, int > PDFGUP
Definition: LesHouches.h:84
list denominator
Definition: cuy.py:484
headers_const_iterator headers_begin() const
T sqrt(T t)
Definition: SSEVec.h:48
tuple result
Definition: query.py:137
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define end
Definition: vmac.h:37
LHERunInfo(std::istream &in)
Definition: LHERunInfo.cc:41
std::vector< double > XERRUP
Definition: LesHouches.h:114
std::vector< double > XMAXUP
Definition: LesHouches.h:119
void setHepRupIndex(int id)
Definition: LHERunInfo.h:133
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
comments_const_iterator comments_begin() const
std::vector< Process > processesLumi
Definition: LHERunInfo.h:175
std::pair< int, int > pdfSetTranslation() const
Definition: LHERunInfo.cc:598
std::pair< int, int > PDFSUP
Definition: LesHouches.h:90
std::vector< std::string > comments
Definition: LHERunInfo.h:168
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
#define begin
Definition: vmac.h:30
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::vector< Header > headers
Definition: LHERunInfo.h:167
void count(int process, CountMode count, double eventWeight=1.0, double brWeight=1.0, double matchWeight=1.0)
Definition: LHERunInfo.cc:147
tuple cout
Definition: gather_cfg.py:121
std::vector< std::string > findHeader(const std::string &tag) const
Definition: LHERunInfo.cc:486
bool operator==(const LHERunInfo &other) const
Definition: LHERunInfo.cc:142
std::vector< double > XSECUP
Definition: LesHouches.h:108
tuple process
Definition: LaserDQM_cfg.py:3
static void fillLines(std::vector< std::string > &lines, const char *data, int len=-1)
Definition: LHERunInfo.cc:450
static std::string const source
Definition: EdmProvDump.cc:43
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
void statistics() const
Definition: LHERunInfo.cc:298
std::vector< int > LPRUP
Definition: LesHouches.h:124
string root
initialization
Definition: dbtoconf.py:70