User API

We describe here some of the public methods potentially seen by a user doing analysis.

Booking and filling

    h1d(const std::string& title,unsigned int Xnumber,double Xmin,double Xmax);
    h1d(const std::string& title,const std::vector<double>& edges);

    bool fill(double X,double Weight = 1);

example :

    #include <tools/histo/h1d>
    #include <tools/randd>
    ...
    tools::histo::h1d h("Gauss",100,-5,5);
    tools::rgaussd rg(1,2);
    for(unsigned int count=0;count<entries;count++) h.fill(rg.shoot(),1.4);

Mean and rms

    tools::histo::h1d h("Gauss",100,-5,5);
    ...
    std::cout << " mean " << h.mean() << ", rms " << h.rms() << std::endl;

Bin infos

When doing a :

    bool fill(double X,double Weight = 1);

the histogram class maintains, for each bin, the number of entries, the sum of weights that we can note "Sw", the sum of W by W "Sw2", the sum of X by Weight "Sxw", the sum of X by X by W "Sx2w". Then bin method names reflect these notations, for example to get the 50 bin sum of X*X*W :

    double Sx2w = h.bin_Sx2w(50);

and the same for the other sums :

    double Sw = h.bin_Sw(50);
    double Sw2 = h.bin_Sw2(50);
    double Sxw = h.bin_Sxw(50);
    unsigned int n = h.bin_entries(50);

You can have also all infos on all bins with:

    tools::histo::h1d h(...);
    ...
    const std::vector<unsigned int>& _entries = h.bins_entries();
    const std::vector<double>& _bins_sum_w = h.bins_sum_w();
    const std::vector<double>& _bins_sum_w2 = h.bins_sum_w2();
    const std::vector< std::vector<double> >& _bins_sum_xw = h.bins_sum_xw();
    const std::vector< std::vector<double> >& _bins_sum_x2w = h.bins_sum_x2w();

for example to dump bin 50 of an histo booked with 100 bins:

    std::cout << "entries[50] = " << _entries[50] << std::endl;
    std::cout << "  sum_w[50] = " << _bins_sum_w[50] << std::endl;
    std::cout << " sum_w2[50] = " << _bins_sum_w2[50] << std::endl;
    std::cout << " sum_xw[50] = " << _bins_sum_xw[50][0] << std::endl;   //0 = xaxis
    std::cout << "sum_x2w[50] = " << _bins_sum_x2w[50][0] << std::endl;  //0 = xaxis

(Take care that the [0] entries in the upper vectors are for the "underflow bin" and the last one is for the "overflow bin").

All data

You can get all internal data of an histo through the histo_data class:

    const tools::histo::h1d::hd_t& hdata = h.dac();  //dac=data access.

and then, for example, find back the bins infos with:

    const std::vector<unsigned int>& _entries = hdata.m_bin_entries;
    const std::vector<double>& _bins_sum_w = hdata.m_bin_Sw;
    const std::vector<double>& _bins_sum_w2 = hdata.m_bin_Sw2;
    const std::vector< std::vector<double> >& _bins_sum_xw = hdata.m_bin_Sxw;
    const std::vector< std::vector<double> >& _bins_sum_x2w = hdata.m_bin_Sx2w;
    // dump bin 50 :
    std::cout << "entries[50] = " << _entries[50] << std::endl;
    std::cout << "  sum_w[50] = " << _bins_sum_w[50] << std::endl;
    std::cout << " sum_w2[50] = " << _bins_sum_w2[50] << std::endl;
    std::cout << " sum_xw[50] = " << _bins_sum_xw[50][0] << std::endl;   //0 = xaxis
    std::cout << "sum_x2w[50] = " << _bins_sum_x2w[50][0] << std::endl;  //0 = xaxis

See the tools/histo/histo_data class for all internal fields.

Projections

From a 2D histo, you can get the x projection with:

    tools::histo::h1d* projection = tools::histo::projection_x(h2d,"ProjX");
    ...
    delete projection;

See test/cpp/histo.cpp for example code. Other slicing and projection methods are:

  // h2d -> h1d. (User gets ownership of the returned object).
  h1d* slice_x(const h2d&,int y_beg_ibin,int y_end_ibin,const std::string& title);
  h1d* projection_x(const h2d&,const std::string& title);
  h1d* slice_y(const h2d&,int x_beg_ibin,int x_end_ibin,const std::string& title);
  h1d* projection_y(const h2d&,const std::string& title);
  // h2d -> p1d. (User gets ownership of the returned object).
  p1d* profile_x(const h2d&,int y_beg_ibin,int y_end_ibin,const std::string& title);
  p1d* profile_x(const h2d&,const std::string&);
  p1d* profile_y(const h2d&,int x_beg_ibin,int x_end_ibin,const std::string& title);
  p1d* profile_y(const h2d&,const std::string& title);
  // h3d -> h2d. (User gets ownership of the returned object).
  h2d* slice_xy(const h3d&,int z_beg_ibin,int z_end_ibin,const std::string& title);
  h2d* projection_xy(const h3d&,const std::string& title);
  h2d* slice_yz(const h3d&,int x_beg_ibin,int x_end_ibin,const std::string& title);
  h2d* projection_yz(const h3d&,const std::string& title);
  h2d* slice_xz(const h3d&,int y_beg_ibin,int y_end_ibin,const std::string& title);
  h2d* projection_xz(const h3d&,const std::string& title);

Bin tweaking

Obviously, you should NEVER do that :

    tools::histo::h1d h(...);
    ...
    unsigned int n;
    double Sw,Sw2,Sxw,Sx2w;
    h.get_bin_content(50,n,Sw,Sw2,Sxw,Sx2w);
    ... // do something naughty on n,Sw,Sw2,Sxw,Sx2w.
    h.set_bin_content(50,n,Sw,Sw2,Sxw,Sx2w); //psss

with a 2D histo :

    tools::histo::h2d h(...);
    ...
    unsigned int n;
    double Sw,Sw2,Sxw,Sx2w,Syw,Sy2w;
    h.get_bin_content(50,n,Sw,Sw2,Sxw,Sx2w,Syw,Sy2w);
    ... // do something naughty on n,Sw,Sw2,Sxw,Sx2w,Syw,Sy2w.
    h.set_bin_content(50,n,Sw,Sw2,Sxw,Sx2w,Syw,Sy2w); //psss