Problem in smearing histogram plotted from a text file

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 :

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 (1.6 KB)

and I get the output as :

Res.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

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

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

Hi @gini,
as explained in this previous topic

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.

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 (1.5 KB)

the original text file is two column data with x and y data points :
yield176_55.txt (6.1 KB)

what i get when i run the code is :

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.

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

try to change in something like

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

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

1 Like

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 ?

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