# Subtract 2 histograms

Hi!
I have two 2D histograms which I’ve already projected to 1D. I want to subtract the histogram from each other and plot it as one.
Here is the code:

{
//Histogram 1
TH2D *H1 = new TH2D(“H1”,"",3,0.,2.0,100,0,3 );
H1->Fill(0.5,2);
H1->Fill(1.5,2);
H1->Fill(1.5,1.5);
TH1D *H1P = H1->ProfileX();
H1P->Draw();

//Histogram 2
TH2D *H2 = new TH2D(“H1”,"",3,0.,2.0,100,0,3 );
H2->Fill(0.5,1.2);
H2->Fill(1.5,1.7);
H2->Fill(0.8,2);
TH1D *H2P = H2->ProfileX();
H2P->Draw(“SAME”);

}
I want to subtract H2 from H1 and get back a single plot.
Thanks in anticipation for a favorable response.

If you want to project a 2D histogram to a 1D histogram, you should use the Projection function rather than the Profile function (Look at the return values here: https://root.cern.ch/doc/master/classTH2.html)
The substraction of two histgrams can be understand as an addition with factor -1 and this is exactly the way it is implemented in root. If you want to keep the original histograms you need to clone the first one first:

``````{
//Histogram 1
TH2D *H1 = new TH2D(“H1”,"",3,0.,2.0,100,0,3 );
H1->Fill(0.5,2);
H1->Fill(1.5,2);
H1->Fill(1.5,1.5);
TH1D *H1P = H1->ProjectionX();
H1P->Draw();

//Histogram 2
TH2D *H2 = new TH2D(“H1”,"",3,0.,2.0,100,0,3 );
H2->Fill(0.5,1.2);
H2->Fill(1.5,1.7);
H2->Fill(0.8,2);
TH1D *H2P = H2->ProjectionX();
H2P->Draw(“SAME”);

// Substraction
TH1D* hDiff = (TH1D*) H1P->Clone("hDiff");