242 bool lheDebug =
debugRun_.exchange(
false);
243 auto weightChoice = std::make_shared<DynamicWeightChoice>();
247 if (iRun.getByLabel(
lheLabel_, lheInfo)) {
248 std::vector<ScaleVarWeight> scaleVariationIDs;
249 std::vector<PDFSetWeights> pdfSetWeightIDs;
251 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
252 std::regex endweightgroup(
"</weightgroup>");
253 std::regex scalew(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)=(\\S+)\\s+(?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
254 std::regex pdfw(
"<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
255 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
258 if (iter->tag() !=
"initrwgt") {
259 if (lheDebug)
std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
262 if (lheDebug)
std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
263 const std::vector<std::string> &
lines = iter->lines();
264 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
266 if (std::regex_search(lines[iLine], groups, weightgroup)) {
268 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
269 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation") {
270 if (lheDebug)
std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
271 for ( ++iLine; iLine < nLines; ++iLine) {
272 if (lheDebug)
std::cout <<
" " << lines[iLine];
273 if (std::regex_search(lines[iLine], groups, scalew)) {
274 if (lheDebug)
std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " << groups[4].str() <<
" , " << groups[5].str() << std::endl;
275 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
276 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
277 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
279 }
else if (std::regex_search(lines[iLine], weightgroup)) {
280 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
285 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
286 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
287 for ( ++iLine; iLine < nLines; ++iLine) {
288 if (lheDebug)
std::cout <<
" " << lines[iLine];
289 if (std::regex_search(lines[iLine], groups, pdfw)) {
290 unsigned int lhaID = std::stoi(groups.str(2));
291 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
292 if (pdfSetWeightIDs.empty() || ! pdfSetWeightIDs.back().maybe_add(groups.str(1),lhaID)) {
293 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
295 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
296 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
298 }
else if (std::regex_search(lines[iLine], weightgroup)) {
299 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
304 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
305 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
306 unsigned int lastid = 0;
307 for ( ++iLine; iLine < nLines; ++iLine) {
308 if (lheDebug)
std::cout <<
" " << lines[iLine];
309 if (std::regex_search(lines[iLine], groups, pdfw)) {
310 unsigned int id = std::stoi(groups.str(1));
311 unsigned int lhaID = std::stoi(groups.str(2));
312 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
313 if (
id != (lastid+1) || pdfSetWeightIDs.empty()) {
314 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
316 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
319 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
320 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
322 }
else if (std::regex_search(lines[iLine], weightgroup)) {
323 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
329 if (lheDebug)
std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
330 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
332 for ( ++iLine; iLine < nLines; ++iLine) {
333 if (lheDebug)
std::cout <<
" " << lines[iLine];
334 if (std::regex_search(lines[iLine], groups, pdfwOld)) {
335 unsigned int member = std::stoi(groups.str(2));
336 unsigned int lhaID = member+firstLhaID;
337 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
340 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
343 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
345 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
346 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
348 }
else if (std::regex_search(lines[iLine], weightgroup)) {
349 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
355 for ( ++iLine; iLine < nLines; ++iLine) {
356 if (lheDebug)
std::cout <<
" " << lines[iLine];
357 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
358 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
360 }
else if (std::regex_search(lines[iLine], weightgroup)) {
361 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
372 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
373 if (lheDebug)
std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
374 std::stringstream scaleDoc; scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
375 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
376 const auto & sw = scaleVariationIDs[isw];
377 if (isw) scaleDoc <<
"; ";
378 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
379 weightChoice->scaleWeightIDs.push_back(sw.wid);
380 if (lheDebug) printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n", sw.wid.c_str(), sw.scales.first, sw.scales.second, sw.label.c_str());
382 if (!scaleVariationIDs.empty()) weightChoice->scaleWeightsDoc = scaleDoc.str();
386 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
387 for (
const auto & pw : pdfSetWeightIDs) printf(
"lhaIDs %6d - %6d (%3lu weights: %s, ... )\n", pw.lhaIDs.first, pw.lhaIDs.second, pw.wids.size(), pw.wids.front().c_str());
390 std::stringstream pdfDoc; pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
393 for (
const auto & pw : pdfSetWeightIDs) {
394 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid+1))
continue;
395 if (pw.wids.size() == 1)
continue;
396 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
397 weightChoice->pdfWeightIDs = pw.wids;
400 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
402 weightChoice->pdfWeightsDoc = pdfDoc.str();
std::unordered_map< std::string, uint32_t > lhaNameToID_
headers_const_iterator headers_end() const
headers_const_iterator headers_begin() const
std::atomic< bool > debugRun_
const edm::InputTag lheLabel_
unsigned int maxPdfWeights_
std::vector< uint32_t > preferredPDFLHAIDs_