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::LogWarning("Generator|LHEInterface")
73  << "Les Houches file contained spurious"
74  " content after the regular data." << 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 
118  proc.process = heprup.LPRUP[i];
119  proc.heprupIndex = (unsigned int)i;
120 
121  processes.push_back(proc);
122  }
123 
124  std::sort(processes.begin(), processes.end());
125 }
126 
127 bool LHERunInfo::operator == (const LHERunInfo &other) const
128 {
129  return heprup == other.heprup;
130 }
131 
132 void LHERunInfo::count(int process, CountMode mode, double eventWeight,
133  double brWeight, double matchWeight)
134 {
135  std::vector<Process>::iterator proc =
136  std::lower_bound(processes.begin(), processes.end(), process);
137  if (proc == processes.end() || proc->process != process)
138  return;
139 
140  switch(mode) {
141  case kAccepted:
142  proc->acceptedBr.add(eventWeight * brWeight * matchWeight);
143  proc->accepted.add(eventWeight * matchWeight);
144  case kKilled:
145  proc->killed.add(eventWeight * matchWeight);
146  case kSelected:
147  proc->selected.add(eventWeight);
148  case kTried:
149  proc->tried.add(eventWeight);
150  }
151 }
152 
154 {
155  double sigSelSum = 0.0;
156  double sigSum = 0.0;
157  double sigBrSum = 0.0;
158  double err2Sum = 0.0;
159  double errBr2Sum = 0.0;
160 
161  for(std::vector<Process>::const_iterator proc = processes.begin();
162  proc != processes.end(); ++proc) {
163  unsigned int idx = proc->heprupIndex;
164 
165  double sigmaSum, sigma2Sum, sigma2Err, xsec;
166  switch(std::abs(heprup.IDWTUP)) {
167  case 2:
168  sigmaSum = proc->tried.sum * heprup.XSECUP[idx];
169  sigma2Sum = proc->tried.sum2 * heprup.XSECUP[idx]
170  * heprup.XSECUP[idx];
171  sigma2Err = proc->tried.sum2 * heprup.XERRUP[idx]
172  * heprup.XERRUP[idx];
173  break;
174  case 3:
175  sigmaSum = proc->tried.n * heprup.XSECUP[idx];
176  sigma2Sum = sigmaSum * heprup.XSECUP[idx];
177  sigma2Err = proc->tried.n * heprup.XERRUP[idx]
178  * heprup.XERRUP[idx];
179  break;
180  default:
181  xsec = proc->tried.sum / proc->tried.n;
182  sigmaSum = proc->tried.sum * xsec;
183  sigma2Sum = proc->tried.sum2 * xsec * xsec;
184  sigma2Err = 0.0;
185  }
186 
187  if (!proc->killed.n)
188  continue;
189 
190  double sigmaAvg = sigmaSum / proc->tried.sum;
191  double fracAcc = proc->killed.sum / proc->selected.sum;
192  double fracBr = proc->accepted.sum > 0.0 ?
193  proc->acceptedBr.sum / proc->accepted.sum : 1;
194  double sigmaFin = sigmaAvg * fracAcc * fracBr;
195  double sigmaFinBr = sigmaFin * fracBr;
196 
197  double relErr = 1.0;
198  if (proc->killed.n > 1) {
199  double sigmaAvg2 = sigmaAvg * sigmaAvg;
200  double delta2Sig =
201  (sigma2Sum / proc->tried.n - sigmaAvg2) /
202  (proc->tried.n * sigmaAvg2);
203  double delta2Veto =
204  ((double)proc->selected.n - proc->killed.n) /
205  ((double)proc->selected.n * proc->killed.n);
206  double delta2Sum = delta2Sig + delta2Veto
207  + sigma2Err / sigma2Sum;
208  relErr = (delta2Sum > 0.0 ?
209  std::sqrt(delta2Sum) : 0.0);
210  }
211  double deltaFin = sigmaFin * relErr;
212  double deltaFinBr = sigmaFinBr * relErr;
213 
214  sigSelSum += sigmaAvg;
215  sigSum += sigmaFin;
216  sigBrSum += sigmaFinBr;
217  err2Sum += deltaFin * deltaFin;
218  errBr2Sum += deltaFinBr * deltaFinBr;
219  }
220 
221  XSec result;
222  result.value = sigBrSum;
223  result.error = std::sqrt(errBr2Sum);
224 
225  return result;
226 }
227 
229 {
230  double sigSelSum = 0.0;
231  double sigSum = 0.0;
232  double sigBrSum = 0.0;
233  double err2Sum = 0.0;
234  double errBr2Sum = 0.0;
235  unsigned long nAccepted = 0;
236  unsigned long nTried = 0;
237 
238  std::cout << std::endl;
239  std::cout << "Process and cross-section statistics" << std::endl;
240  std::cout << "------------------------------------" << std::endl;
241  std::cout << "Process\tevents\ttried\txsec [pb]\t\taccepted [%]"
242  << std::endl;
243 
244  for(std::vector<Process>::const_iterator proc = processes.begin();
245  proc != processes.end(); ++proc) {
246  unsigned int idx = proc->heprupIndex;
247 
248  double sigmaSum, sigma2Sum, sigma2Err, xsec;
249  switch(std::abs(heprup.IDWTUP)) {
250  case 2:
251  sigmaSum = proc->tried.sum * heprup.XSECUP[idx];
252  sigma2Sum = proc->tried.sum2 * heprup.XSECUP[idx]
253  * heprup.XSECUP[idx];
254  sigma2Err = proc->tried.sum2 * heprup.XERRUP[idx]
255  * heprup.XERRUP[idx];
256  break;
257  case 3:
258  sigmaSum = proc->tried.n * heprup.XSECUP[idx];
259  sigma2Sum = sigmaSum * heprup.XSECUP[idx];
260  sigma2Err = proc->tried.n * heprup.XERRUP[idx]
261  * heprup.XERRUP[idx];
262  break;
263  default:
264  xsec = proc->tried.sum / proc->tried.n;
265  sigmaSum = proc->tried.sum * xsec;
266  sigma2Sum = proc->tried.sum2 * xsec * xsec;
267  sigma2Err = 0.0;
268  }
269 
270  if (!proc->selected.n) {
271  std::cout << proc->process << "\t0\t0\tn/a\t\t\tn/a"
272  << std::endl;
273  continue;
274  }
275 
276  double sigmaAvg = sigmaSum / proc->tried.sum;
277  double fracAcc = proc->killed.sum / proc->selected.sum;
278  double fracBr = proc->accepted.sum > 0.0 ?
279  proc->acceptedBr.sum / proc->accepted.sum : 1;
280  double sigmaFin = sigmaAvg * fracAcc;
281  double sigmaFinBr = sigmaFin * fracBr;
282 
283  double relErr = 1.0;
284  if (proc->killed.n > 1) {
285  double sigmaAvg2 = sigmaAvg * sigmaAvg;
286  double delta2Sig =
287  (sigma2Sum / proc->tried.n - sigmaAvg2) /
288  (proc->tried.n * sigmaAvg2);
289  double delta2Veto =
290  ((double)proc->selected.n - proc->killed.n) /
291  ((double)proc->selected.n * proc->killed.n);
292  double delta2Sum = delta2Sig + delta2Veto
293  + sigma2Err / sigma2Sum;
294  relErr = (delta2Sum > 0.0 ?
295  std::sqrt(delta2Sum) : 0.0);
296  }
297  double deltaFin = sigmaFin * relErr;
298  double deltaFinBr = sigmaFinBr * relErr;
299 
300  std::cout << proc->process << "\t"
301  << proc->accepted.n << "\t"
302  << proc->tried.n << "\t"
303  << std::scientific << std::setprecision(3)
304  << sigmaFinBr << " +/- "
305  << deltaFinBr << "\t"
306  << std::fixed << std::setprecision(1)
307  << (fracAcc * 100) << std::endl;
308 
309  nAccepted += proc->accepted.n;
310  nTried += proc->tried.n;
311  sigSelSum += sigmaAvg;
312  sigSum += sigmaFin;
313  sigBrSum += sigmaFinBr;
314  err2Sum += deltaFin * deltaFin;
315  errBr2Sum += deltaFinBr * deltaFinBr;
316  }
317 
318  std::cout << "Total\t"
319  << nAccepted << "\t"
320  << nTried << "\t"
321  << std::scientific << std::setprecision(3)
322  << sigBrSum << " +/- "
323  << std::sqrt(errBr2Sum) << "\t"
324  << std::fixed << std::setprecision(1)
325  << (sigSum / sigSelSum * 100) << std::endl;
326 }
327 
329  xmlDoc(0)
330 {
331 }
332 
334  LHERunInfoProduct::Header(tag), xmlDoc(0)
335 {
336 }
337 
339  LHERunInfoProduct::Header(orig), xmlDoc(0)
340 {
341 }
342 
344  LHERunInfoProduct::Header(orig), xmlDoc(0)
345 {
346 }
347 
349 {
350  if (xmlDoc)
351  xmlDoc->release();
352 }
353 
354 static void fillLines(std::vector<std::string> &lines, const char *data,
355  int len = -1)
356 {
357  const char *end = len >= 0 ? (data + len) : 0;
358  while(*data && (!end || data < end)) {
359  std::size_t len = std::strcspn(data, "\r\n");
360  if (end && data + len > end)
361  len = end - data;
362  if (data[len] == '\r' && data[len + 1] == '\n')
363  len += 2;
364  else if (data[len])
365  len++;
366  lines.push_back(std::string(data, len));
367  data += len;
368  }
369 }
370 
371 static std::vector<std::string> domToLines(const DOMNode *node)
372 {
373  std::vector<std::string> result;
374  DOMImplementation *impl =
375  DOMImplementationRegistry::getDOMImplementation(
376  XMLUniStr("Core"));
377  std::auto_ptr<DOMWriter> writer(
378  static_cast<DOMImplementationLS*>(impl)->createDOMWriter());
379 
380  writer->setEncoding(XMLUniStr("UTF-8"));
381  XMLSimpleStr buffer(writer->writeToString(*node));
382 
383  const char *p = std::strchr((const char*)buffer, '>') + 1;
384  const char *q = std::strrchr(p, '<');
385  fillLines(result, p, q - p);
386 
387  return result;
388 }
389 
390 std::vector<std::string> LHERunInfo::findHeader(const std::string &tag) const
391 {
392  const LHERunInfo::Header *header = 0;
393  for(std::vector<Header>::const_iterator iter = headers.begin();
394  iter != headers.end(); ++iter) {
395  if (iter->tag() == tag)
396  return std::vector<std::string>(iter->begin(),
397  iter->end());
398  if (iter->tag() == "header")
399  header = &*iter;
400  }
401 
402  if (!header)
403  return std::vector<std::string>();
404 
405  const DOMNode *root = header->getXMLNode();
406  if (!root)
407  return std::vector<std::string>();
408 
409  for(const DOMNode *iter = root->getFirstChild();
410  iter; iter = iter->getNextSibling()) {
411  if (iter->getNodeType() != DOMNode::ELEMENT_NODE)
412  continue;
413  if (tag == (const char*)XMLSimpleStr(iter->getNodeName()))
414  return domToLines(iter);
415  }
416 
417  return std::vector<std::string>();
418 }
419 
420 namespace {
421  class HeaderReader : public CBInputStream::Reader {
422  public:
423  HeaderReader(const LHERunInfo::Header *header) :
424  header(header), mode(kHeader),
425  iter(header->begin())
426  {
427  }
428 
429  const std::string &data()
430  {
431  switch(mode) {
432  case kHeader:
433  tmp = "<" + header->tag() + ">";
434  mode = kBody;
435  break;
436  case kBody:
437  if (iter != header->end())
438  return *iter++;
439  tmp = "</" + header->tag() + ">";
440  mode = kFooter;
441  break;
442  case kFooter:
443  tmp.clear();
444  }
445 
446  return tmp;
447  }
448 
449  private:
450  enum Mode {
451  kHeader,
452  kBody,
453  kFooter
454  };
455 
456  const LHERunInfo::Header *header;
457  Mode mode;
460  };
461 } // anonymous namespace
462 
463 const DOMNode *LHERunInfo::Header::getXMLNode() const
464 {
465  if (tag().empty())
466  return 0;
467 
468  if (!xmlDoc) {
469  XercesDOMParser parser;
470  parser.setValidationScheme(XercesDOMParser::Val_Auto);
471  parser.setDoNamespaces(false);
472  parser.setDoSchema(false);
473  parser.setValidationSchemaFullChecking(false);
474 
475  HandlerBase errHandler;
476  parser.setErrorHandler(&errHandler);
477  parser.setCreateEntityReferenceNodes(false);
478 
479  try {
480  std::auto_ptr<CBInputStream::Reader> reader(
481  new HeaderReader(this));
483  parser.parse(source);
484  xmlDoc = parser.adoptDocument();
485  } catch(const XMLException &e) {
486  throw cms::Exception("Generator|LHEInterface")
487  << "XML parser reported DOM error no. "
488  << (unsigned long)e.getCode()
489  << ": " << XMLSimpleStr(e.getMessage()) << "."
490  << std::endl;
491  } catch(const SAXException &e) {
492  throw cms::Exception("Generator|LHEInterface")
493  << "XML parser reported: "
494  << XMLSimpleStr(e.getMessage()) << "."
495  << std::endl;
496  }
497  }
498 
499  return xmlDoc->getDocumentElement();
500 }
501 
502 std::pair<int, int> LHERunInfo::pdfSetTranslation() const
503 {
504  int pdfA = -1, pdfB = -1;
505 
506  if (heprup.PDFGUP.first >= 0) {
507  pdfA = heprup.PDFSUP.first;
508  }
509 
510  if (heprup.PDFGUP.second >= 0) {
511  pdfB = heprup.PDFSUP.second;
512  }
513 
514  return std::make_pair(pdfA, pdfB);
515 }
516 
517 } // 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
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:371
#define abs(x)
Definition: mlp_lapack.h:159
XSec xsec() const
Definition: LHERunInfo.cc:153
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:123
unsigned int heprupIndex
Definition: LHERunInfo.h:105
tuple node
Definition: Node.py:50
std::pair< int, int > PDFGUP
Definition: LesHouches.h:84
headers_const_iterator headers_begin() const
T sqrt(T t)
Definition: SSEVec.h:48
tuple result
Definition: query.py:137
#define end
Definition: vmac.h:38
LHERunInfo(std::istream &in)
Definition: LHERunInfo.cc:41
std::vector< double > XERRUP
Definition: LesHouches.h:114
std::vector< double > XMAXUP
Definition: LesHouches.h:119
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
comments_const_iterator comments_begin() const
std::pair< int, int > pdfSetTranslation() const
Definition: LHERunInfo.cc:502
std::pair< int, int > PDFSUP
Definition: LesHouches.h:90
std::vector< std::string > comments
Definition: LHERunInfo.h:125
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
#define begin
Definition: vmac.h:31
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::vector< Header > headers
Definition: LHERunInfo.h:124
void count(int process, CountMode count, double eventWeight=1.0, double brWeight=1.0, double matchWeight=1.0)
Definition: LHERunInfo.cc:132
tuple cout
Definition: gather_cfg.py:121
std::vector< std::string > findHeader(const std::string &tag) const
Definition: LHERunInfo.cc:390
bool operator==(const LHERunInfo &other) const
Definition: LHERunInfo.cc:127
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:354
void statistics() const
Definition: LHERunInfo.cc:228
std::vector< int > LPRUP
Definition: LesHouches.h:124
string root
initialization
Definition: dbtoconf.py:70