HIPO4 C++ Library 4.4.1
Columnar I/O library for CLAS12 physics data
Loading...
Searching...
No Matches
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
25#ifndef __TWIG_LIBRARY__
26#define __TWIG_LIBRARY__
27
28#include "chart/ascii.h"
29#include <cstdlib>
30#include <iostream>
31
32namespace twig {
33
34 class axis {
35 private:
36 std::vector<double> bins;
37 public:
38 axis(){}
39 axis(int n, double min, double max){ init(n,min,max);}
40 virtual ~axis(){}
41 void init(int n, double min, double max){
42 bins.resize(n+1); double step = (max-min)/n;
43 for(int i = 0; i <=n; i++){ bins[i] = min + i*step;}
44 }
45 int find(double value){
46 auto it = std::lower_bound(bins.begin(), bins.end(), value);
47 return it-bins.begin()-1;
48 } int nbins(){ return bins.size()-1;}
49 double center(int bin){ return bins[bin] + 0.5*(bins[bin+1]-bins[bin]);}
50 double min(){ return bins[0];}
51 double max(){return bins[bins.size()-1];}
52 };
53
54class h1d {
55
56 protected:
57 int hid;
59 std::vector<double> container;
60 public:
61
62 h1d(int n, double min, double max){
63 hid = 0; x.init(n,min,max);container.resize(n+2);
64 }
65
66 h1d(int __id, int n, double min, double max){
67 hid = __id; x.init(n,min,max);container.resize(n+2);
68 }
69 void id(int __id){ hid = __id;}
70 int id(){return hid;}
71 void fill(double value){
72 int bin = x.find(value); if(bin<0) { container[0] = container[0] + 1.0; return;}
73 if(bin>=x.nbins()) { container[container.size()-1] = container[container.size()-1] + 1.0; return;}
74 container[bin+1] = container[bin+1] + 1.0;
75 }
76 double content(int bin) { return container[bin+1]; }
77 void show(){
78 for(int b = 0; b < x.nbins(); b++) printf("%12.5f %12.5f\n",x.center(b), content(b));
79 }
80 void setContent(int bin, double value){
81 container[bin] = value;
82 }
83
84
85 void series(std::vector<double> &data){
86 data.resize(container.size()-2);
87 for(decltype(container)::size_type i = 1; i < container.size()-1;i++) data[i-1] = container[i];
88 }
89 static h1d accumulate(std::vector<h1d> &buffer){
90 h1d h(buffer[0].id(), buffer[0].x.nbins(), buffer[0].x.min(),buffer[0].x.max());
91
92 for(int bin = 0; bin < buffer[0].x.nbins(); bin++){
93 double content = 0.0;
94 for(int j = 0; j < (int) buffer.size(); j++){
95 content += buffer[j].content(bin);
96 }
97 //printf(" accumulated for bin %d = %f\n",bin,content);
98 h.setContent(bin,content);
99 }
100 return h;
101 }
102
103 static std::vector<h1d> declare(int count, int bins, double xmin, double xmax){
104 std::vector<h1d> buffer;
105 for(int j = 0; j < count; j++){
106 h1d h(100+j,bins,xmin,xmax);
107 buffer.push_back(std::move(h));
108 }
109 return buffer;
110 }
111
112 void print(){
113 std::vector<double> data;
114 series(data);
115 //ascii::Asciichart asciichart(std::vector<std::vector<double>>{data});
116 ascii::Asciichart asciichart( {{"h1f",data}});
117
118 std::string screen = asciichart.height(25).offset(4).Plot();
119 int index = screen.find_first_of("┤");
120 //std::cout << " AXIS x position = " << index << '\n';
121 std::cout << '\n'
122 << asciichart.height(25).offset(4).Plot(); // rescale to -3 ~ +3 lines
123 //<< '\n';
124 std::cout << "\033[38;5;45m";
125 for(int i = 0; i < (index-52); i++) std::cout << " ";
126 std::cout << "\u2514\u252C";
127 for(int i = 0; i < x.nbins()/4; i++) std::cout << "\u2500\u2500\u2500\u252C";
128 std::cout << '\n';
129 for(int i = 0; i < (index-51); i++) std::cout << " ";
130 std::cout << x.min();
131 for(int i = 0; i < x.nbins()-4; i++) std::cout << " ";
132 std::cout << x.max() << "\033[0m" << '\n';
133 }
134 };
135}
136#endif
Definition ascii.h:18
Asciichart & height(double height)
Set height of chart.
Definition ascii.h:62
std::string Plot()
Generate this chart.
Definition ascii.h:112
Asciichart & offset(size_t offset)
Set offset of label from axis.
Definition ascii.h:88
Definition twig.h:34
void init(int n, double min, double max)
Definition twig.h:41
axis(int n, double min, double max)
Definition twig.h:39
double max()
Definition twig.h:51
double min()
Definition twig.h:50
int find(double value)
Definition twig.h:45
int nbins()
Definition twig.h:48
virtual ~axis()
Definition twig.h:40
axis()
Definition twig.h:38
double center(int bin)
Definition twig.h:49
Definition twig.h:54
int hid
Definition twig.h:57
void fill(double value)
Definition twig.h:71
void series(std::vector< double > &data)
Definition twig.h:85
axis x
Definition twig.h:58
static h1d accumulate(std::vector< h1d > &buffer)
Definition twig.h:89
static std::vector< h1d > declare(int count, int bins, double xmin, double xmax)
Definition twig.h:103
int id()
Definition twig.h:70
void print()
Definition twig.h:112
void id(int __id)
Definition twig.h:69
h1d(int n, double min, double max)
Definition twig.h:62
double content(int bin)
Definition twig.h:76
std::vector< double > container
Definition twig.h:59
void setContent(int bin, double value)
Definition twig.h:80
void show()
Definition twig.h:77
h1d(int __id, int n, double min, double max)
Definition twig.h:66
Definition twig.h:32