# Derivative of histogram

**URL:** <https://root-forum.cern.ch/t/derivative-of-histogram/52762>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [December 14, 2022, 11:40am UTC](https://root-forum.cern.ch/t/derivative-of-histogram/52762 "2022-12-14T11:40:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 14, 2022, 11:40am UTC](https://root-forum.cern.ch/t/derivative-of-histogram/52762/1 "2022-12-14T11:40:21Z")

</div>

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(“”);

}

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 14, 2022, 11:48am UTC](https://root-forum.cern.ch/t/derivative-of-histogram/52762/2 "2022-12-14T11:48:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 14, 2022, 11:53am UTC](https://root-forum.cern.ch/t/derivative-of-histogram/52762/3 "2022-12-14T11:53:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 14, 2022, 12:26pm UTC](https://root-forum.cern.ch/t/derivative-of-histogram/52762/4 "2022-12-14T12:26:20Z")

</div>

```cpp
{
  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[0]);},
            h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax(), 0);
  f->Draw("same");
}

```

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 14, 2022, 12:59pm UTC](https://root-forum.cern.ch/t/derivative-of-histogram/52762/5 "2022-12-14T12:59:26Z")

</div>

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[0]);},  
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 :

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/7/c/7c7b56bba158239b2471e32f3f5176d408113f80.png)

---

<div class="post-metadata">

**Author:** ![TomChilly](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@TomChilly](https://root-forum.cern.ch/u/TomChilly)\
**Post date:** [June 13, 2024, 9:57am UTC](https://root-forum.cern.ch/t/derivative-of-histogram/52762/6 "2024-06-13T09:57:42Z")

</div>

Hi gini, the threads a little old so maybe you fixed your issue.  
Anyway I think you want:

```auto
h2->Fill(f->Derivative(j, h1->GetBinCenter(j)));

```
