# FindBin

…So I was trying to have a histogram that would serve as sort of an array. In other words I was trying…

TH2F eff (“eff”,"",32,-3.2,3.2,16,0,.6);

…then I fill the appropriate values from a text file into the TH2F by doing…

ifstream fin;
fin.open(“Efficiencies.t”);

float a, b, c;

fin >> a;
fin >> b;
fin >> c;

eff.SetBinContent(eff.FindBin(a,b),c);

…where a and b are not bin values, rather coordinates on the x and y-axis. Similarly I want to recall these values by inputting some variables x and y from calculations…

float z = eff.GetBinContent(eff.FindBin(x,y));

…anyway, the nice thing about this is it’s convenient to run through and fill a histogram and save it to a .root file and use it in other code, and that it’s simple to find the values you need.

The problem I’m having is that FinBin doesn’t seem to find negative values thus: first fills only the positive quadrant and also reads only the positive coordinates.

If I’m just being stupid pleas tell me why

Why do you want to make things complicated when they are simple? :
Simply replace

by

Rene

…ok, that’s what I did initially but then my questions is: is there a similar function that will get the value of a bin based on the coordinates rather than on bin number? My impression is that GetBinContent gets things by bin value and I’m not sure if there is a “GetContent” function that will get things based on histogram axis. But yea, I would love to do this as simply as possible. Thanks

In case of a TH2, you can do something like

``` Int_t binx = h2.GetXaxis()->FindBin(x); Int_t biny = h2.GetYaxis()->FindBin(y); Int_t bin = h2->GetBin(binx,biny,0); //see doc of TH1::GetBin h2->SetBinContent(bin,c); //where bin is the linearized bin number ```
Rene

1 Like

…Thanks Alot!..that made my day and made me do alot less work! Cheers!

…Also I was wondering if there is a nice way to do 2-dimensional histograms with variable binning?

yes, see the TH2 constructors, eg for TH2F

``` TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup ,Int_t nbinsy,Double_t ylow,Double_t yup); TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins ,Int_t nbinsy,Double_t ylow,Double_t yup); TH2F(const char *name,const char *title,Int_t nbinsx,Double_t xlow,Double_t xup ,Int_t nbinsy,const Double_t *ybins); TH2F(const char *name,const char *title,Int_t nbinsx,const Double_t *xbins ,Int_t nbinsy,const Double_t *ybins); TH2F(const char *name,const char *title,Int_t nbinsx,const Float_t *xbins ,Int_t nbinsy,const Float_t *ybins);```

Rene