8 #include "Pythia8/Info.h"
9 #include "Pythia8/Settings.h"
10 #include "Pythia8/Weights.h"
23 void WeightsBase::collectWeightValues(vector<double>& outputWeights,
25 for (
int iwt=1; iwt < getWeightsSize(); ++iwt) {
26 double value = getWeightsValue(iwt)*norm;
27 outputWeights.push_back(value);
35 void WeightsBase::collectWeightNames(vector<string>& outputNames) {
36 for (
int iwt=1; iwt < getWeightsSize(); ++iwt) {
37 string name = getWeightsName(iwt);
38 outputNames.push_back(name);
50 void WeightsSimpleShower::clear() {
51 for (
size_t i=0; i < weightValues.size(); ++i) weightValues[i] = 1.;
57 void WeightsSimpleShower::init(
bool doMerging ) {
60 weightValues.resize(0);
61 weightNames.resize(0);
62 mergingVarNames.resize(0);
65 bookWeight(
"Baseline");
68 if (!infoPtr->settingsPtr->flag(
"UncertaintyBands:doVariations") &&
69 infoPtr->weightContainerPtr->weightsMerging.getMuRVarFactors().size()
71 infoPtr->settingsPtr->flag(
"UncertaintyBands:doVariations",
true);
73 infoPtr->settingsPtr->wvec(
"UncertaintyBands:List",vector<string>(0));
78 for (
double fac: infoPtr->weightContainerPtr->weightsMerging.
80 string stringfsr =
"fsr:murfac=" + std::to_string(fac);
81 string stringisr =
"isr:murfac=" + std::to_string(fac);
82 mergingVarNames.push_back({stringfsr,stringisr});
90 void WeightsSimpleShower::bookVectors(vector<double> weights,
91 vector<string> names) {
93 replaceWhitespace(names);
95 for (
size_t i=0; i < weights.size(); ++i) bookWeight(names[i], weights[i]);
103 void WeightsSimpleShower::replaceWhitespace( vector<string>& namesIn) {
105 for (
size_t i=0; i < namesIn.size(); ++i) {
106 string name=namesIn[i];
107 replace(name.begin(), name.end(),
' ',
'_');
116 void WeightsSimpleShower::reweightValueByIndex(
int iPos,
double val) {
117 weightValues[iPos] *= val;
122 void WeightsSimpleShower::reweightValueByName(
string name,
double val) {
124 int iPos = findIndexOfName(name);
125 reweightValueByIndex(iPos, val);
131 void WeightsSimpleShower::initAutomatedVariationGroups(
bool isISR) {
132 vector<string> variationListIn = infoPtr->settingsPtr->
133 wvec(
"UncertaintyBands:List");
134 size_t vNames = weightNames.size();
135 externalVariations.clear();
136 externalVarNames.clear();
137 externalGroupNames.clear();
139 initialNameSave.clear();
140 externalVariations.push_back(
"Baseline");
141 initialNameSave.push_back(
"Baseline");
142 for(vector<string>::const_iterator v=variationListIn.begin();
143 v != variationListIn.end(); ++v) {
146 while (line.find(
" ") == 0) line.erase( 0, 1);
149 if( isISR && ((pos = line.find(
"isr:pdf:family")) != string::npos) ) {
150 size_t posEnd = line.find(
" ",pos);
151 if( posEnd == string::npos ) posEnd = line.size();
152 for(
size_t i=0; i < vNames; ++i) {
153 string local = weightNames[i];
154 if( local.find(
"isr:pdf:member") != string::npos ) {
155 size_t iEqual = local.find(
"=")+1;
156 string nMember = local.substr(iEqual,local.size());
158 string tmpLine = line;
159 tmpLine.replace(pos,posEnd-pos,local);
160 size_t iBlank = line.find_first_of(
" ");
161 tmpLine.replace(iBlank,1,nMember);
162 externalVariations.push_back(tmpLine);
163 initialNameSave.push_back(line.substr(0,line.find_first_of(
" ")));
167 externalVariations.push_back(line);
168 initialNameSave.push_back(line.substr(0,line.find_first_of(
" ")));
171 externalVariationsSize = externalVariations.size();
172 size_t nNames = externalVariationsSize;
173 externalVarNames.resize(nNames);
174 externalVarNames[0].push_back(
"Baseline");
175 externalGroupNames.resize(nNames);
176 externalGroupNames[0]=
"Baseline";
177 for(
size_t iWeight = 0; iWeight < nNames; ++iWeight) {
178 string uVarString = toLower(externalVariations[iWeight]);
179 size_t firstBlank = uVarString.find_first_of(
" ");
180 size_t endLine = uVarString.size();
181 if( firstBlank > endLine)
continue;
182 externalGroupNames[iWeight] = uVarString.substr(0,firstBlank);
183 uVarString = uVarString.substr(firstBlank+1,endLine);
185 while ((pos = uVarString.find(
" ")) != string::npos) {
186 string token = uVarString.substr(0, pos);
187 externalVarNames[iWeight].push_back(token);
188 uVarString.erase(0, pos + 1);
190 if (uVarString ==
"" || uVarString ==
" ")
continue;
191 externalVarNames[iWeight].push_back(uVarString);
193 externalMap.resize(nNames);
194 for(
size_t iWeight = 0; iWeight < vNames; ++iWeight) {
195 for(
size_t iV = 0; iV < nNames; ++iV) {
196 for(
size_t iW = 0; iW < externalVarNames[iV].size(); ++iW) {
197 if( externalVarNames[iV][iW] == weightNames[iWeight] ) {
198 externalMap[iV].push_back(iWeight);
199 }
else if ( isISR && externalVarNames[iV][iW].find(
"isr:pdf:family")
200 != string::npos && weightNames[iWeight].find(
"isr:pdf:member")
202 externalMap[iV].push_back(iWeight);
212 string WeightsSimpleShower::getGroupName(
int iGN)
const {
213 string tmpString(
"Null");
214 if( iGN < 0 || iGN >= externalVariationsSize )
216 return externalGroupNames[iGN];
222 double WeightsSimpleShower::getGroupWeight(
int iGW)
const {
223 double tempWeight(1.0);
224 if( iGW < 0 || iGW >= externalVariationsSize )
226 for( vector<int>::const_iterator cit = externalMap[iGW].
227 begin(); cit < externalMap[iGW].end(); ++cit )
228 tempWeight *= getWeightsValue(*cit);
235 vector<string> WeightsSimpleShower::getUniqueShowerVars() {
237 vector<string> uVars = infoPtr->settingsPtr->wvec(
"UncertaintyBands:List");
238 size_t varSize = uVars.size();
239 vector<string> uniqueVars;
242 for (
size_t iWeight = 0; iWeight < varSize; ++iWeight) {
245 string uVarString = toLower(uVars[iWeight]);
246 while (uVarString.find(
" ") == 0) uVarString.erase( 0, 1);
247 int iEnd = uVarString.find(
" ", 0);
248 uVarString.erase(0,iEnd+1);
249 while (uVarString.find(
"=") != string::npos) {
250 iEnd = uVarString.find_first_of(
" ", 0);
251 if( iEnd<0 ) iEnd = uVarString.length();
252 string insertString = uVarString.substr(0,iEnd);
253 if( uniqueVars.size() == 0 ) {
254 uniqueVars.push_back(insertString);
255 }
else if ( find(uniqueVars.begin(), uniqueVars.end(), insertString)
256 == uniqueVars.end() ) {
257 uniqueVars.push_back(insertString);
259 uVarString.erase(0,iEnd+1);
264 for (vector<string> mergingVariation: mergingVarNames) {
265 for (
string varString: mergingVariation) {
266 uniqueVars.push_back(varString);
276 vector<double> WeightsSimpleShower::getMuRWeightVector() {
277 int nVarCombs = mergingVarNames.size();
278 vector<double> ret( nVarCombs, 1. );
279 for (
int iVarComb = 0; iVarComb < nVarCombs; ++iVarComb) {
280 int nVars = mergingVarNames[iVarComb].size();
281 for (
int iVar = 0; iVar < nVars; ++iVar) {
282 int index = findIndexOfName(mergingVarNames[iVarComb][iVar]);
283 if (index != -1) ret[iVarComb] *= getWeightsValue(index);
286 ret[iVarComb] /= getWeightsValue(0);
294 void WeightsSimpleShower::collectWeightNames(vector<string>& outputNames) {
295 for (
int iwt=1; iwt < getWeightsSize(); ++iwt) {
296 string name = getWeightsName(iwt);
297 outputNames.push_back(
"AUX_"+name);
299 for (
int iwtGrp = 1; iwtGrp < nVariationGroups(); ++iwtGrp) {
300 string name = getGroupName(iwtGrp);
301 outputNames.push_back(
"AUX_"+name);
309 void WeightsSimpleShower::collectWeightValues(vector<double>& outputWeights,
311 for (
int iwt=1; iwt < getWeightsSize(); ++iwt) {
312 double value = getWeightsValue(iwt)*norm;
313 outputWeights.push_back(value);
315 for (
int iwtGrp = 1; iwtGrp < nVariationGroups(); ++iwtGrp) {
316 double value = getGroupWeight(iwtGrp)*norm;
317 outputWeights.push_back(value);
327 void WeightsLHEF::clear() {
328 weightValues.resize(0);
329 weightNames.resize(0);
335 void WeightsLHEF::bookVectors(vector<double> weights_detailed_vecIn,
336 vector<string> weights_detailed_name_vecIn){
337 weightValues = weights_detailed_vecIn;
339 double norm = 1./infoPtr->eventWeightLHEF;
340 for (
double& value: weightValues)
342 weightNames = weightnames_lhef2hepmc(weights_detailed_name_vecIn);
348 void WeightsLHEF::collectWeightValues(vector<double>& ret,
double norm) {
352 for (
int iwt=0; iwt < getWeightsSize(); ++iwt) {
353 double value = getWeightsValue(iwt);
354 string name = getWeightsName(iwt);
355 if (name.find(
"MUR") == string::npos || name.find(
"MUF") == string::npos)
357 ret.push_back(value*norm);
359 for (
int iwt=0; iwt < getWeightsSize(); ++iwt) {
360 double value = getWeightsValue(iwt);
361 string name = getWeightsName(iwt);
362 if (name.find(
"MUR") != string::npos || name.find(
"MUF") != string::npos)
364 ret.push_back(value*norm);
374 void WeightsLHEF::collectWeightNames(vector<string>& ret) {
378 for (
int iwt=0; iwt < getWeightsSize(); ++iwt) {
379 string name = getWeightsName(iwt);
380 if (name.find(
"MUR") == string::npos || name.find(
"MUF") == string::npos)
382 ret.push_back(
"AUX_"+name);
384 for (
int iwt=0; iwt < getWeightsSize(); ++iwt) {
385 string name = getWeightsName(iwt);
386 if (name.find(
"MUR") != string::npos || name.find(
"MUF") != string::npos)
388 ret.push_back(
"AUX_"+name);
399 vector<string> WeightsLHEF::weightnames_lhef2hepmc(
400 vector<string> weights_detailed_name_vecIn) {
402 for (
size_t i=0; i < weights_detailed_name_vecIn.size(); ++i) {
403 string name=weights_detailed_name_vecIn[i];
404 if (name==
"1001") name=
"MUR1.0_MUF1.0";
405 if (name==
"1002") name=
"MUR1.0_MUF2.0";
406 if (name==
"1003") name=
"MUR1.0_MUF0.5";
407 if (name==
"1004") name=
"MUR2.0_MUF1.0";
408 if (name==
"1005") name=
"MUR2.0_MUF2.0";
409 if (name==
"1006") name=
"MUR2.0_MUF0.5";
410 if (name==
"1007") name=
"MUR0.5_MUF1.0";
411 if (name==
"1008") name=
"MUR0.5_MUF2.0";
412 if (name==
"1009") name=
"MUR0.5_MUF0.5";
422 void WeightsLHEF::identifyVariationsFromLHAinit( map<string,LHAweight>
426 for (map<string,LHAweight>::const_iterator it =
427 init_weightsIn->begin(); it != init_weightsIn->end(); ++it) {
428 string cont = it->second.contents;
429 double muR = 0, muF = 0;
433 while (cont.substr(0,4) !=
"muR=" && cont.substr(0,4) !=
"muF=") {
434 if (cont.find_first_of(
" ") != string::npos) {
435 cont = cont.substr(cont.find_first_of(
" ") + 1);
439 if (cont.substr(0,4) ==
"muR=") {
440 muR = stof(cont.substr(4,cont.find_first_of(
" ")));
441 cont = cont.substr(cont.find_first_of(
" ") + 1);
443 if (cont.substr(0,4) ==
"muF=") {
444 muF = stof(cont.substr(4,cont.find_first_of(
" ")));
445 cont = cont.substr(cont.find_first_of(
" ") + 1);
448 if (muR && muF)
break;
450 if (cont.find_first_of(
" ") == string::npos)
break;
453 if (muF == 1.) muRvars[index] = muR;
465 void WeightsMerging::clear() {
466 for (
size_t i=0; i < weightValues.size(); ++i) {
467 weightValues[i] = 1.;
468 weightValuesFirst[i] = 0.;
470 for (
size_t i=0; i < weightValuesP.size(); ++i) {
471 weightValuesP[i] = 1.;
472 weightValuesFirstP[i] = 0.;
473 weightValuesPC[i] = 1.;
474 weightValuesFirstPC[i] = 0.;
481 void WeightsMerging::init() {
484 weightValues.resize(0);
485 weightNames.resize(0);
486 weightValuesFirst.resize(0);
487 weightValuesP.resize(0);
488 weightValuesPC.resize(0);
489 weightValuesFirstP.resize(0);
490 weightValuesFirstPC.resize(0);
493 bookWeight(
"MUR1.0_MUF1.0", 1., 0.);
495 isNLO = (infoPtr->settingsPtr->flag(
"Merging:doUNLOPSLoop") ||
496 infoPtr->settingsPtr->flag(
"Merging:doUNLOPSSubtNLO") ||
497 infoPtr->settingsPtr->flag(
"Merging:doNL3LOOP") );
503 void WeightsMerging::bookWeight(
string name,
double value,
double valueFirst) {
504 weightNames.push_back(name);
505 weightValues.push_back(value);
506 weightValuesFirst.push_back(valueFirst);
512 void WeightsMerging::bookVectors(vector<double> weights, vector<string> names){
515 weightValues.resize(0);
516 weightNames.resize(0);
517 weightValuesFirst.resize(0);
518 weightValuesP.resize(0);
519 weightValuesPC.resize(0);
520 weightValuesFirstP.resize(0);
521 weightValuesFirstPC.resize(0);
523 for (
size_t i=0; i < weights.size(); ++i) bookWeight(names[i],
532 void WeightsMerging::bookVectors(vector<double> weights,
533 vector<double> weightsFirst, vector<string> names) {
536 weightValues.resize(0);
537 weightNames.resize(0);
538 weightValuesFirst.resize(0);
539 weightValuesP.resize(0);
540 weightValuesPC.resize(0);
541 weightValuesFirstP.resize(0);
542 weightValuesFirstPC.resize(0);
544 for (
size_t i=0; i < weights.size(); ++i) bookWeight(names[i],
545 weights[i], weightsFirst[i]);
553 void WeightsMerging::reweightValueByIndex(
int iPos,
double val) {
554 weightValues[iPos] *= val;
561 void WeightsMerging::reweightValueByName(
string name,
double val) {
563 int iPos = findIndexOfName(name);
564 reweightValueByIndex(iPos, val);
571 void WeightsMerging::setValueFirstByIndex(
int iPos,
double val) {
572 weightValuesFirst[iPos] = val;
579 void WeightsMerging::setValueFirstByName(
string name,
double val) {
581 int iPos = findIndexOfName(name);
582 setValueFirstByIndex(iPos, val);
588 void WeightsMerging::setValueVector(vector<double> valueVector) {
589 weightValues = valueVector;
595 void WeightsMerging::setValueFirstVector(vector<double> valueFirstVector) {
596 weightValuesFirst = valueFirstVector;
603 vector<double> WeightsMerging::getMuRVarFactors() {
604 vector<double> ret = infoPtr->settingsPtr->pvec(
"Merging:muRfactors");
611 void WeightsMerging::collectWeightValues(vector<double>& ret,
double norm) {
612 vector<double> showerWeights = infoPtr->weightContainerPtr->weightsPS.
613 getMuRWeightVector();
614 for (
int iwt=1; iwt < getWeightsSize(); ++iwt) {
615 double value = getWeightsValue(iwt)*norm;
616 if (getWeightsValue(0) != 0 ) value /= getWeightsValue(0);
620 if (muRVarLHEFindex.find(iwt) == muRVarLHEFindex.end()) {
621 string errormsg =
"Error in WeightsMerging::collectWeightValues: "
622 "Requested muR variation ";
623 errormsg += std::to_string(getMuRVarFactors()[iwt-1]) +
624 " not found in LHE file.";
625 infoPtr->errorMsg(errormsg);
627 value *= infoPtr->weightContainerPtr->weightsLHEF.
628 getWeightsValue(muRVarLHEFindex[iwt]);
631 value *= showerWeights[iwt-1];
632 ret.push_back(value);
636 if (weightValuesP.size()) {
637 for (
int iwt=0; iwt < getWeightsSize(); ++iwt) {
640 double valueP = getWeightsValueP(iwt)*norm;
641 double valuePC = getWeightsValuePC(iwt)*norm;
642 if (getWeightsValue(0) != 0 ) {
643 valueP /= getWeightsValue(0);
644 valuePC /= getWeightsValue(0);
650 if (muRVarLHEFindex.find(iwt) != muRVarLHEFindex.end()) {
651 double fact = infoPtr->weightContainerPtr->weightsLHEF.
652 getWeightsValue(muRVarLHEFindex[iwt]);
659 valueP *= showerWeights[iwt-1];
660 valuePC *= showerWeights[iwt-1];
662 ret.push_back(valueP);
663 ret.push_back(valuePC);
674 void WeightsMerging::collectWeightNames(vector<string>& outputNames) {
675 for (
int iwt=1; iwt < getWeightsSize(); ++iwt) {
676 string name = getWeightsName(iwt);
677 outputNames.push_back(name);
681 if (weightValuesP.size()) {
682 for (
int iwt=0; iwt < getWeightsSize(); ++iwt) {
683 string nameP = getWeightsName(iwt)+
"_SCHEMEP";
684 string namePC = getWeightsName(iwt)+
"_SCHEMEPC";
685 outputNames.push_back(nameP);
686 outputNames.push_back(namePC);
696 void WeightsMerging::setLHEFvariationMapping() {
698 map<int,double> muRvarsLHEF = infoPtr->weightContainerPtr->weightsLHEF.
700 vector<double> muRvarsMerging = getMuRVarFactors();
701 for (
unsigned int iMerVar = 0; iMerVar < muRvarsMerging.size(); ++iMerVar) {
702 for (pair<int,double> muRvarLHEF: muRvarsLHEF) {
703 if (abs(muRvarLHEF.second - muRvarsMerging[iMerVar]) < 1e-10) {
704 muRVarLHEFindex[iMerVar+1] = muRvarLHEF.first;
718 void WeightContainer::setWeightNominal(
double weightNow) {
719 weightNominal = weightNow;
726 double WeightContainer::collectWeightNominal() {
727 return weightNominal * weightsPS.getWeightsValue(0)
728 * weightsMerging.getWeightsValue(0);
735 int WeightContainer::numberOfWeights() {
737 int nMergingWeights = weightsMerging.getWeightsSize() - 1;
738 if (weightsMerging.weightValuesP.size())
739 nMergingWeights += 2*weightsMerging.weightValuesP.size();
741 int nShowerWeights = weightsPS.getWeightsSize()
742 + weightsPS.nVariationGroups()
746 if (doSuppressAUXweights)
return 1 + nMergingWeights;
747 else return (1 + weightsLHEF.getWeightsSize()
748 + nShowerWeights + nMergingWeights);
751 double WeightContainer::weightValueByIndex(
int key) {
752 vector<double> values = weightValueVector();
756 string WeightContainer::weightNameByIndex(
int key) {
757 vector<string> names = weightNameVector();
765 vector<double> WeightContainer::weightValueVector() {
769 double collWgtNom = collectWeightNominal();
770 ret.push_back(collWgtNom);
774 if (!doSuppressAUXweights) weightsLHEF.collectWeightValues(ret,collWgtNom);
775 if (!doSuppressAUXweights) weightsPS.collectWeightValues(ret,collWgtNom);
776 weightsMerging.collectWeightValues(ret,collWgtNom);
787 vector<string> WeightContainer::weightNameVector() {
791 ret.push_back(
"Weight");
794 if (!doSuppressAUXweights) weightsLHEF.collectWeightNames(ret);
795 if (!doSuppressAUXweights) weightsPS.collectWeightNames(ret);
796 weightsMerging.collectWeightNames(ret);
806 void WeightContainer::clear() {
810 weightsMerging.clear();
816 void WeightContainer::clearTotal() {
817 if (sigmaTotal.size()) {
818 sigmaTotal = vector<double>(sigmaTotal.size(),0.);
819 errorTotal = vector<double>(errorTotal.size(),0.);
826 void WeightContainer::initPtrs(Info* infoPtrIn) {
828 weightsLHEF.setPtrs(infoPtrIn);
829 weightsPS.setPtrs(infoPtrIn);
830 weightsMerging.setPtrs(infoPtrIn);
836 void WeightContainer::init(
bool doMerging ) {
837 weightsPS.init(doMerging);
838 weightsMerging.init();
839 doSuppressAUXweights = infoPtr->settingsPtr->
840 flag(
"Weights:suppressAUX");
842 sigmaSample = vector<double>(sigmaSample.size(),0.);
843 errorSample = vector<double>(errorSample.size(),0.);
850 void WeightContainer::initXsecVec() {
852 sigmaTotal = vector<double>(weightNameVector().size(),0.);
853 sigmaSample = vector<double>(weightNameVector().size(),0.);
854 errorTotal = vector<double>(weightNameVector().size(),0.);
855 errorSample = vector<double>(weightNameVector().size(),0.);
863 vector<double> WeightContainer::getSampleXsec() {
865 for (
double sigma: sigmaSample)
866 ret.push_back(sigma);
873 vector<double> WeightContainer::getSampleXsecErr() {
875 for (
double error: errorSample)
876 ret.push_back(sqrt(error));
883 vector<double> WeightContainer::getTotalXsec() {
885 for (
double sigma: sigmaTotal)
886 ret.push_back(sigma);
893 vector<double> WeightContainer::getTotalXsecErr() {
895 for (
double error: errorTotal)
896 ret.push_back(sqrt(error));
903 void WeightContainer::accumulateXsec(
double norm) {
904 if (!xsecIsInit) initXsecVec();
905 vector<double> weights = weightValueVector();
906 for (
unsigned int iWgt = 0; iWgt < weights.size(); ++iWgt) {
907 sigmaTotal[iWgt] += weights[iWgt]*norm;
908 sigmaSample[iWgt] += weights[iWgt]*norm;
909 errorTotal[iWgt] += pow2(weights[iWgt]*norm);
910 errorSample[iWgt] += pow2(weights[iWgt]*norm);