# How to add experimental resolution to a 1D Histogram in root

**URL:** <https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005>\
**Category:** Newbie\
**Tags:** root\
**Created:** [May 17, 2022, 3:07pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005 "2022-05-17T15:07:29Z")\
**Posts on this page:** 20\
**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:** [May 17, 2022, 3:07pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/1 "2022-05-17T15:07:29Z")

</div>

Hi,  
I am quite new to root.  
Can someone tell me how to smear my theoritical mass spectra with my experimental mass resolution ?  
The experimental mass resolution is 3.5 amu. I want to incorporate this mass resolution in the mass spectrum which I obtain from my theory calculation and want to see how the width changes.

Thanking you in advance!

---

<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:** [May 17, 2022, 3:28pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/2 "2022-05-17T15:28:26Z")

</div>

Welcome to the ROOT forum.  
Have you looked at the ROOT tutorials ? the ROOT forum ?

---

<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:** [May 18, 2022, 9:28am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/3 "2022-05-18T09:28:48Z")

</div>

Yes, i have gone through the root tutorials and I had made a macro to do this. But It has some problems. i am sharing the marco, unfortunately I cant upload the root files.

```auto
void MassResolution()
{

TH1D*h2_Mass_resolution = new TH1D("h2_Mass_resolution","h2_Mass_resolution",101.,39.5,140.5); // Defined a histogram

TFile *file=TFile::Open("QnmFF_ROOT_Au_158.root");
TTree *tree=(TTree*)file->Get("DITY");

TH1D*h1 = new TH1D("h1","Os_187.46_MeV",101.,39.5,140.5);
h1->SetLineColor(kRed);
tree->Draw("Aff1>>h1"); // retrived histogram fom the TTree

Double_t x1,y1,z;
Double_t content=0;
Double_t sigma=3.5;

const UInt_t nX = h1->GetNbinsX();

	for(UInt_t i=1;i<=nX;++i)
	{
		x1 = h1->GetXaxis()->GetBinCenter(i);
                y1 = h1->GetBinContent(i);
                z = h1->GetBinWidth(i);

TF1 *f1 = new TF1("f1","TMath::Gaus(x,[1],[2])",40,140);
f1->SetParameter(0,y1);
f1->SetParameter(1,sigma);

h2_Mass_resolution->Fill(x1, f1->GetRandom()); 

     
        }

```

---

<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:** [May 18, 2022, 11:48am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/4 "2022-05-18T11:48:14Z")

</div>

> [@gini](#):
>
> But It has some problems.

which problems ?

> [@gini](#):
>
> unfortunately I cant upload the root files

that would help though …

---

<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:** [May 18, 2022, 12:33pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/5 "2022-05-18T12:33:16Z")

</div>

Assuming that “`Aff1`” is in “amu” units and you want to add a Gaussian smearing with “3.5 amu” sigma, you could try:

```cpp
tree->Draw("Aff1 + 3.5 * TMath::Sqrt2() * TMath::ErfInverse(2. * rndm() - 1.) >> h1"); // inverse Gaussian CDF transform sampling

```

---

<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:** [May 18, 2022, 12:55pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/6 "2022-05-18T12:55:59Z")

</div>

![image](https://root-forum.cern.ch/uploads/default/original/3X/8/b/8bd9fc4a77ff41234e328bbc0c6ee915eac3f738.png)

The problem as you can see from the image, I am not getting the smeared histogram.  
Red is my Theoritical data, to which I want to add my mass resolution of 3.5 amu. and when I run my macro I get the blue histogram which is not correct.

---

<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:** [May 18, 2022, 12:57pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/7 "2022-05-18T12:57:28Z")

</div>

Since I am a new user of the root forum I am not able to upload my root file.

---

<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:** [May 18, 2022, 1:52pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/11 "2022-05-18T13:52:20Z")

</div>

[The method I proposed](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/5) makes the “`h1`” histogram smeared (no need for any “`h2`” gymnastics).

---

<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:** [May 18, 2022, 1:56pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/12 "2022-05-18T13:56:57Z")

</div>

I was just going through what you proposed. It is working, but I need to understand how you did it.

---

<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:** [May 18, 2022, 2:11pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/13 "2022-05-18T14:11:02Z")

</div>

![image](https://root-forum.cern.ch/uploads/default/original/3X/8/b/8b0988bb07a437a90cd1f566066a8b2dd081d11a.png)  
Red is the theoretical data and blue is the theoretical data smeared to 3.5 amu.

---

<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:** [May 23, 2022, 9:18am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/14 "2022-05-23T09:18:04Z")

</div>

Hi,  
Inside my ttree I have all the theoretical masses for which I wanted to add my masss resolution of 3.5 amu. For that I declared a tree branch and variable “Aff1\_Res\_amu” and did

Aff1\_Res\_amu = Aff1 + 3.5 \* TMath::Sqrt2() \* TMath::ErfInverse(2. \* rndm() - 1.);

When I do this I get the following error :

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/d/8/d83dc0a9b9f4ba57df9927b04c7859db3e4b5200.png)

Could you please help me resolve it?

---

<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:** [May 23, 2022, 9:21am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/15 "2022-05-23T09:21:35Z")

</div>

In a C++ code, instead of “`rndm()`” try to use “`gRandom->Rndm(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:** [May 23, 2022, 9:35am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/16 "2022-05-23T09:35:37Z")

</div>

Thank you for the prompt response !

So I replaced rndm(), like you told 🙂  
Aff1\_Res\_amu = Aff1 + 3.5 \* TMath::Sqrt2() \* TMath::ErfInverse(2. \* gRandom-\>Rndm(1) - 1.);

but I get this error

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

I have used the header file " #include\<TRandom.h\>"

---

<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:** [May 23, 2022, 9:58am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/17 "2022-05-23T09:58:29Z")

</div>

It seems you have an extra “g” in line 221 (column 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:** [May 23, 2022, 10:07am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/18 "2022-05-23T10:07:18Z")

</div>

That was right, there was g. But there is still some problem with gRandom.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/3/8350e1b38a0d2a60a39fc4abbe6a77d6f05cb097.png)

---

<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:** [May 23, 2022, 10:39am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/19 "2022-05-23T10:39:35Z")

</div>

Try to add somewhere in your code:  
`if (!gRandom) gRandom = new TRandom3();`

---

<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:** [May 23, 2022, 10:50am UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/20 "2022-05-23T10:50:38Z")

</div>

Thats is working.

Once again thanks a lot!

---

<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:** [May 24, 2022, 12:18pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/21 "2022-05-24T12:18:09Z")

</div>

Actually, in a C++ code you can simply use:  
`Aff1_Res_amu = gRandom->Gaus(Aff1, 3.5);`

---

<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:** [May 24, 2022, 1:10pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/22 "2022-05-24T13:10:52Z")

</div>

Yes, it works. Thank You.

---

<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:** [November 5, 2023, 1:37pm UTC](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005/24 "2023-11-05T13:37:09Z")

</div>

At `energy = Edep_info1 = 1` (a typical value in your plots), your `fwhm = resolution * energy = (p0 + p1 / sqrt(Edep_info1)) * Edep_info1 = 110.5` (i.e., `fwhm >> Edep_info1`, which makes no sense to me). Even if one assumes `fwhm = resolution / 100. * energy` (i.e., your “resolution %” to real values), you get `fwhm = 1.1` (so, still `fwhm > Edep_info1`, which looks suspicious to me).

[Next page](https://root-forum.cern.ch/t/how-to-add-experimental-resolution-to-a-1d-histogram-in-root/50005.md?page=2)
