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" || tag=="MGRunCard" || tag == "mgruncard" || tag=="MadSpin" || tag=="madspin");
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  return false;
220  }
221 
222  bool compatibleHeaders = headers_ == other.headers_;
223 
224  // try to merge different, but compatible headers
225  while(!compatibleHeaders) {
226  // okay, something is different.
227  // Let's try to merge, but don't duplicate identical headers
228  // and test the rest against a whitelist
229 
230  std::set<Header, HeaderLess> headers;
232  std::inserter(headers, headers.begin()));
233 
234  bool failed = false;
235  for(std::vector<LHERunInfoProduct::Header>::const_iterator
236  header = other.headers_begin();
237  header != other.headers_end(); ++header) {
238  if (headers.count(*header)) {
239  continue;
240  }
241 
242  if(isTagComparedInMerge(header->tag())) {
243  failed = true;
244  } else {
245  addHeader(*header);
246  headers.insert(*header);
247  }
248  }
249  if (failed) {
250  break;
251  }
252 
253  compatibleHeaders = true;
254  }
255 
256  // still not compatible after fixups
257  if (!compatibleHeaders) {
258  return false;
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
tuple result
Definition: mps_fire.py:84
std::pair< int, int > PDFGUP
Definition: LesHouches.h:84
headers_const_iterator headers_begin() const
T sqrt(T t)
Definition: SSEVec.h:18
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()
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