320 bool lheDebug =
debugRun_.exchange(
false);
321 auto weightChoice = std::make_shared<DynamicWeightChoice>();
326 iRun.getByLabel(lheLabel, lheInfo);
332 std::vector<ScaleVarWeight> scaleVariationIDs;
333 std::vector<PDFSetWeights> pdfSetWeightIDs;
334 std::vector<std::string> lheReweighingIDs;
336 std::regex weightgroupmg26x(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s+combine=\"(.*)\"\\s*>");
337 std::regex weightgroup(
"<weightgroup\\s+combine=\"(.*)\"\\s+(?:name|type)=\"(.*)\"\\s*>");
338 std::regex weightgroupRwgt(
"<weightgroup\\s+(?:name|type)=\"(.*)\"\\s*>");
339 std::regex endweightgroup(
"</weightgroup>");
340 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>");
341 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>");
342 std::regex pdfw(
"<weight\\s+id=\"(\\d+)\">\\s*(?:PDF set|lhapdf|PDF|pdfset)\\s*=\\s*(\\d+)\\s*(?:\\s.*)?</weight>");
343 std::regex pdfwOld(
"<weight\\s+(?:.*\\s+)?id=\"(\\d+)\">\\s*Member \\s*(\\d+)\\s*(?:.*)</weight>");
344 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>");
345 std::regex rwgt(
"<weight\\s+id=\"(.+)\">(.+)?(</weight>)?");
348 if (iter->tag() !=
"initrwgt") {
349 if (lheDebug)
std::cout <<
"Skipping LHE header with tag" << iter->tag() << std::endl;
352 if (lheDebug)
std::cout <<
"Found LHE header with tag" << iter->tag() << std::endl;
353 std::vector<std::string>
lines = iter->lines();
354 bool missed_weightgroup=
false;
356 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
357 boost::replace_all(lines[iLine],
"<",
"<");
358 boost::replace_all(lines[iLine],
">",
">");
359 if(std::regex_search(lines[iLine],groups,weightgroupmg26x)){
363 for (
unsigned int iLine = 0, nLines = lines.size(); iLine < nLines; ++iLine) {
365 if (std::regex_search(lines[iLine], groups, ismg26x ? weightgroupmg26x : weightgroup) ) {
367 if (ismg26x) groupname = groups.str(1);
368 if (lheDebug)
std::cout <<
">>> Looks like the beginning of a weight group for '" << groupname <<
"'" << std::endl;
369 if (groupname.find(
"scale_variation") == 0 || groupname ==
"Central scale variation") {
370 if (lheDebug)
std::cout <<
">>> Looks like scale variation for theory uncertainties" << std::endl;
371 for ( ++iLine; iLine < nLines; ++iLine) {
372 if (lheDebug)
std::cout <<
" " << lines[iLine];
373 if (std::regex_search(lines[iLine], groups, ismg26x ? scalewmg26x : scalew)) {
374 if (lheDebug)
std::cout <<
" >>> Scale weight " << groups[1].str() <<
" for " << groups[3].str() <<
" , " << groups[4].str() <<
" , " << groups[5].str() << std::endl;
375 scaleVariationIDs.emplace_back(groups.str(1), groups.str(2), groups.str(3), groups.str(4));
376 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
377 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
378 if (!missed_weightgroup){
380 }
else missed_weightgroup=
false;
381 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
382 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;
383 if (ismg26x) missed_weightgroup=
true;
388 }
else if (groupname ==
"PDF_variation" || groupname.find(
"PDF_variation ") == 0) {
389 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for one or more pdfs" << std::endl;
390 for ( ++iLine; iLine < nLines; ++iLine) {
391 if (lheDebug)
std::cout <<
" " << lines[iLine];
392 if (std::regex_search(lines[iLine], groups, pdfw)) {
393 unsigned int lhaID = std::stoi(groups.str(2));
394 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
395 if (pdfSetWeightIDs.empty() || ! pdfSetWeightIDs.back().maybe_add(groups.str(1),lhaID)) {
396 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
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_variation1" || groupname ==
"PDF_variation2") {
411 if (lheDebug)
std::cout <<
">>> Looks like a new-style block of PDF weights for multiple pdfs" << std::endl;
412 unsigned int lastid = 0;
413 for ( ++iLine; iLine < nLines; ++iLine) {
414 if (lheDebug)
std::cout <<
" " << lines[iLine];
415 if (std::regex_search(lines[iLine], groups, pdfw)) {
416 unsigned int id = std::stoi(groups.str(1));
417 unsigned int lhaID = std::stoi(groups.str(2));
418 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << groups.str(2) <<
" = " << lhaID << std::endl;
419 if (
id != (lastid+1) || pdfSetWeightIDs.empty()) {
420 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
422 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
425 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
426 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
427 if(!missed_weightgroup) {
429 }
else missed_weightgroup=
false;
430 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
431 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;
432 if (ismg26x) missed_weightgroup=
true;
438 if (lheDebug)
std::cout <<
">>> Looks like an old-style PDF weight for an individual pdf" << std::endl;
439 unsigned int firstLhaID =
lhaNameToID_.find(groupname)->second;
441 for ( ++iLine; iLine < nLines; ++iLine) {
442 if (lheDebug)
std::cout <<
" " << lines[iLine];
443 if (std::regex_search(lines[iLine], groups, ismg26x ? pdfwmg26x : pdfwOld)) {
444 unsigned int member = 0;
446 member = std::stoi(groups.str(2));
448 if (!groups.str(4).empty()){
449 member = std::stoi(groups.str(4));
452 unsigned int lhaID = member+firstLhaID;
453 if (lheDebug)
std::cout <<
" >>> PDF weight " << groups.str(1) <<
" for " << member <<
" = " << lhaID << std::endl;
456 pdfSetWeightIDs.emplace_back(groups.str(1),lhaID);
459 pdfSetWeightIDs.back().add(groups.str(1),lhaID);
461 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
462 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
463 if (!missed_weightgroup) {
465 }
else missed_weightgroup=
false;
466 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
467 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;
468 if (ismg26x) missed_weightgroup=
true;
474 for ( ++iLine; iLine < nLines; ++iLine) {
475 if (lheDebug)
std::cout <<
" " << lines[iLine];
476 if (std::regex_search(lines[iLine], groups, endweightgroup)) {
477 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
478 if (!missed_weightgroup){
480 }
else missed_weightgroup=
false;
481 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
482 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;
483 if (ismg26x) missed_weightgroup=
true;
489 }
else if(std::regex_search(lines[iLine], groups, weightgroupRwgt) ) {
491 if (groupname ==
"mg_reweighting") {
492 if (lheDebug)
std::cout <<
">>> Looks like a LHE weights for reweighting" << std::endl;
493 for ( ++iLine; iLine < nLines; ++iLine) {
494 if (lheDebug)
std::cout <<
" " << lines[iLine];
495 if (std::regex_search(lines[iLine], groups, rwgt)) {
497 if (lheDebug)
std::cout <<
" >>> LHE reweighting weight: " << rwgtID << std::endl;
498 if (
std::find(lheReweighingIDs.begin(), lheReweighingIDs.end(), rwgtID) == lheReweighingIDs.end()) {
500 lheReweighingIDs.emplace_back(rwgtID);
502 }
else if (std::regex_search(lines[iLine], endweightgroup)) {
503 if (lheDebug)
std::cout <<
">>> Looks like the end of a weight group" << std::endl;
504 if (!missed_weightgroup){
506 }
else missed_weightgroup=
false;
507 }
else if (std::regex_search(lines[iLine], ismg26x ? weightgroupmg26x : weightgroup)) {
508 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;
509 if (ismg26x) missed_weightgroup=
true;
520 std::sort(scaleVariationIDs.begin(), scaleVariationIDs.end());
521 if (lheDebug)
std::cout <<
"Found " << scaleVariationIDs.size() <<
" scale variations: " << std::endl;
522 std::stringstream scaleDoc; scaleDoc <<
"LHE scale variation weights (w_var / w_nominal); ";
523 for (
unsigned int isw = 0, nsw = scaleVariationIDs.size(); isw < nsw; ++isw) {
524 const auto & sw = scaleVariationIDs[isw];
525 if (isw) scaleDoc <<
"; ";
526 scaleDoc <<
"[" << isw <<
"] is " << sw.label;
527 weightChoice->scaleWeightIDs.push_back(sw.wid);
528 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());
530 if (!scaleVariationIDs.empty()) weightChoice->scaleWeightsDoc = scaleDoc.str();
534 std::cout <<
"Found " << pdfSetWeightIDs.size() <<
" PDF set errors: " << std::endl;
535 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());
540 std::cout <<
"Found " << lheReweighingIDs.size() <<
" reweighting weights" << std::endl;
542 std::copy(lheReweighingIDs.begin(), lheReweighingIDs.end(), std::back_inserter(weightChoice->rwgtIDs));
544 std::stringstream pdfDoc; pdfDoc <<
"LHE pdf variation weights (w_var / w_nominal) for LHA IDs ";
547 for (
const auto & pw : pdfSetWeightIDs) {
548 if (pw.lhaIDs.first !=
lhaid && pw.lhaIDs.first != (
lhaid+1))
continue;
549 if (pw.wids.size() == 1)
continue;
550 pdfDoc << pw.lhaIDs.first <<
" - " << pw.lhaIDs.second;
551 weightChoice->pdfWeightIDs = pw.wids;
554 pdfDoc <<
", truncated to the first " <<
maxPdfWeights_ <<
" replicas";
556 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_