HighLAND
Parameters.hxx
1 #ifndef Parameters_hxx_seen
2 #define Parameters_hxx_seen
3 
4 #include "TString.h"
5 #include <iostream>
6 #include <string>
7 #include <fstream>
8 #include <map>
9 #include <set>
10 #include <stdexcept>
11 #include "UnitsParser.hxx"
12 
13 /// This exception class indicates an error when reading a parameters file.
14 class BadParameterFileException: public std::runtime_error {
15  public:
17  std::runtime_error("Error reading parameters") {
18  }
19 };
20 
21 /// This exception class indicates that the user asked for a parameter that we
22 /// know nothing about.
23 class NoSuchParameterException: public std::runtime_error {
24  public:
26  std::runtime_error("No such parameter") {
27  }
28 };
29 
30 /// This class provides access to a set of parameters that are defined in a
31 /// set of text files. The parameters can be used at runtime, by calling
32 /// ND::params().GetParameterI("tutAnalsyis.param.name") or similar.
33 ///
34 /// Parameters files should be placed in the "parameters" directory in an
35 /// analysis package, and named as tutAnalysis.parameters.dat, where tutAnalysis
36 /// is the name of the analysis package. This is where the code will look for a parameter, unless you
37 /// have already specified the parameter in an "override" file (see below).
38 ///
39 /// The format for parameters files is:
40 /// < tutAnalysis.param.name = 12345 >
41 /// Note that you should have spaces between the <>= symbols and your parameter names / values.
42 /// The first bit of your parameter name MUST be the same as your package name. After that you can
43 /// have whatever you like. It is useful to use the "dots" to logically group parameters that are related.
44 /// For example, you could have \c tutAnalysis.Cuts.PullMu.Max and \c tutAnalysis.Cuts.PullMu.Min.
45 ///
46 /// Parameters can be read as integers, doubles or strings, using the ND::params().GetParameterI(),
47 /// ND::params().GetParameterD() and ND::params().GetParameterS() functions, respectively.
48 ///
49 /// You don't need to do anything special to use the ND::params() singleton - the first time you call
50 /// one of the ND::params().GetParameter functions it will parse the relevant parameters file.
51 ///
52 /// You can use a parameters "override" file to override the values set in the normal parameters file.
53 /// This override file can contain a subset of the parameters (even just 1 or 2), and can be placed
54 /// anywhere on your file system. Specify the path to this override file using the \c -p command-line
55 /// option. This is very useful if you're using the FLuxWeighting, for example, so that you can run
56 /// multiple jobs in parallel, and can specify to apply the run 1 flux to run 1 MC files, run 2 flux to
57 /// run 2 MC files etc.
58 ///
59 class Parameters {
60 
61  public:
62 
63  ~Parameters();
64 
65  /// Get a reference to the singleton instance of dummy
66  /// database information. If this is first attempt at reference
67  /// then singleton is instantiated and parameters are
68  /// read from text files.
69  static Parameters& Get(void) {
70  if (!fParameters)
71  fParameters = new Parameters();
72  return *fParameters;
73  }
74 
75  /// Check if Parameter is stored in database
76  bool HasParameter(std::string);
77 
78  /// Get parameter. Value is returned as integer.
79  int GetParameterI(std::string);
80 
81  /// Get parameter. Value is returned as double.
82  double GetParameterD(std::string);
83 
84  /// Get parameter. Value is returned as string.
85  std::string GetParameterS(std::string);
86 
87  /// Clear all the parameters that have been read.
88  void ClearMapOfParameters();
89 
90  /// Return the class which parses any units specified in the parameters
91  /// file, and converts them to a consistent set of HEP units.
93  return fUnitsParser;
94  }
95 
96  /// This command allows the user to set parameters from the
97  /// command line; the command is different from the standard
98  /// file reading, in that the parameters that are set are 'fixed'.
99  /// Ie, they are immutable and cannot be changed, even if they
100  /// exist in some other parameters file that is read in later.
101  void ReadParamOverrideFile(TString filename);
102 
103  /// Load the parameters for a given package - needed for multi-threading
104  int LoadParametersFile(TString packageName, UInt_t tryFileMode=0, bool fixParameters = false);
105 
106  /// Load the parameters for a list of packages
107  void LoadParametersFiles(const std::vector<std::string>& packageNames, bool fixed=false);
108 
109  /// Set whether the point in which the overwride parameters file is read is passed or not
110  void SetReadParamOverrideFilePointPassed(){fReadParamOverrideFilePointPassed=true;}
111 
112  /// Checks whether the point in which the overwride parameters file is read is passed or not.
113  /// To be used in GetParameters methods. It stops and prints an error message if the
114  /// ReadParamOverrideFile point is not yet passed
115  void CheckReadParamOverrideFilePointPassed(std::string parameterName);
116 
117  private:
118 
119  /// Private constructor as this is a singleton. Use ND::params() to access.
120  Parameters();
121 
122  /// Unimplemented copy constructor as this is a singleton.
123  Parameters(Parameters const&);
124 
125  /// Unimplemented assignment operator as this is a singleton.
126  void operator=(Parameters const&);
127 
128  /// Reads parameters from input files.
129  /// Function can be used to read in extra parameter files.
130  void ReadInputFile(TString filename, TString dirName = "", UInt_t tryFileMode = 0, bool fixParameters = false);
131 
132  /// Prints list of saved parameters
133  void PrintListOfParameters();
134 
135  /// Helper method to attempt to read parameters file based on parameters name.
136  /// The return value indicates whether we can now find this parameter after
137  /// trying to open this file (return 1 means parameter found).
138  int TryLoadingParametersFile(std::string parameterName);
139 
140  /// Map containing list of parameters and their values.
141  std::map<std::string, std::string, std::less<std::string> > mapOfParameters;
142 
143  /// Define an iterator for mapOfParameters, to make the code more readable.
144  typedef std::map<std::string, std::string, std::less<std::string> >::iterator mapIterator;
145 
146  /// A set of all the 'fixed'. 'Fixed' parameters cannot be changed, even if they reappear
147  /// in a different parameters file. This is used for the parameter override files.
148  std::set<std::string> fixedParameters;
149 
150  /// Units can be specified in the parameters files. The UnitsParser will
151  /// convert them to a consistent set of HEP units.
152  UnitsParser *fUnitsParser;
153 
154  /// Indicates whether the point in which the overwride parameters file is read is passed or not
155  bool fReadParamOverrideFilePointPassed;
156 
157  /// The static pointer to the singleton instance.
158  static Parameters* fParameters;
159 };
160 
161 namespace ND {
162  /// Access to parameters. See Parameters class documentation for more.
163  Parameters& params();
164 }
165 
166 #endif
UnitsParser * GetUnitsTableParser()
Definition: Parameters.hxx:92
This exception class indicates an error when reading a parameters file.
Definition: Parameters.hxx:14
static Parameters & Get(void)
Definition: Parameters.hxx:69
void SetReadParamOverrideFilePointPassed()
Set whether the point in which the overwride parameters file is read is passed or not...
Definition: Parameters.hxx:110