# Derivative of histogram

Hi,

I want to find the derivative of a histogram with respect to each point on the histogram. The following is my macro and unfortunately it doesnt works :

{

auto h1 = new TH1D(“h1”,“h1”,101.0,39.5,140.5);
auto h2 = new TH1D(“h2”,“h2”,101.0,39.5,140.5);

``````double amu=0;
for(long int i=0;i<100000;i++)
{
amu=gRandom->Gaus(87.52,12.);
h1->Fill( amu );

}
``````

TF1 *myfun = h1->GetFunction(“myfun”);

``````for(long int i=0;i<100000;i++)
{
h2->Fill(myfun->Derivative(h1->GetBinCenter(i)));
}
``````

auto c1 = new TCanvas(“c1”,“Without constraint”,800,600);
c1->Divide(2,1);

c1->cd(1);
h1->Draw();

c1->cd(2);
h2->Draw(“”);

}

Your “`h1`” has no “`myfun`” (so the “`myfun`” pointer is just a “`nullptr`”).

is it possible to convert my histogram to the corresponding function?

``````{
TH1D *h = new TH1D("h", "h", 100, -5., 5.);
h->FillRandom("gaus", 10000);
// h->Smooth();
h->Draw();
TF1 *f =
new TF1("f",
[=](double *x, double */*p*/){return h->Interpolate(x);},
h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax(), 0);
f->Draw("same");
}
``````

Thanks @Wile_E_Coyote .

So I converted my histogram to function and I tried to find the derivative at each point on the histogram. the following is the macro :

{

auto h = new TH1D(“h1”,“h1”,101.0,39.5,140.5);
auto h2 = new TH1D(“h2”,“h2”,200,-100,100);

//auto h2 = new TH1D(“h2”,“h2”,101.0,39.5,140.5);

``````double amu=0;
for(long int i=0;i<1000;i++)
{
amu=gRandom->Gaus(87.52,12.);
h->Fill( amu );
}
``````

// Converting the histogram to its corresponding function
TF1 *f =
new TF1(“f”, [=](double *x, double */p/)
{return h->Interpolate(x);},
h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax(), 0);

``````for(long int j=0;j<1000;j++)
{
h2->Fill(f->Derivative(h1->GetBinCenter(j)));
}
``````

auto c1 = new TCanvas(“c1”,“c1”,800,600);
c1->Divide(2,2);

c1->cd(1);
h->Draw();
c1->cd(2);
f->Draw();
c1->cd(3);
h->Draw();
f->Draw(“same”);
c1->cd(4);
h2->Draw();

}

Is this how it has to be done as I am not sure ?

The following is what I get :