560 auto weightChoice = std::make_shared<DynamicWeightChoice>();
565 iRun.getByLabel(lheLabel, lheInfo);
571 std::vector<ScaleVarWeight> scaleVariationIDs;
572 std::vector<PDFSetWeights> pdfSetWeightIDs;
573 std::vector<std::string> lheReweighingIDs;
574 bool isFirstGroup =
true;
576 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
577 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
578 std::regex weightgroupRwgt(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
579 std::regex endweightgroup(
"</weightgroup>");
580 std::regex scalewmg26x(
581 "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"(" 582 "\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
583 std::regex scalewmg26xNew(
584 "<weight\\s*((?:[mM][uU][fF]|facscfact)=\"(\\S+)\"\\s+(?:[mM][uU][Rr]|renscfact)=\"(\\S+)\").+id=\"(\\d+)\"(." 589 "<weight\\s+(?:.*\\s+)?id=\"(\\d+|\\d+-NNLOPS)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)" 590 "=(\\S+)\\s+(?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
592 "<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
593 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
594 std::regex pdfwmg26x(
595 "<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF " 596 "set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</" 601 std::regex pdfwmg26xNew(
602 "<weight\\s+MUF=\"(?:\\S+)\"\\s*MUR=\"(?:\\S+)\"\\s*PDF=\"(?:\\S+)\"\\s*id=\"(\\S+)\"\\s*>" 603 "\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</" 606 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
609 if (iter->tag() !=
"initrwgt") {
611 std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
615 std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
616 std::vector<std::string>
lines = iter->lines();
617 bool missed_weightgroup =
619 bool ismg26x =
false;
620 bool ismg26xNew =
false;
621 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines;
623 boost::replace_all(lines[iLine],
"<",
"<");
624 boost::replace_all(lines[iLine],
">",
">");
625 if (std::regex_search(lines[iLine], groups, weightgroupmg26x)) {
627 }
else if (std::regex_search(lines[iLine], groups, scalewmg26xNew) ||
628 std::regex_search(lines[iLine], groups, pdfwmg26xNew)) {
632 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
635 if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
638 groupname = groups.str(1);
640 std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
641 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation" || isFirstGroup) {
642 if (lheDebug && groupname.find(
"scale_variation") != 0 && groupname !=
"Central scale variation")
643 std::cout <<
">>> First weight is not scale variation, but assuming is the Central Weight" << std::endl;
645 std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
646 isFirstGroup =
false;
647 for (++iLine; iLine < nLines; ++iLine) {
651 if (std::regex_search(
652 lines[iLine], groups, ismg26x ? scalewmg26x : (ismg26xNew ? scalewmg26xNew : scalew))) {
654 std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " 655 << groups[4].str() <<
" , " << groups[5].str() << std::endl;
657 scaleVariationIDs.emplace_back(groups.str(4), groups.str(1), groups.str(3), groups.str(2));
659 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
661 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
663 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
664 if (!missed_weightgroup) {
667 missed_weightgroup =
false;
668 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
670 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 673 if (ismg26x || ismg26xNew)
674 missed_weightgroup =
true;
679 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
681 std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
682 for (++iLine; iLine < nLines; ++iLine) {
685 if (std::regex_search(lines[iLine], groups, pdfw)) {
686 unsigned int lhaID = std::stoi(groups.str(2));
688 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
690 if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
691 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
693 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
695 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
696 if (!missed_weightgroup) {
699 missed_weightgroup =
false;
700 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
702 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 705 if (ismg26x || ismg26xNew)
706 missed_weightgroup =
true;
711 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
713 std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
714 unsigned int lastid = 0;
715 for (++iLine; iLine < nLines; ++iLine) {
718 if (std::regex_search(lines[iLine], groups, pdfw)) {
719 unsigned int id = std::stoi(groups.str(1));
720 unsigned int lhaID = std::stoi(groups.str(2));
722 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
724 if (
id != (lastid + 1) || pdfSetWeightIDs.empty()) {
725 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
727 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
730 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
732 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
733 if (!missed_weightgroup) {
736 missed_weightgroup =
false;
737 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
739 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 742 if (ismg26x || ismg26xNew)
743 missed_weightgroup =
true;
750 std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
751 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
753 for (++iLine; iLine < nLines; ++iLine) {
756 if (std::regex_search(
757 lines[iLine], groups, ismg26x ? pdfwmg26x : (ismg26xNew ? pdfwmg26xNew : pdfwOld))) {
758 unsigned int member = 0;
759 if (!ismg26x && !ismg26xNew) {
760 member = std::stoi(groups.str(2));
761 }
else if (ismg26xNew) {
762 if (!groups.str(3).empty()) {
763 member = std::stoi(groups.str(3));
766 if (!groups.str(4).empty()) {
767 member = std::stoi(groups.str(4));
770 unsigned int lhaID = member + firstLhaID;
772 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID
776 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
779 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
781 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
783 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
784 if (!missed_weightgroup) {
787 missed_weightgroup =
false;
788 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
790 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 793 if (ismg26x || ismg26xNew)
794 missed_weightgroup =
true;
799 }
else if (groupname ==
"mass_variation" || groupname ==
"sthw2_variation" ||
800 groupname ==
"width_variation") {
802 std::cout <<
">>> Looks like an EW parameter weight" << std::endl;
803 for (++iLine; iLine < nLines; ++iLine) {
806 if (std::regex_search(lines[iLine], groups, rwgt)) {
809 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
810 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
812 lheReweighingIDs.emplace_back(rwgtID);
814 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
816 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
820 for (++iLine; iLine < nLines; ++iLine) {
823 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
825 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
826 if (!missed_weightgroup) {
829 missed_weightgroup =
false;
830 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
832 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 835 if (ismg26x || ismg26xNew)
836 missed_weightgroup =
true;
842 }
else if (std::regex_search(lines[iLine], groups, weightgroupRwgt)) {
844 if (groupname ==
"mg_reweighting") {
846 std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
847 for (++iLine; iLine < nLines; ++iLine) {
850 if (std::regex_search(lines[iLine], groups, rwgt)) {
853 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
854 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
856 lheReweighingIDs.emplace_back(rwgtID);
858 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
860 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
861 if (!missed_weightgroup) {
864 missed_weightgroup =
false;
865 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
867 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 871 missed_weightgroup =
true;
882 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
884 std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
885 std::stringstream scaleDoc;
886 scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
887 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
888 const auto& sw = scaleVariationIDs[isw];
891 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
892 weightChoice->scaleWeightIDs.push_back(sw.wid);
894 printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n",
900 if (!scaleVariationIDs.empty())
901 weightChoice->scaleWeightsDoc = scaleDoc.str();
905 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
906 for (
const auto& pw : pdfSetWeightIDs)
907 printf(
"lhaIDs %6d - %6d (%3lu weights: %s, ... )\n",
911 pw.wids.front().c_str());
916 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
918 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
920 std::stringstream pdfDoc;
921 pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
923 for (
const auto& pw : pdfSetWeightIDs) {
925 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid + 1))
927 if (pw.wids.size() == 1)
929 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
930 weightChoice->pdfWeightIDs = pw.wids;
933 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
935 weightChoice->pdfWeightsDoc = pdfDoc.str();
headers_const_iterator headers_end() const
std::unordered_map< std::string, uint32_t > lhaNameToID_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
const std::vector< edm::InputTag > lheLabel_
headers_const_iterator headers_begin() const
std::atomic< bool > debugRun_
unsigned int maxPdfWeights_
std::vector< uint32_t > preferredPDFLHAIDs_