# Convolution of two TH1F Histograms

I was wondering if there was a way to convolve two TH1F histograms? I was able to find this in the forum:

but still was left wondering if it was possible.

I also found this older post regarding deconvolution:

I saw that ROOT has a built in class TF1Convolution, but it seems that only will accept functions as the arguments.

Hi,

if you look closely to the code in the post you already identified, viewtopic.php?t=18405, the procedure does start with TH1F instances.

Cheers,
Danilo

Hi Danilo,

Thanks for the hint. I looked at the code more closely as you suggested and implemented some of the RooFit functions.

Here is my updated code:

``````/////////////////___Begin Code___////////////////////////
void fitConvolution()
{
TH1F* h2 = new TH1F("h2", "Spectrum;time[ns];counts", 2000, 0., 2000.);
TH1F* h3 = new TH1F("h3", "Spectrum;time[ns];counts" ,20000, 0., 60000.);

TCanvas* c = new TCanvas("c1","rando",200,10,600,400);
c->Divide(2,2);

for ( Int_t i=0; i<10000; i++)
{
Float_t x1, x2, x3, E1, E2, E3,
meanE1, meanE2, meanE3, sig1, sig2, sig3;

double eVToJ = 1.55E-18;
double ns =  1E9;
double us = 1E6;
double d = 8.5;
double mN = 1.67E-27;

meanE1 = 1.;
meanE2 = 5.;
meanE3 = 10.;

sig1 = 0.5;
sig2 = 1.;
sig3 = 1.5;

x1 = gRandom->Gaus(meanE1, sig1);//(mean, sigma)
E1 = (d / sqrt(2 * x1*1E6 / mN * eVToJ) ) * ns;

x2 = gRandom->Gaus(meanE2, sig2);
E2 = (d / sqrt(2 * x2*1E6 / mN * eVToJ) ) * ns;

x3 = gRandom->Gaus(meanE3, sig3);
E3 = (d / sqrt(2 * x3*1E6 / mN * eVToJ) ) * ns;

h2->Fill(E1);
h2->Fill(E2);
h2->Fill(E3);
}

for ( Int_t i=0; i<1E6; i++)
{
double val;
val = gRandom->Uniform(0.,10000);
h3->Fill(val);
}

RooRealVar x("x","x", 0, 2000);

RooDataHist* RDHh2 = new RooDataHist("RDHh2", " ", x, h2);
RooDataHist* RDHaccPulse = new RooDataHist("RDHaccPulse", " ", x, accPulseHisto);

RooHistPdf* pdfh2 = new RooHistPdf("pdfh2", "pdfh2", x, *RDHh2, 2);
RooHistPdf* pdfaccPulse = new RooHistPdf("pdfaccPulse", "pdfaccPulse", x, *RDHaccPulse, 2);

RooNumConvPdf expTOF("ExpTOF", "ExpTOF", x, *pdfh2, *pdfaccPulse);

RooPlot* frame = x.frame();
expTOF.plotOn(frame,LineColor(kRed),LineStyle(kDashed));

c->cd(2);
h2->Draw();
c->cd(3);
accPulseHisto->Draw();
c->cd(4);
frame->Draw();
c->Update();
}
/////////////____End Code____//////////////////////``````

I getting the following error:
[#0] ERROR:Integration – RooAdaptiveGaussKronrodIntegrator1D::integral() ERROR: number of iterations was insufficient

Any help would be greatly appreciated.

-Jason

I installed FFTW and performed the convolution with RooFFTConvPdf, which cleared the error I was receiving.