293 bool lheDebug =
debugRun_.exchange(
false);
294 auto weightChoice = std::make_shared<DynamicWeightChoice>();
298 if (iRun.getByLabel(
lheLabel_, lheInfo)) {
299 std::vector<ScaleVarWeight> scaleVariationIDs;
300 std::vector<PDFSetWeights> pdfSetWeightIDs;
302 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
303 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
304 std::regex endweightgroup(
"</weightgroup>");
305 std::regex scalewmg26x(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"(\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
306 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>");
307 std::regex pdfw(
"<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
308 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
309 std::regex pdfwmg26x(
"<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</weight>");
312 if (iter->tag() !=
"initrwgt") {
313 if (lheDebug)
std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
316 if (lheDebug)
std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
317 std::vector<std::string>
lines = iter->lines();
318 bool missed_weightgroup=
false;
320 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
321 boost::replace_all(lines[iLine],
"<",
"<");
322 boost::replace_all(lines[iLine],
">",
">");
323 if(std::regex_search(lines[iLine],groups,weightgroupmg26x)){
327 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
329 if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup) ) {
331 if (ismg26x) groupname = groups.str(1);
332 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
333 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation") {
334 if (lheDebug)
std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
335 for ( ++iLine; iLine < nLines; ++iLine) {
336 if (lheDebug)
std::cout <<
" " << lines[iLine];
337 if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
338 if (lheDebug)
std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " << groups[4].str() <<
" , " << groups[5].str() << std::endl;
339 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
340 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
341 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
342 if (!missed_weightgroup){
344 }
else missed_weightgroup=
false;
345 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
346 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;
347 if (ismg26x) missed_weightgroup=
true;
352 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
353 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
354 for ( ++iLine; iLine < nLines; ++iLine) {
355 if (lheDebug)
std::cout <<
" " << lines[iLine];
356 if (std::regex_search(lines[iLine], groups, pdfw)) {
357 unsigned int lhaID = std::stoi(groups.str(2));
358 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
359 if (pdfSetWeightIDs.empty() || ! pdfSetWeightIDs.back().maybe_add(groups.str(1),lhaID)) {
360 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
362 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
363 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
364 if (!missed_weightgroup){
366 }
else missed_weightgroup=
false;
367 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
368 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;
369 if (ismg26x) missed_weightgroup=
true;
374 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
375 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
376 unsigned int lastid = 0;
377 for ( ++iLine; iLine < nLines; ++iLine) {
378 if (lheDebug)
std::cout <<
" " << lines[iLine];
379 if (std::regex_search(lines[iLine], groups, pdfw)) {
380 unsigned int id = std::stoi(groups.str(1));
381 unsigned int lhaID = std::stoi(groups.str(2));
382 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
383 if (
id != (lastid+1) || pdfSetWeightIDs.empty()) {
384 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
386 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
389 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
390 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
391 if(!missed_weightgroup) {
393 }
else missed_weightgroup=
false;
394 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
395 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;
396 if (ismg26x) missed_weightgroup=
true;
402 if (lheDebug)
std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
403 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
405 for ( ++iLine; iLine < nLines; ++iLine) {
406 if (lheDebug)
std::cout <<
" " << lines[iLine];
407 if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
408 unsigned int member = 0;
410 member = std::stoi(groups.str(2));
412 if (!groups.str(4).empty()){
413 member = std::stoi(groups.str(4));
416 unsigned int lhaID = member+firstLhaID;
417 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID << std::endl;
420 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
423 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
425 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
426 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
427 if (!missed_weightgroup) {
429 }
else missed_weightgroup=
false;
430 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
431 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;
432 if (ismg26x) missed_weightgroup=
true;
438 for ( ++iLine; iLine < nLines; ++iLine) {
439 if (lheDebug)
std::cout <<
" " << lines[iLine];
440 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
441 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
442 if (!missed_weightgroup){
444 }
else missed_weightgroup=
false;
445 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
446 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;
447 if (ismg26x) missed_weightgroup=
true;
458 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
459 if (lheDebug)
std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
460 std::stringstream scaleDoc; scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
461 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
462 const auto & sw = scaleVariationIDs[isw];
463 if (isw) scaleDoc <<
"; ";
464 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
465 weightChoice->scaleWeightIDs.push_back(sw.wid);
466 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());
468 if (!scaleVariationIDs.empty()) weightChoice->scaleWeightsDoc = scaleDoc.str();
472 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
473 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());
476 std::stringstream pdfDoc; pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
479 for (
const auto & pw : pdfSetWeightIDs) {
480 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid+1))
continue;
481 if (pw.wids.size() == 1)
continue;
482 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
483 weightChoice->pdfWeightIDs = pw.wids;
486 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
488 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_