CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/EventFilter/Utilities/interface/AsciiHisto.h

Go to the documentation of this file.
00001 #ifndef EVENTFILTER_UTILITIES_ASCIIHISTO_H
00002 #define EVENTFILTER_UTILITIES_ASCIIHISTO_H
00003 
00004 #include <string>
00005 #include <iostream>
00006 #include <sstream>
00007 #include <iomanip>
00008 
00009 #include <vector>
00010 #include <deque>
00011 
00012 namespace evf{
00013   
00014   class AsciiHisto{
00015     
00016   public:
00017     AsciiHisto(std::string title, unsigned int nbins,float min, float max) : 
00018       uptodate_(false),  
00019       nbins_(nbins), xmin_(min), xmax_(max), ymax_(-100000.), cont_(nbins_,0), title_(title)
00020     {
00021       float span = xmax_-xmin_;
00022       binsize_ = span/nbins_;
00023     }
00024     
00025     void fill(float x)
00026     {
00027       uptodate_ = false;
00028       int bin = (x-xmin_)/binsize_;
00029       cont_[bin]++;
00030     }
00031     void fill(float *cont)
00032     {
00033       uptodate_ = false;
00034       for(unsigned int i = 0; i < cont_.size(); i++)
00035         cont_[i] = cont[i];
00036     }
00037 
00038     void fillDeque(std::deque<float> & cont)
00039     {
00040       uptodate_ = false;
00041       for(unsigned int i = 0; i < cont_.size(); i++)
00042         cont_[i] = cont.at(i);
00043     }
00044 
00045     int maxbin()
00046     {
00047       int retval = -1;
00048       ymax_ = -100000.;
00049       for(unsigned int i = 0; i < nbins_; i++)
00050         if(ymax_ < cont_[i]){ymax_ = cont_[i]; retval = i;}
00051 /*       std::cout << "max bin " << retval << " has " << ymax_ << std::endl; */
00052 /*       std::cout << "nbins " << nbins_  */
00053 /*              << " binsize " << binsize_ << std::endl; */
00054       return retval;
00055     }
00056     std::string &draw()
00057     {
00058       if(uptodate_) return current_;
00059       maxbin();
00060       float yscale = (ymax_*1.2) /20.;
00061       std::ostringstream ost;
00062       ost << "         ^"; 
00063       ost << std::setw(nbins_-title_.size()) << " ";                
00064       ost << title_ << std::endl;
00065       //      std::cout << "         ^" <<  "                " << title_ << std::endl;
00066       for(int j = 20; j>=0; j--)
00067         {
00068           //      std::cout << "--------------> "<< j << std::endl;
00069           if(j%5==0){
00070             ost << std::setw(8) << yscale*j << "-|";
00071             //      std::cout << std::setw(8) << yscale*j << "-|";
00072           }
00073           else{
00074             ost << "         |";
00075             //      std::cout  << "         |";
00076           }
00077           for(unsigned int i = 0; i < nbins_+5; i++)
00078             {
00079               if(j==0) {ost << "-"; /*std::cout << "-";*/}
00080               else
00081                 {
00082                   if(i<nbins_ && cont_[i] > yscale*j) 
00083                     {ost << "*"; /*std::cout << "*";*/}
00084                   else
00085                     {ost << " "; /*std::cout << " ";*/}
00086                 }
00087             }
00088           if(j==0){ost << ">"; /*std::cout << ">";*/}
00089           ost << std::endl;
00090           //      std::cout << std::endl;
00091         }
00092       ost << "         ";
00093       //      std::cout  << "         ";
00094 
00095       for(unsigned int i = 0; i < nbins_+5; i++)
00096         {
00097           if(i%10==0)
00098             {ost << "|"; /*std::cout << "|";*/}
00099           else
00100             {ost << " "; /*std::cout << " ";*/}
00101         }
00102       ost << std::endl;
00103       //      std::cout << std::endl;
00104       ost << std::setw(10) << xmin_ ;
00105       //      std::cout << std::setw(10) << xmin_; 
00106       for(unsigned int i = 0; i < nbins_+5; i++)
00107         if((i+3)%10==0){ost << std::setw(10) << xmin_+binsize_*(i+3); 
00108           //      std::cout << std::setw(10) << xmin_+binsize_*(i+3); 
00109         }
00110       ost << std::endl;
00111       //      std::cout << std::endl;
00112       
00113       current_ = ost.str();
00114       return current_;
00115     }
00116 
00117   private:
00118     bool uptodate_;
00119     unsigned int nbins_;
00120     float binsize_;
00121     float xmin_;
00122     float xmax_;
00123     float ymax_;
00124     float ymin_;
00125     std::vector<float> cont_;
00126     std::string current_;
00127     std::string title_;
00128   };
00129 
00130 }
00131 #endif