// // stacked_container.h // mc_electron // // Created by Christian Reiling on 10.05.19. // Copyright © 2019 Christian Reiling. All rights reserved. // #ifndef stacked_container_h #define stacked_container_h #include "average_container.h" #include "random_operations.h" template class Stacked_Container : public Base_Container { double lowerEdge = 0; public: Stacked_Container() : Base_Container() {} ; Stacked_Container(const double &lowerEdge, std::string load_path) : Base_Container(load_path) { this->lowerEdge = lowerEdge; } Stacked_Container(const double &lowerEdge, const Average_Container &averageContainer) { this->lowerEdge = lowerEdge; calcMap(averageContainer); } public: void calcMap(const Average_Container &averageContainer, const double &lowerEdge); private: void rec_calcMap(const dim1Map &subMap, std::deque Points); template void rec_calcMap(const T &subMapm, std::deque Points); double calc_tot(const dim1Map &subMap); public: double sample(const std::deque &Points, UniRNG *rngUni); }; template void Stacked_Container::calcMap(const Average_Container &averageContainer, const double &lowerEdge) { this->lowerEdge = lowerEdge; T_Map averageMap = averageContainer.getMap(); if (averageMap.empty()) { std::stringstream ss; ss<<"Stacked_Container::calcMap: Input Average_Container empty.\n"; throw MyException(ss.str()); } Base_Container::Map.clear(); std::deque Points; rec_calcMap(averageMap, Points); } template void Stacked_Container::rec_calcMap(const dim1Map &subMap, std::deque Points) { double tot = calc_tot(subMap); double curVal, width, heigth; // first value manually, because of lowerEdge width = subMap.cbegin()->first - lowerEdge; heigth = subMap.cbegin()->second/tot; curVal = width * heigth; Points.push_back(curVal); Points.push_back(subMap.cbegin()->first); Base_Container::addPoint(Points); Points.pop_back(); Points.pop_back(); dim1Map::const_iterator preitr; for (dim1Map::const_iterator curitr = (++subMap.cbegin()); curitr!=subMap.cend(); curitr++) { preitr = curitr; --preitr; width = curitr->first - preitr->first; heigth = curitr->second/tot; curVal += width*heigth; Points.push_back(curVal); Points.push_back(curitr->first); Base_Container::addPoint(Points); Points.pop_back(); Points.pop_back(); } } template template void Stacked_Container::rec_calcMap(const T &subMap, std::deque Points) { for (typename T::const_iterator itr = subMap.cbegin(); itr!= subMap.cend(); itr++) { Points.push_back(itr->first); rec_calcMap(itr->second, Points); Points.pop_back(); } } template double Stacked_Container::calc_tot(const dim1Map &subMap) { double tot = 0; // insert first value manually double width, heigth; width = subMap.begin()->first - lowerEdge; heigth = subMap.begin()->second; tot += width*heigth; dim1Map::const_iterator preitr; for (dim1Map::const_iterator curitr = (++subMap.cbegin()); curitr!=subMap.cend(); curitr++) { preitr = curitr; --preitr; width = curitr->first - preitr->first; heigth = curitr->second; tot += width*heigth; } return tot; } template double Stacked_Container::sample(const std::deque &Points, UniRNG *rngUni) { Base_Container::checkEmpty(); if (Points.size() != Base_Container::Dim - 1) { std::stringstream ss; ss<<"Stacked_Container::sample: deque Point dimension missmatch.\n"; throw MyException(ss.str()); } dim1Map *endMap = Base_Container::template getSubMap(Points, Lower_bound_Over); return endMap->lower_bound(rngUni->random())->second; } #endif /* stacked_container_h */