# Problem in smearing histogram plotted from a text file

**URL:** <https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [February 15, 2023, 11:58am UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641 "2023-02-15T11:58:46Z")\
**Posts on this page:** 7\
**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:** [February 15, 2023, 11:58am UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641/1 "2023-02-15T11:58:46Z")

</div>

Hi,

i have the following code for plotting a histogram by reading a text file. To this histogram I want to add my experimental resolution of 3.5. The follwing is the macro :

```auto
void Analysis()
{

gStyle->SetOptStat(2211);
gStyle->SetOptFit(1111);

auto h1 = new TH1D("h1_gaus","Mass (amu);Counts",121.0,27.5,148.5);
TH1D*h2 = new TH1D("h2","h2_Derivative", 121.0,27.5,148.5); 

ifstream inp;
double x;

inp.open("gaus_theo.txt");
for (int i=0;i<120;i++)
{
  inp>>x;
  h1->SetBinContent(i,x); 
  h2->SetBinContent(i,x+ gRandom->Gaus(0,3.5));
}

TCanvas *c1 = new TCanvas("c1","Without constraint",800,600);
c1->Divide(1,2);
c1->cd(1);
h1->Draw("HIST");

c1->cd(2);
gStyle->SetOptStat(1002211);
gStyle->SetOptFit(1111);
h2->Draw("HIST");

}

```

and the text file is :  
[gaus\_theo.txt](https://root-forum.cern.ch/uploads/short-url/rsoOZ28SRw7KhS84W86Z9yfTkCa.txt) (1.6 KB)

and I get the output as :

[Res.pdf](https://root-forum.cern.ch/uploads/short-url/fOeaG2djK2pgu2PFjvQSco4Ow9t.pdf) (16.3 KB)

What is my mistake here, which I dont understand that gives me bizzard result for the second histogram with resolution ?

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:** [February 15, 2023, 12:05pm UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641/2 "2023-02-15T12:05:36Z")

</div>

Try with: `h2->SetBinContent(i, x + gRandom->Gaus(0., 0.35));`

BTW. `for (int i = 1; i <= 121; i++)`

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [February 16, 2023, 1:45pm UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641/3 "2023-02-16T13:45:26Z")

</div>

Hi @gini,  
as explained in this previous topic

> [@Problem in smearing histogram](https://root-forum.cern.ch/t/problem-in-smearing-histogram/52707/6):
>
> Hi @gini Smear the data means to adding the effect of a resolution broadening your histogram. What you are doing in your code is not smearing, but just changing the number of counts in each bin. From your code I see that you have just a file with the number of counts for each bin. I slightly modified your code to correctly smear the histogram. I generate a gaussian using the mean and std of h1. { gStyle-\>SetOptStat(2211); gStyle-\>SetOptFit(1111); auto c1 = new TCanvas("c1"…

adding a random gaussian resolution to the content bin does not smear the histogram, you are just randomizing the total counts as can be seen from the integral value. Such value should not change after a smearing operation.

We discussed quite a lot such issue in your previous post linked above.

---

<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:** [February 16, 2023, 2:36pm UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641/4 "2023-02-16T14:36:40Z")

</div>

Hi @Dilicus ,

I had actually tried it, but the results of the width after incorporating resolution of 3.5 units are not correct:

{  
gStyle-\>SetOptStat(2211);  
gStyle-\>SetOptFit(1111);

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

auto h1 = new TH1D(“h1\_gaus”,“Mass (amu);Counts”,121.0,27.5,148.5);  
auto h2 = new TH1D(“h2”,“h2\_Mass\_resolution”,121.0,27.5,148.5);  
auto h3 = new TH1D(“h3”,“h3\_Mass\_resolution”,121.0,27.5,148.5);

h2-\>SetLineColor(6);  
ifstream inp; double x;  
inp.open(“onecolumn.txt”);  
for (int i=1; i\<=121; i++)  
{  
inp \>\> x;  
h1-\>SetBinContent(i,x);

```
for(double j=0;j<x;j++)

```

{  
h2-\>Fill( gRandom-\>Gaus(h1-\>GetBinCenter(i) ,3.5));  
h3-\>Fill( h1-\>GetBinCenter(i) + 3.5 \* TMath::Sqrt(2.)\* TMath::ErfInverse(2. \* gRandom-\>Uniform(1.) - 1.) );  
}  
}

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

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

c1-\>cd(3);  
h1-\>Draw();  
h2-\>Draw(“HIST SAMES”);

c1-\>cd(4);  
h3-\>Draw(“”);  
}

the text file used here is :

[onecolumn.txt](https://root-forum.cern.ch/uploads/short-url/csAhYXqC4xyZfjDq8JGak4mHvfG.txt) (1.5 KB)

the original text file is two column data with x and y data points :  
[yield176\_55.txt](https://root-forum.cern.ch/uploads/short-url/HIf4ute4HvpgJoD8sfB7m40gJh.txt) (6.1 KB)

what i get when i run the code is :

 ![Capture](https://root-forum.cern.ch/uploads/default/original/3X/4/a/4a8af27dc190e444074e600143b233a451cf14f6.png)

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [February 16, 2023, 2:56pm UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641/5 "2023-02-16T14:56:31Z")

</div>

Yes this is because your TH1D has some sort of normalization.  
You cannot use the for like this without accounting for the normalization you used.

```auto
for(double j=0;j<x;j++)

```

try to change in something like

```auto
for(double j=0;j<x*normalization_constant;j++)

```

I think your normalization constant should be something like this ~1430

---

<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:** [February 17, 2023, 7:30am UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641/6 "2023-02-17T07:30:01Z")

</div>

Thank you @Dilicus.

I have one more question, now if I want to do the same ie., add resolution to the data stored inside a branch and say “a” is the branch then, to add resolution, what i need to do is to use :

a\_res= a+ 3.5 \* TMath::Sqrt(2.)\* TMath::ErfInverse(2. \* gRandom-\>Uniform(1.) - 1.) );

for each of the entries right ?

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [February 17, 2023, 8:23am UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram-plotted-from-a-text-file/53641/7 "2023-02-17T08:23:48Z")

</div>

If the entries are just single count, the procedure is correct.
