AnalysisSettings.h
1 /* -*- C++ -*-.***************************************************************************************
2  Author: Ben Strutt
3  Email: strutt@physics.ucla.edu
4 
5  Description:
6  Never again will a forgetful analyst (me) not be sure what the state of the analysis software was when he ran a script.
7  This class should read, set and store all the configurable numbers in the analysis.
8  The mantra here is no arbitrary or semi-arbitrary choices should be hard coded. They should be here.
9 *****************************************************************************************************/
10 
11 #ifndef ACCLAIM_ANALYSIS_SETTINGS_H
12 #define ACCLAIM_ANALYSIS_SETTINGS_H
13 
14 #include <iostream>
15 #include "TString.h"
16 #include "TFile.h"
17 #include <map>
18 
19 // Macro used to define an analysis SettingVariable and create the associate getter/setter functions
20 //
21 // This is evil, but a lesser of two evils I think.
22 // It ensures that the functions always match the naming conventions expected by ROOT.
23 // Using ANALYSIS_SETTING(Int_t, Example) in a class definition defines the following
24 // Int_t fExample;
25 // Int_t GetExample();
26 // void SetExample(Int_t)
27 //
28 // NOTE: Does NOT work with string-like types. TString, const char*, Option_t* etc all cause problems!
29 // Known working types are Int_t, Bool_t, Double_t, stick to those.
30 // Current work around is define your string option in an array and pick with an index.
31 
32 #define ANALYSIS_SETTING(var_type, SettingVariable) \
33  protected: \
34  var_type f##SettingVariable; \
35  public: \
36  var_type Get##SettingVariable() const \
37  {\
38  return f##SettingVariable; \
39  }\
40  void Set##SettingVariable(var_type val) \
41  {\
42  f##SettingVariable = val; \
43  }
44 
45 // This one is useful for converting an enum in a file to Int_t
46 #define ENUM_ANALYSIS_SETTING(enum_type, SettingVariable) \
47  protected: \
48  enum_type f##SettingVariable; \
49  public: \
50  Int_t Get##SettingVariable() const \
51  { \
52  return (Int_t)f##SettingVariable; \
53  } \
54  enum_type GetEnum##SettingVariable() const \
55  { \
56  return f##SettingVariable; \
57  } \
58  void Set##SettingVariable(Int_t val) \
59  { \
60  f##SettingVariable = (enum_type)val; \
61  } \
62  void Set##SettingVariable(enum_type val) \
63  { \
64  f##SettingVariable = val; \
65  }
66 
67 
68 
69 namespace Acclaim {
70 
72 
73  // nested maps
74  typedef std::map<TString, TString> VariableMap_t;
75  typedef std::map<TString, VariableMap_t*> SectionMap_t;
76 
77  public:
78  AnalysisSettings(const char* fName = NULL);
79  void apply(TObject* obj) const;
80  void write(TFile* f) const;
81  void print() const;
82 
83  Bool_t getSetting(const char* settingName, Bool_t& settingVal) const;
84  Bool_t getSetting(const char* settingName, Int_t& settingVal) const;
85  Bool_t getSetting(const char* settingName, Double_t& settingVal) const;
86 
87  protected:
88  Bool_t stringIsKeyValuePair(const TString& commentStrippedLine, TString& key, TString& value) const;
89  Bool_t stringIsSection(const TString& commentStrippedLine, TString& secName) const;
90  Bool_t stringIsAlphaNumeric(const TString& commentStrippedLine) const;
91 
92  void handleKeyValue(VariableMap_t* variableMap, const TString& key, const TString& value);
93  VariableMap_t* handleSection(const TString& section);
94 
95  Bool_t getSetting(const char* settingName, TString& settingVal) const;
96 
97 
98  void parseSettingsFile();
99  Bool_t tryFile(const char* fName);
100  void findFile();
101 
102  TString fileName;
103  TString parsedFileCopy;
104  SectionMap_t sectionMap;
105 };
106 
107 
108 }
109 
110 
111 #endif
Namespace which wraps everything in the library.
Bool_t getSetting(const char *settingName, Bool_t &settingVal) const