Histo1DFunction.hh 6.32 KB
/*
 * Histo1Dfunction.hh
 *
 *  Created on: Jan 30, 1013
 *      Author: AKKODIS
 */

#ifndef HISTO1DFUNCTION_HH_
#define HISTO1DFUNCTION_HH_

#include <iostream>
#include <map>
#include <string>
#include <algorithm> 
#include <boost/algorithm/string.hpp>
#include "Matrix.hh"
#include "Range.hh"

namespace plot {

/**
* Class that manages the function to apply on the 1Dhistogram
*/

class Histo1DFunction {
	public:
		Histo1DFunction(){

		}
		~Histo1DFunction(){}
		virtual std::string getFunctionName() = 0;
		void apply(std::shared_ptr<Histo1DFunction> functionToApply, std::vector<std::pair<double,double>> &grid,  double* xData, double* yData, int nbData, Range xRange, double& xBinSize,
							bool yIsLog, double& yMin, double& yMax);

		virtual std::string getTextLegend()
		{
			return "";
		}
		double mean(const std::vector<double>& data) {
			double sum = 0.0;
			for (const auto& value : data) {
				sum += value;
			}
			return sum / data.size();
		}

		double standardDeviation(const std::vector<double>& data, double mean) {
			double sum = 0.0;
			for (const auto& value : data) {
				sum += std::pow(value - mean, 2);
			}
			return std::sqrt(sum / data.size());
		}

		double sum(const std::vector<double>& data) {
			double sum = 0.0;
			for (const auto& value : data) {
				sum += value;
			}
			return sum;
		}	

		double max(const std::vector<double>& data) {
			if (data.empty()) {
				std::cerr << "Error: Empty data for max calculation." << std::endl;
				return 0.0;
			}

			return *std::max_element(data.begin(), data.end());
		}

		double min(const std::vector<double>& data) {
			if (data.empty()) {
				std::cerr << "Error: Empty data for min calculation." << std::endl;
				return 0.0;
			}

			return *std::min_element(data.begin(), data.end());
		}

		double median(const std::vector<double>& data) {
			if (data.empty()) {
				std::cerr << "Error: Empty data for median calculation." << std::endl;
				return 0.0;
			}

			std::vector<double> sortedData = data;
			std::sort(sortedData.begin(), sortedData.end());

			size_t size = sortedData.size();
			if (size % 2 == 0) {
				// If the size is even, take the average of the two middle elements.
				return (sortedData[size / 2 - 1] + sortedData[size / 2]) / 2.0;
			} else {
				// If the size is odd, return the middle element.
				return sortedData[size / 2];
			}
		}

		std::map<int,std::vector<double>>  getGridValues(double* xData, double* yData, int nbData, Range xRange, double xBinSize);
		virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog) = 0;
		void resetCache() {
			_cacheGrid.clear();
		}
	protected:
		std::vector<std::pair<double,double>> _cacheGrid;

};

/**
*  All the way down, different classes that apply a function on 1Dhistogram and return result
*/

class Density1DFunction : public Histo1DFunction {
public:
	Density1DFunction() {}
	~Density1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "density";};
	virtual std::string getTextLegend()
	{
		return "Events";
	}
};

class NormDensity1DFunction : public Histo1DFunction {
public:
	NormDensity1DFunction() {}
	~NormDensity1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "normdensity";};
	virtual std::string getTextLegend()
	{
		return "Frequency";
	}
};

class Mean1DFunction : public Histo1DFunction {
public:
	Mean1DFunction() {}
	~Mean1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "mean";};
	virtual std::string getTextLegend(){return "Mean for ";}
};

class Max1DFunction : public Histo1DFunction {
public:
	Max1DFunction() {}
	~Max1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "max";};
	virtual std::string getTextLegend(){return "Max for ";}
};

class Min1DFunction : public Histo1DFunction {
public:
	Min1DFunction() {}
	~Min1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "min";};
	virtual std::string getTextLegend(){return "Min for ";}
};

class Median1DFunction : public Histo1DFunction {
public:
	Median1DFunction() {}
	~Median1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "median";};
	virtual std::string getTextLegend(){return "Median for ";}
};

class StandardDeviation1DFunction : public Histo1DFunction {
public:
	StandardDeviation1DFunction() {}
	~StandardDeviation1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "stadev";};
	virtual std::string getTextLegend(){return "Sta. dev. for ";}
};

class Kurtosis1DFunction : public Histo1DFunction {
public:
	Kurtosis1DFunction() {}
	~Kurtosis1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "kurtosis";};
	virtual std::string getTextLegend(){return "Kurtosis for ";}
};

class Skewness1DFunction : public Histo1DFunction {
public:
	Skewness1DFunction() {}
	~Skewness1DFunction() {}

	virtual void calculate(std::vector<std::pair<double,double>> &grid, std::map<int,std::vector<double>> allValues, Range xRange, double& xBinSize, bool yIsLog);
	virtual std::string getFunctionName(){return "skewness";};
	virtual std::string getTextLegend(){return "Skewness for ";}
};

} /* namespace plot */

#endif /* HISTO1DFUNCTION_HH_ */