#ifndef CONFIGDICTIONARY_H #define CONFIGDICTIONARY_H #include "TObject.h" #include "ReturnCodes.h" #include "TStopwatch.h" #include "TROOT.h" #include #include #include #include #include "TLorentzVector.h" #include #include "TMath.h" #include "TString.h" #include "TTree.h" #define ARRAY_DELIM '$' class ConfigDictionary{ public: typedef std::map::iterator CDIter; ConfigDictionary(); ConfigDictionary(std::string); virtual ~ConfigDictionary(){};//empty virtual destructor ClassDef(ConfigDictionary,1); std::string ToString(); void FromString(std::string); //These throw errors if couldn't find key: //Most of methods below are just using templates to //make them usable in Cint. std::string GetString(std::string); int GetInt(std::string); double GetDouble(std::string); bool GetBool(std::string); //These will always set 'something' into map: void SetString(std::string,std::string); void SetDouble(std::string,double); void SetInt(std::string,int); void SetBool(std::string,bool); CDIter Begin(){return configMap.begin();}; CDIter End(){return configMap.end();}; //_________________________________________________________________________ template T Get(std::string key) { //This method will get object of any class //that was - presumably - stored before in string format in CD. //Object needs to have proper >> operator. //If operation fails, returned item should have default value //depending on its empty constructor. //For strings containing spaces it won't work properly, as it //would extract only first space followed item! if (configMap.find(key) == configMap.end()) { Error( "ConfigDictionary::Get", "Couldn't find the key: %s!", key.c_str()); throw std::runtime_error("Couldn't find the key: " + key); } // Use std::istringstream to convert the string to the desired type std::istringstream inStream(configMap[key]); T item; // Extract the value from the stringstream inStream >> item; // Check if extraction failed if (inStream.fail()) { // Throw std::runtime_error with an informative message throw std::runtime_error("Failed to extract value for key: " + key); } // Return the extracted item return item; } //_____________________________________________________________________________ template void Set(std::string key,T value){ //Sets value to key, converts T to string first with << operator. std::stringstream ss; ss< int GetArray(std::string key,std::vector & output){ //Preconditions: T must have >> operator //Effect: searches for key in map and pushes values stored in it into output vector //Returns: number of elements pushed into vector int retNumber = 0; // Counter for the number of elements pushed into the vector // Check if the key exists in the config map if (configMap.find(key) == configMap.end()){ Error( "ConfigDictionary::GetInt", "Couldn't find the key: %s!",key.c_str()); throw std::runtime_error("Couldn't find the key: " + key); } // Create a string stream from the value associated with the key std::istringstream arrayString(configMap[key]); // Temporary string to hold each item from the string stream std::string itemString; //Dangerous - maybe? - exceptions for ss not handled... // Loop to extract each item from the string stream while (std::getline(arrayString,itemString,ARRAY_DELIM)){ std::stringstream itemStream(itemString); // Create a string stream from the item string if (!itemString.empty()) { // Check if the item string is not empty T item; // Temporary variable to hold the converted item itemStream >> item; // Extract the item from the string stream output.push_back(item); // Push the item into the output vector retNumber++; // Increment the counter } } // Return the number of elements pushed into the vector return retNumber; } //_________________________________________________________________________ template void SetArray(std::string key,std::vector & input){ std::ostringstream arrayString; for (unsigned int i=0;i configMap; }; #endif