Substract a function from a histogram

Dear All,

I’m trying to subtract a function “f” from a histogram “h” but for some reason it’s not working.
Could you please let me know what I’m doing wrong?

TF1 *f = new TF1("f","pol4");
TH1F *h = new TH1F("h","",1000,2.8,3.59);

chain->Draw("Mph>>h");
h->Fit("f");
h->Add(f,-1);
h->Draw();

The fit is done perfectly, but the function is not being subtracted.

Hi,

In ROOT, functions are only “available” in a given range - which defaults to [0,1]. (That might be surprising and we’re fixing that for the new interface generation.). Thus this does it:

TF1 *f = new TF1("f","pol4",2.8,3.59); //  NOTE the range!
TH1F *h = new TH1F("h","",1000,2.8,3.59);

chain->Draw("Mph>>h");
h->Fit("f");
h->Add(f,-1);
h->Draw();
1 Like

Thank you very much Axel, it worked :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.