# Problem in smearing histogram

**URL:** <https://root-forum.cern.ch/t/problem-in-smearing-histogram/52707>\
**Category:** Newbie\
**Created:** [December 9, 2022, 12:39pm UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram/52707 "2022-12-09T12:39:21Z")\
**Posts on this page:** 1\
**Showing post:** 6

<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:** [December 10, 2022, 4:50pm UTC](https://root-forum.cern.ch/t/problem-in-smearing-histogram/52707/6 "2022-12-10T16:50:10Z")

</div>

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.

```auto
{
   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",101.0,39.5,140.5);
   auto h2 = new TH1D("h2","h2_Mass_resolution",101.0,39.5,140.5);
   auto h3 = new TH1D("h3","h3_Mass_resolution",101.0,39.5,140.5);

   h2->SetLineColor(6);
   ifstream inp; double x;
   inp.open("try.txt");
   for (int i=1; i<=100; i++) {
      inp >> x;
      h1->SetBinContent(i,x);
       //loop over the events inside a bin and change it with a resolution of 3.5 with the 2 different methods
	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->Fit("gaus","","",44.5, 130.5);
   h1->Draw();

   c1->cd(2);
   h2->Fit("gaus","","",44.5, 130.5);
   h2->Draw("");

   c1->cd(3);
   h1->Draw();
   h2->Draw("HIST SAMES");

   c1->cd(4);
   h3->Draw("");
}

```

I loop over the bin of h1, and for each bin I generate `x` events with amu equal to `h1->GetBinCenter(i)` and for these events I add the effect of a resolution.

The procedure is not really correct, but considering that the bin width is 1/100 of the total range the approximation is fine.

 ![smeared_histos](https://root-forum.cern.ch/uploads/default/original/3X/4/e/4e929c1bec3f6edadf4b395978f91ce3d29c748a.png)

As you can see in the picture the sigma of h2 and h3 is more less equal to sqrt(3.5^2 +12.86^2 ) ~ 13.34, as expected.

I hope to have been clear in my explenation.

---

_[View the full topic](https://root-forum.cern.ch/t/problem-in-smearing-histogram/52707)._
