CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
LHERunInfoProduct.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iterator>
3 #include <iostream>
4 #include <iomanip>
5 #include <sstream>
6 #include <string>
7 #include <cmath>
8 #include <map>
9 #include <set>
10 
12 
15 
17  (const const_iterator &other) const
18 {
19  if (mode != other.mode)
20  return false;
21 
22  switch(mode) {
23  case kFooter:
24  case kDone:
25  return true;
26 
27  case kHeader:
28  return header == other.header;
29 
30  case kBody:
31  return header == other.header && iter == other.iter;
32 
33  case kInit:
34  return line == other.line;
35  }
36 
37  return false;
38 }
39 
41 {
42  tmp.clear();
43 
44  do {
45  switch(mode) {
46  case kHeader:
47  if (header == runInfo->headers_end()) {
48  if (line++ == 1)
49  tmp = "</header>\n";
50  else {
51  mode = kInit;
52  tmp = "<init>\n";
53  line = 0;
54  }
55  break;
56  } else if (!line) {
57  line++;
58  tmp = "<header>\n";
59  break;
60  } else {
61  mode = kBody;
62  const std::string &tag = header->tag();
63  tmp = tag.empty() ? "<!--" :
64  (tag == "<>") ? "" : ("<" + tag + ">");
65  iter = header->begin();
66  continue;
67  }
68 
69  case kBody:
70  if (iter == header->end()) {
71  mode = kHeader;
72  const std::string &tag = header->tag();
73  tmp += tag.empty() ? "-->" :
74  (tag == "<>") ? "" : ("</" + tag + ">");
75  tmp += "\n";
76  header++;
77  } else {
78  tmp += *iter++;
79  if (iter == header->end() &&
80  (tmp.empty() ||
81  (tmp[tmp.length() - 1] != '\r' &&
82  tmp[tmp.length() - 1] != '\n')))
83  continue;
84  }
85  break;
86 
87  case kInit: {
88  const lhef::HEPRUP &heprup = runInfo->heprup();
89  if (!line++) {
90  std::ostringstream ss;
91  ss << std::setprecision(7)
92  << std::scientific
93  << std::uppercase
94  << " " << heprup.IDBMUP.first
95  << " " << heprup.IDBMUP.second
96  << " " << heprup.EBMUP.first
97  << " " << heprup.EBMUP.second
98  << " " << heprup.PDFGUP.first
99  << " " << heprup.PDFGUP.second
100  << " " << heprup.PDFSUP.first
101  << " " << heprup.PDFSUP.second
102  << " " << heprup.IDWTUP
103  << " " << heprup.NPRUP << std::endl;
104  tmp = ss.str();
105  break;
106  }
107  if (line >= (unsigned int)heprup.NPRUP +
108  runInfo->comments_size() + 2) {
109  tmp = "</init>\n";
110  mode = kFooter;
111  break;
112  } else if (line >= (unsigned int)heprup.NPRUP + 2) {
113  tmp = *(runInfo->comments_begin() + (line -
114  (unsigned int)heprup.NPRUP - 2));
115  break;
116  }
117 
118  std::ostringstream ss;
119  ss << std::setprecision(7)
120  << std::scientific
121  << std::uppercase
122  << "\t" << heprup.XSECUP[line - 2]
123  << "\t" << heprup.XERRUP[line - 2]
124  << "\t" << heprup.XMAXUP[line - 2]
125  << "\t" << heprup.LPRUP[line - 2] << std::endl;
126  tmp = ss.str();
127  } break;
128 
129  case kFooter:
130  mode = kDone;
131 
132  default:
133  /* ... */;
134  }
135  } while(false);
136 }
137 
139 {
141 
142  result.runInfo = this;
143  result.header = headers_begin();
144  result.mode = const_iterator::kHeader;
145  result.line = 0;
146  result.tmp = "<LesHouchesEvents version=\"1.0\">\n";
147 
148  return result;
149 }
150 
152 {
154 
155  result.runInfo = this;
156  result.mode = const_iterator::kInit;
157  result.line = 0;
158  result.tmp = "<init>\n";
159 
160  return result;
161 }
162 
163 const std::string &LHERunInfoProduct::endOfFile()
164 {
165  static const std::string theEnd("</LesHouchesEvents>\n");
166 
167  return theEnd;
168 }
169 
170 namespace {
171  struct XSec {
172  inline XSec() : xsec(0.0), err(0.0), max(0.0) {}
173 
174  double xsec;
175  double err;
176  double max;
177  };
178 
179  struct HeaderLess {
180  bool operator() (const LHERunInfoProduct::Header &a,
181  const LHERunInfoProduct::Header &b) const;
182  };
183 }
184 
185 bool HeaderLess::operator() (const LHERunInfoProduct::Header &a,
186  const LHERunInfoProduct::Header &b) const
187 {
188  if (a == b)
189  return false;
190  if (a.tag() < b.tag())
191  return true;
192  if (a.tag() > b.tag())
193  return false;
194 
197 
198  for(; iter1 != a.end() && iter2 != b.end(); ++iter1, ++iter2) {
199  if (*iter1 < *iter2)
200  return true;
201  else if (*iter1 != *iter2)
202  return false;
203  }
204 
205  return iter2 != b.end();
206 }
207 
209 {
210  if (heprup_.IDBMUP != other.heprup_.IDBMUP ||
211  heprup_.EBMUP != other.heprup_.EBMUP ||
212  heprup_.PDFGUP != other.heprup_.PDFGUP ||
213  heprup_.PDFSUP != other.heprup_.PDFSUP ||
214  heprup_.IDWTUP != other.heprup_.IDWTUP) {
215  throw cms::Exception("ProductsNotMergeable")
216  << "Error in LHERunInfoProduct: LHE headers differ. "
217  "Cannot merge products." << std::endl;
218  }
219 
220  bool compatibleHeaders = headers_ == other.headers_;
221 
222  // try to merge different, but compatible headers
223  while(!compatibleHeaders) {
224  // okay, something is different.
225  // Let's try to merge, but don't duplicate identical headers
226  // and test the rest against a whitelist
227 
228  std::set<Header, HeaderLess> headers;
230  std::inserter(headers, headers.begin()));
231 
232  bool failed = false;
233  for(std::vector<LHERunInfoProduct::Header>::const_iterator
234  header = other.headers_begin();
235  header != other.headers_end(); ++header) {
236  if (headers.count(*header))
237  continue;
238 
239  if (header->tag() == "" ||
240  header->tag().find("Alpgen") == 0 ||
241  header->tag() == "MGGridCard" ||
242  header->tag() == "MGGenerationInfo") {
243  addHeader(*header);
244  headers.insert(*header);
245  } else
246  failed = true;
247  }
248  if (failed)
249  break;
250 
251  compatibleHeaders = true;
252  }
253 
254  // still not compatible after fixups
255  if (!compatibleHeaders) {
256  throw cms::Exception("ProductsNotMergeable")
257  << "Error in LHERunInfoProduct: LHE headers differ. "
258  "Cannot merge products." << std::endl;
259  }
260 
261  // it is exactly the same, so merge
262  if (heprup_ == other.heprup_)
263  return true;
264 
265  // the input files are different ones, presumably generation
266  // of the same process in different runs with identical run number
267  // attempt merge of processes and cross-sections
268 
269  std::map<int, XSec> processes;
270 
271  for(int i = 0; i < heprup_.NPRUP; i++) {
272  int id = heprup_.LPRUP[i];
273  XSec &x = processes[id];
274  x.xsec = heprup_.XSECUP[i];
275  x.err = heprup_.XERRUP[i];
276  x.max = heprup_.XMAXUP[i];
277  }
278 
279  for(int i = 0; i < other.heprup_.NPRUP; i++) {
280  int id = other.heprup_.LPRUP[i];
281  XSec &x = processes[id];
282  if (x.xsec) {
283  double wgt1 = 1.0 / (x.err * x.err);
284  double wgt2 = 1.0 / (other.heprup_.XERRUP[i] *
285  other.heprup_.XERRUP[i]);
286  x.xsec = (wgt1 * x.xsec +
287  wgt2 * other.heprup_.XSECUP[i]) /
288  (wgt1 + wgt2);
289  x.err = 1.0 / std::sqrt(wgt1 + wgt2);
290  x.max = std::max(x.max, other.heprup_.XMAXUP[i]);
291  } else {
292  x.xsec = other.heprup_.XSECUP[i];
293  x.err = other.heprup_.XERRUP[i];
294  x.max = other.heprup_.XMAXUP[i];
295  }
296  }
297 
298  heprup_.resize(processes.size());
299  unsigned int i = 0;
300  for(std::map<int, XSec>::const_iterator iter = processes.begin();
301  iter != processes.end(); ++iter, i++) {
302  heprup_.LPRUP[i] = iter->first;
303  heprup_.XSECUP[i] = iter->second.xsec;
304  heprup_.XERRUP[i] = iter->second.err;
305  heprup_.XMAXUP[i] = iter->second.max;
306  }
307 
308  return true;
309 }
int i
Definition: DBlmapReader.cc:9
void resize(int nrup)
Definition: LesHouches.h:52
void addHeader(const Header &header)
std::pair< double, double > EBMUP
Definition: LesHouches.h:78
const LHERunInfoProduct * runInfo
headers_const_iterator headers_end() const
std::pair< int, int > IDBMUP
Definition: LesHouches.h:73
std::vector< std::string >::const_iterator const_iterator
const_iterator end() const
const lhef::HEPRUP & heprup() const
std::pair< int, int > PDFGUP
Definition: LesHouches.h:84
const T & max(const T &a, const T &b)
headers_const_iterator headers_begin() const
T sqrt(T t)
Definition: SSEVec.h:46
tuple result
Definition: query.py:137
const std::string & tag() const
std::vector< double > XERRUP
Definition: LesHouches.h:114
std::vector< double > XMAXUP
Definition: LesHouches.h:119
size_type comments_size() const
comments_const_iterator comments_begin() const
double b
Definition: hdecay.h:120
std::pair< int, int > PDFSUP
Definition: LesHouches.h:90
bool mergeProduct(const LHERunInfoProduct &other)
double a
Definition: hdecay.h:121
std::vector< Header > headers_
static const std::string & endOfFile()
Definition: DDAxes.h:10
std::vector< double > XSECUP
Definition: LesHouches.h:108
const_iterator init() const
const_iterator begin() const
std::vector< int > LPRUP
Definition: LesHouches.h:124
const_iterator begin() const