CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AsciiHisto.h
Go to the documentation of this file.
1 #ifndef EVENTFILTER_UTILITIES_ASCIIHISTO_H
2 #define EVENTFILTER_UTILITIES_ASCIIHISTO_H
3 
4 #include <string>
5 #include <iostream>
6 #include <sstream>
7 #include <iomanip>
8 
9 #include <vector>
10 #include <deque>
11 
12 namespace evf{
13 
14  class AsciiHisto{
15 
16  public:
17  AsciiHisto(std::string title, unsigned int nbins,float min, float max) :
18  uptodate_(false),
19  nbins_(nbins), xmin_(min), xmax_(max), ymax_(-100000.), cont_(nbins_,0), title_(title)
20  {
21  float span = xmax_-xmin_;
22  binsize_ = span/nbins_;
23  }
24 
25  void fill(float x)
26  {
27  uptodate_ = false;
28  int bin = (x-xmin_)/binsize_;
29  cont_[bin]++;
30  }
31  void fill(float *cont)
32  {
33  uptodate_ = false;
34  for(unsigned int i = 0; i < cont_.size(); i++)
35  cont_[i] = cont[i];
36  }
37 
38  void fillDeque(std::deque<float> & cont)
39  {
40  uptodate_ = false;
41  for(unsigned int i = 0; i < cont_.size(); i++)
42  cont_[i] = cont.at(i);
43  }
44 
45  int maxbin()
46  {
47  int retval = -1;
48  ymax_ = -100000.;
49  for(unsigned int i = 0; i < nbins_; i++)
50  if(ymax_ < cont_[i]){ymax_ = cont_[i]; retval = i;}
51 /* std::cout << "max bin " << retval << " has " << ymax_ << std::endl; */
52 /* std::cout << "nbins " << nbins_ */
53 /* << " binsize " << binsize_ << std::endl; */
54  return retval;
55  }
56  std::string &draw()
57  {
58  if(uptodate_) return current_;
59  maxbin();
60  float yscale = (ymax_*1.2) /20.;
61  std::ostringstream ost;
62  ost << " ^";
63  ost << std::setw(nbins_-title_.size()) << " ";
64  ost << title_ << std::endl;
65  // std::cout << " ^" << " " << title_ << std::endl;
66  for(int j = 20; j>=0; j--)
67  {
68  // std::cout << "--------------> "<< j << std::endl;
69  if(j%5==0){
70  ost << std::setw(8) << yscale*j << "-|";
71  // std::cout << std::setw(8) << yscale*j << "-|";
72  }
73  else{
74  ost << " |";
75  // std::cout << " |";
76  }
77  for(unsigned int i = 0; i < nbins_+5; i++)
78  {
79  if(j==0) {ost << "-"; /*std::cout << "-";*/}
80  else
81  {
82  if(i<nbins_ && cont_[i] > yscale*j)
83  {ost << "*"; /*std::cout << "*";*/}
84  else
85  {ost << " "; /*std::cout << " ";*/}
86  }
87  }
88  if(j==0){ost << ">"; /*std::cout << ">";*/}
89  ost << std::endl;
90  // std::cout << std::endl;
91  }
92  ost << " ";
93  // std::cout << " ";
94 
95  for(unsigned int i = 0; i < nbins_+5; i++)
96  {
97  if(i%10==0)
98  {ost << "|"; /*std::cout << "|";*/}
99  else
100  {ost << " "; /*std::cout << " ";*/}
101  }
102  ost << std::endl;
103  // std::cout << std::endl;
104  ost << std::setw(10) << xmin_ ;
105  // std::cout << std::setw(10) << xmin_;
106  for(unsigned int i = 0; i < nbins_+5; i++)
107  if((i+3)%10==0){ost << std::setw(10) << xmin_+binsize_*(i+3);
108  // std::cout << std::setw(10) << xmin_+binsize_*(i+3);
109  }
110  ost << std::endl;
111  // std::cout << std::endl;
112 
113  current_ = ost.str();
114  return current_;
115  }
116 
117  private:
118  bool uptodate_;
119  unsigned int nbins_;
120  float binsize_;
121  float xmin_;
122  float xmax_;
123  float ymax_;
124  float ymin_;
125  std::vector<float> cont_;
126  std::string current_;
127  std::string title_;
128  };
129 
130 }
131 #endif
void fillDeque(std::deque< float > &cont)
Definition: AsciiHisto.h:38
int i
Definition: DBlmapReader.cc:9
std::string & draw()
Definition: AsciiHisto.h:56
void fill(float *cont)
Definition: AsciiHisto.h:31
#define min(a, b)
Definition: mlp_lapack.h:161
AsciiHisto(std::string title, unsigned int nbins, float min, float max)
Definition: AsciiHisto.h:17
const T & max(const T &a, const T &b)
int j
Definition: DBlmapReader.cc:9
std::vector< float > cont_
Definition: AsciiHisto.h:125
unsigned int nbins_
Definition: AsciiHisto.h:119
int cont
std::string title_
Definition: AsciiHisto.h:127
void fill(float x)
Definition: AsciiHisto.h:25
x
Definition: VDTMath.h:216
std::string current_
Definition: AsciiHisto.h:126