ConfigDictionary.h 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#ifndef CONFIGDICTIONARY_H
#define CONFIGDICTIONARY_H


#include "TObject.h"
#include "ReturnCodes.h"
#include "TStopwatch.h"
#include "TROOT.h"
#include <string>
#include <map>
#include <sstream>
#include <vector>
#include "TLorentzVector.h"
#include <iostream>
#include "TMath.h"
#include "TString.h"
#include "TTree.h"


#define ARRAY_DELIM '$'

class ConfigDictionary{
public:
	typedef std::map<std::string,std::string>::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.
Vratislav Chudoba's avatar
Vratislav Chudoba committed
36 37 38 39
	std::string GetString(std::string);
	int	GetInt(std::string);
	double GetDouble(std::string);
	bool GetBool(std::string);
40 41 42 43 44 45 46 47 48 49 50 51 52

	//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 <class T>
Vratislav Chudoba's avatar
Vratislav Chudoba committed
53
	T Get(std::string key) {
54 55 56 57 58 59 60 61 62 63 64
		//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());
Vratislav Chudoba's avatar
Vratislav Chudoba committed
65
			throw std::runtime_error("Couldn't find the key: " + key);
66
		}
Vratislav Chudoba's avatar
Vratislav Chudoba committed
67
		// Use std::istringstream to convert the string to the desired type
68 69
		std::istringstream inStream(configMap[key]);
		T item;
Vratislav Chudoba's avatar
Vratislav Chudoba committed
70
		// Extract the value from the stringstream
71
		inStream >> item;
Vratislav Chudoba's avatar
Vratislav Chudoba committed
72 73 74 75 76 77 78
		// 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		return item;
	}


	//_____________________________________________________________________________
	template <class T>
	void Set(std::string key,T value){
		//Sets value to key, converts T to string first with << operator.
		std::stringstream ss;
		ss<<value;
		configMap[key] = ss.str();
	}


	//_________________________________________________________________________
	template <class T>
Vratislav Chudoba's avatar
Vratislav Chudoba committed
95
	int GetArray(std::string key,std::vector<T> & output){
96 97 98
		//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
Vratislav Chudoba's avatar
Vratislav Chudoba committed
99 100 101 102
		
		int retNumber = 0;	// Counter for the number of elements pushed into the vector
		
		// Check if the key exists in the config map
103 104 105
		if (configMap.find(key) == configMap.end()){
			Error(	"ConfigDictionary::GetInt",
					"Couldn't find the key: %s!",key.c_str());
Vratislav Chudoba's avatar
Vratislav Chudoba committed
106
			throw std::runtime_error("Couldn't find the key: " + key);
107
		}
Vratislav Chudoba's avatar
Vratislav Chudoba committed
108 109

		// Create a string stream from the value associated with the key
110
		std::istringstream arrayString(configMap[key]);
Vratislav Chudoba's avatar
Vratislav Chudoba committed
111
		// Temporary string to hold each item from the string stream
112
		std::string itemString;
Vratislav Chudoba's avatar
Vratislav Chudoba committed
113

114
		//Dangerous - maybe? - exceptions for ss not handled...
Vratislav Chudoba's avatar
Vratislav Chudoba committed
115 116 117 118 119 120 121 122
		// 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
123
			}
Vratislav Chudoba's avatar
Vratislav Chudoba committed
124
			
125
		}
Vratislav Chudoba's avatar
Vratislav Chudoba committed
126 127

		// Return the number of elements pushed into the vector
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
		return retNumber;
	}

	//_________________________________________________________________________
	template <class T>
	void SetArray(std::string key,std::vector<T> & input){
		std::ostringstream arrayString;
		for (unsigned int i=0;i<input.size();i++){
			arrayString<<input[i]<<ARRAY_DELIM;
		}
		configMap[key] = arrayString.str();
	}



private:
	std::map<std::string,std::string> configMap;
};

#endif