549 auto weightChoice = std::make_shared<DynamicWeightChoice>();
554 iRun.getByLabel(lheLabel,
lheInfo);
560 std::vector<ScaleVarWeight> scaleVariationIDs;
561 std::vector<PDFSetWeights> pdfSetWeightIDs;
562 std::vector<std::string> lheReweighingIDs;
563 bool isFirstGroup =
true;
565 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
566 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
567 std::regex weightgroupRwgt(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
568 std::regex endweightgroup(
"</weightgroup>");
569 std::regex scalewmg26x(
570 "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"("
571 "\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
572 std::regex scalewmg26xNew(
573 "<weight\\s*((?:[mM][uU][fF]|facscfact)=\"(\\S+)\"\\s+(?:[mM][uU][Rr]|renscfact)=\"(\\S+)\").+id=\"(\\d+)\"(."
578 "<weight\\s+(?:.*\\s+)?id=\"(\\d+|\\d+-NNLOPS)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)"
579 "=(\\S+)\\s+(?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
581 "<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
582 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
583 std::regex pdfwmg26x(
584 "<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF "
585 "set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</"
590 std::regex pdfwmg26xNew(
591 "<weight\\s+MUF=\"(?:\\S+)\"\\s*MUR=\"(?:\\S+)\"\\s*PDF=\"(?:\\S+)\"\\s*id=\"(\\S+)\"\\s*>"
592 "\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</"
595 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
598 if (iter->tag() !=
"initrwgt") {
600 std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
604 std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
605 std::vector<std::string>
lines = iter->lines();
606 bool missed_weightgroup =
608 bool ismg26x =
false;
609 bool ismg26xNew =
false;
612 boost::replace_all(
lines[iLine],
"<",
"<");
613 boost::replace_all(
lines[iLine],
">",
">");
614 if (std::regex_search(
lines[iLine], groups, weightgroupmg26x)) {
616 }
else if (std::regex_search(
lines[iLine], groups, scalewmg26xNew) ||
617 std::regex_search(
lines[iLine], groups, pdfwmg26xNew)) {
624 if (std::regex_search(
lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
627 groupname = groups.str(1);
629 std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
630 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation" || isFirstGroup) {
631 if (lheDebug && groupname.find(
"scale_variation") != 0 && groupname !=
"Central scale variation")
632 std::cout <<
">>> First weight is not scale variation, but assuming is the Central Weight" << std::endl;
634 std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
635 isFirstGroup =
false;
636 for (++iLine; iLine <
nLines; ++iLine) {
640 if (std::regex_search(
641 lines[iLine], groups, ismg26x ? scalewmg26x : (ismg26xNew ? scalewmg26xNew : scalew))) {
643 std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , "
644 << groups[4].str() <<
" , " << groups[5].str() << std::endl;
646 scaleVariationIDs.emplace_back(groups.str(4), groups.str(1), groups.str(3), groups.str(2));
648 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
650 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
652 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
653 if (!missed_weightgroup) {
656 missed_weightgroup =
false;
657 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
659 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end "
662 if (ismg26x || ismg26xNew)
663 missed_weightgroup =
true;
668 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
670 std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
671 for (++iLine; iLine <
nLines; ++iLine) {
674 if (std::regex_search(
lines[iLine], groups, pdfw)) {
675 unsigned int lhaID = std::stoi(groups.str(2));
677 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
679 if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
680 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
682 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
684 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
685 if (!missed_weightgroup) {
688 missed_weightgroup =
false;
689 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
691 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end "
694 if (ismg26x || ismg26xNew)
695 missed_weightgroup =
true;
700 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
702 std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
703 unsigned int lastid = 0;
704 for (++iLine; iLine <
nLines; ++iLine) {
707 if (std::regex_search(
lines[iLine], groups, pdfw)) {
708 unsigned int id = std::stoi(groups.str(1));
709 unsigned int lhaID = std::stoi(groups.str(2));
711 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
713 if (
id != (lastid + 1) || pdfSetWeightIDs.empty()) {
714 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
716 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
719 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
721 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
722 if (!missed_weightgroup) {
725 missed_weightgroup =
false;
726 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
728 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end "
731 if (ismg26x || ismg26xNew)
732 missed_weightgroup =
true;
739 std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
740 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
742 for (++iLine; iLine <
nLines; ++iLine) {
745 if (std::regex_search(
746 lines[iLine], groups, ismg26x ? pdfwmg26x : (ismg26xNew ? pdfwmg26xNew : pdfwOld))) {
747 unsigned int member = 0;
748 if (!ismg26x && !ismg26xNew) {
749 member = std::stoi(groups.str(2));
750 }
else if (ismg26xNew) {
751 if (!groups.str(3).empty()) {
752 member = std::stoi(groups.str(3));
755 if (!groups.str(4).empty()) {
756 member = std::stoi(groups.str(4));
759 unsigned int lhaID = member + firstLhaID;
761 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID
765 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
768 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
770 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
772 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
773 if (!missed_weightgroup) {
776 missed_weightgroup =
false;
777 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
779 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end "
782 if (ismg26x || ismg26xNew)
783 missed_weightgroup =
true;
788 }
else if (groupname ==
"mass_variation" || groupname ==
"sthw2_variation" ||
789 groupname ==
"width_variation") {
791 std::cout <<
">>> Looks like an EW parameter weight" << std::endl;
792 for (++iLine; iLine <
nLines; ++iLine) {
795 if (std::regex_search(
lines[iLine], groups, rwgt)) {
798 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
799 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
801 lheReweighingIDs.emplace_back(rwgtID);
803 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
805 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
809 for (++iLine; iLine <
nLines; ++iLine) {
812 if (std::regex_search(
lines[iLine], groups, endweightgroup)) {
814 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
815 if (!missed_weightgroup) {
818 missed_weightgroup =
false;
819 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
821 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end "
824 if (ismg26x || ismg26xNew)
825 missed_weightgroup =
true;
831 }
else if (std::regex_search(
lines[iLine], groups, weightgroupRwgt)) {
833 if (groupname ==
"mg_reweighting") {
835 std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
836 for (++iLine; iLine <
nLines; ++iLine) {
839 if (std::regex_search(
lines[iLine], groups, rwgt)) {
842 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
843 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
845 lheReweighingIDs.emplace_back(rwgtID);
847 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
849 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
850 if (!missed_weightgroup) {
853 missed_weightgroup =
false;
854 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
856 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end "
860 missed_weightgroup =
true;
871 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
873 std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
874 std::stringstream scaleDoc;
875 scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
876 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
877 const auto& sw = scaleVariationIDs[isw];
880 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
881 weightChoice->scaleWeightIDs.push_back(sw.wid);
883 printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n",
889 if (!scaleVariationIDs.empty())
890 weightChoice->scaleWeightsDoc = scaleDoc.str();
894 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
895 for (
const auto& pw : pdfSetWeightIDs)
896 printf(
"lhaIDs %6d - %6d (%3lu weights: %s, ... )\n",
900 pw.wids.front().c_str());
905 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
907 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
909 std::stringstream pdfDoc;
910 pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
912 for (
const auto& pw : pdfSetWeightIDs) {
914 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid + 1))
916 if (pw.wids.size() == 1)
918 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
919 weightChoice->pdfWeightIDs = pw.wids;
922 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
924 weightChoice->pdfWeightsDoc = pdfDoc.str();