#include "IOARuntimeParameters.hxx" #include #include "HEPUnits.hxx" #include "HEPConstants.hxx" #include "ICOMETLog.hxx" using namespace std; COMET::IOARuntimeParameters* COMET::IOARuntimeParameters::fTOARuntimeParameters = NULL; COMET::IOARuntimeParameters::IOARuntimeParameters() { COMETLog("*** Initializing COMET::IOARuntimeParameters"); // Build the set of units for Geant4. fUnitsTableParser = new COMET::IUnitsTableParser(); // We do not open any parameter files by default in the constructor. // Instead, parameter files are open automatically when an entry // in the file is requested by the user. } COMET::IOARuntimeParameters::~IOARuntimeParameters() { } /// Read in each parameter file. void COMET::IOARuntimeParameters::ReadInputFile(TString fileName, TString dirName, bool tryFile, bool fixParameters) { TString fullFileName; // Make full file name. if(!dirName.CompareTo("")) fullFileName = fileName.Data(); else fullFileName = dirName.Data() + TString("/") + fileName.Data(); COMETInfo("Opening parameter file: " << fullFileName.Data()); ifstream inputFile(fullFileName.Data(), ios::in); // If input file doesn't exist. if (!inputFile){ // Just return if 'tryFile' is specified. if(tryFile){ COMETInfo("\n***** COMET::IOARuntimeParameters::ReadInputFile *****\n" << "Cannot open input file '" << fullFileName.Data() << "'."); return; }else{ // Otherwise, throw exception. COMETError("\n***** COMET::IOARuntimeParameters::ReadInputFile *****\n" << "Cannot open input file '" << fullFileName.Data() << "'."); throw COMET::EBadParameterFile(); } } int inputState = 0; string inputString; string parameterName; string parameterValueUnit; string parameterValue; while (inputFile >> inputString) { if (inputState == 0) { if (inputString == "<") inputState = 1; } else if (inputState == 1) { parameterName = inputString; inputState = 2; } else if (inputState == 2) { if (inputString == "=") inputState = 3; else { COMETError("\n***** COMET::IOARuntimeParameters::ReadInputFile *****\n" "Input file '" << fileName << "'. Last parameter '" << parameterName << "'.\n" << "Cannot find symbol '='.\n" << "Badly formatted parameters file."); throw COMET::EBadParameterFile(); } } else if (inputState == 3) { // parameterValue = atof(inputString.c_str()); parameterValue = inputString.c_str(); parameterValueUnit = inputString; inputState = 4; } else if (inputState == 4) { if (inputString == ">"){ // Finished reading. Save parameter; but only if the parameter // isn't already 'fixed' if(fixedParameters.find(parameterName) == fixedParameters.end()){ mapOfTOARuntimeParameters[parameterName] = parameterValue; // If fixParameters bool is set, fix this parameter now. if(fixParameters) fixedParameters.insert(parameterName); } inputState = 0; } else if (inputString == "<") { COMETError("\n***** COMET::IOARuntimeParameters::ReadInputFile *****\n" << "Input file '" << fileName << "'. Last parameter '" << parameterName << "'.\n" << "Unexpected symbol '<'.\n" << "Badly formatted parameters file."); throw COMET::EBadParameterFile(); } else { // The parameter must have a unit. Resave the value with the correct unit. parameterValueUnit.append(" "); parameterValueUnit.append(inputString); // Use COMET::IUnitsTableParser class to convert string of value+unit to // a double with value+unit. parameterValue = fUnitsTableParser->Convert2DoubleWithUnit(parameterValueUnit); } } } if (inputState != 0) { COMETError("\n***** COMET::IOARuntimeParameters::ReadInputFile *****\n" << "Input file '" << fileName << "'. Last parameter '" << parameterName << "'.\n" << "Cannot find symbol '>' at the end of file.\n" << "Badly formatted parameters file."); throw COMET::EBadParameterFile(); } inputFile.close(); } void COMET::IOARuntimeParameters::PrintListOfParameters() { COMETInfo("***** COMET::IOARuntimeParameters::PrintListOfParameters *****"); COMETInfo("List of parameters:"); for (mapIterator i = mapOfTOARuntimeParameters.begin(); i != mapOfTOARuntimeParameters.end(); i++) COMETInfo(" " << (*i).first << " = " << (*i).second); COMETInfo(""); } int COMET::IOARuntimeParameters::TryLoadingParametersFile(std::string parameterName){ COMETInfo("Trying to load parameters file for parameter: " << parameterName); // Figure out the name of the package int pos = parameterName.find("."); TString packageName(parameterName.c_str(), pos); // and the file name for parameters file TString fileName = packageName + ".parameters.dat"; // and the directory of parameters file. packageName.ToUpper(); TString packageROOT = packageName + "ROOT"; TString dirName = getenv(packageROOT.Data()) + TString("/parameters/"); // Now try reading in this file. Last input variable is set to true, // indicating that we don't want to throw exception if a file is not found. ReadInputFile(fileName,dirName,true); // Now try to find this parameter again mapIterator i = mapOfTOARuntimeParameters.find(parameterName); if(i != mapOfTOARuntimeParameters.end()) return 1; else{ return 0; } } void COMET::IOARuntimeParameters::ReadParamOverrideFile(TString filename){ COMETLog("Using IOARuntimeParameters override file = " << filename); // Setting final input variable to true forces the parameters // that are loaded to be 'fixed'; ie immutable. ReadInputFile(filename,"",false,true); } bool COMET::IOARuntimeParameters::HasParameter(string parameterName) { mapIterator i = mapOfTOARuntimeParameters.find(parameterName); if(i != mapOfTOARuntimeParameters.end()) return 1; else{ // Okay, we didn't find this parameter on the first try. // Let's see if we can try loading the parameters file. // The function will return whether this parameter // was found afterwards. return TryLoadingParametersFile(parameterName); } } bool COMET::IOARuntimeParameters::HasPrefix(string paramPrefix){ // Search for element whose key would not go before the prefex, i.e. in // greater than or equal to mapIterator i = mapOfTOARuntimeParameters.lower_bound(paramPrefix); if (i != mapOfTOARuntimeParameters.end()) { const string& key = i->first; // Check if the prefix is a match to the element if (key.compare(0, paramPrefix.size(), paramPrefix) == 0) return true; }; // Otherwise return false return false; } int COMET::IOARuntimeParameters::GetParameterI(string parameterName) { if(HasParameter(parameterName)) { return atoi(mapOfTOARuntimeParameters[parameterName].c_str()); }else{ COMETError("\n***** COMET::IOARuntimeParameters::GetParameterAsInteger *****\n" << "Cannot find parameter '" << parameterName << "'."); throw COMET::ENonexistantDatabaseParameter(); } return -1; } double COMET::IOARuntimeParameters::GetParameterD(string parameterName) { if(HasParameter(parameterName)) { return atof(mapOfTOARuntimeParameters[parameterName].c_str()); }else{ COMETError("\n***** COMET::IOARuntimeParameters::GetParameterAsDouble *****\n" << "Cannot find parameter '" << parameterName << "'."); throw COMET::ENonexistantDatabaseParameter(); } return -1; } std::string COMET::IOARuntimeParameters::GetParameterS(string parameterName) { if(HasParameter(parameterName)) { return mapOfTOARuntimeParameters[parameterName]; }else{ COMETError("\n***** COMET::IOARuntimeParameters::GetParameterAsString *****\n" << "Cannot find parameter '" << parameterName << "'."); throw COMET::ENonexistantDatabaseParameter(); } return std::string(); } void COMET::IOARuntimeParameters::ClearMapOfTOARuntimeParameters() { mapOfTOARuntimeParameters.clear(); }