-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVinciaWeights.cc
246 lines (208 loc) · 8.71 KB
/
VinciaWeights.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// VinciaWeights.cc is a part of the PYTHIA event generator.
// Copyright (C) 2024 Peter Skands, Torbjorn Sjostrand.
// PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
// Please respect the MCnet Guidelines, see GUIDELINES for details.
// Function definitions (not found in the header) for the VinciaWeights class.
#include "Pythia8/VinciaWeights.h"
namespace Pythia8 {
using namespace VinciaConstants;
//==========================================================================
// The VinciaWeights class.
//--------------------------------------------------------------------------
// Constants.
const double VinciaWeights::PACCEPTVARMAX = 0.99;
const double VinciaWeights::MINVARWEIGHT = 0.01;
//--------------------------------------------------------------------------
// Initilize pointers.
bool VinciaWeights::initPtr(Info* infoPtrIn,
VinciaCommon* vinComPtrIn) {
infoPtr = infoPtrIn;
settingsPtr = infoPtr->settingsPtr;
vinComPtr = vinComPtrIn;
isInitPtr = true;
return true;
}
//--------------------------------------------------------------------------
// Initialize.
void VinciaWeights::init( bool doMergingIn ) {
// Check initPtr.
if (!isInitPtr) {
printOut("VinciaWeights::init","Error! pointers not initialized");
return;
}
verbose = settingsPtr->mode("Vincia:verbose");
uncertaintyBands = settingsPtr->flag("UncertaintyBands:doVariations");
varLabels.resize(0);
bookWeight("Vincia", 1.);
// Merging on/off ?
doMerging = doMergingIn;
// List of all keywords recognised by Vincia.
allKeywords.clear(); allKeywords.resize(0);
string ffKeys[3] = { ":", ":emit:", ":split:" };
string iiKeys[4] = { ":", ":emit:", ":split:", ":conv:" };
for (int i = 0; i < 3; i++) allKeywords.push_back("fsr"+ffKeys[i]+"murfac");
for (int i = 0; i < 3; i++) allKeywords.push_back("fsr"+ffKeys[i]+"cns");
for (int i = 0; i < 4; i++) allKeywords.push_back("isr"+iiKeys[i]+"murfac");
for (int i = 0; i < 4; i++) allKeywords.push_back("isr"+iiKeys[i]+"cns");
// Convert antFunTypePhys to keyword.
antFunTypeToKeyFSR.clear();
antFunTypeToKeyFSR[QQEmitFF] = "emit";
antFunTypeToKeyFSR[QGEmitFF] = "emit";
antFunTypeToKeyFSR[GQEmitFF] = "emit";
antFunTypeToKeyFSR[GGEmitFF] = "emit";
antFunTypeToKeyFSR[GXSplitFF] = "split";
antFunTypeToKeyFSR[QQEmitRF] = "emit";
antFunTypeToKeyFSR[QGEmitRF] = "emit";
antFunTypeToKeyFSR[XGSplitRF] = "split";
antFunTypeToKeyISR.clear();
antFunTypeToKeyISR[QQEmitII] = "emit";
antFunTypeToKeyISR[GQEmitII] = "emit";
antFunTypeToKeyISR[GGEmitII] = "emit";
antFunTypeToKeyISR[QXConvII] = "split";
antFunTypeToKeyISR[GXConvII] = "conv";
antFunTypeToKeyISR[QQEmitIF] = "emit";
antFunTypeToKeyISR[QGEmitIF] = "emit";
antFunTypeToKeyISR[GQEmitIF] = "emit";
antFunTypeToKeyISR[GGEmitIF] = "emit";
antFunTypeToKeyISR[QXConvIF] = "split";
antFunTypeToKeyISR[GXConvIF] = "conv";
antFunTypeToKeyISR[XGSplitIF] = "split";
// Clean up the names of requested variations.
for (int i = 0; i < (int)varLabels.size(); i++) {
int strBegin = varLabels[i].find_first_not_of(" ");
if ((i == 0) && (varLabels[i].find("{") != string::npos)) strBegin += 1;
int strEnd = varLabels[i].find_last_not_of(" ");
if ((i == (int)varLabels.size()-1) && (varLabels[i].find("}") !=
string::npos)) strEnd -= 1;
int strRange = strEnd - strBegin + 1;
varLabels[i] = toLower(varLabels[i].substr(strBegin, strRange));
}
// Parse names of requested variations and check for keywords.
varKeys.clear(); varKeys.resize(varLabels.size());
varVals.clear(); varVals.resize(varLabels.size());
for (int i = 0; i < (int)varLabels.size(); i++) {
varKeys[i].clear(); varKeys[i].resize(0);
varVals[i].clear(); varVals[i].resize(0);
string var = varLabels[i];
int iEndName = var.find(" ", 0);
string varName = var.substr(0, iEndName);
string left = var.substr(iEndName, var.length());
left = left.substr(left.find_first_not_of(" "), left.length());
varLabels[i] = varName;
while (left.length() > 1) {
int iEnd = left.find("=", 0);
int iAlt = left.find(" ", 0);
if ( (iAlt < iEnd) && (iAlt > 0) ) iEnd = iAlt;
string key = left.substr(0, iEnd);
if (1+key.find_last_not_of(" ") < key.length())
key = key.substr(0, 1+key.find_last_not_of(" "));
varKeys[i].push_back(key);
string val = left.substr(iEnd+1, left.length());
val = val.substr(val.find_first_not_of(" "), val.length());
val = val.substr(0, val.find_first_of(" "));
varVals[i].push_back(atof(val.c_str()));
left = left.substr(iEnd+1, left.length());
if (left.find_first_of(" ") >= left.length()) break;
left = left.substr(left.find_first_of(" "), left.length());
if (left.find_first_not_of(" ") >= left.length()) break;
left = left.substr(left.find_first_not_of(" "), left.length());
}
}
if (uncertaintyBands && (verbose >= Logger::REPORT)) {
printOut("VinciaWeights", "List of variations, keywords and values:");
for (int i = 0; i < (int)varLabels.size(); i++) {
cout << " " << varLabels[i] << " : ";
for (int j=0; j<(int)varVals[i].size(); j++) {
cout << " " << varKeys[i][j] << " -> " << varVals[i][j];
if (j < (int)varVals[i].size() - 1) cout << ",";
}
cout << endl;
}
}
// Safety check for non-sensible input.
if (uncertaintyBands) {
for (int i = 0; i < (int)varKeys.size(); i++) {
for (int j = 0; j < (int)varKeys[i].size(); j++) {
// Check input keywords against known ones.
bool foundValidKey = false;
for (int k = 0; k < (int)allKeywords.size(); k++) {
if (allKeywords[k] == varKeys[i][j]) {
foundValidKey = true;
// If a weight with this name does not yet exist, book one.
if (findIndexOfName(varKeys[i][j]) < 0)
bookWeight(varKeys[i][j],1.);
break;
}
}
if (!foundValidKey)
printOut("VinciaWeights", "Error! Unknown key " +
varKeys[i][j] + " found, please check!");
}
}
}
}
//--------------------------------------------------------------------------
// Scaling functions. They determine what needs to be done, to which
// weight(s), and then call the relevant base class method:
// reweightValueByIndex.
void VinciaWeights::scaleWeightVar(vector<double> pAccept, bool accept,
bool isHard) {
if (!uncertaintyBands) return;
if (getWeightsSize() <= 1) return;
// Variations only pertain to hard process and resonance decays.
if (!isHard) return;
if (accept) scaleWeightVarAccept(pAccept);
else scaleWeightVarReject(pAccept);
}
void VinciaWeights::scaleWeightVarAccept(vector<double> pAccept) {
for (int iWeight = 1; iWeight<getWeightsSize(); iWeight++) {
double pAcceptVar = pAccept[iWeight];
if (pAcceptVar > PACCEPTVARMAX) pAcceptVar = PACCEPTVARMAX;
reweightValueByIndex( iWeight, pAcceptVar/pAccept[0] );
}
}
void VinciaWeights::scaleWeightVarReject(vector<double> pAccept) {
for (int iWeight = 1; iWeight<getWeightsSize(); iWeight++) {
double pAcceptVar = pAccept[iWeight];
if (pAcceptVar > PACCEPTVARMAX) pAcceptVar = PACCEPTVARMAX;
double reWeight = (1.0-pAcceptVar)/(1.0-pAccept[0]);
if (reWeight < MINVARWEIGHT) reWeight = MINVARWEIGHT;
reweightValueByIndex( iWeight, reWeight );
}
}
void VinciaWeights::scaleWeightEnhanceAccept(double enhanceFac) {
if (enhanceFac == 1.0) return;
else reweightValueByIndex( 0, 1./enhanceFac);
}
void VinciaWeights::scaleWeightEnhanceReject(double pAcceptUnenhanced,
double enhanceFac) {
if (enhanceFac == 1.0) return;
if (enhanceFac > 1.0) {
double rRej =
(1. - pAcceptUnenhanced/enhanceFac)/(1. - pAcceptUnenhanced);
reweightValueByIndex( 0, rRej );
} else {
double rRej =
(1. - pAcceptUnenhanced)/(1. - enhanceFac*pAcceptUnenhanced);
reweightValueByIndex( 0, rRej );
}
}
//--------------------------------------------------------------------------
// Helper function for keyword evaluation
int VinciaWeights::doVarNow(string keyIn, enum AntFunType antFunTypePhys,
bool isFSR) {
// Check variation for all branching types.
string asKey = ":murfac";
string nsKey = ":cns";
string type = (isFSR ? "fsr" : "isr");
if (type + asKey == keyIn) return 1;
if (type + nsKey == keyIn) return 2;
// Check variation for specific branching type.
map<enum AntFunType, string> keyCvt = (isFSR ? antFunTypeToKeyFSR :
antFunTypeToKeyISR);
if (type + ":" + keyCvt[antFunTypePhys] + asKey == keyIn) return 1;
if (type + ":" + keyCvt[antFunTypePhys] + nsKey == keyIn) return 2;
return -1;
}
//==========================================================================
} // end namespace Pythia8