File indexing completed on 2025-09-18 09:25:43
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef Pythia8_Weights_H
0009 #define Pythia8_Weights_H
0010
0011 #include "Pythia8/Basics.h"
0012 #include "Pythia8/LHEF3.h"
0013 #include "Pythia8/PythiaStdlib.h"
0014 #include "Pythia8/SharedPointers.h"
0015
0016 namespace Pythia8 {
0017
0018
0019 class History;
0020 class Info;
0021 class PartonLevel;
0022 class Merging;
0023 class WeightContainer;
0024 class StringFlav;
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 class WeightsBase {
0037
0038 public:
0039
0040
0041 friend class History;
0042 friend class PartonLevel;
0043
0044
0045 virtual void init() {weightValues.resize(0); weightNames.resize(0);
0046 bookWeight("Baseline");}
0047 virtual void init(bool) {}
0048
0049
0050 virtual void clear() {fill(weightValues.begin(), weightValues.end(), 1.);}
0051
0052
0053
0054 virtual void bookVectors(vector<double> weights, vector<string> names) {
0055 for (int i = 0; i < (int)weights.size(); ++i) {
0056 replace(names[i].begin(), names[i].end(), ' ', '_');
0057 bookWeight(names[i], weights[i]);
0058 }
0059 }
0060
0061
0062
0063 virtual void collectWeightValues(vector<double>& outputWeights,
0064 double norm = 1.);
0065
0066
0067 virtual void collectWeightNames(vector<string>& outputNames);
0068
0069
0070
0071
0072
0073
0074
0075
0076 string getWeightsName(int iPos) const {
0077 string name = iPos >= 0
0078 && iPos < (int)weightNames.size() ? weightNames[iPos] : "";
0079 if (name.find(":") != string::npos)
0080 replace(name.begin(), name.end(), ':', '.');
0081 return name == "" ? to_string(iPos) : name;}
0082 virtual double getWeightsValue(int iPos) const { return weightValues[iPos]; }
0083 int getWeightsSize() const { return weightValues.size(); }
0084
0085
0086 void bookWeight(string name, double defaultValue = 1.) {
0087
0088 if (findIndexOfName(name) != -1) setValueByName(name, defaultValue);
0089 else {
0090 weightNames.push_back(name);
0091 weightValues.push_back(defaultValue);
0092 }
0093 }
0094
0095
0096 void setValueByIndex(int iPos, double val) {
0097 if (iPos < 0 || iPos >= (int)weightValues.size()) return;
0098 weightValues[iPos] = val;
0099 }
0100 void setValueByName(string name, double val) {
0101 setValueByIndex(findIndexOfName(name), val);}
0102
0103
0104 virtual void reweightValueByIndex(int iPos, double val) {
0105 if (iPos < 0 || iPos >= (int)weightValues.size()) return;
0106 weightValues[iPos] *= val;
0107 };
0108 virtual void reweightValueByName(string name, double val) {
0109
0110 int iPos = findIndexOfName(name);
0111 reweightValueByIndex(iPos, val);
0112 }
0113
0114
0115
0116 int findIndexOfName(string name) {
0117 vector<string>::iterator it
0118 = find(weightNames.begin(), weightNames.end(), name);
0119 unsigned long int index = distance(weightNames.begin(), it);
0120 if (index == weightNames.size()) return -1;
0121 return distance(weightNames.begin(), it);
0122 }
0123
0124
0125 void setPtrs(Info* infoPtrIn) { infoPtr = infoPtrIn; }
0126
0127 protected:
0128
0129
0130 void parse(string wvecKey, map<string, map<string, double> > &dct);
0131
0132
0133 vector<double> weightValues;
0134 vector<string> weightNames;
0135
0136
0137 Info* infoPtr{};
0138
0139 };
0140
0141
0142
0143
0144
0145 class WeightsShower : public WeightsBase {
0146
0147 public:
0148
0149
0150 void init(bool) override {}
0151
0152
0153 virtual void initWeightGroups(bool = false) {}
0154 virtual int nWeightGroups() const {return 0;}
0155 virtual string getGroupName( int ) const {return "";}
0156 virtual double getGroupWeight( int ) const {return 1.;}
0157
0158 };
0159
0160
0161
0162
0163
0164 class WeightsSimpleShower : public WeightsShower {
0165
0166 public:
0167
0168
0169 void init( bool doMerging) override;
0170
0171
0172
0173 void collectWeightNames(vector<string>& outputNames) override;
0174 void collectWeightValues(vector<double>& outputWeights,
0175 double norm = 1.) override;
0176
0177
0178 void initWeightGroups(bool = false) override;
0179
0180
0181 string getGroupName(int iGN) const override;
0182
0183
0184 double getGroupWeight(int iGW) const override;
0185 int nWeightGroups() const override { return externalVariations.size(); }
0186
0187
0188 bool initUniqueShowerVars();
0189
0190
0191 void setEnhancedTrial( double pTIn, double wtIn) { pTEnhanced = pTIn;
0192 wtEnhanced = wtIn; }
0193 double getEnhancedTrialPT() { return pTEnhanced;}
0194 double getEnhancedTrialWeight() { return wtEnhanced;}
0195
0196
0197 vector<string> getUniqueShowerVars() { return uniqueShowerVars; }
0198 vector<string> getUniqueShowerVars(vector<string> keys);
0199
0200
0201 bool initEnhanceFactors();
0202 unordered_map<string,double> getEnhanceFactors() { return enhanceFactors; }
0203
0204 string getInitialName(int iG) const { return initialNameSave[iG]; }
0205
0206
0207 map<int,double> varPDFplus, varPDFminus, varPDFmember;
0208
0209
0210 vector<string> uniqueShowerVars;
0211 unordered_map<string,double> enhanceFactors;
0212
0213
0214 vector<string> externalVariations;
0215 vector<vector<string> > externalVarNames;
0216 vector<string> externalGroupNames;
0217 vector<string> initialNameSave;
0218 vector<vector<int> > externalMap;
0219 int externalVariationsSize{};
0220
0221
0222 vector<double> getMuRWeightVector();
0223 vector<vector<string> > mergingVarNames;
0224 double pTEnhanced{}, wtEnhanced{};
0225
0226 };
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 class WeightsMerging : public WeightsBase {
0237
0238 public:
0239
0240
0241 friend class Merging;
0242 friend class WeightContainer;
0243
0244
0245 void init() override;
0246
0247
0248 void clear() override;
0249
0250
0251 void bookWeight(string name, double value, double valueFirst);
0252
0253
0254 void bookVectors(vector<double> weights, vector<string> names) override;
0255 void bookVectors(vector<double> weights, vector<double> weightsFirst,
0256 vector<string> names);
0257
0258 double getWeightsValue(int iPos) const override {
0259 return weightValues[iPos] - weightValuesFirst[iPos]; }
0260
0261 double getWeightsValueP(int iPos) const {
0262 return weightValuesP[iPos] - weightValuesFirstP[iPos]; }
0263 double getWeightsValuePC(int iPos) const {
0264 return weightValuesPC[iPos] - weightValuesFirstPC[iPos]; }
0265
0266
0267 void reweightValueByIndex(int iPos, double val) override;
0268 void reweightValueByName(string name, double val) override;
0269
0270
0271 void setValueFirstByIndex(int iPos, double val);
0272 void setValueFirstByName(string name, double val);
0273
0274
0275 void setValueVector(vector<double> ValueVector);
0276 void setValueFirstVector(vector<double> ValueFirstVector);
0277
0278
0279 vector<double> getMuRVarFactors();
0280
0281
0282 void setLHEFvariationMapping();
0283
0284
0285 void collectWeightNames(vector<string>& outputNames) override;
0286
0287
0288 void collectWeightValues(vector<double>& outputWeights,
0289 double norm = 1.) override;
0290
0291 protected:
0292
0293
0294 map<int,int> muRVarLHEFindex;
0295
0296
0297 vector<double> weightValuesFirst;
0298
0299
0300 vector<double> weightValuesP, weightValuesPC,
0301 weightValuesFirstP, weightValuesFirstPC;
0302
0303
0304 bool isNLO;
0305
0306 };
0307
0308
0309
0310
0311
0312
0313
0314 class WeightsLHEF : public WeightsBase {
0315
0316 public:
0317
0318
0319 friend class WeightsMerging;
0320
0321
0322 double centralWeight;
0323
0324
0325 void clear() override;
0326
0327
0328 void bookVectors(vector<double> weights, vector<string> names) override;
0329
0330
0331
0332 void collectWeightValues(vector<double>& outputWeights,
0333 double norm = 1.) override;
0334 void collectWeightNames(vector<string>& outputNames) override;
0335
0336
0337
0338 vector<string> convertNames(vector<string> names);
0339
0340 void identifyVariationsFromLHAinit(map<string,LHAweight>* weights);
0341
0342 protected:
0343
0344
0345 map<int,double> muRvars;
0346
0347 };
0348
0349
0350
0351
0352
0353
0354 class WeightsFragmentation : public WeightsBase {
0355
0356 public:
0357
0358
0359 friend class StringFlav;
0360
0361
0362 void init() override;
0363
0364
0365 void clear() override {
0366 WeightsBase::clear(); fill(flavBreaks.begin(), flavBreaks.end(), 0);}
0367
0368 int nWeightGroups() const {return externalGroupNames.size();}
0369
0370 string getGroupName(int iGN) const {return iGN < 0 || iGN >= nWeightGroups()
0371 ? "Null" : externalGroupNames[iGN];}
0372
0373
0374 double getGroupWeight(int iGW) const {
0375 if (iGW < 0 || iGW >= nWeightGroups()) return 1.0;
0376 double wgt(1.);
0377 for (const int &iWgt : externalMap[iGW]) wgt *= getWeightsValue(iWgt);
0378 return wgt;}
0379
0380
0381
0382 void collectWeightNames(vector<string>& outputNames) override;
0383 void collectWeightValues(vector<double>& outputWeights,
0384 double norm = 1.) override;
0385
0386
0387 vector<double> flavParms(double xi, double rho, double x, double y);
0388
0389
0390 double flavWeight(const vector<double>& parms) {
0391 return flavWeight(parms, flavBreaks);}
0392 double flavWeight(const vector<double>& parms, const vector<int>& breaks);
0393
0394
0395 vector<map<vector<double>, int> > weightParms{};
0396 vector<string> externalGroupNames{};
0397 vector<vector<int> > externalMap{};
0398
0399
0400 vector<int> flavBreaks;
0401
0402
0403 enum FactIndex{Z, Flav, PT};
0404
0405 private:
0406
0407
0408 const vector<vector< pair<string, string> > > keyOrder{
0409 {{"frag:alund", "StringZ:aLund"}, {"frag:blund", "StringZ:bLund"},
0410 {"frag:rfactc", "StringZ:rFactC"}, {"frag:rfactb", "StringZ:rFactB"}},
0411 {{"frag:xi", "StringFlav:ProbQQtoQ"}, {"frag:rho", "StringFlav:ProbStoUD"},
0412 {"frag:x", "StringFlav:ProbSQtoQQ"},
0413 {"frag:y", "StringFlav:ProbQQ1toQQ0"}},
0414 {{"frag:ptsigma", "StringPT:sigma"}}};
0415
0416
0417 const vector<int> flavIdxs{0, 1, 2, 3, 6};
0418
0419
0420 vector<double> flavBase;
0421
0422
0423 void flavCount(int idIn, bool early, bool noChoice);
0424
0425 };
0426
0427
0428
0429
0430
0431
0432
0433 class WeightContainer {
0434
0435 public:
0436
0437
0438
0439 WeightContainer() : weightNominal(1.0), xsecIsInit(false) {}
0440
0441
0442 double weightNominal;
0443 void setWeightNominal( double weightNow );
0444 double collectWeightNominal();
0445
0446
0447 WeightsLHEF weightsLHEF{};
0448
0449
0450
0451
0452
0453 WeightsShower* weightsShowerPtr{};
0454 WeightsSimpleShower weightsSimpleShower{};
0455
0456
0457 WeightsMerging weightsMerging{};
0458
0459
0460 WeightsFragmentation weightsFragmentation{};
0461
0462
0463 WeightsBase weightsUserHooks{};
0464
0465
0466 int numberOfWeights();
0467 double weightValueByIndex(int key = 0);
0468 string weightNameByIndex(int key = 0);
0469
0470
0471
0472
0473
0474 vector<double> weightValueVector();
0475
0476
0477
0478 vector<string> weightNameVector();
0479
0480
0481 void clear();
0482
0483
0484 void clearTotal();
0485
0486
0487 void init(bool doMerging);
0488
0489
0490 void initPtrs(Info* infoPtrIn);
0491
0492
0493 void initXsecVec();
0494
0495
0496 vector<double> getSampleXsec();
0497 vector<double> getTotalXsec();
0498 vector<double> getSampleXsecErr();
0499 vector<double> getTotalXsecErr();
0500
0501
0502 void accumulateXsec(double norm = 1.);
0503
0504 private:
0505
0506
0507 Info* infoPtr{};
0508
0509
0510 bool doSuppressAUXweights{};
0511
0512
0513 vector<double> sigmaTotal, sigmaSample, errorTotal, errorSample;
0514 bool xsecIsInit;
0515
0516 };
0517
0518
0519
0520 }
0521
0522 #endif