00001
00002 #include <fstream>
00003 #include <sstream>
00004
00005
00006 #include <string>
00007
00008 #include "MessageService/MsgService.h"
00009 #include "NtupleUtils/NuFCConfig.h"
00010
00011 CVSID("$Id: NuFCConfig.cxx,v 1.6 2009/06/03 10:29:09 nickd Exp $");
00012
00013 using namespace std;
00014
00017
00018
00019
00020 NuFCConfig NuFCConfig::fInstance;
00021
00022 NuFCConfig::NuFCConfig()
00023 {
00024
00025
00026 fHelperFile = "CombinedHelpersRun1LE_bravo.root";
00027 fNDMC = "NDMCRun1LE_bravo.root";
00028
00029
00030 fFCSystematics = true;
00031 fFCevents_Near = "/minos/data/analysis/NuMuBar/Helpers/Bravo/near_events.root";
00032 fFCevents_Far = "/minos/data/analysis/NuMuBar/Helpers/Bravo/far_events.root";
00033 fFCevents_Tau = "/minos/data/analysis/NuMuBar/Helpers/Bravo/tau_events.root";
00034
00035 fPOT = 3.2e20;
00036
00037 fFixedMixingAngle = false;
00038 }
00039
00040 void NuFCConfig::PrintStatus()
00041 {
00042
00043 string syststatus = "No";
00044 if (fFCSystematics) syststatus = "Yes";
00045
00046 string fixedsin = "No";
00047 if (fFixedMixingAngle) fixedsin = "Yes";
00048
00049 cout << "***************************************************" << endl;
00050 cout << "Configuration:" << endl
00051 << "POT: " << fPOT << endl
00052 << "Helper File: " << fHelperFile << endl
00053 << "ND MC: " << fNDMC << endl
00054 << "Systematics? " << syststatus << endl
00055 << "Fixed Sin? " << fixedsin << endl;
00056
00057
00058 cout << "FC Events" << endl
00059 << "Near Events: " << fFCevents_Near << endl
00060 << "Far Events: " << fFCevents_Far << endl
00061 << "Tau Events: " << fFCevents_Tau << endl;
00062 cout << "***************************************************" << endl;
00063 }
00064
00065
00066 NuFCConfig &NuFCConfig::GetConfig()
00067 {
00068
00069
00070
00071
00072
00073
00074 return fInstance;
00075 }
00076
00077 void NuFCConfig::LoadConfig(std::string filename)
00078 {
00079 ConfigFile cfg(filename);
00080
00081
00082
00083
00084 fHelperFile = cfg.GetString("Helper", "MC", fHelperFile);
00085 fNDMC = cfg.GetString("Neardet", "MC", fNDMC);
00086
00087
00088 fFCSystematics = cfg.GetBool("Systematics", "FC_Systematics", fFCSystematics);
00089 fFCevents_Near = cfg.GetString("Near_Events", "FC_Systematics", fFCevents_Near);
00090 fFCevents_Far = cfg.GetString("Far_Events", "FC_Systematics", fFCevents_Far);
00091 fFCevents_Tau = cfg.GetString("Tau_Events", "FC_Systematics", fFCevents_Tau);
00092
00093 fPOT = cfg.GetFloat("POT", "Generation", fPOT);
00094
00095
00096 fFixedMixingAngle = cfg.GetBool("OneParameter", "Fitting", fFixedMixingAngle);
00097 }
00098
00103
00104
00105 ConfigFile::ConfigFile(string filename)
00106 {
00108 ifstream cfg(filename.c_str());
00109 if(cfg.is_open())
00110 {
00111
00112 ReadFile(cfg);
00113
00114 } else {
00115
00116 if (filename != "") {
00117 MSG("ConfigFile",Msg::kFatal) << "Could not read configuration file " << filename << endl;
00118 throw runtime_error("Could not read configuration file.");
00119 }
00120 }
00121
00123 }
00124
00125 string ConfigFile::Decomment(const string &source)
00126 {
00127
00128
00129
00130 int comment = source.find_first_of("#%");
00131 if (comment == -1) return source;
00132 return source.substr(0, comment);
00133 }
00134 string ConfigFile::Trim(const string &source)
00135 {
00136
00137 int first = source.find_first_not_of("\n\r\t ");
00138 int last = source.find_last_not_of("\n\r\t ");
00139 if (last == -1) return "";
00140 return source.substr(first, last - first + 1);
00141 }
00142
00143 void ConfigFile::ReadFile(std::ifstream &file)
00144 {
00145 int lineNo = 0;
00146 string line, section = "";
00147
00148
00149 while(std::getline(file, line))
00150 {
00151 ++lineNo;
00152
00153
00154 line = Decomment(line);
00155 line = Trim(line);
00156
00157
00158 if (line.size() == 0) continue;
00159
00160
00161
00162 if (IsSectionChange(line))
00163 {
00164 section = Section(line);
00165 continue;
00166 }
00167
00168
00169 if (!ReadKeyPair(section, line))
00170 {
00171 MSG("ConfigFile",Msg::kWarning) << "Invalid line " << lineNo << ": \"" << line << "\"" << endl;
00172 }
00173
00174 }
00175 }
00176
00177 bool ConfigFile::IsSectionChange(const string &line)
00178 {
00179
00180 if (line[0] == '[') {
00181
00182 if (line[line.size()-1] != ']')
00183 {
00184
00185 MSG("ConfigFile",Msg::kWarning) << "Invalid section change: " << line << endl;
00186 } else {
00187
00188
00189 string newsec = line.substr(1, line.size()-2);
00190 if (newsec.size() == 0) {
00191 MSG("ConfigFile", Msg::kWarning) << "Invalid Section Change: Section cannot be blank" << endl;
00192 } else {
00193 return true;
00194 }
00195 }
00196 }
00197
00198 return false;
00199 }
00200
00201 string ConfigFile::Section(const string &line)
00202 {
00203
00204 string newsec = line.substr(1, line.size()-2);
00205
00206 return newsec;
00207 }
00208
00209 bool ConfigFile::ReadKeyPair(const string §ion, const string &line)
00210 {
00211
00212 int split = line.find_first_of("=:");
00213
00214
00215 if (split == -1)
00216 {
00217
00218 return false;
00219 }
00220
00221
00222 string key = Trim(line.substr(0, split));
00223 string keyval = Trim(line.substr(split+1, line.size()-split+2));
00224 fStore[section][key] = keyval;
00225
00226 return true;
00227 }
00228
00229 string ConfigFile::GetString(string key, string section, string defaultval)
00230 {
00231 string value = fStore[section][key];
00232
00233 if (value.size() == 0)
00234 return defaultval;
00235 else
00236 return value;
00237 }
00238
00239 int ConfigFile::GetInt(string key, string section, int defaultval)
00240 {
00241 string value = fStore[section][key];
00242 if (value.size() == 0) return defaultval;
00243
00244
00245 istringstream iss (value);
00246 int ival = 0;
00247 iss >> ival;
00248
00249 if (!iss.fail())
00250 {
00251 return ival;
00252 } else {
00253 MAXMSG("ConfigFile",5,Msg::kWarning) << "Failed to read [" << section << "]." << key << " = " << value << " as integer" << endl;
00254 return defaultval;
00255 }
00256 }
00257
00258 double ConfigFile::GetFloat(string key, string section, double defaultval)
00259 {
00260 string value = fStore[section][key];
00261 if (value.size() == 0) return defaultval;
00262
00263
00264 istringstream iss (value);
00265 double ival = 0.0;
00266 iss >> ival;
00267
00268 if (!iss.fail())
00269 {
00270 return ival;
00271 } else {
00272 MAXMSG("ConfigFile",5,Msg::kWarning) << "Failed to read [" << section << "]." << key << " = " << value << " as integer" << endl;
00273 return defaultval;
00274 }
00275 }
00276
00277 bool ConfigFile::GetBool(string key, string section, bool defaultval)
00278 {
00279 string value = fStore[section][key];
00280 if (value.size() == 0) return defaultval;
00281
00282
00283 if (value == "True" || value == "true") return true;
00284 if (value == "False" || value == "false") return false;
00285
00286
00287 istringstream iss (value);
00288 int ival = 0;
00289 iss >> ival;
00290
00291
00292 if (!iss.fail())
00293 {
00294 return ival;
00295 }
00296
00297
00298 MAXMSG("ConfigFile",5,Msg::kWarning) << "Failed to read [" << section << "]." << key << " = " << value << " as boolean" << endl;
00299 return defaultval;
00300 }