552 auto weightChoice = std::make_shared<DynamicWeightChoice>();
557 iRun.getByLabel(lheLabel, lheInfo);
563 std::vector<ScaleVarWeight> scaleVariationIDs;
564 std::vector<PDFSetWeights> pdfSetWeightIDs;
565 std::vector<std::string> lheReweighingIDs;
567 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
568 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
569 std::regex weightgroupRwgt(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
570 std::regex endweightgroup(
"</weightgroup>");
571 std::regex scalewmg26x(
572 "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"(" 573 "\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
575 "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)=(\\S+)\\s+(" 576 "?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
578 "<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
579 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
580 std::regex pdfwmg26x(
581 "<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF " 582 "set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</" 584 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
587 if (iter->tag() !=
"initrwgt") {
589 std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
593 std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
594 std::vector<std::string>
lines = iter->lines();
595 bool missed_weightgroup =
597 bool ismg26x =
false;
598 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines;
600 boost::replace_all(lines[iLine],
"<",
"<");
601 boost::replace_all(lines[iLine],
">",
">");
602 if (std::regex_search(lines[iLine], groups, weightgroupmg26x)) {
606 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
609 if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
612 groupname = groups.str(1);
614 std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
615 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation") {
617 std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
618 for (++iLine; iLine < nLines; ++iLine) {
621 if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
623 std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " 624 << groups[4].str() <<
" , " << groups[5].str() << std::endl;
625 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
626 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
628 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
629 if (!missed_weightgroup) {
632 missed_weightgroup =
false;
633 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
635 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 639 missed_weightgroup =
true;
644 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
646 std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
647 for (++iLine; iLine < nLines; ++iLine) {
650 if (std::regex_search(lines[iLine], groups, pdfw)) {
651 unsigned int lhaID = std::stoi(groups.str(2));
653 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
655 if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
656 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
658 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
660 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
661 if (!missed_weightgroup) {
664 missed_weightgroup =
false;
665 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
667 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 671 missed_weightgroup =
true;
676 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
678 std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
679 unsigned int lastid = 0;
680 for (++iLine; iLine < nLines; ++iLine) {
683 if (std::regex_search(lines[iLine], groups, pdfw)) {
684 unsigned int id = std::stoi(groups.str(1));
685 unsigned int lhaID = std::stoi(groups.str(2));
687 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
689 if (
id != (lastid + 1) || pdfSetWeightIDs.empty()) {
690 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
692 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
695 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
697 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
698 if (!missed_weightgroup) {
701 missed_weightgroup =
false;
702 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
704 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 708 missed_weightgroup =
true;
715 std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
716 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
718 for (++iLine; iLine < nLines; ++iLine) {
721 if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
722 unsigned int member = 0;
724 member = std::stoi(groups.str(2));
726 if (!groups.str(4).empty()) {
727 member = std::stoi(groups.str(4));
730 unsigned int lhaID = member + firstLhaID;
732 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID
736 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
739 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
741 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
743 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
744 if (!missed_weightgroup) {
747 missed_weightgroup =
false;
748 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
750 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 754 missed_weightgroup =
true;
760 for (++iLine; iLine < nLines; ++iLine) {
763 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
765 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
766 if (!missed_weightgroup) {
769 missed_weightgroup =
false;
770 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
772 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 776 missed_weightgroup =
true;
782 }
else if (std::regex_search(lines[iLine], groups, weightgroupRwgt)) {
784 if (groupname ==
"mg_reweighting") {
786 std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
787 for (++iLine; iLine < nLines; ++iLine) {
790 if (std::regex_search(lines[iLine], groups, rwgt)) {
793 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
794 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
796 lheReweighingIDs.emplace_back(rwgtID);
798 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
800 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
801 if (!missed_weightgroup) {
804 missed_weightgroup =
false;
805 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
807 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 811 missed_weightgroup =
true;
822 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
824 std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
825 std::stringstream scaleDoc;
826 scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
827 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
828 const auto& sw = scaleVariationIDs[isw];
831 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
832 weightChoice->scaleWeightIDs.push_back(sw.wid);
834 printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n",
840 if (!scaleVariationIDs.empty())
841 weightChoice->scaleWeightsDoc = scaleDoc.str();
845 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
846 for (
const auto& pw : pdfSetWeightIDs)
847 printf(
"lhaIDs %6d - %6d (%3lu weights: %s, ... )\n",
851 pw.wids.front().c_str());
856 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
858 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
860 std::stringstream pdfDoc;
861 pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
864 for (
const auto& pw : pdfSetWeightIDs) {
865 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid + 1))
867 if (pw.wids.size() == 1)
869 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
870 weightChoice->pdfWeightIDs = pw.wids;
873 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
875 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_