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 
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  return !(tag == "" || tag.find("Alpgen") == 0 || tag == "MGGridCard" || tag == "MGGenerationInfo");
210 }
211 
213 {
214  if (heprup_.IDBMUP != other.heprup_.IDBMUP ||
215  heprup_.EBMUP != other.heprup_.EBMUP ||
216  heprup_.PDFGUP != other.heprup_.PDFGUP ||
217  heprup_.PDFSUP != other.heprup_.PDFSUP ||
218  heprup_.IDWTUP != other.heprup_.IDWTUP) {
219  throw cms::Exception("ProductsNotMergeable")
220  << "Error in LHERunInfoProduct: LHE headers differ. "
221  "Cannot merge products." << std::endl;
222  }
223 
224  bool compatibleHeaders = headers_ == other.headers_;
225 
226  // try to merge different, but compatible headers
227  while(!compatibleHeaders) {
228  // okay, something is different.
229  // Let's try to merge, but don't duplicate identical headers
230  // and test the rest against a whitelist
231 
232  std::set<Header, HeaderLess> headers;
234  std::inserter(headers, headers.begin()));
235 
236  bool failed = false;
237  for(std::vector<LHERunInfoProduct::Header>::const_iterator
238  header = other.headers_begin();
239  header != other.headers_end(); ++header) {
240  if (headers.count(*header)) {
241  continue;
242  }
243 
244  if(isTagComparedInMerge(header->tag())) {
245  failed = true;
246  } else {
247  addHeader(*header);
248  headers.insert(*header);
249  }
250  }
251  if (failed) {
252  break;
253  }
254 
255  compatibleHeaders = true;
256  }
257 
258  // still not compatible after fixups
259  if (!compatibleHeaders) {
260  throw cms::Exception("ProductsNotMergeable")
261  << "Error in LHERunInfoProduct: LHE headers differ. "
262  "Cannot merge products." << std::endl;
263  }
264 
265  // it is exactly the same, so merge
266  if (heprup_ == other.heprup_)
267  return true;
268 
269  // the input files are different ones, presumably generation
270  // of the same process in different runs with identical run number
271  // attempt merge of processes and cross-sections
272 
273  std::map<int, XSec> processes;
274 
275  for(int i = 0; i < heprup_.NPRUP; i++) {
276  int id = heprup_.LPRUP[i];
277  XSec &x = processes[id];
278  x.xsec = heprup_.XSECUP[i];
279  x.err = heprup_.XERRUP[i];
280  x.max = heprup_.XMAXUP[i];
281  }
282 
283  for(int i = 0; i < other.heprup_.NPRUP; i++) {
284  int id = other.heprup_.LPRUP[i];
285  XSec &x = processes[id];
286  if (x.xsec) {
287  double wgt1 = 1.0 / (x.err * x.err);
288  double wgt2 = 1.0 / (other.heprup_.XERRUP[i] *
289  other.heprup_.XERRUP[i]);
290  x.xsec = (wgt1 * x.xsec +
291  wgt2 * other.heprup_.XSECUP[i]) /
292  (wgt1 + wgt2);
293  x.err = 1.0 / std::sqrt(wgt1 + wgt2);
294  x.max = std::max(x.max, other.heprup_.XMAXUP[i]);
295  } else {
296  x.xsec = other.heprup_.XSECUP[i];
297  x.err = other.heprup_.XERRUP[i];
298  x.max = other.heprup_.XMAXUP[i];
299  }
300  }
301 
302  heprup_.resize(processes.size());
303  unsigned int i = 0;
304  for(std::map<int, XSec>::const_iterator iter = processes.begin();
305  iter != processes.end(); ++iter, i++) {
306  heprup_.LPRUP[i] = iter->first;
307  heprup_.XSECUP[i] = iter->second.xsec;
308  heprup_.XERRUP[i] = iter->second.err;
309  heprup_.XMAXUP[i] = iter->second.max;
310  }
311 
312  return true;
313 }
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:48
tuple result
Definition: query.py:137
const std::string & tag() const
static bool isTagComparedInMerge(const std::string &tag)
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