342 bool lheDebug =
debugRun_.exchange(
false);
343 auto weightChoice = std::make_shared<DynamicWeightChoice>();
348 iRun.getByLabel(lheLabel, lheInfo);
354 std::vector<ScaleVarWeight> scaleVariationIDs;
355 std::vector<PDFSetWeights> pdfSetWeightIDs;
356 std::vector<std::string> lheReweighingIDs;
358 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
359 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
360 std::regex weightgroupRwgt(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
361 std::regex endweightgroup(
"</weightgroup>");
362 std::regex scalewmg26x(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\"\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:[mM][uU][rR]|renscfact)=\"(\\S+)\"\\s+(?:[mM][uU][Ff]|facscfact)=\"(\\S+)\")(\\s+.*)?</weight>");
363 std::regex scalew(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*(?:lhapdf=\\d+|dyn=\\s*-?\\d+)?\\s*((?:mu[rR]|renscfact)=(\\S+)\\s+(?:mu[Ff]|facscfact)=(\\S+)(\\s+.*)?)</weight>");
364 std::regex pdfw(
"<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
365 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
366 std::regex pdfwmg26x(
"<weight\\s+id=\"(\\d+)\"\\s*MUR=\"(?:\\S+)\"\\s*MUF=\"(?:\\S+)\"\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*\"(\\d+)\"\\s*>\\s*(?:PDF=(\\d+)\\s*MemberID=(\\d+))?\\s*(?:\\s.*)?</weight>");
367 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
370 if (iter->tag() !=
"initrwgt") {
371 if (lheDebug)
std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
374 if (lheDebug)
std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
375 std::vector<std::string>
lines = iter->lines();
376 bool missed_weightgroup=
false;
378 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
379 boost::replace_all(lines[iLine],
"<",
"<");
380 boost::replace_all(lines[iLine],
">",
">");
381 if(std::regex_search(lines[iLine],groups,weightgroupmg26x)){
385 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
387 if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup) ) {
389 if (ismg26x) groupname = groups.str(1);
390 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
391 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation") {
392 if (lheDebug)
std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
393 for ( ++iLine; iLine < nLines; ++iLine) {
394 if (lheDebug)
std::cout <<
" " << lines[iLine];
395 if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
396 if (lheDebug)
std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " << groups[4].str() <<
" , " << groups[5].str() << std::endl;
397 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
398 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
399 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
400 if (!missed_weightgroup){
402 }
else missed_weightgroup=
false;
403 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
404 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
405 if (ismg26x) missed_weightgroup=
true;
410 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
411 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
412 for ( ++iLine; iLine < nLines; ++iLine) {
413 if (lheDebug)
std::cout <<
" " << lines[iLine];
414 if (std::regex_search(lines[iLine], groups, pdfw)) {
415 unsigned int lhaID = std::stoi(groups.str(2));
416 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
417 if (pdfSetWeightIDs.empty() || ! pdfSetWeightIDs.back().maybe_add(groups.str(1),lhaID)) {
418 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
420 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
421 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
422 if (!missed_weightgroup){
424 }
else missed_weightgroup=
false;
425 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
426 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
427 if (ismg26x) missed_weightgroup=
true;
432 }
else if (groupname ==
"PDF_variation1" || groupname ==
"PDF_variation2") {
433 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
434 unsigned int lastid = 0;
435 for ( ++iLine; iLine < nLines; ++iLine) {
436 if (lheDebug)
std::cout <<
" " << lines[iLine];
437 if (std::regex_search(lines[iLine], groups, pdfw)) {
438 unsigned int id = std::stoi(groups.str(1));
439 unsigned int lhaID = std::stoi(groups.str(2));
440 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
441 if (
id != (lastid+1) || pdfSetWeightIDs.empty()) {
442 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
444 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
447 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
448 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
449 if(!missed_weightgroup) {
451 }
else missed_weightgroup=
false;
452 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
453 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
454 if (ismg26x) missed_weightgroup=
true;
460 if (lheDebug)
std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
461 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
463 for ( ++iLine; iLine < nLines; ++iLine) {
464 if (lheDebug)
std::cout <<
" " << lines[iLine];
465 if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
466 unsigned int member = 0;
468 member = std::stoi(groups.str(2));
470 if (!groups.str(4).empty()){
471 member = std::stoi(groups.str(4));
474 unsigned int lhaID = member+firstLhaID;
475 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID << std::endl;
478 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
481 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
483 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
484 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
485 if (!missed_weightgroup) {
487 }
else missed_weightgroup=
false;
488 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
489 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
490 if (ismg26x) missed_weightgroup=
true;
496 for ( ++iLine; iLine < nLines; ++iLine) {
497 if (lheDebug)
std::cout <<
" " << lines[iLine];
498 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
499 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
500 if (!missed_weightgroup){
502 }
else missed_weightgroup=
false;
503 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
504 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
505 if (ismg26x) missed_weightgroup=
true;
511 }
else if(std::regex_search(lines[iLine], groups, weightgroupRwgt) ) {
513 if (groupname ==
"mg_reweighting") {
514 if (lheDebug)
std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
515 for ( ++iLine; iLine < nLines; ++iLine) {
516 if (lheDebug)
std::cout <<
" " << lines[iLine];
517 if (std::regex_search(lines[iLine], groups, rwgt)) {
519 if (lheDebug)
std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
520 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
522 lheReweighingIDs.emplace_back(rwgtID);
524 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
525 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
526 if (!missed_weightgroup){
528 }
else missed_weightgroup=
false;
529 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
530 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a new weight group, I will assume I missed the end of the group." << std::endl;
531 if (ismg26x) missed_weightgroup=
true;
542 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
543 if (lheDebug)
std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
544 std::stringstream scaleDoc; scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
545 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
546 const auto & sw = scaleVariationIDs[isw];
547 if (isw) scaleDoc <<
"; ";
548 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
549 weightChoice->scaleWeightIDs.push_back(sw.wid);
550 if (lheDebug) printf(
" id %s: scales ren = % .2f fact = % .2f text = %s\n", sw.wid.c_str(), sw.scales.first, sw.scales.second, sw.label.c_str());
552 if (!scaleVariationIDs.empty()) weightChoice->scaleWeightsDoc = scaleDoc.str();
556 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
557 for (
const auto & pw : pdfSetWeightIDs) printf(
"lhaIDs %6d - %6d (%3lu weights: %s, ... )\n", pw.lhaIDs.first, pw.lhaIDs.second, pw.wids.size(), pw.wids.front().c_str());
562 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
564 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
566 std::stringstream pdfDoc; pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
569 for (
const auto & pw : pdfSetWeightIDs) {
570 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid+1))
continue;
571 if (pw.wids.size() == 1)
continue;
572 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
573 weightChoice->pdfWeightIDs = pw.wids;
576 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
578 weightChoice->pdfWeightsDoc = pdfDoc.str();
std::unordered_map< std::string, uint32_t > lhaNameToID_
headers_const_iterator headers_end() const
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_