Example of Integration in TH1 & TF1

In [1]:
//%jsroot on
auto c1=new TCanvas;

1.Create Backgrond and Signal Function

In [2]:
TF1 *fB = new TF1("fB","pol1",0,10);
TF1 *fS = new TF1("fS","gaus",0,10);
TF1 *fSB = new TF1("fSB","pol1+gaus(2)",0,10);
fB->SetParameters(600,70);
fS->SetParameters(340,5,0.2);
fSB->SetParameters(600,70,340,5,0.2);
fSB->SetMinimum(0);
fSB->Draw();
fSB->SetLineColor(kBlue);
fS->Draw("same");
fB->Draw("same");
c1->Draw();

2.Create Histograms

In [3]:
TH1D *hB=new TH1D("hB","hB",100,0,10);
TH1D *hS=new TH1D("hS","hS",100,0,10);
TH1D *hSB=new TH1D("hSB","hSB",100,0,10);
hB->FillRandom("fB",50000);
hS->FillRandom("fS",2000);
hB->FillRandom("fB",50000);
hSB->Add(hB,hS,1,1);

3.Separate Background & Signals by fitting Background+Signal histogram

In [4]:
double par[5];
hSB->Fit("fSB","","",3.5,6.5);
fSB->GetParameters(&par[0]);
fB->SetParameters(&par[0]);
fS->SetParameters(&par[2]);
hSB->SetMinimum(0);
hSB->Draw();
hSB->SetLineColor(kRed);
hS->Draw("same");
fS->Draw("same");
hB->Draw("same");
c1->Draw();
 FCN=26.9453 FROM MIGRAD    STATUS=CONVERGED     148 CALLS         149 TOTAL
                     EDM=7.57716e-08    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  p0           6.53111e+02   3.40335e+01   1.53251e-02  -4.63201e-05
   2  p1           7.13021e+01   6.78329e+00   3.05147e-03  -2.41271e-04
   3  p2           3.91895e+02   2.46912e+01   5.09922e-02  -7.46021e-06
   4  p3           5.02095e+00   1.33812e-02   3.38312e-05  -2.04568e-02
   5  p4           1.91622e-01   1.35505e-02   2.63566e-05  -1.32757e-02

3. Calculate Signal counts from TH1 & TF1

In [5]:
double x1=3.6;//x1,x2:Both values must be within the fitting range
double x2=6.3;
int bin1=hSB->FindBin(x1);
int bin2=hSB->FindBin(x2);
int nhSB=hSB->Integral(bin1,bin2);
int nhB=hB->Integral(bin1,bin2);
int nhS=hS->Integral(bin1,bin2);
double fx1=hSB->GetBinLowEdge(bin1);
double fx2=hSB->GetBinLowEdge(bin2)+hSB->GetBinWidth(bin2);
cout<<bin1<<" "<<bin2<<endl;
int nfB=fB->Integral(fx1,fx2)/hSB->GetBinWidth(bin2);
int nfS=fS->Integral(fx1,fx2)/hSB->GetBinWidth(bin2);
int nfSB=fSB->Integral(fx1,fx2)/hSB->GetBinWidth(bin2);
cout<<fx1<<","<<fx2<<endl;
cout<<"TH1: hB="<<nhB<<", hS="<<nhS<<", hSB="<<nhSB<<endl;
cout<<"TF1: fB="<<nfB<<", fS="<<nfS<<", fSB="<<nfSB<<endl;
cout<<"hS="<<nhS<<", hSB-fB="<<nhSB-nfB<<endl;
37 64
3.6,6.4
TH1: hB=28236, hS=2000, hSB=30236
TF1: fB=28269, fS=1882, fSB=30151
hS=2000, hSB-fB=1967
In [ ]: