HIPO  4.3.0
High Performance Output data format for experimental physics
twig.h
Go to the documentation of this file.
1 /***************************************************************************
2 * ████████╗██╗ ██╗██╗ ██████╗
3 * ╚══██╔══╝██║ ██║██║██╔════╝
4 * ██║ ██║ █╗ ██║██║██║ ███╗
5 * ██║ ██║███╗██║██║██║ ██║
6 * ██║ ╚███╔███╔╝██║╚██████╔╝
7 * ╚═╝ ╚══╝╚══╝ ╚═╝ ╚═════╝
8 *
9 * ██╗ ██╗██████╗ ██████╗ █████╗ ██████╗ ██╗ ██╗
10 * ██║ ██║██╔══██╗██╔══██╗██╔══██╗██╔══██╗╚██╗ ██╔╝
11 * ██║ ██║██████╔╝██████╔╝███████║██████╔╝ ╚████╔╝
12 * ██║ ██║██╔══██╗██╔══██╗██╔══██║██╔══██╗ ╚██╔╝
13 * ███████╗██║██████╔╝██║ ██║██║ ██║██║ ██║ ██║
14 * ╚══════╝╚═╝╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
15 * DESCRIPTION:
16 * The light weight implementation of histogramming classes for hipo4 package.
17 * Provides simple 1D and 2D (soon to be implemented) histograms, that are
18 * seriazible to HIPO files. The idea is to provide compatibility with TWIG
19 * Java library that provides visualization.
20 * the java library can be found at : https://github.com/gavalian/twig
21 * Author: G.Gavalian
22 * Date: 11/22/2023
23 ****************************************************************************/
24 
30 
31 #ifndef __TWIG_LIBRARY__
32 #define __TWIG_LIBRARY__
33 
34 #include "chart/ascii.h"
35 #include <cstdlib>
36 #include <iostream>
37 
38 namespace twig {
39 
45  class axis {
46  private:
47  std::vector<double> bins;
48  public:
50  axis(){}
55  axis(int n, double min, double max){ init(n,min,max);}
56  virtual ~axis(){}
61  void init(int n, double min, double max){
62  bins.resize(n+1); double step = (max-min)/n;
63  for(int i = 0; i <=n; i++){ bins[i] = min + i*step;}
64  }
68  int find(double value){
69  auto it = std::lower_bound(bins.begin(), bins.end(), value);
70  return it-bins.begin()-1;
71  }
73  int nbins(){ return bins.size()-1;}
75  double center(int bin){ return bins[bin] + 0.5*(bins[bin+1]-bins[bin]);}
77  double min(){ return bins[0];}
79  double max(){return bins[bins.size()-1];}
80  };
81 
89 class h1d {
90 
91  protected:
92  int hid;
94  std::vector<double> container;
95  public:
96 
101  h1d(int n, double min, double max){
102  hid = 0; x.init(n,min,max);container.resize(n+2);
103  }
104 
110  h1d(int __id, int n, double min, double max){
111  hid = __id; x.init(n,min,max);container.resize(n+2);
112  }
114  void id(int __id){ hid = __id;}
116  int id(){return hid;}
119  void fill(double value){
120  int bin = x.find(value); if(bin<0) { container[0] = container[0] + 1.0; return;}
121  if(bin>=x.nbins()) { container[container.size()-1] = container[container.size()-1] + 1.0; return;}
122  container[bin+1] = container[bin+1] + 1.0;
123  }
126  double content(int bin) { return container[bin+1]; }
128  void show(){
129  for(int b = 0; b < x.nbins(); b++) printf("%12.5f %12.5f\n",x.center(b), content(b));
130  }
134  void setContent(int bin, double value){
135  container[bin] = value;
136  }
137 
138 
141  void series(std::vector<double> &data){
142  data.resize(container.size()-2);
143  for(decltype(container)::size_type i = 1; i < container.size()-1;i++) data[i-1] = container[i];
144  }
148  static h1d accumulate(std::vector<h1d> &buffer){
149  h1d h(buffer[0].id(), buffer[0].x.nbins(), buffer[0].x.min(),buffer[0].x.max());
150 
151  for(int bin = 0; bin < buffer[0].x.nbins(); bin++){
152  double content = 0.0;
153  for(int j = 0; j < (int) buffer.size(); j++){
154  content += buffer[j].content(bin);
155  }
156  //printf(" accumulated for bin %d = %f\n",bin,content);
157  h.setContent(bin,content);
158  }
159  return h;
160  }
161 
167  static std::vector<h1d> declare(int count, int bins, double xmin, double xmax){
168  std::vector<h1d> buffer;
169  for(int j = 0; j < count; j++){
170  h1d h(100+j,bins,xmin,xmax);
171  buffer.push_back(std::move(h));
172  }
173  return buffer;
174  }
175 
177  void print(){
178  std::vector<double> data;
179  series(data);
180  //ascii::Asciichart asciichart(std::vector<std::vector<double>>{data});
181  ascii::Asciichart asciichart( {{"h1f",data}});
182 
183  std::string screen = asciichart.height(25).offset(4).Plot();
184  int index = screen.find_first_of("┤");
185  //std::cout << " AXIS x position = " << index << '\n';
186  std::cout << '\n'
187  << asciichart.height(25).offset(4).Plot(); // rescale to -3 ~ +3 lines
188  //<< '\n';
189  std::cout << "\033[38;5;45m";
190  for(int i = 0; i < (index-52); i++) std::cout << " ";
191  std::cout << "\u2514\u252C";
192  for(int i = 0; i < x.nbins()/4; i++) std::cout << "\u2500\u2500\u2500\u252C";
193  std::cout << '\n';
194  for(int i = 0; i < (index-51); i++) std::cout << " ";
195  std::cout << x.min();
196  for(int i = 0; i < x.nbins()-4; i++) std::cout << " ";
197  std::cout << x.max() << "\033[0m" << '\n';
198  }
199  };
200 }
202 #endif
ASCII chart renderer for plotting data series in the terminal.
Definition: ascii.h:35
Asciichart & height(double height)
Set the height of the chart in data units.
Definition: ascii.h:102
std::string Plot()
Generate and return the ASCII chart as a string.
Definition: ascii.h:193
Asciichart & offset(size_t offset)
Set the offset (left margin) for the chart content.
Definition: ascii.h:151
Uniform binning axis for histograms.
Definition: twig.h:45
void init(int n, double min, double max)
Initialize the axis with uniform bins.
Definition: twig.h:61
axis(int n, double min, double max)
Construct an axis with uniform bins.
Definition: twig.h:55
double max()
Definition: twig.h:79
double min()
Definition: twig.h:77
int find(double value)
Find the bin index for a given value.
Definition: twig.h:68
int nbins()
Definition: twig.h:73
virtual ~axis()
Definition: twig.h:56
axis()
Default constructor.
Definition: twig.h:50
double center(int bin)
Definition: twig.h:75
Lightweight 1D histogram.
Definition: twig.h:89
int hid
Definition: twig.h:92
void fill(double value)
Fill the histogram with a value.
Definition: twig.h:119
void series(std::vector< double > &data)
Extract bin contents (excluding under/overflow) into a vector.
Definition: twig.h:141
axis x
Definition: twig.h:93
static std::vector< h1d > declare(int count, int bins, double xmin, double xmax)
Create a vector of identical histograms.
Definition: twig.h:167
static h1d accumulate(std::vector< h1d > &buffer)
Sum multiple histograms bin-by-bin.
Definition: twig.h:148
int id()
Definition: twig.h:116
void print()
Print an ASCII chart of the histogram to stdout.
Definition: twig.h:177
void id(int __id)
Set the histogram ID.
Definition: twig.h:114
h1d(int n, double min, double max)
Construct a histogram with uniform binning.
Definition: twig.h:101
double content(int bin)
Get the content of a bin.
Definition: twig.h:126
std::vector< double > container
Definition: twig.h:94
void setContent(int bin, double value)
Set the content of a bin directly.
Definition: twig.h:134
void show()
Print histogram contents as text columns to stdout.
Definition: twig.h:128
h1d(int __id, int n, double min, double max)
Construct a histogram with an ID and uniform binning.
Definition: twig.h:110
Definition: twig.h:38