522 auto weightChoice = std::make_shared<DynamicWeightChoice>();
527 iRun.getByLabel(lheLabel, lheInfo);
533 std::vector<ScaleVarWeight> scaleVariationIDs;
534 std::vector<PDFSetWeights> pdfSetWeightIDs;
535 std::vector<std::string> lheReweighingIDs;
537 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
538 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
539 std::regex weightgroupRwgt(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
540 std::regex endweightgroup(
"</weightgroup>");
541 std::regex scalewmg26x(
542 "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"(" 543 "\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
545 "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)=(\\S+)\\s+(" 546 "?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
548 "<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
549 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
550 std::regex pdfwmg26x(
551 "<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF " 552 "set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</" 554 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
557 if (iter->tag() !=
"initrwgt") {
559 std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
563 std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
564 std::vector<std::string>
lines = iter->lines();
565 bool missed_weightgroup =
567 bool ismg26x =
false;
568 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines;
570 boost::replace_all(lines[iLine],
"<",
"<");
571 boost::replace_all(lines[iLine],
">",
">");
572 if (std::regex_search(lines[iLine], groups, weightgroupmg26x)) {
576 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
579 if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
582 groupname = groups.str(1);
584 std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
585 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation") {
587 std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
588 for (++iLine; iLine < nLines; ++iLine) {
591 if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
593 std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " 594 << groups[4].str() <<
" , " << groups[5].str() << std::endl;
595 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
596 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
598 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
599 if (!missed_weightgroup) {
602 missed_weightgroup =
false;
603 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
605 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 609 missed_weightgroup =
true;
614 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
616 std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
617 for (++iLine; iLine < nLines; ++iLine) {
620 if (std::regex_search(lines[iLine], groups, pdfw)) {
621 unsigned int lhaID = std::stoi(groups.str(2));
623 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
625 if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
626 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
628 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
630 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
631 if (!missed_weightgroup) {
634 missed_weightgroup =
false;
635 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
637 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 641 missed_weightgroup =
true;
646 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
648 std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
649 unsigned int lastid = 0;
650 for (++iLine; iLine < nLines; ++iLine) {
653 if (std::regex_search(lines[iLine], groups, pdfw)) {
654 unsigned int id = std::stoi(groups.str(1));
655 unsigned int lhaID = std::stoi(groups.str(2));
657 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
659 if (
id != (lastid + 1) || pdfSetWeightIDs.empty()) {
660 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
662 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
665 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
667 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
668 if (!missed_weightgroup) {
671 missed_weightgroup =
false;
672 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
674 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 678 missed_weightgroup =
true;
685 std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
686 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
688 for (++iLine; iLine < nLines; ++iLine) {
691 if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
692 unsigned int member = 0;
694 member = std::stoi(groups.str(2));
696 if (!groups.str(4).empty()) {
697 member = std::stoi(groups.str(4));
700 unsigned int lhaID = member + firstLhaID;
702 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID
706 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
709 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
711 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
713 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
714 if (!missed_weightgroup) {
717 missed_weightgroup =
false;
718 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
720 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 724 missed_weightgroup =
true;
730 for (++iLine; iLine < nLines; ++iLine) {
733 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
735 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
736 if (!missed_weightgroup) {
739 missed_weightgroup =
false;
740 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
742 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 746 missed_weightgroup =
true;
752 }
else if (std::regex_search(lines[iLine], groups, weightgroupRwgt)) {
754 if (groupname ==
"mg_reweighting") {
756 std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
757 for (++iLine; iLine < nLines; ++iLine) {
760 if (std::regex_search(lines[iLine], groups, rwgt)) {
763 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
764 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
766 lheReweighingIDs.emplace_back(rwgtID);
768 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
770 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
771 if (!missed_weightgroup) {
774 missed_weightgroup =
false;
775 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
777 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 781 missed_weightgroup =
true;
792 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
794 std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
795 std::stringstream scaleDoc;
796 scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
797 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
798 const auto& sw = scaleVariationIDs[isw];
801 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
802 weightChoice->scaleWeightIDs.push_back(sw.wid);
804 printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n",
810 if (!scaleVariationIDs.empty())
811 weightChoice->scaleWeightsDoc = scaleDoc.str();
815 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
816 for (
const auto& pw : pdfSetWeightIDs)
817 printf(
"lhaIDs %6d - %6d (%3lu weights: %s, ... )\n",
821 pw.wids.front().c_str());
826 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
828 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
830 std::stringstream pdfDoc;
831 pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
834 for (
const auto& pw : pdfSetWeightIDs) {
835 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid + 1))
837 if (pw.wids.size() == 1)
839 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
840 weightChoice->pdfWeightIDs = pw.wids;
843 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
845 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_