# Subtracting two histograms with different axis limit

I made two histograms with different functions having different domain. I tried to subtract the two histograms by doing the following, but it gave me junk. What is the propoer way to subtract two histograms with different axis limit?

``````TF1 fun1("fun1", "exp(-x)", 0, 5.)
TF1 fun2("fun2", "exp(-2*x)", 0, 3.)

TH1F *hfun1 = fun1.GetHistogram()
TH1F *hfun2 = fun2.GetHistogram()

hfun1->Draw() gives me junk result.

Thanks,
asubedi

The warning is just a warning. You can bypass it as shown in the example below:

Rene

void reb() {
TCanvas *c1 = new TCanvas(“c1”,“c1”,800,1000);
c1->Divide(1,2);
c1->cd(1);
TF1 *fun1 = new TF1(“fun1”, “exp(-x)”, 0, 5.);
TF1 fun2 = new TF1(“fun2”, "exp(-2x)", 0, 3.);

fun1->Draw();
fun2->Draw(“same”);

TH1F *hfun1 = new TH1F(“hfun1”,“fun1”,100,0,5);
TH1F *hfun2 = new TH1F(“hfun2”,“fun2”,100,0,3);
hfun1->Eval(fun1);
hfun2->Eval(fun2);

c1->cd(2);
hfun1->DrawCopy();
hfun2->Draw(“same”);
//to bypass the warning
hfun2->GetXaxis()->SetLimits(0,5);
hfun2->GetXaxis()->SetLimits(0,3);
hfun1->SetLineColor(kBlue);
hfun1->Draw(“same”);
}

But compare the resulting histogram with the function:

TF1 fun3 = new TF1(“fun3”, "exp(-x) - exp(-2x)", 0, 5.)

The shape of fun3 and the histogram hfun1->Add(hfun2, -1) should be the same.

I am using version 4.00/06

The Warning message is appropriate because the subtraction is bin by bin.
In your case, you should do:

void reb() {
TCanvas *c1 = new TCanvas(“c1”,“c1”,800,1000);
c1->Divide(1,2);
c1->cd(1);
TF1 *fun1 = new TF1(“fun1”, “exp(-x)”, 0, 5.);
TF1 fun2 = new TF1(“fun2”, "exp(-2x)", 0, 3.);
TF1 fun3 = new TF1(“fun3”, "exp(-x) - exp(-2x)", 0, 5.);
fun1->Draw();
fun2->Draw(“same”);
fun3->Draw(“same”);

TH1F *hfun1 = new TH1F(“hfun1”,“fun1”,100,0,5);
TH1F *hfun2 = new TH1F(“hfun2”,“fun2”,100,0,5);
hfun1->Eval(fun1);
hfun2->Eval(fun2);

c1->cd(2);
hfun1->DrawCopy();
hfun2->Draw(“same”);
Thanks rene. But doing hfun2->Eval(fun2) evalutates fun2 over whole axis of hfun2. Is the option `r' for Eval() implemented? In the TH1.cxx.html at htmldocs, it looks like only options`a ’ and `s’ are implemented.