votca 2025.1-dev
Loading...
Searching...
No Matches
histogramnew.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2025 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <algorithm>
20
21// Local VOTCA includes
23
24using namespace std;
25namespace votca {
26namespace tools {
27
29 if (periodic_) {
30 step_ = (max_ - min_) / double(nbins_);
31 } else {
32 step_ = (max_ - min_) / (double(nbins_) - 1.0);
33 }
34
35 if (nbins_ == 1) {
36 step_ = 1;
37 }
38 data_.resize(nbins_);
39 double v = min_;
40 for (Index i = 0; i < nbins_; v += step_, ++i) {
41 data_.x(i) = v;
42 }
43 data_.y() = Eigen::VectorXd::Zero(nbins_);
44 data_.yerr() = Eigen::VectorXd::Zero(nbins_);
45 data_.flags() = std::vector<char>(nbins_, 'i');
46}
47
48void Histogram::Initialize(double min, double max, Index nbins) {
49 min_ = min;
50 max_ = max;
51 nbins_ = nbins;
53}
54
55void Histogram::Process(const double &v, double scale) {
56
57 Index i = (Index)floor((v - min_) / step_ + 0.5);
58 if (i < 0 || i >= nbins_) {
59 if (periodic_) {
60 if (i < 0) {
61 i = nbins_ - ((-i) % nbins_);
62 } else {
63 i = i % nbins_;
64 }
65 } else {
66 return;
67 }
68 }
69 data_.y(i) += scale;
70}
71
72double Histogram::getMinBinVal() const { return data_.getMinY(); }
73
74double Histogram::getMaxBinVal() const { return data_.getMaxY(); }
75
76pair<double, double> Histogram::getInterval(Index bin) const {
77 pair<double, double> bounds;
78 double value = static_cast<double>(bin);
79 bounds.first = value * step_ + min_;
80 bounds.second += step_;
81 return bounds;
82}
83
85 double area = 0;
86 area = data_.y().cwiseAbs().sum() * step_;
87 double scale = 1. / area;
88 data_.y() *= scale;
89}
90
92 data_.y() = Eigen::VectorXd::Zero(nbins_);
93 data_.yerr() = Eigen::VectorXd::Zero(nbins_);
94}
95
96} // namespace tools
97} // namespace votca
double getMinBinVal() const
Get the count of the bin with the fewest counts.
void Normalize()
normalize the histogram that the integral is 1
void Process(const double &v, double scale=1.0)
process a data point
std::pair< double, double > getInterval(Index bin) const
Given the bin number get the Inverval bounds.
void Clear()
clear all data
double getMaxBinVal() const
Get the count of the bin with the maximum counts.
void Initialize(double min, double max, Index nbins)
Initialize the Histogram.
STL namespace.
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26