# For loop for different bin widths

Hello,

I am trying to make a Gaussian distribution over bins of variable width. For that Gaussian distribution, I am extracting the bin content of bins that are occupied by the distribution and applying Poisson distribution to each bin and finally redrawing the new distribution. (I am only considering the odd numbered occupied bins).

Following is the code for the same.

But for the Gaussian distribution with the larger values of sigma, more bins are covered. So how can I apply ‘for’ loop over bins of variable width to get the bin content?

``````const Int_t Nx = 8;
Double_t edgesx[Nx + 1] = {0,3,3.2,6.2,6.4,9.4,9.6,12.6,12.8};

const Int_t Ny = 8;
Double_t edgesy[Ny + 1] = {0,3,3.2,6.2,6.4,9.4,9.6,12.6,12.8};

TH2F *h2 = new TH2F("h2","h",Nx,edgesx,Ny,edgesy);
TF2 *f2 = new TF2("f2","xygaus",0,20,0,20);
f2->SetParameters(1,6.3,1.028,6.3,1.028);  //amplitude, meanx,sigmax,meany,sigmay
h2->FillRandom("f2",3000);

Double_t bin1 = (h2->GetBinContent(3,3))*0.34;
Double_t bin2 = (h2->GetBinContent(5,3))*0.34;
Double_t bin3 = (h2->GetBinContent(3,5))*0.34;
Double_t bin4 = (h2->GetBinContent(5,5))*0.34;

TF1 *f3 = new TF1("f3", "TMath::Poisson(x,[1])",0,10000);
f3->SetParameter(1,bin1);
TF1 *f4 = new TF1("f4", "TMath::Poisson(x,[2])",0,10000);
f4->SetParameter(2,bin2);
TF1 *f5 = new TF1("f5", "TMath::Poisson(x,[3])",0,10000);
f5->SetParameter(3,bin3);
TF1 *f6 = new TF1("f6", "TMath::Poisson(x,[4])",0,10000);
f6->SetParameter(4,bin4);

TH2F *h5 = new TH2F("h5", "d=0.8 and t = 0.4mm", Nx, edgesx, Ny, edgesy);
h5->Fill(4.7,4.7, f3->GetRandom());
h5->Fill(7.9,4.7, f4->GetRandom());
h5->Fill(4.7,7.9, f5->GetRandom());
h5->Fill(7.9,7.9, f6->GetRandom());
h5->Draw();``````

Thank you so much for your help.

Regards,
Kajal

I have no idea what you’re trying to achieve, but try:

``````{
const Int_t Nx = 8;
Double_t edgesx[(Nx + 1)] = {0, 3, 3.2, 6.2, 6.4, 9.4, 9.6, 12.6, 12.8};

const Int_t Ny = 8;
Double_t edgesy[(Ny + 1)] = {0, 3, 3.2, 6.2, 6.4, 9.4, 9.6, 12.6, 12.8};

TF2 *f2 = new TF2("f2", "xygaus", 0, 20, 0, 20);
// amplitude, meanx, sigmax, meany, sigmay
f2->SetParameters(1, 6.3, 1.028, 6.3, 1.028);

TH2F *h2 = new TH2F("h2", "h", Nx, edgesx, Ny, edgesy);
h2->FillRandom("f2", 3000);

TF1 *f5 = new TF1("f5", "TMath::Poisson(x, [0])", 0, 10000);
f5->SetParameter(0, 1);

TH2F *h5 = new TH2F("h5", "d=0.8 and t=0.4mm", Nx, edgesx, Ny, edgesy);
for (Int_t ix = 1; ix <= h5->GetNbinsX(); ix++) {
for (Int_t iy = 1; iy <= h5->GetNbinsY(); iy++) {
Double_t par = 0.34 * h2->GetBinContent(ix, iy);
if (par > 0.0) {
f5->SetParameter(0, par);
h5->SetBinContent(ix, iy, f5->GetRandom());
} else {
// h5->SetBinContent(ix, iy, 0.0);
}
}
}
h5->Draw("LEGO2Z");
}
``````

Hello,

Thank you very much. This is what I was trying to do. Thanks for the help.