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  (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 
208 static std::vector<std::string> checklist{"iseed","Random",".log",".dat",".lhe"};
209 static std::vector<std::string> tag_comparison_checklist{"","MGRunCard","mgruncard"};
210 
211 bool LHERunInfoProduct::find_if_checklist(const std::string x, std::vector<std::string> checklist) {
212  return checklist.end() != std::find_if(checklist.begin(),checklist.end(),[&](const std::string& y)
213  { return x.find(y) != std::string::npos; } );
214 }
215 
217  return !(tag == "" || tag.find("Alpgen") == 0 || tag == "MGGridCard" || tag=="MGRunCard" || tag == "mgruncard" || tag=="MadSpin" || tag=="madspin");
218 }
219 
221 {
222 
223  if (heprup_.IDBMUP != other.heprup_.IDBMUP ||
224  heprup_.EBMUP != other.heprup_.EBMUP ||
225  heprup_.PDFGUP != other.heprup_.PDFGUP ||
226  heprup_.PDFSUP != other.heprup_.PDFSUP ||
227  heprup_.IDWTUP != other.heprup_.IDWTUP) {
228 
229  return false;
230  }
231 
232  bool compatibleHeaders = (headers_ == other.headers_);
233 
234  // try to merge not equal but compatible headers (i.e. different iseed)
235  while(!compatibleHeaders) {
236  // okay, something is not the same.
237  // Let's try to merge, but don't duplicate identical headers
238  // and test the rest against a whitelist
239 
240  std::set<Header, HeaderLess> headers;
242  std::inserter(headers, headers.begin()));
243 
244  // make a list of headers contained in the second file
245  std::vector<std::vector<std::string> > runcard_v2;
246  std::vector<std::string> runcard_v2_header;
247  for(auto header2 : headers_) {
248  // fill a vector with the relevant header tags that can be not equal but sill compatible
249  if(find_if_checklist(header2.tag(),tag_comparison_checklist)){
250  runcard_v2.push_back(header2.lines());
251  runcard_v2_header.push_back(header2.tag());
252  }
253  }
254 
255  // loop over the headers of the original file
256  bool failed = false;
257  for(std::vector<LHERunInfoProduct::Header>::const_iterator
258  header = other.headers_begin();
259  header != other.headers_end(); ++header) {
260 
261  if (headers.count(*header)) {
262  continue;
263  }
264 
266  bool header_compatible = false;
267  for (unsigned int iter_runcard = 0; iter_runcard < runcard_v2.size(); iter_runcard++){
268 
269  std::vector<std::string> runcard_v1 = header->lines();
270  runcard_v1.erase( std::remove_if( runcard_v1.begin(), runcard_v1.end(),
271  [&](const std::string& x){ return find_if_checklist(x,checklist); } ),
272  runcard_v1.end() );
273  runcard_v2[iter_runcard].erase( std::remove_if( runcard_v2[iter_runcard].begin(), runcard_v2[iter_runcard].end(),
274  [&](const std::string& x){ return find_if_checklist(x,checklist); } ),
275  runcard_v2[iter_runcard].end() );
276 
277  if(std::equal(runcard_v1.begin(), runcard_v1.end(), runcard_v2[iter_runcard].begin())){
278  header_compatible = true;
279  break;
280  }
281  }
282  if(header_compatible) continue;
283  }
284 
285  if(isTagComparedInMerge(header->tag())){
286  failed = true;
287  } else {
288  addHeader(*header);
289  headers.insert(*header);
290  }
291 
292  }
293 
294  if (failed) {
295  break;
296  }
297 
298  compatibleHeaders = true;
299  }
300 
301 
302  // still not compatible after fixups
303  if (!compatibleHeaders) {
304  return false;
305  }
306 
307  // it is exactly the same, so merge
308  return true;
309 }
void addHeader(const Header &header)
def copy(args, dbName)
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
bool equal(const T &first, const T &second)
Definition: Equal.h:34
const lhef::HEPRUP & heprup() const
static std::vector< std::string > checklist
std::pair< int, int > PDFGUP
Definition: LesHouches.h:84
headers_const_iterator headers_begin() const
static std::vector< std::string > tag_comparison_checklist
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
bool find_if_checklist(const std::string x, std::vector< std::string > checklist)
std::vector< Header > headers_
static const std::string & endOfFile()
std::vector< double > XSECUP
Definition: LesHouches.h:108
const_iterator end() const
const_iterator init() const
const_iterator begin() const
std::vector< int > LPRUP
Definition: LesHouches.h:124
const_iterator begin() const