StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Weights.h
1 // Weights.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2020 Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 // This file contains classes that keep track of event weights.
7 
8 #ifndef Pythia8_Weights_H
9 #define Pythia8_Weights_H
10 
11 #include "Pythia8/Basics.h"
12 #include "Pythia8/LHEF3.h"
13 #include "Pythia8/PythiaStdlib.h"
14 #include "Pythia8/SharedPointers.h"
15 
16 namespace Pythia8 {
17 
18 // Forward declare Info class for pointer
19 class Info;
20 
21 //==========================================================================
22 
23 // This is a base class to store weight information in a way that allows
24 // unified access in the structure that contains all event generation weights
25 // information (WeightContainer below). The main purpuse of this class is to
26 // supply convenience features to make defining new categories of weights easy.
27 // All weights should inherit from this base class. The specialized classes
28 // may then contain specialized functions, or only act as a glorified
29 // book-keeping struncture.
30 
31 class WeightsBase {
32 
33  public:
34 
35  // Reset all internal values;
36  virtual void clear() { return; };
37 
38  // Store the current event information.
39  virtual void bookVectors(vector<double> /*weightValues*/,
40  vector<string> /*weightNames*/) { return; }
41 
42  // Function to return processed weights to weight container, e.g. if
43  // weights should be combined before proceeding.
44  virtual void collectWeightValues(vector<double>& outputWeights,
45  double norm = 1.);
46 
47  // Similar function to return processed weight names.
48  virtual void collectWeightNames(vector<string>& outputNames);
49 
50  // Get the stored information.
51  // Direcly use storage members here in the base class,
52  // and access those through non-virtual getters.
53  // Note: NOT opting for a map data structure, since information will anyway
54  // have to be serialized in output.
55  vector<double> weightValues;
56  vector<string> weightNames;
57  string getWeightsName(int iPos) const {
58  if (iPos >= 0 && iPos < (int)weightNames.size())
59  return weightNames[iPos];
60  else return "";}
61  virtual double getWeightsValue(int iPos) const { return weightValues[iPos]; }
62  int getWeightsSize() const { return weightValues.size(); }
63 
64  // Function to create a new, synchronized, pair of weight name and value.
65  void bookWeight(string name, double defaultValue = 1.) {
66  weightNames.push_back(name);
67  weightValues.push_back(defaultValue);
68  }
69 
70  // Functions to set values of weights.
71  void setValueByIndex(int iPos, double val) {
72  weightValues[iPos] = val;
73  }
74  void setValueByName(string name, double val) {
75  // Use existing functions: Find index of name, then set by index.
76  int iPos = findIndexOfName(name);
77  setValueByIndex(iPos, val);
78  }
79 
80  // Function to find the index of a given entry in the weightNames vector,
81  // e.g., to be able to synchronize with the weightValues vector.
82  int findIndexOfName(string name) {
83  vector<string>::iterator it
84  = find(weightNames.begin(), weightNames.end(), name);
85  unsigned long int index = distance(weightNames.begin(), it);
86  if (index == weightNames.size()) return -1;
87  return distance(weightNames.begin(), it);
88  }
89 
90  // Pointers necessary for variation initialization
91  Info* infoPtr;
92 
93  void setPtrs(Info* infoPtrIn) { infoPtr = infoPtrIn; };
94 
95 };
96 
97 //==========================================================================
98 
99 // This is a short example class to collect information on parton shower
100 // weights into a container class that can be part of Weight, which
101 // in turn is part of InfoHub.
102 
104 
105  public:
106 
107  // Initialize weights (more can be booked at any time)
108  void init( bool doMerging);
109 
110  // Reset all internal values;
111  void clear();
112 
113  // Store the current event information.
114  void bookVectors(vector<double> weights, vector<string> names);
115 
116  // Functions to set values of weights.
117  void reweightValueByIndex(int iPos, double val);
118  void reweightValueByName(string name, double val);
119 
120  void replaceWhitespace( vector<string>& namesIn);
121 
122  // Variations that must be known by TimeShower and Spaceshower
123  map<int,double> varPDFplus, varPDFminus, varPDFmember;
124 
125  // Return group name (want to integrate this in weightNameVector?)
126  string getGroupName(int iGN) const;
127 
128  // Return group weight (want to integrate this in weightValueVector?)
129  double getGroupWeight(int iGW) const;
130 
131  int nVariationGroups() const { return externalVariations.size(); }
132 
133  // Initialize the weight group structure
134  void initAutomatedVariationGroups(bool = false);
135 
136  // Return list of atomic weight variations to be performed by shower
137  vector<string> getUniqueShowerVars();
138 
139  string getInitialName(int iG) const { return initialNameSave[iG]; }
140 
141  // Vectors for weight group handling
142  vector<string> externalVariations;
143  vector<vector<string> > externalVarNames;
144  vector<string> externalGroupNames;
145  vector<string> initialNameSave;
146  vector<vector<int> > externalMap;
147  int externalVariationsSize{};
148 
149  // Vector for merging requested weight handling
150  vector<vector<string>> mergingVarNames;
151  vector<double> getMuRWeightVector();
152 
153  void collectWeightNames(vector<string>& outputNames);
154  void collectWeightValues(vector<double>& outputWeights,
155  double norm = 1.);
156 };
157 
158 //==========================================================================
159 
160 // This class collects information on weights generated in the merging
161 // framework. The first weight is always required for CKKW-L, UMEPS and
162 // NLO merging. The additional weights are required for simultaneous
163 // renormalization scale variation in matrix element generation and parton
164 // shower.
165 
166 class WeightsMerging : public WeightsBase {
167 
168  public:
169 
170  // Initialize weights (more can be booked at any time)
171  void init();
172 
173  // Reset all internal values;
174  void clear();
175 
176  // Function to create a new, synchronized, pair of weight name and value.
177  void bookWeight(string name, double value, double valueFirst);
178 
179  // Store the current event information.
180  void bookVectors(vector<double> weights, vector<string> names);
181  void bookVectors(vector<double> weights,vector<double> weightsFirst,
182  vector<string> names);
183  // Modified weight getter to include first order weight
184  double getWeightsValue(int iPos) const {
185  return weightValues[iPos] - weightValuesFirst[iPos]; }
186  // Also add getters for UNLOPS-P and -PC schemes
187  double getWeightsValueP(int iPos) const {
188  return weightValuesP[iPos] - weightValuesFirstP[iPos]; }
189  double getWeightsValuePC(int iPos) const {
190  return weightValuesPC[iPos] - weightValuesFirstPC[iPos]; }
191 
192  // Functions to set values of weights.
193  void reweightValueByIndex(int iPos, double val);
194  void reweightValueByName(string name, double val);
195 
196  // Data member for first order weight
197  vector<double> weightValuesFirst;
198 
199  // Data members for UNLOPS-P and -PC
200  vector<double> weightValuesP, weightValuesPC,
201  weightValuesFirstP, weightValuesFirstPC;
202 
203  // Functions to set values of first order weights.
204  void setValueFirstByIndex(int iPos, double val);
205  void setValueFirstByName(string name, double val);
206 
207  // Functions to set values as whole vector.
208  void setValueVector(vector<double> ValueVector);
209  void setValueFirstVector(vector<double> ValueFirstVector);
210 
211  // Function telling merging which muR variations to perform
212  vector<double> getMuRVarFactors();
213 
214  // Set up mapping between LHEF variations and
215  void setLHEFvariationMapping();
216  // Corresponding vector with respective LHEF weight indices
217  map<int,int> muRVarLHEFindex;
218 
219  // Function to collect weight names
220  void collectWeightNames(vector<string>& outputNames);
221 
222  // Function collecting weight values
223  void collectWeightValues(vector<double>& outputWeights,
224  double norm = 1.);
225 
226  // Boolean to memorize if LHEF weight needs to be applied (only for NLO)
227  bool isNLO;
228 };
229 
230 //==========================================================================
231 //
232 // This is a short example class to collect information on Les Houches Event
233 // weights into a container class that can be part of Weight, which
234 // in turn is part of InfoHub.
235 
236 class WeightsLHEF : public WeightsBase {
237 
238  public:
239 
240  // Central weight, needed for normalization, set from ProcessContainer.cc
241  double centralWeight;
242 
243  // Reset all internal values;
244  void clear();
245 
246  // Store the current event information.
247  void bookVectors(vector<double> weights_detailed_vecIn,
248  vector<string> weights_detailed_name_vecIn);
249 
250  // Function to return processed weights to weight container, e.g. if
251  // weights should be combined before proceeding.
252  void collectWeightValues(vector<double>& outputWeights,
253  double norm = 1.);
254  void collectWeightNames(vector<string>& outputNames);
255 
256  // Convert weight names in MadGraph5 convention to the convention outlined
257  // in https://arxiv.org/pdf/1405.1067.pdf, page 162ff.
258  vector<string> weightnames_lhef2hepmc(
259  vector<string> weights_detailed_name_vecIn);
260 
261  void identifyVariationsFromLHAinit( map<string,LHAweight> *init_weightsIn );
262 
263  map<int,double> muRvars;
264 
265 };
266 
267 //==========================================================================
268 
269 // This is a container class to collect all event generation weight
270 // information into a wrapper which is in turn is part of InfoHub. In this
271 // way, we could avoid cluttering InfoHub.
272 
274 
275  public:
276 
277  // Default constructor only ensures that members are initialized with
278  // sensible default values.
279  WeightContainer() : weightNominal(1.0), xsecIsInit(false) {}
280 
281  // The nominal Pythia weight, in pb for lha strategy 4 and -4
282  double weightNominal;
283  void setWeightNominal( double weightNow );
284  double collectWeightNominal();
285 
286  // First example of a weight subcategory.
287  WeightsLHEF weightsLHEF;
288 
289  // Other possible sub-categories:
290  WeightsSimpleShower weightsPS;
291 
292  WeightsMerging weightsMerging;
293 
294  // Other possible sub-categories:
295  //WeightsHadronization weightInfoHadronization;
296 
297  // Functions to retrieve information stored in the subcategory members.
298  int numberOfWeights();
299  double weightValueByIndex(int key=0);
300  string weightNameByIndex(int key=0);
301 
302  // Function to return the vector of weight values, combining all weights from
303  // all subcategories.
304  // Currently, only the nominal weight and LHEF weights are
305  // considered. Internal Pythia weights should also be included eventually.
306  vector<double> weightValueVector();
307 
308  // Function to return the vector of weight names, combining all names from
309  // all subcategories, cf. weightValueVector function.
310  vector<string> weightNameVector();
311 
312  // Reset all members to default stage.
313  void clear();
314 
315  // Reset total cross section estimate
316  void clearTotal();
317 
318  // Pointers necessary for variation initialization
319  Info* infoPtr;
320 
321  // Init, for those classes that need it
322  void init( bool doMerging);
323 
324  // Function to set Pointers in weight classes
325  void initPtrs(Info* infoPtrIn);
326 
327  // Suppress AUX_ weights
328  bool doSuppressAUXweights;
329 
330  vector<double> sigmaTotal, sigmaSample, errorTotal, errorSample;
331  bool xsecIsInit;
332 
333  void initXsecVec();
334 
335  vector<double> getSampleXsec();
336  vector<double> getTotalXsec();
337  vector<double> getSampleXsecErr();
338  vector<double> getTotalXsecErr();
339 
340  // Accumulate cross section for all weights.
341  void accumulateXsec(double norm = 1.);
342 
343 };
344 
345 //==========================================================================
346 
347 } // end namespace Pythia8
348 
349 #endif // Pythia8_Weights_H