gini
December 16, 2022, 10:04am
#1
Hi,

I have a histogram and I find its derivavtive and fills it in another histogram.

the histogram with the derivative has error bars, Is it possible to plot the histogram (derivative histogram) with errors?

Also how can I fit the derivative histogram with a sine function ?

The following is the macro :

```
void Re_Der()
{
/////////////////// Tree opening \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
TFile *file1=TFile::Open("Analysis_OS.root");
TTree *tree1=(TTree*)file1->Get("Analysis");
TH1D*h1 = new TH1D("h1","h1", 100.0,40.5,140.5);
TH1D*h2 = new TH1D("h2","h2_Derivative", 100.0,40.5,140.5);
TCanvas *c1 = new TCanvas("c1","Without constraint",800,600);
c1->Divide(1,2);
c1->cd(1);
tree1->Draw("M_Tot>>h1");
Int_t nbins = h1->GetNbinsX();
cout<<"\n"<<"a"<<"\t"<<"a_cent"<<"\t"<<"b"<<"\t"<<"b_cent"<<"\t"<<"c"<<"\t"<<"c_cent"<<"\t"<<"d_mid"<<"\t"<<"d"<<"\n";
for(Int_t i=0;i<nbins;i++)
{
double a=h1->GetBinContent(i);
double a_cent=h1->GetBinCenter(i);
double b=h1->GetBinContent(i+1);
double b_cent=h1->GetBinCenter(i+1);
double c=h1->GetBinContent(i+2);
double c_cent=h1->GetBinCenter(i+2);
double d = (c-a)/(c_cent-a_cent);
double d_mid = (c_cent+a_cent)/2;
cout<<"\n"<<a<<"\t"<<a_cent<<"\t"<<b<<"\t"<<b_cent<<"\t"<<c<<"\t"<<c_cent<<"\t"<<d_mid<<"\t"<<d;
//printf("%d \t %g\n",i,h1->GetBinContent(i));
//h2->Fill(d);
h2->Fill(d_mid,d);
}
c1->cd(2);
h2->Draw("*");
}
```

Emil_R
December 16, 2022, 11:24am
#2
Hi,
I am not an experienced user either, but the THistPainter Class Reference says, that the Option â€śHISTâ€ť will turn the errors off (h2->Draw(â€śhist *â€ť)). You can perform the fitting either with RooFit or you can open a TBrowser and under â€śToolsâ€ť you can open the FitPanel in order to fit the histogram. I hope this works and helps you.
Cheers,
Emil

gini
December 16, 2022, 12:55pm
#3
That works @Emil_R . Thank you.

I want to fit the derivative of the histogram using a sin function, unfortunately, itâ€™s not predefined in FitPanel of the Root.

Emil_R
December 16, 2022, 12:59pm
#4
If it is not available in the FitPanel, you can do the following:
Click on the field that says â€śgaussâ€ť and scroll down, there you should find â€śuserâ€ť. Chose this. Than type in the blank space beneath: â€ś[0]sin([1] (x-[2]))+[3]â€ť and then click fit. This should work and fit a sin function.

1 Like

Dilicus
December 16, 2022, 5:47pm
#5
To fit the histogram just define your function have a look at TF1 on how define and use function.

For define your sine function I used this constructor

```
TF1 *f_sin= new TF1("sin", "[0]*sin([1]*x - [2]) + [3]", 0,10);
```

Remember to set reasonable starting condition using TF1::SetParameter() or TF1::SetParameters()

When you have the function just call the TH1::Fit() method

Look all the different option that you can use, below I used the range option

```
h2->Fit(f_sin,"R");
```

Best,
Dilicus

gini
December 20, 2022, 9:14am
#6
That works, its just that the resonalable values needs to be set for the parameters.

thanks @Dilicus and @Emil_R

2 Likes