Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

NuFCConfig.cxx

Go to the documentation of this file.
00001 
00002 #include <fstream>
00003 #include <sstream>
00004 
00005 // General includes
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 // NuFCConfig
00018 
00019 // Initialise the base Instance
00020 NuFCConfig NuFCConfig::fInstance;
00021 
00022 NuFCConfig::NuFCConfig()
00023 {
00024   // Initialise everything with defaults
00025 
00026   fHelperFile = "CombinedHelpersRun1LE_bravo.root";
00027   fNDMC = "NDMCRun1LE_bravo.root";
00028 
00029   // FC Systematics variables
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   // Make a nicified string for the systematics status
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   // Only show the systematics only vars if we want systematics
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 // Grabs hold of (or creates) the sole instance
00066 NuFCConfig &NuFCConfig::GetConfig()
00067 {
00068   // if (fInstance == 0) {
00069   //   // We need to create a configuration
00070   //   fInstance = new NuFCConfig;
00071   // }
00072   // return fInstance;
00073   
00074   return fInstance;
00075 }
00076 
00077 void NuFCConfig::LoadConfig(std::string filename)
00078 {
00079   ConfigFile cfg(filename);
00080   
00081   // Load all the variables out of it
00082   
00083   // Generation sources
00084   fHelperFile = cfg.GetString("Helper", "MC", fHelperFile);
00085   fNDMC = cfg.GetString("Neardet", "MC", fNDMC);
00086   
00087   // Systematics variables
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   // Fitting variables
00096   fFixedMixingAngle = cfg.GetBool("OneParameter", "Fitting", fFixedMixingAngle);
00097 }
00098 
00103 // Configuration File Reader
00104 
00105 ConfigFile::ConfigFile(string filename)
00106 {
00108   ifstream cfg(filename.c_str());
00109   if(cfg.is_open())
00110   {
00111     // All is well, read the file in!
00112     ReadFile(cfg);
00113     
00114   } else {
00115     // Print an error message if we were trying to open a file
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   // Strips comments from a line
00128   
00129   // Find any instances of '#', and then remove them
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   // Find the first non-whitespace
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   // Loop over all lines in the file
00149   while(std::getline(file, line))
00150   {
00151     ++lineNo;
00152     
00153     // Clean up the line
00154     line = Decomment(line);
00155     line = Trim(line);
00156     
00157     // Ignore blank lines
00158     if (line.size() == 0) continue;
00159     
00160     // Several paths here: Section header, Key-Value pair or invalid.
00161     // First test for section header
00162     if (IsSectionChange(line))
00163     {
00164       section = Section(line);
00165       continue;
00166     }
00167     
00168     // Must be a key pair, or invalid
00169     if (!ReadKeyPair(section, line))
00170     {
00171       MSG("ConfigFile",Msg::kWarning) << "Invalid line " << lineNo << ": \"" << line << "\"" << endl; 
00172     }
00173     
00174   } // Go to next line
00175 }
00176 
00177 bool ConfigFile::IsSectionChange(const string &line)
00178 {
00179   // Work out if this is a change of section by checking the first char
00180   if (line[0] == '[') {
00181     // Check the last char too
00182     if (line[line.size()-1] != ']')
00183     {
00184       // cout << "Warning Parsing file " << lineNo << ": Invalid section change!" << endl;
00185       MSG("ConfigFile",Msg::kWarning) << "Invalid section change: " << line << endl;
00186     } else {
00187       // Both start and ends are valid... is a section change line
00188       // Make sure it is valid
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   // Extracts the section from a line
00204   string newsec = line.substr(1, line.size()-2);
00205   
00206   return newsec;
00207 }
00208 
00209 bool ConfigFile::ReadKeyPair(const string &section, const string &line)
00210 {
00211   // Check to see if this is a key pair
00212   int split = line.find_first_of("=:");
00213   
00214   // what if we don't find a split?
00215   if (split == -1)
00216   {
00217     // MSG("ConfigFile",Msg::kWarning) << "Invalid key-value pair: \"" << line << "\"" << endl;
00218     return false;
00219   }
00220   
00221   // Read out the values
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   // Attempt to read an integer out of this stream
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   // Attempt to read an integer out of this stream
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   // Now try to work out what it is
00283   if (value == "True" || value == "true") return true;
00284   if (value == "False" || value == "false") return false;
00285   
00286   // Try to convert to an integer, and see if it is non-zero
00287   istringstream iss (value);
00288   int ival = 0;
00289   iss >> ival;
00290   
00291   // It worked - just return the value
00292   if (!iss.fail())
00293   {
00294     return ival;
00295   }
00296   
00297   // It failed..... give up here for now
00298   MAXMSG("ConfigFile",5,Msg::kWarning) << "Failed to read [" << section << "]." << key << " = " << value << " as boolean" << endl;
00299   return defaultval;
00300 }

Generated on Mon Feb 15 11:07:13 2010 for loon by  doxygen 1.3.9.1