Create sliced projections

Input program

/* slice_proj.cpp */
#include <iostream.h>
#include <iomanip.h> 		// Formatting output string.
#include "HTL/Histograms.h" // Transient histograms.
class Histo_App 
{
public:
  Histo_App() {}
public:
  void run();
private:
  void slice ();
  void project ();
  Histo2D *histo;
};
//
// Implementation:
//
void Histo_App::run() {
  // Create a 2D histo using Weighted_Bin and Even_Partition:
  histo = new Histo2D( "Histo_2D",10, 5., 15., 10, 5., 15. );
  // Let's fill the histo with 50000 points:
  double x, w = 0.5;
  for( long i=0; i<50000; i++ ) {
    x = -i*sin(float(i));
    histo->fill(x,x,w);
    histo->fill(20.-x,x,w);
  }
  cout << "2D histogram content" << endl;
  for( long y=0; y<histo->partition_Y().bin_count(); y++ )
    for( long x=0; x<histo->partition_X().bin_count(); x++ ) {
	cout << "bin(" << x << "," << y << "): "
	     << histo->bin(x,y).value() << " -/+ "
	     << histo->bin(x,y).error() << endl;
    }
  // Make two slices
  slice();
  // and two projections
  project();
  delete histo;
}
void Histo_App::slice() {
  long x;
  HSlicer mySlicer;
  // First slice in X . The 2nd parameter is the bin no. along y (0->first bin)
  Histo1DVar *slice1 = mySlicer.xBand (*histo,0);
  // First slice in Y . The 2nd parameter is the x coordinate
  Histo1DVar *slice2 = mySlicer.yBand (*histo,5.);
  cout << endl << "First slice";
  for( x=0; x<slice1->bin_count(); x++ ) {
    cout << endl << ". " << setw(2) << x << ": "
	 << setw(9) << slice1->i_bin(x).value() << " +/- "
	 << setw(4) << slice1->i_bin(x).error();
  }
  cout << endl << endl << "Second slice";
  for( x=0; x<slice2->bin_count(); x++ ) {
    cout << endl << ". " << setw(2) << x << ": "
	 << setw(9) << slice2->i_bin(x).value() << " +/- "
	 << setw(4) << slice2->i_bin(x).error();
  }
  delete slice1;
  delete slice2;
}
void Histo_App::project() {
  long x;
  HSlicer myProjector;
  Histo1DVar *proj1 = myProjector.xProject (*histo);
  Histo1DVar *proj2 = myProjector.yProject (*histo);
  cout << endl << endl << "First projection";
  for( x=0; x<proj1->bin_count(); x++ ) {
    cout << endl << ". " << setw(2) << x << ": "
	 << setw(9) << proj1->i_bin(x).value() << " +/- "
	 << setw(4) << proj1->i_bin(x).error();
  }
  cout << endl << endl << "Second projection";
  for( x=0; x<proj2->bin_count(); x++ ) {
    cout << endl << ". " << setw(2) << x << ": "
	 << setw(9) << proj2->i_bin(x).value() << " +/- "
	 << setw(4) << proj2->i_bin(x).error();
  }
  cout << endl;
  delete proj1;
  delete proj2;
}
int main( int argc, char **argv )
{
  Histo_App app;
  app.run();
}

Output Generated

2D histogram content
bin(0,0): 1 -/+ 0.707107
bin(1,0): 0 -/+ 0
bin(2,0): 0 -/+ 0
bin(3,0): 0 -/+ 0
bin(4,0): 0 -/+ 0
bin(5,0): 0 -/+ 0
bin(6,0): 0 -/+ 0
bin(7,0): 0 -/+ 0
bin(8,0): 0 -/+ 0
bin(9,0): 1 -/+ 0.707107
bin(0,1): 0 -/+ 0
bin(1,1): 2 -/+ 1
bin(2,1): 0 -/+ 0
bin(3,1): 0 -/+ 0
bin(4,1): 0 -/+ 0
bin(5,1): 0 -/+ 0
bin(6,1): 0 -/+ 0
bin(7,1): 0 -/+ 0
bin(8,1): 2 -/+ 1
bin(9,1): 0 -/+ 0
bin(0,2): 0 -/+ 0
bin(1,2): 0 -/+ 0
bin(2,2): 2 -/+ 1
bin(3,2): 0 -/+ 0
bin(4,2): 0 -/+ 0
bin(5,2): 0 -/+ 0
bin(6,2): 0 -/+ 0
bin(7,2): 2 -/+ 1
bin(8,2): 0 -/+ 0
bin(9,2): 0 -/+ 0
bin(0,3): 0 -/+ 0
bin(1,3): 0 -/+ 0
bin(2,3): 0 -/+ 0
bin(3,3): 0.5 -/+ 0.5
bin(4,3): 0 -/+ 0
bin(5,3): 0 -/+ 0
bin(6,3): 0.5 -/+ 0.5
bin(7,3): 0 -/+ 0
bin(8,3): 0 -/+ 0
bin(9,3): 0 -/+ 0
bin(0,4): 0 -/+ 0
bin(1,4): 0 -/+ 0
bin(2,4): 0 -/+ 0
bin(3,4): 0 -/+ 0
bin(4,4): 1 -/+ 0.707107
bin(5,4): 1 -/+ 0.707107
bin(6,4): 0 -/+ 0
bin(7,4): 0 -/+ 0
bin(8,4): 0 -/+ 0
bin(9,4): 0 -/+ 0
bin(0,5): 0 -/+ 0
bin(1,5): 0 -/+ 0
bin(2,5): 0 -/+ 0
bin(3,5): 0 -/+ 0
bin(4,5): 2 -/+ 1
bin(5,5): 2 -/+ 1
bin(6,5): 0 -/+ 0
bin(7,5): 0 -/+ 0
bin(8,5): 0 -/+ 0
bin(9,5): 0 -/+ 0
bin(0,6): 0 -/+ 0
bin(1,6): 0 -/+ 0
bin(2,6): 0 -/+ 0
bin(3,6): 1 -/+ 0.707107
bin(4,6): 0 -/+ 0
bin(5,6): 0 -/+ 0
bin(6,6): 1 -/+ 0.707107
bin(7,6): 0 -/+ 0
bin(8,6): 0 -/+ 0
bin(9,6): 0 -/+ 0
bin(0,7): 0 -/+ 0
bin(1,7): 0 -/+ 0
bin(2,7): 2 -/+ 1
bin(3,7): 0 -/+ 0
bin(4,7): 0 -/+ 0
bin(5,7): 0 -/+ 0
bin(6,7): 0 -/+ 0
bin(7,7): 2 -/+ 1
bin(8,7): 0 -/+ 0
bin(9,7): 0 -/+ 0
bin(0,8): 0 -/+ 0
bin(1,8): 1.5 -/+ 0.866025
bin(2,8): 0 -/+ 0
bin(3,8): 0 -/+ 0
bin(4,8): 0 -/+ 0
bin(5,8): 0 -/+ 0
bin(6,8): 0 -/+ 0
bin(7,8): 0 -/+ 0
bin(8,8): 1.5 -/+ 0.866025
bin(9,8): 0 -/+ 0
bin(0,9): 1.5 -/+ 0.866025
bin(1,9): 0 -/+ 0
bin(2,9): 0 -/+ 0
bin(3,9): 0 -/+ 0
bin(4,9): 0 -/+ 0
bin(5,9): 0 -/+ 0
bin(6,9): 0 -/+ 0
bin(7,9): 0 -/+ 0
bin(8,9): 0 -/+ 0
bin(9,9): 1.5 -/+ 0.866025
First slice
.  0:         1 +/- 0.707107
.  1:         0 +/-    0
.  2:         0 +/-    0
.  3:         0 +/-    0
.  4:         0 +/-    0
.  5:         0 +/-    0
.  6:         0 +/-    0
.  7:         0 +/-    0
.  8:         0 +/-    0
.  9:         1 +/- 0.707107
Second slice
.  0:         1 +/- 0.707107
.  1:         0 +/-    0
.  2:         0 +/-    0
.  3:         0 +/-    0
.  4:         0 +/-    0
.  5:         0 +/-    0
.  6:         0 +/-    0
.  7:         0 +/-    0
.  8:         0 +/-    0
.  9:       1.5 +/- 0.866025
First projection
.  0:         2 +/-    1
.  1:         4 +/- 1.41421
.  2:         4 +/- 1.41421
.  3:         1 +/- 0.707107
.  4:         2 +/-    1
.  5:         4 +/- 1.41421
.  6:         2 +/-    1
.  7:         4 +/- 1.41421
.  8:         3 +/- 1.22474
.  9:         3 +/- 1.22474
Second projection
.  0:       2.5 +/- 1.11803
.  1:       3.5 +/- 1.32288
.  2:         4 +/- 1.41421
.  3:       1.5 +/- 0.866025
.  4:         3 +/- 1.22474
.  5:         3 +/- 1.22474
.  6:       1.5 +/- 0.866025
.  7:         4 +/- 1.41421
.  8:       3.5 +/- 1.32288
.  9:       2.5 +/- 1.11803