StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Settings.h
1 // Settings.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2018 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 // Header file for the settings database.
7 // Flag: helper class with bool flags.
8 // Mode: helper class with int modes.
9 // Parm: (short for parameter) helper class with double parameters.
10 // Word: helper class with string words.
11 // FVec: vector of Flags (bools).
12 // MVec: vector of Modes (integers).
13 // PVec: vector of Parms (doubles).
14 // WVec: vector of Words (strings).
15 // Settings: maps of flags, modes, parms and words with input/output.
16 
17 #ifndef Pythia8_Settings_H
18 #define Pythia8_Settings_H
19 
20 #include "Pythia8/Info.h"
21 #include "Pythia8/PythiaStdlib.h"
22 
23 namespace Pythia8 {
24 
25 //==========================================================================
26 
27 // Class for bool flags.
28 
29 class Flag {
30 
31 public:
32 
33  // Constructor
34  Flag(string nameIn = " ", bool defaultIn = false) : name(nameIn),
35  valNow(defaultIn) , valDefault(defaultIn) { }
36 
37  // Data members.
38  string name;
39  bool valNow, valDefault;
40 
41 };
42 
43 //==========================================================================
44 
45 // Class for integer modes.
46 
47 class Mode {
48 
49 public:
50 
51  // Constructor
52  Mode(string nameIn = " ", int defaultIn = 0, bool hasMinIn = false,
53  bool hasMaxIn = false, int minIn = 0, int maxIn = 0,
54  bool optOnlyIn = false) : name(nameIn), valNow(defaultIn),
55  valDefault(defaultIn), hasMin(hasMinIn), hasMax(hasMaxIn),
56  valMin(minIn), valMax(maxIn), optOnly(optOnlyIn) { }
57 
58  // Data members.
59  string name;
60  int valNow, valDefault;
61  bool hasMin, hasMax;
62  int valMin, valMax;
63  bool optOnly;
64 
65 };
66 
67 //==========================================================================
68 
69 // Class for double parms (where parm is shorthand for parameter).
70 
71 class Parm {
72 
73 public:
74 
75  // Constructor
76  Parm(string nameIn = " ", double defaultIn = 0.,
77  bool hasMinIn = false, bool hasMaxIn = false, double minIn = 0.,
78  double maxIn = 0.) : name(nameIn), valNow(defaultIn),
79  valDefault(defaultIn), hasMin(hasMinIn), hasMax(hasMaxIn),
80  valMin(minIn), valMax(maxIn) { }
81 
82  // Data members.
83  string name;
84  double valNow, valDefault;
85  bool hasMin, hasMax;
86  double valMin, valMax;
87 
88 };
89 
90 //==========================================================================
91 
92 // Class for string words.
93 
94 class Word {
95 
96 public:
97 
98  // Constructor
99  Word(string nameIn = " ", string defaultIn = " ") : name(nameIn),
100  valNow(defaultIn) , valDefault(defaultIn) { }
101 
102  // Data members.
103  string name, valNow, valDefault;
104 
105 };
106 
107 //==========================================================================
108 
109 // Class for vector of bool flags.
110 
111 class FVec {
112 
113 public:
114 
115  // Constructor
116  FVec(string nameIn = " ", vector<bool> defaultIn = vector<bool>(1, false)) :
117  name(nameIn), valNow(defaultIn) , valDefault(defaultIn) { }
118 
119  // Data members.
120  string name;
121  vector<bool> valNow, valDefault;
122 
123 };
124 
125 //==========================================================================
126 
127 // Class for vector of integers.
128 
129 class MVec {
130 
131 public:
132 
133  // Constructor
134  MVec(string nameIn = " ", vector<int> defaultIn = vector<int>(1, 0),
135  bool hasMinIn = false, bool hasMaxIn = false, int minIn = 0,
136  int maxIn = 0) : name(nameIn), valNow(defaultIn),
137  valDefault(defaultIn), hasMin(hasMinIn), hasMax(hasMaxIn),
138  valMin(minIn), valMax(maxIn) { }
139 
140  // Data members.
141  string name;
142  vector<int> valNow, valDefault;
143  bool hasMin, hasMax;
144  int valMin, valMax;
145 
146 };
147 
148 //==========================================================================
149 
150 // Class for vector of doubles.
151 
152 class PVec {
153 
154 public:
155 
156  // Constructor
157  PVec(string nameIn = " ", vector<double> defaultIn = vector<double>(1, 0.),
158  bool hasMinIn = false, bool hasMaxIn = false, double minIn = 0.,
159  double maxIn = 0.) : name(nameIn), valNow(defaultIn),
160  valDefault(defaultIn), hasMin(hasMinIn), hasMax(hasMaxIn),
161  valMin(minIn), valMax(maxIn) { }
162 
163  // Data members.
164  string name;
165  vector<double> valNow, valDefault;
166  bool hasMin, hasMax;
167  double valMin, valMax;
168 
169 };
170 
171 //==========================================================================
172 
173 // Class for vector of strings.
174 
175 class WVec {
176 
177 public:
178 
179  // Constructor
180  WVec(string nameIn = " ", vector<string> defaultIn = vector<string>(1, " "))
181  : name(nameIn), valNow(defaultIn) , valDefault(defaultIn) { }
182 
183  // Data members.
184  string name;
185  vector<string> valNow, valDefault;
186 
187 };
188 
189 //==========================================================================
190 
191 // This class holds info on flags (bool), modes (int), parms (double),
192 // words (string), fvecs (vector of bool), mvecs (vector of int),
193 // pvecs (vector of double) and wvecs (vector of string).
194 
195 class Settings {
196 
197 public:
198 
199  // Constructor.
200  Settings() : isInit(false), readingFailedSave(false), lineSaved(false) {}
201 
202  // Initialize Info pointer.
203  void initPtr(Info* infoPtrIn) {infoPtr = infoPtrIn;}
204 
205  // Read in database from specific file.
206  bool init(string startFile = "../share/Pythia8/xmldoc/Index.xml",
207  bool append = false) ;
208 
209  // Read in database from stream.
210  bool init(istream& is, bool append = false) ;
211 
212  // Overwrite existing database by reading from specific file.
213  bool reInit(string startFile = "../share/Pythia8/xmldoc/Index.xml") ;
214 
215  // Read in one update from a single line.
216  bool readString(string line, bool warn = true) ;
217 
218  // Write updates or everything to user-defined file or to stream.
219  bool writeFile(string toFile, bool writeAll = false) ;
220  bool writeFile(ostream& os = cout, bool writeAll = false) ;
221  bool writeFileXML(ostream& os = cout) ;
222 
223  // Print out table of database, either all or only changed ones,
224  // or ones containing a given string.
225  void listAll() { list( true, false, " "); }
226  void listChanged() { list (false, false, " "); }
227  void list(string match) { list (false, true, match); }
228 
229  // Give back current value(s) as a string, whatever the type.
230  string output(string keyIn, bool fullLine = true);
231 
232  // Retrieve readString history (e.g., for inspection). Everything
233  // (subrun=-999), up to first subrun (=-1), or subrun-specific (>=0).
234  vector<string> getReadHistory(int subrun=-999) {
235  if (subrun == -999) return readStringHistory;
236  else if (readStringSubrun.find(subrun) != readStringSubrun.end())
237  return readStringSubrun[subrun];
238  else return vector<string>();
239  }
240 
241  // Reset all values to their defaults.
242  void resetAll() ;
243 
244  // Query existence of an entry.
245  bool isFlag(string keyIn) {
246  return (flags.find(toLower(keyIn)) != flags.end()); }
247  bool isMode(string keyIn) {
248  return (modes.find(toLower(keyIn)) != modes.end()); }
249  bool isParm(string keyIn) {
250  return (parms.find(toLower(keyIn)) != parms.end()); }
251  bool isWord(string keyIn) {
252  return (words.find(toLower(keyIn)) != words.end()); }
253  bool isFVec(string keyIn) {
254  return (fvecs.find(toLower(keyIn)) != fvecs.end()); }
255  bool isMVec(string keyIn) {
256  return (mvecs.find(toLower(keyIn)) != mvecs.end()); }
257  bool isPVec(string keyIn) {
258  return (pvecs.find(toLower(keyIn)) != pvecs.end()); }
259  bool isWVec(string keyIn) {
260  return (wvecs.find(toLower(keyIn)) != wvecs.end()); }
261 
262  // Add new entry.
263  void addFlag(string keyIn, bool defaultIn) {
264  flags[toLower(keyIn)] = Flag(keyIn, defaultIn); }
265  void addMode(string keyIn, int defaultIn, bool hasMinIn,
266  bool hasMaxIn, int minIn, int maxIn, bool optOnlyIn = false) {
267  modes[toLower(keyIn)] = Mode(keyIn, defaultIn, hasMinIn, hasMaxIn,
268  minIn, maxIn, optOnlyIn); }
269  void addParm(string keyIn, double defaultIn, bool hasMinIn,
270  bool hasMaxIn, double minIn, double maxIn) { parms[toLower(keyIn)]
271  = Parm(keyIn, defaultIn, hasMinIn, hasMaxIn, minIn, maxIn); }
272  void addWord(string keyIn, string defaultIn) {
273  words[toLower(keyIn)] = Word(keyIn, defaultIn); }
274  void addFVec(string keyIn, vector<bool> defaultIn) {
275  fvecs[toLower(keyIn)] = FVec(keyIn, defaultIn); }
276  void addMVec(string keyIn, vector<int> defaultIn, bool hasMinIn,
277  bool hasMaxIn, int minIn, int maxIn) { mvecs[toLower(keyIn)]
278  = MVec(keyIn, defaultIn, hasMinIn, hasMaxIn, minIn, maxIn); }
279  void addPVec(string keyIn, vector<double> defaultIn, bool hasMinIn,
280  bool hasMaxIn, double minIn, double maxIn) { pvecs[toLower(keyIn)]
281  = PVec(keyIn, defaultIn, hasMinIn, hasMaxIn, minIn, maxIn); }
282  void addWVec(string keyIn, vector<string> defaultIn) {
283  wvecs[toLower(keyIn)] = WVec(keyIn, defaultIn); }
284 
285  // Give back current value, with check that key exists.
286  bool flag(string keyIn);
287  int mode(string keyIn);
288  double parm(string keyIn);
289  string word(string keyIn);
290  vector<bool> fvec(string keyIn);
291  vector<int> mvec(string keyIn);
292  vector<double> pvec(string keyIn);
293  vector<string> wvec(string keyIn);
294 
295  // Give back default value, with check that key exists.
296  bool flagDefault(string keyIn);
297  int modeDefault(string keyIn);
298  double parmDefault(string keyIn);
299  string wordDefault(string keyIn);
300  vector<bool> fvecDefault(string keyIn);
301  vector<int> mvecDefault(string keyIn);
302  vector<double> pvecDefault(string keyIn);
303  vector<string> wvecDefault(string keyIn);
304 
305  // Give back a map of all entries whose names match the string "match".
306  map<string, Flag> getFlagMap(string match);
307  map<string, Mode> getModeMap(string match);
308  map<string, Parm> getParmMap(string match);
309  map<string, Word> getWordMap(string match);
310  map<string, FVec> getFVecMap(string match);
311  map<string, MVec> getMVecMap(string match);
312  map<string, PVec> getPVecMap(string match);
313  map<string, WVec> getWVecMap(string match);
314 
315  // Change current value, respecting limits.
316  void flag(string keyIn, bool nowIn, bool force = false);
317  bool mode(string keyIn, int nowIn, bool force = false);
318  void parm(string keyIn, double nowIn, bool force = false);
319  void word(string keyIn, string nowIn, bool force = false);
320  void fvec(string keyIn, vector<bool> nowIn, bool force = false);
321  void mvec(string keyIn, vector<int> nowIn, bool force = false);
322  void pvec(string keyIn, vector<double> nowIn, bool force = false);
323  void wvec(string keyIn, vector<string> nowIn, bool force = false);
324 
325  // Methods kept for backwards compatability with 8.223 and earlier.
326  // (To be removed in next major release.)
327  void forceMode(string keyIn, int nowIn) {mode(keyIn,nowIn,true);}
328  void forceParm(string keyIn, double nowIn) {parm(keyIn,nowIn,true);}
329  void forceMVec(string keyIn, vector<int> nowIn) {mvec(keyIn,nowIn,true);}
330  void forcePVec(string keyIn, vector<double> nowIn) {pvec(keyIn,nowIn,true);}
331 
332  // Restore current value to default.
333  void resetFlag(string keyIn);
334  void resetMode(string keyIn);
335  void resetParm(string keyIn);
336  void resetWord(string keyIn);
337  void resetFVec(string keyIn);
338  void resetMVec(string keyIn);
339  void resetPVec(string keyIn);
340  void resetWVec(string keyIn);
341 
342  // Check initialisation status.
343  bool getIsInit() {return isInit;}
344 
345  // Keep track whether any readings have failed, invalidating run setup.
346  bool readingFailed() {return readingFailedSave;}
347 
348  // Check whether input openend with { not yet closed with }.
349  bool unfinishedInput() {return lineSaved;}
350 
351  private:
352 
353  // Pointer to various information on the generation.
354  Info* infoPtr;
355 
356  // Map for bool flags.
357  map<string, Flag> flags;
358 
359  // Map for integer modes.
360  map<string, Mode> modes;
361 
362  // Map for double parms.
363  map<string, Parm> parms;
364 
365  // Map for string words.
366  map<string, Word> words;
367 
368  // Map for vectors of bool.
369  map<string, FVec> fvecs;
370 
371  // Map for vectors of int.
372  map<string, MVec> mvecs;
373 
374  // Map for vectors of double.
375  map<string, PVec> pvecs;
376 
377  // Map for vectors of string.
378  map<string, WVec> wvecs;
379 
380  // Flags that initialization has been performed; whether any failures.
381  bool isInit, readingFailedSave;
382 
383  // Store temporary line when searching for continuation line.
384  bool lineSaved;
385  string savedLine;
386 
387  // Stored history of readString statements (common and by subrun).
388  vector<string> readStringHistory;
389  map<int, vector<string> > readStringSubrun;
390 
391  // Print out table of database, called from listAll and listChanged.
392  void list(bool doListAll, bool doListString, string match);
393 
394  // Master switch for program printout.
395  void printQuiet(bool quiet);
396 
397  // Restore settings used in tunes to e+e- and pp/ppbar data.
398  void resetTuneEE();
399  void resetTunePP();
400 
401  // Initialize tunes to e+e- and pp/ppbar data.
402  void initTuneEE(int eeTune);
403  void initTunePP(int ppTune);
404 
405  // Useful functions for string handling.
406  bool boolString(string tag);
407  string attributeValue(string line, string attribute);
408  bool boolAttributeValue(string line, string attribute);
409  int intAttributeValue(string line, string attribute);
410  double doubleAttributeValue(string line, string attribute);
411  vector<bool> boolVectorAttributeValue(string line, string attribute);
412  vector<int> intVectorAttributeValue(string line, string attribute);
413  vector<double> doubleVectorAttributeValue(string line, string attribute);
414  vector<string> stringVectorAttributeValue(string line, string attribute);
415 
416 };
417 
418 //==========================================================================
419 
420 } // end namespace Pythia8
421 
422 #endif // Pythia8_Settings_H