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) 2014 Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL version 2, 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 // MVec: vector of Modes (integers).
12 // PVec: vector of Parms (doubles).
13 // Settings: maps of flags, modes, parms and words with input/output.
14 
15 #ifndef Pythia8_Settings_H
16 #define Pythia8_Settings_H
17 
18 #include "Pythia8/Info.h"
19 #include "Pythia8/PythiaStdlib.h"
20 
21 namespace Pythia8 {
22 
23 //==========================================================================
24 
25 // Class for bool flags.
26 
27 class Flag {
28 
29 public:
30 
31  // Constructor
32  Flag(string nameIn = " ", bool defaultIn = false) : name(nameIn),
33  valNow(defaultIn) , valDefault(defaultIn) { }
34 
35  // Data members.
36  string name;
37  bool valNow, valDefault;
38 
39 };
40 
41 //==========================================================================
42 
43 // Class for integer modes.
44 
45 class Mode {
46 
47 public:
48 
49  // Constructor
50  Mode(string nameIn = " ", int defaultIn = 0, bool hasMinIn = false,
51  bool hasMaxIn = false, int minIn = 0, int maxIn = 0) : name(nameIn),
52  valNow(defaultIn), valDefault(defaultIn), hasMin(hasMinIn),
53  hasMax(hasMaxIn), valMin(minIn), valMax(maxIn) { }
54 
55  // Data members.
56  string name;
57  int valNow, valDefault;
58  bool hasMin, hasMax;
59  int valMin, valMax;
60 
61 };
62 
63 //==========================================================================
64 
65 // Class for double parms (where parm is shorthand for parameter).
66 
67 class Parm {
68 
69 public:
70 
71  // Constructor
72  Parm(string nameIn = " ", double defaultIn = 0.,
73  bool hasMinIn = false, bool hasMaxIn = false, double minIn = 0.,
74  double maxIn = 0.) : name(nameIn), valNow(defaultIn),
75  valDefault(defaultIn), hasMin(hasMinIn), hasMax(hasMaxIn),
76  valMin(minIn), valMax(maxIn) { }
77 
78  // Data members.
79  string name;
80  double valNow, valDefault;
81  bool hasMin, hasMax;
82  double valMin, valMax;
83 
84 };
85 
86 //==========================================================================
87 
88 // Class for string words.
89 
90 class Word {
91 
92 public:
93 
94  // Constructor
95  Word(string nameIn = " ", string defaultIn = " ") : name(nameIn),
96  valNow(defaultIn) , valDefault(defaultIn) { }
97 
98  // Data members.
99  string name, valNow, valDefault;
100 
101 };
102 
103 //==========================================================================
104 
105 // Class for vector of bool flags.
106 
107 class FVec {
108 
109 public:
110 
111  // Constructor
112  FVec(string nameIn = " ", vector<bool> defaultIn = vector<bool>(1, false)) :
113  name(nameIn), valNow(defaultIn) , valDefault(defaultIn) { }
114 
115  // Data members.
116  string name;
117  vector<bool> valNow, valDefault;
118 
119 };
120 
121 //==========================================================================
122 
123 // Class for vector of integers.
124 
125 class MVec {
126 
127 public:
128 
129  // Constructor
130  MVec(string nameIn = " ", vector<int> defaultIn = vector<int>(1, 0),
131  bool hasMinIn = false, bool hasMaxIn = false, int minIn = 0,
132  int maxIn = 0) : name(nameIn), valNow(defaultIn),
133  valDefault(defaultIn), hasMin(hasMinIn), hasMax(hasMaxIn),
134  valMin(minIn), valMax(maxIn) { }
135 
136  // Data members.
137  string name;
138  vector<int> valNow, valDefault;
139  bool hasMin, hasMax;
140  int valMin, valMax;
141 
142 };
143 
144 //==========================================================================
145 
146 // Class for vector of doubles.
147 
148 class PVec {
149 
150 public:
151 
152  // Constructor
153  PVec(string nameIn = " ", vector<double> defaultIn = vector<double>(1, 0.),
154  bool hasMinIn = false, bool hasMaxIn = false, double minIn = 0.,
155  double maxIn = 0.) : name(nameIn), valNow(defaultIn),
156  valDefault(defaultIn), hasMin(hasMinIn), hasMax(hasMaxIn),
157  valMin(minIn), valMax(maxIn) { }
158 
159  // Data members.
160  string name;
161  vector<double> valNow, valDefault;
162  bool hasMin, hasMax;
163  double valMin, valMax;
164 
165 };
166 
167 //==========================================================================
168 
169 // This class holds info on flags (bool), modes (int), parms (double),
170 // words (string), fvecs (vector of bool), mvecs (vector of int) and pvecs
171 // (vector of double).
172 
173 class Settings {
174 
175 public:
176 
177  // Constructor.
178  Settings() : isInit(false), readingFailedSave(false) {}
179 
180  // Initialize Info pointer.
181  void initPtr(Info* infoPtrIn) {infoPtr = infoPtrIn;}
182 
183  // Read in database from specific file.
184  bool init(string startFile = "../xmldoc/Index.xml", bool append = false,
185  ostream& os = cout) ;
186 
187  // Overwrite existing database by reading from specific file.
188  bool reInit(string startFile = "../xmldoc/Index.xml", ostream& os = cout) ;
189 
190  // Read in one update from a single line.
191  bool readString(string line, bool warn = true, ostream& os = cout) ;
192 
193  // Keep track whether any readings have failed, invalidating run setup.
194  bool readingFailed() {return readingFailedSave;}
195 
196  // Write updates or everything to user-defined file.
197  bool writeFile(string toFile, bool writeAll = false) ;
198  bool writeFile(ostream& os = cout, bool writeAll = false) ;
199 
200  // Print out table of database, either all or only changed ones,
201  // or ones containing a given string.
202  void listAll(ostream& os = cout) {
203  list( true, false, " ", os); }
204  void listChanged(ostream& os = cout) {
205  list (false, false, " ", os); }
206  void list(string match, ostream& os = cout) {
207  list (false, true, match, os); }
208 
209  // Reset all values to their defaults.
210  void resetAll() ;
211 
212  // Query existence of an entry.
213  bool isFlag(string keyIn) {
214  return (flags.find(toLower(keyIn)) != flags.end()); }
215  bool isMode(string keyIn) {
216  return (modes.find(toLower(keyIn)) != modes.end()); }
217  bool isParm(string keyIn) {
218  return (parms.find(toLower(keyIn)) != parms.end()); }
219  bool isWord(string keyIn) {
220  return (words.find(toLower(keyIn)) != words.end()); }
221  bool isFVec(string keyIn) {
222  return (fvecs.find(toLower(keyIn)) != fvecs.end()); }
223  bool isMVec(string keyIn) {
224  return (mvecs.find(toLower(keyIn)) != mvecs.end()); }
225  bool isPVec(string keyIn) {
226  return (pvecs.find(toLower(keyIn)) != pvecs.end()); }
227 
228  // Add new entry.
229  void addFlag(string keyIn, bool defaultIn) {
230  flags[toLower(keyIn)] = Flag(keyIn, defaultIn); }
231  void addMode(string keyIn, int defaultIn, bool hasMinIn,
232  bool hasMaxIn, int minIn, int maxIn) { modes[toLower(keyIn)]
233  = Mode(keyIn, defaultIn, hasMinIn, hasMaxIn, minIn, maxIn); }
234  void addParm(string keyIn, double defaultIn, bool hasMinIn,
235  bool hasMaxIn, double minIn, double maxIn) { parms[toLower(keyIn)]
236  = Parm(keyIn, defaultIn, hasMinIn, hasMaxIn, minIn, maxIn); }
237  void addWord(string keyIn, string defaultIn) {
238  words[toLower(keyIn)] = Word(keyIn, defaultIn); }
239  void addFVec(string keyIn, vector<bool> defaultIn) {
240  fvecs[toLower(keyIn)] = FVec(keyIn, defaultIn); }
241  void addMVec(string keyIn, vector<int> defaultIn, bool hasMinIn,
242  bool hasMaxIn, int minIn, int maxIn) { mvecs[toLower(keyIn)]
243  = MVec(keyIn, defaultIn, hasMinIn, hasMaxIn, minIn, maxIn); }
244  void addPVec(string keyIn, vector<double> defaultIn, bool hasMinIn,
245  bool hasMaxIn, double minIn, double maxIn) { pvecs[toLower(keyIn)]
246  = PVec(keyIn, defaultIn, hasMinIn, hasMaxIn, minIn, maxIn); }
247 
248  // Give back current value, with check that key exists.
249  bool flag(string keyIn);
250  int mode(string keyIn);
251  double parm(string keyIn);
252  string word(string keyIn);
253  vector<bool> fvec(string keyIn);
254  vector<int> mvec(string keyIn);
255  vector<double> pvec(string keyIn);
256 
257  // Give back default value, with check that key exists.
258  bool flagDefault(string keyIn);
259  int modeDefault(string keyIn);
260  double parmDefault(string keyIn);
261  string wordDefault(string keyIn);
262  vector<bool> fvecDefault(string keyIn);
263  vector<int> mvecDefault(string keyIn);
264  vector<double> pvecDefault(string keyIn);
265 
266  // Give back a map of all entries whose names match the string "match".
267  map<string, Flag> getFlagMap(string match);
268  map<string, Mode> getModeMap(string match);
269  map<string, Parm> getParmMap(string match);
270  map<string, Word> getWordMap(string match);
271  map<string, FVec> getFVecMap(string match);
272  map<string, MVec> getMVecMap(string match);
273  map<string, PVec> getPVecMap(string match);
274 
275  // Change current value, respecting limits.
276  void flag(string keyIn, bool nowIn);
277  void mode(string keyIn, int nowIn);
278  void parm(string keyIn, double nowIn);
279  void word(string keyIn, string nowIn);
280  void fvec(string keyIn, vector<bool> nowIn);
281  void mvec(string keyIn, vector<int> nowIn);
282  void pvec(string keyIn, vector<double> nowIn);
283 
284  // Change current value, disregarding limits.
285  void forceMode(string keyIn, int nowIn);
286  void forceParm(string keyIn, double nowIn);
287  void forceMVec(string keyIn, vector<int> nowIn);
288  void forcePVec(string keyIn, vector<double> nowIn);
289 
290  // Restore current value to default.
291  void resetFlag(string keyIn);
292  void resetMode(string keyIn);
293  void resetParm(string keyIn);
294  void resetWord(string keyIn);
295  void resetFVec(string keyIn);
296  void resetMVec(string keyIn);
297  void resetPVec(string keyIn);
298 
299 private:
300 
301  // Pointer to various information on the generation.
302  Info* infoPtr;
303 
304  // Map for bool flags.
305  map<string, Flag> flags;
306 
307  // Map for integer modes.
308  map<string, Mode> modes;
309 
310  // Map for double parms.
311  map<string, Parm> parms;
312 
313  // Map for string words.
314  map<string, Word> words;
315 
316  // Map for vectors of bool.
317  map<string, FVec> fvecs;
318 
319  // Map for vectors of int.
320  map<string, MVec> mvecs;
321 
322  // Map for vectors of double.
323  map<string, PVec> pvecs;
324 
325  // Flags that initialization has been performed; whether any failures.
326  bool isInit, readingFailedSave;
327 
328  // Print out table of database, called from listAll and listChanged.
329  void list(bool doListAll, bool doListString, string match,
330  ostream& os = cout);
331 
332  // Master switch for program printout.
333  void printQuiet(bool quiet);
334 
335  // Restore settings used in tunes to e+e- and pp/ppbar data.
336  void resetTuneEE();
337  void resetTunePP();
338 
339  // Initialize tunes to e+e- and pp/ppbar data.
340  void initTuneEE(int eeTune);
341  void initTunePP(int ppTune);
342 
343  // Useful functions for string handling.
344  string toLower(const string& name);
345  bool boolString(string tag);
346  string attributeValue(string line, string attribute);
347  bool boolAttributeValue(string line, string attribute);
348  int intAttributeValue(string line, string attribute);
349  double doubleAttributeValue(string line, string attribute);
350  vector<bool> boolVectorAttributeValue(string line, string attribute);
351  vector<int> intVectorAttributeValue(string line, string attribute);
352  vector<double> doubleVectorAttributeValue(string line, string attribute);
353 
354 };
355 
356 //==========================================================================
357 
358 } // end namespace Pythia8
359 
360 #endif // Pythia8_Settings_H