-
Notifications
You must be signed in to change notification settings - Fork 1
/
histogram.h
50 lines (42 loc) · 952 Bytes
/
histogram.h
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef HISTOGRAM_H
#define HISTOGRAM_H 1
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <assert.h>
class Histogram {
public:
Histogram();
Histogram(const float xmin, const float xmax, const int nbin);
~Histogram();
void initialize(const float xmin, const float xmax, const int nbin);
void clear();
long long ndata() const { return ntot; }
int operator[](const int i) const { return n[i]; }
void add(const float x) {
#ifdef _OPENMP
#pragma omp atomic
#endif
ntot++;
if(xmin <= x && x < xmax) {
int i= (int)((x-xmin)/(xmax-xmin)*nbin);
assert(0<=i && i<nbin);
#ifdef _OPENMP
#pragma omp atomic
#endif
n[i]++;
}
}
void write(FILE* const fp) const;
void read(FILE* const fp);
void write_header(FILE* const fp) const;
void print() const;
Histogram& operator+=(const Histogram&);
private:
int *n;
long long ntot;
double sum;
float xmin, xmax;
int nbin;
};
#endif