548 auto weightChoice = std::make_shared<DynamicWeightChoice>();
553 iRun.getByLabel(lheLabel,
lheInfo);
559 std::vector<ScaleVarWeight> scaleVariationIDs;
560 std::vector<PDFSetWeights> pdfSetWeightIDs;
561 std::vector<std::string> lheReweighingIDs;
562 bool isFirstGroup =
true;
564 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
565 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
566 std::regex weightgroupRwgt(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
567 std::regex endweightgroup(
"</weightgroup>");
568 std::regex scalewmg26x(
569 "<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"(" 570 "\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
571 std::regex scalewmg26xNew(
572 "<weight\\s*((?:[mM][uU][fF]|facscfact)=\"(\\S+)\"\\s+(?:[mM][uU][Rr]|renscfact)=\"(\\S+)\").+id=\"(\\d+)\"(." 577 "<weight\\s+(?:.*\\s+)?id=\"(\\d+|\\d+-NNLOPS)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)" 578 "=(\\S+)\\s+(?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
580 "<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
581 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
582 std::regex pdfwmg26x(
583 "<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF " 584 "set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</" 589 std::regex pdfwmg26xNew(
590 "<weight\\s+MUF=\"(?:\\S+)\"\\s*MUR=\"(?:\\S+)\"\\s*PDF=\"(?:\\S+)\"\\s*id=\"(\\S+)\"\\s*>" 591 "\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</" 594 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
597 if (iter->tag() !=
"initrwgt") {
599 std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
603 std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
604 std::vector<std::string>
lines = iter->lines();
605 bool missed_weightgroup =
607 bool ismg26x =
false;
608 bool ismg26xNew =
false;
611 boost::replace_all(
lines[iLine],
"<",
"<");
612 boost::replace_all(
lines[iLine],
">",
">");
613 if (std::regex_search(
lines[iLine], groups, weightgroupmg26x)) {
615 }
else if (std::regex_search(
lines[iLine], groups, scalewmg26xNew) ||
616 std::regex_search(
lines[iLine], groups, pdfwmg26xNew)) {
623 if (std::regex_search(
lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup)) {
626 groupname = groups.str(1);
628 std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
629 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation" || isFirstGroup) {
630 if (lheDebug && groupname.find(
"scale_variation") != 0 && groupname !=
"Central scale variation")
631 std::cout <<
">>> First weight is not scale variation, but assuming is the Central Weight" << std::endl;
633 std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
634 isFirstGroup =
false;
635 for (++iLine; iLine <
nLines; ++iLine) {
639 if (std::regex_search(
640 lines[iLine], groups, ismg26x ? scalewmg26x : (ismg26xNew ? scalewmg26xNew : scalew))) {
642 std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " 643 << groups[4].str() <<
" , " << groups[5].str() << std::endl;
645 scaleVariationIDs.emplace_back(groups.str(4), groups.str(1), groups.str(3), groups.str(2));
647 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
649 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
651 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
652 if (!missed_weightgroup) {
655 missed_weightgroup =
false;
656 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
658 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 661 if (ismg26x || ismg26xNew)
662 missed_weightgroup =
true;
667 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
669 std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
670 for (++iLine; iLine <
nLines; ++iLine) {
673 if (std::regex_search(
lines[iLine], groups, pdfw)) {
674 unsigned int lhaID = std::stoi(groups.str(2));
676 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
678 if (pdfSetWeightIDs.empty() || !pdfSetWeightIDs.back().maybe_add(groups.str(1), lhaID)) {
679 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
681 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
683 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
684 if (!missed_weightgroup) {
687 missed_weightgroup =
false;
688 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
690 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 693 if (ismg26x || ismg26xNew)
694 missed_weightgroup =
true;
699 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
701 std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
702 unsigned int lastid = 0;
703 for (++iLine; iLine <
nLines; ++iLine) {
706 if (std::regex_search(
lines[iLine], groups, pdfw)) {
707 unsigned int id = std::stoi(groups.str(1));
708 unsigned int lhaID = std::stoi(groups.str(2));
710 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID
712 if (
id != (lastid + 1) || pdfSetWeightIDs.empty()) {
713 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
715 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
718 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
720 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
721 if (!missed_weightgroup) {
724 missed_weightgroup =
false;
725 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
727 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 730 if (ismg26x || ismg26xNew)
731 missed_weightgroup =
true;
738 std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
739 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
741 for (++iLine; iLine <
nLines; ++iLine) {
744 if (std::regex_search(
745 lines[iLine], groups, ismg26x ? pdfwmg26x : (ismg26xNew ? pdfwmg26xNew : pdfwOld))) {
746 unsigned int member = 0;
747 if (!ismg26x && !ismg26xNew) {
748 member = std::stoi(groups.str(2));
749 }
else if (ismg26xNew) {
750 if (!groups.str(3).empty()) {
751 member = std::stoi(groups.str(3));
754 if (!groups.str(4).empty()) {
755 member = std::stoi(groups.str(4));
758 unsigned int lhaID = member + firstLhaID;
760 std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID
764 pdfSetWeightIDs.emplace_back(groups.str(1), lhaID);
767 pdfSetWeightIDs.back().add(groups.str(1), lhaID);
769 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
771 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
772 if (!missed_weightgroup) {
775 missed_weightgroup =
false;
776 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
778 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 781 if (ismg26x || ismg26xNew)
782 missed_weightgroup =
true;
787 }
else if (groupname ==
"mass_variation" || groupname ==
"sthw2_variation" ||
788 groupname ==
"width_variation") {
790 std::cout <<
">>> Looks like an EW parameter weight" << std::endl;
791 for (++iLine; iLine <
nLines; ++iLine) {
794 if (std::regex_search(
lines[iLine], groups, rwgt)) {
797 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
798 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
800 lheReweighingIDs.emplace_back(rwgtID);
802 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
804 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
808 for (++iLine; iLine <
nLines; ++iLine) {
811 if (std::regex_search(
lines[iLine], groups, endweightgroup)) {
813 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
814 if (!missed_weightgroup) {
817 missed_weightgroup =
false;
818 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
820 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 823 if (ismg26x || ismg26xNew)
824 missed_weightgroup =
true;
830 }
else if (std::regex_search(
lines[iLine], groups, weightgroupRwgt)) {
832 if (groupname.find(
"mg_reweighting") != std::string::npos) {
834 std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
835 for (++iLine; iLine <
nLines; ++iLine) {
838 if (std::regex_search(
lines[iLine], groups, rwgt)) {
841 std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
842 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
844 lheReweighingIDs.emplace_back(rwgtID);
846 }
else if (std::regex_search(
lines[iLine], endweightgroup)) {
848 std::cout <<
">>> Looks like the end of a weight group" << std::endl;
849 if (!missed_weightgroup) {
852 missed_weightgroup =
false;
853 }
else if (std::regex_search(
lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
855 std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end " 859 missed_weightgroup =
true;
870 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
872 std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
873 std::stringstream scaleDoc;
874 scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
875 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
876 const auto& sw = scaleVariationIDs[isw];
879 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
880 weightChoice->scaleWeightIDs.push_back(sw.wid);
882 printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n",
888 if (!scaleVariationIDs.empty())
889 weightChoice->scaleWeightsDoc = scaleDoc.str();
893 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
894 for (
const auto& pw : pdfSetWeightIDs)
895 printf(
"lhaIDs %6d - %6d (%3lu weights: %s, ... )\n",
899 pw.wids.front().c_str());
904 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
906 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
908 std::stringstream pdfDoc;
909 pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
911 for (
const auto& pw : pdfSetWeightIDs) {
913 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid + 1))
915 if (pw.wids.size() == 1)
917 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
918 weightChoice->pdfWeightIDs = pw.wids;
921 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
923 weightChoice->pdfWeightsDoc = pdfDoc.str();
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_
std::atomic< bool > debugRun_
unsigned int maxPdfWeights_
std::vector< uint32_t > preferredPDFLHAIDs_