CMS 3D CMS Logo

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  if (mode != other.mode)
18  return false;
19 
20  switch (mode) {
21  case kFooter:
22  case kDone:
23  return true;
24 
25  case kHeader:
26  return header == other.header;
27 
28  case kBody:
29  return header == other.header && iter == other.iter;
30 
31  case kInit:
32  return line == other.line;
33  }
34 
35  return false;
36 }
37 
39  tmp.clear();
40 
41  do {
42  switch (mode) {
43  case kHeader:
44  if (header == runInfo->headers_end()) {
45  if (line++ == 1)
46  tmp = "</header>\n";
47  else {
48  mode = kInit;
49  tmp = "<init>\n";
50  line = 0;
51  }
52  break;
53  } else if (!line) {
54  line++;
55  tmp = "<header>\n";
56  break;
57  } else {
58  mode = kBody;
59  const std::string &tag = header->tag();
60  tmp = tag.empty() ? "<!--" : (tag == "<>") ? "" : ("<" + tag + ">");
61  iter = header->begin();
62  continue;
63  }
64 
65  case kBody:
66  if (iter == header->end()) {
67  mode = kHeader;
68  const std::string &tag = header->tag();
69  tmp += tag.empty() ? "-->" : (tag == "<>") ? "" : ("</" + tag + ">");
70  tmp += "\n";
71  header++;
72  } else {
73  tmp += *iter++;
74  if (iter == header->end() &&
75  (tmp.empty() || (tmp[tmp.length() - 1] != '\r' && tmp[tmp.length() - 1] != '\n')))
76  continue;
77  }
78  break;
79 
80  case kInit: {
81  const lhef::HEPRUP &heprup = runInfo->heprup();
82  if (!line++) {
83  std::ostringstream ss;
84  ss << std::setprecision(7) << std::scientific << std::uppercase << " " << heprup.IDBMUP.first << " "
85  << heprup.IDBMUP.second << " " << heprup.EBMUP.first << " " << heprup.EBMUP.second << " "
86  << heprup.PDFGUP.first << " " << heprup.PDFGUP.second << " " << heprup.PDFSUP.first << " "
87  << heprup.PDFSUP.second << " " << heprup.IDWTUP << " " << heprup.NPRUP << std::endl;
88  tmp = ss.str();
89  break;
90  }
91  if (line >= (unsigned int)heprup.NPRUP + runInfo->comments_size() + 2) {
92  tmp = "</init>\n";
93  mode = kFooter;
94  break;
95  } else if (line >= (unsigned int)heprup.NPRUP + 2) {
96  tmp = *(runInfo->comments_begin() + (line - (unsigned int)heprup.NPRUP - 2));
97  break;
98  }
99 
100  std::ostringstream ss;
101  ss << std::setprecision(7) << std::scientific << std::uppercase << "\t" << heprup.XSECUP[line - 2] << "\t"
102  << heprup.XERRUP[line - 2] << "\t" << heprup.XMAXUP[line - 2] << "\t" << heprup.LPRUP[line - 2] << std::endl;
103  tmp = ss.str();
104  } break;
105 
106  case kFooter:
107  mode = kDone;
108 
109  default:
110  /* ... */;
111  }
112  } while (false);
113 }
114 
117 
118  result.runInfo = this;
119  result.header = headers_begin();
121  result.line = 0;
122  result.tmp = "<LesHouchesEvents version=\"1.0\">\n";
123 
124  return result;
125 }
126 
129 
130  result.runInfo = this;
132  result.line = 0;
133  result.tmp = "<init>\n";
134 
135  return result;
136 }
137 
139  static const std::string theEnd("</LesHouchesEvents>\n");
140 
141  return theEnd;
142 }
143 
144 namespace {
145  struct XSec {
146  inline XSec() : xsec(0.0), err(0.0), max(0.0) {}
147 
148  double xsec;
149  double err;
150  double max;
151  };
152 
153  struct HeaderLess {
154  bool operator()(const LHERunInfoProduct::Header &a, const LHERunInfoProduct::Header &b) const;
155  };
156 } // namespace
157 
158 bool HeaderLess::operator()(const LHERunInfoProduct::Header &a, const LHERunInfoProduct::Header &b) const {
159  if (a == b)
160  return false;
161  if (a.tag() < b.tag())
162  return true;
163  if (a.tag() > b.tag())
164  return false;
165 
168 
169  for (; iter1 != a.end() && iter2 != b.end(); ++iter1, ++iter2) {
170  if (*iter1 < *iter2)
171  return true;
172  else if (*iter1 != *iter2)
173  return false;
174  }
175 
176  return iter2 != b.end();
177 }
178 
179 static std::vector<std::string> checklist{"iseed", "Random", ".log", ".dat", ".lhe"};
180 static std::vector<std::string> tag_comparison_checklist{"", "MGRunCard", "mgruncard"};
181 
182 bool LHERunInfoProduct::find_if_checklist(const std::string x, std::vector<std::string> checklist) {
183  return checklist.end() != std::find_if(checklist.begin(), checklist.end(), [&](const std::string &y) {
184  return x.find(y) != std::string::npos;
185  });
186 }
187 
189  return !(tag.empty() || tag.find("Alpgen") == 0 || tag == "MGGridCard" || tag == "MGRunCard" || tag == "mgruncard" ||
190  tag == "MadSpin" || tag == "madspin");
191 }
192 
194  if (heprup_.IDBMUP != other.heprup_.IDBMUP || heprup_.EBMUP != other.heprup_.EBMUP ||
195  heprup_.PDFGUP != other.heprup_.PDFGUP || heprup_.PDFSUP != other.heprup_.PDFSUP ||
196  heprup_.IDWTUP != other.heprup_.IDWTUP) {
197  return false;
198  }
199 
200  bool compatibleHeaders = (headers_ == other.headers_);
201 
202  // try to merge not equal but compatible headers (i.e. different iseed)
203  while (!compatibleHeaders) {
204  // okay, something is not the same.
205  // Let's try to merge, but don't duplicate identical headers
206  // and test the rest against a whitelist
207 
208  std::set<Header, HeaderLess> headers;
209  std::copy(headers_begin(), headers_end(), std::inserter(headers, headers.begin()));
210 
211  // make a list of headers contained in the second file
212  std::vector<std::vector<std::string> > runcard_v2;
213  std::vector<std::string> runcard_v2_header;
214  for (const auto &header2 : headers_) {
215  // fill a vector with the relevant header tags that can be not equal but sill compatible
216  if (find_if_checklist(header2.tag(), tag_comparison_checklist)) {
217  runcard_v2.push_back(header2.lines());
218  runcard_v2_header.push_back(header2.tag());
219  }
220  }
221 
222  // loop over the headers of the original file
223  bool failed = false;
224  for (std::vector<LHERunInfoProduct::Header>::const_iterator header = other.headers_begin();
225  header != other.headers_end();
226  ++header) {
227  if (headers.count(*header)) {
228  continue;
229  }
230 
232  bool header_compatible = false;
233  for (unsigned int iter_runcard = 0; iter_runcard < runcard_v2.size(); iter_runcard++) {
234  std::vector<std::string> runcard_v1 = header->lines();
235  runcard_v1.erase(std::remove_if(runcard_v1.begin(),
236  runcard_v1.end(),
237  [&](const std::string &x) { return find_if_checklist(x, checklist); }),
238  runcard_v1.end());
239  runcard_v2[iter_runcard].erase(
240  std::remove_if(runcard_v2[iter_runcard].begin(),
241  runcard_v2[iter_runcard].end(),
242  [&](const std::string &x) { return find_if_checklist(x, checklist); }),
243  runcard_v2[iter_runcard].end());
244 
245  if (std::equal(runcard_v1.begin(), runcard_v1.end(), runcard_v2[iter_runcard].begin())) {
246  header_compatible = true;
247  break;
248  }
249  }
250  if (header_compatible)
251  continue;
252  }
253 
254  if (isTagComparedInMerge(header->tag())) {
255  failed = true;
256  } else {
257  addHeader(*header);
258  headers.insert(*header);
259  }
260  }
261 
262  if (failed) {
263  break;
264  }
265 
266  compatibleHeaders = true;
267  }
268 
269  // still not compatible after fixups
270  if (!compatibleHeaders) {
271  return false;
272  }
273 
274  // it is exactly the same, so merge
275  return true;
276 }
277 
279  heprup_.swap(other.heprup_);
280  headers_.swap(other.headers_);
281  comments_.swap(other.comments_);
282 }
void addHeader(const Header &header)
const_iterator end() const
bool operator==(const const_iterator &other) const
std::pair< double, double > EBMUP
Definition: LesHouches.h:82
headers_const_iterator headers_begin() const
std::pair< int, int > IDBMUP
Definition: LesHouches.h:77
std::vector< std::string >::const_iterator const_iterator
bool equal(const T &first, const T &second)
Definition: Equal.h:32
static std::vector< std::string > checklist
std::pair< int, int > PDFGUP
Definition: LesHouches.h:88
void swap(LHERunInfoProduct &other)
headers_const_iterator headers_end() const
const lhef::HEPRUP & heprup() const
static std::vector< std::string > tag_comparison_checklist
static bool isTagComparedInMerge(const std::string &tag)
const_iterator init() const
std::vector< double > XERRUP
Definition: LesHouches.h:118
std::vector< double > XMAXUP
Definition: LesHouches.h:123
double b
Definition: hdecay.h:118
std::pair< int, int > PDFSUP
Definition: LesHouches.h:94
bool mergeProduct(const LHERunInfoProduct &other)
const_iterator begin() const
double a
Definition: hdecay.h:119
bool find_if_checklist(const std::string x, std::vector< std::string > checklist)
void swap(HEPRUP &other)
Definition: LesHouches.h:61
std::vector< Header > headers_
static const std::string & endOfFile()
std::vector< double > XSECUP
Definition: LesHouches.h:112
tmp
align.sh
Definition: createJobs.py:716
std::vector< std::string > comments_
std::vector< int > LPRUP
Definition: LesHouches.h:128