Adopting binning when stacking 1D histos in a 2D histo

Hello co-rooters!

I am trying to create a 2D histogram, by stacking 1D histograms on it.
The 1D histograms have in x-axis time in us.
The weird thing about them is that the first bin isn’t 0, but something greater (usally around 8.1 us)

The code that creates the 2D histogram is the following

[code]TH2F* mean_Gamma_Flash() {
gStyle->SetPalette(52); //Use Grayscale palette–51-59
//Load File.
TFile *f = new TFile(“FIMG5_300.root”);

//Loop over each histogram and store pointer in vector.
std::vector<TH1F*> hists;
for (int i=0;i<3;i++) {
TH1F hIndv = (TH1F)f->Get(Form(“FIMG5_EV_%d;1”,i));
if (hIndv)
hists.push_back(hIndv);
}
TH2F *hStack = Stack(hists);
hStack->SetStats(kFALSE);
hStack->Draw(“COLZ”);
Pad->SetLogz();
gPad->Update();
return hStack;
}

TH2F Stack(std::vector<TH1F> hists) {
TH2F *hStack = new TH2F(“hStack”,“Stack”,hists[1]->GetNbinsX(),0,hists[1]->GetNbinsX(),300,0,300);
for (size_t i=0;i < hists.size(); i++) {
for (int bin = 0; bin < hists[i]->GetNbinsX(); bin++) {
int resultBin = hStack->FindBin(bin, hists[i]->GetBinContent(bin));
int binContent = hStack->GetBinContent(resultBin) + 1;
hStack->SetBinContent(resultBin,binContent);
}
}
return hStack;
}[/code]

The resutling 2D histogram doesn’t adopt the binning of the original 1D histograms. I suspect the reason is that the 1D histograms don’t start counting from 0.

Any idea on how keep the same bin numbers, the same first and last bin on the 2D histogram?

Thank you very much in advance!

P.S. You can find the root file here
https://www.dropbox.com/s/1y3j5rkcswsejm2/FIMG5.root?dl=0

Search for all occurrences of “numbering bins” in the TH1 class description.

Thank you very much for your post!

I was reading the TH1 and trying to find everything relevant with “numbering bins” but I can’t seem able to use anything. The root file that i have, contains histograms that don’t start from bin_number = 1.

I can understand that there must be a calibration formula or something similar, but I am not sure.

Any advice or help, please?

Hi,

I don;t understand your code. TH2::FindBin(x,y) takes a input the x and y axis values of the 2D histograms and returns the corresponding bin numbers.
Bin numbers always start from 1 and go until bins.

Read carefully the TH1 reference documentation or the User guide chapter about histograms

root.cern.ch/root/htmldoc/guide … grams.html

Lorenzo