# Create function from histogram

**URL:** <https://root-forum.cern.ch/t/create-function-from-histogram/51470>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [September 6, 2022, 7:41am UTC](https://root-forum.cern.ch/t/create-function-from-histogram/51470 "2022-09-06T07:41:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Riccardo\_Manfredi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/riccardo_manfredi/32/20853_2.png) [@Riccardo\_Manfredi](https://root-forum.cern.ch/u/Riccardo_Manfredi)\
**Post date:** [September 6, 2022, 7:41am UTC](https://root-forum.cern.ch/t/create-function-from-histogram/51470/1 "2022-09-06T07:41:42Z")

</div>

Hi ROOTers,  
I am following up from a topic that was signed as closed even if not (my bad for never noticiging the last reply). The topic is [this one](https://root-forum.cern.ch/t/create-a-tf1-from-a-th1f/41429).  
In the last comment I am suggested to use the constructor

```auto
auto f1 = new TF1("f1", conv_histo, &convert_1DHisto::Evaluate, hInput->GetXaxis()->GetXmin(), hInput->GetXaxis()->GetXmax(), 0);

```

however this line gives me the error

```auto
error: use of undeclared identifier 'convert_1DHisto'

```

and I don’t understand what it means, since the class is defined just above.

Thanks for your help,

Riccardo

* * *

_ROOT Version:_ 6.24/00  
_Platform:_ Linux 3.10.0-1127.13.1.el7.x86\_64

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 6, 2022, 7:47am UTC](https://root-forum.cern.ch/t/create-function-from-histogram/51470/2 "2022-09-06T07:47:16Z")

</div>

Can you post a small macro reproducing the problem?

---

<div class="post-metadata">

**Author:** ![Riccardo\_Manfredi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/riccardo_manfredi/32/20853_2.png) [@Riccardo\_Manfredi](https://root-forum.cern.ch/u/Riccardo_Manfredi)\
**Post date:** [September 6, 2022, 9:34am UTC](https://root-forum.cern.ch/t/create-function-from-histogram/51470/3 "2022-09-06T09:34:02Z")

</div>

Sure, find it here below.  
Thanks

```auto
#include <TFile.h>
#include <TCanvas.h>
#include <TMath.h>
#include <TH1F.h>
#include <TF1.h>

class convert_1Dhisto {
public:
  TH1F* h_input;
  void set_histo (TH1F* h_in) {
    h_input = (TH1F*)h_in;
    return;
  }
  double Evaluate (double *vx, double *p) {
    double x = vx[0];
    if (x < h_input->GetBinLowEdge(1) || x > h_input->GetBinLowEdge(h_input->GetNbinsX())+ h_input->GetBinWidth(1))
      return 0.;

    return h_input->GetBinContent(h_input->FindBin(x));
  }
};

void test(){

  TFile* file = new TFile("/eos/infnts/belle2/users/rmanfred/rhorho/b2dkpp0_fit/histograms_belle2/angular_b2rhorho_LP22_sidebandReweight_ch2D_rewAll_data.root");
  TH1F* hTest = (TH1F*)file->Get("h1D_sig_lo_dE");

  convert_1Dhisto * conv_histo = new convert_1Dhisto();
  conv_histo->set_histo(hTest);

  auto funcHisto = new TF1("funcHisto", conv_histo, &convert_1DHisto::Evaluate, hTest->GetXaxis()->GetXmin(), hTest->GetXaxis()->GetXmax(), 0);

  TCanvas* c = new TCanvas("c", "c", 400, 800);
  c->Divide(2,1);
  c->cd(1);
  hTest->Draw();
  c->cd(2);
  funcHisto->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:** [September 6, 2022, 1:22pm UTC](https://root-forum.cern.ch/t/create-function-from-histogram/51470/4 "2022-09-06T13:22:42Z")

</div>

` auto funcHisto = new TF1("funcHisto", conv_histo, &convert_1Dhisto::Evaluate, hTest->GetXaxis()->GetXmin(), hTest->GetXaxis()->GetXmax(), 0);`
