Inconsistent GetBinContent

Hello,
From a ROOT file I hosted here, I try to extract the content of a bin using the following macro

[code]#include <TFile.h>
#include <TH1D.h>
#include <TGraph.h>
#include <TAxis.h>

#include
#include
#include

void calculRendement() {
std::map <int, float> energyRun;
energyRun[0] = 10.00;
energyRun[1] = 14.00;
energyRun[2] = 17.55;
energyRun[3] = 22.10;
energyRun[4] = 25.07;
energyRun[5] = 32.06;
energyRun[6] = 36.45;
energyRun[7] = 37.3;
energyRun[8] = 59.541;
energyRun[9] = 88.034;
energyRun[10] = 122.061;
energyRun[11] = 136.474;
energyRun[12] = 255.134;
energyRun[13] = 391.698;
energyRun[14] = 661.657;
energyRun[15] = 834.838;
energyRun[16] = 1000.0;

float Nsimulation = 1000000.;

std::ofstream ofile(“rendement_Porthos1.txt”);
ofile << “Energy (keV) \t PORTHOS” << std::endl;

TFile *f = new TFile(“fichier2.root”,“READ”);
if(f->IsZombie()) {
std::cout << “Problem while opening fichier2.root” << std::endl;
return;
}

TH1D *hPorthos = NULL;
std::vector < double > x, y;

for(unsigned int i=11 ; i<energyRun.size() ; i++) {
TH1D hPorthos = (TH1D)f->Get(Form(“h%d”,i));
x.push_back(energyRun[i]);
y.push_back(static_cast(hPorthos->GetBinContent(hPorthos->FindBin(energyRun[i]/1000.))));
std::cout << x.back() << " keV, PORTHOS : " << y.back() << ", bin: " << hPorthos->FindBin(energyRun[i]/1000.) << std::endl;
ofile << x.back() << “\t” << y.back() << std::endl;
delete hPorthos;
}
f->Close();
ofile.close();

TGraph *g = new TGraph(x.size(),&(x[0]),&(y[0]));
g->GetXaxis()->SetTitle(“E_{#gamma} (keV)”);
g->GetXaxis()->CenterTitle(true);
g->GetYaxis()->SetTitle(“Efficiency (%)”);
g->GetYaxis()->CenterTitle(true);
g->Draw(“ALP”);
}[/code]

The problem is I get the following output

[quote] 10 keV, PORTHOS : 1007, bin: 41
14 keV, PORTHOS : 20889, bin: 57
17.55 keV, PORTHOS : 73301, bin: 71
22.1 keV, PORTHOS : 145005, bin: 89
25.07 keV, PORTHOS : 183889, bin: 101
32.06 keV, PORTHOS : 241432, bin: 129
36.45 keV, PORTHOS : 262068, bin: 146
37.3 keV, PORTHOS : 264629, bin: 150
59.541 keV, PORTHOS : 297476, bin: 239
88.034 keV, PORTHOS : 297023, bin: 353
122.061 keV, PORTHOS : 278702, bin: 489
136.474 keV, PORTHOS : 118, bin: 546
255.134 keV, PORTHOS : 168187, bin: 1021
391.698 keV, PORTHOS : 108797, bin: 1567
661.657 keV, PORTHOS : 66596, bin: 2647
834.838 keV, PORTHOS : 54315, bin: 3340
1000 keV, PORTHOS : 44053, bin: 4001[/quote]

My problem comes from the fact that the content for the energy 136.474 keV given by the macro is 118 whereas it is not the real content of the bin 546.
You can check with the following commands

TFile *f = new TFile("fichier2.root") TH1D *h = (TH1D*)f->Get("h11") h->GetBinContent(h->FindBin(546))

What is going wrong?
Thank you in advance.

h->GetBinContent(h->FindBin(136.474))
h->GetBinContent(546)

If I open the ROOT file manually and I type theses commands, I get

[quote]h->GetBinContent(h->FindBin(136.474/1000.))
267750.
h->GetBinContent(546)
267750.[/quote]
This is exactly what my macro should find. Yet, it find 118 instead and this is what I do not understand. What is wrong within my macro?

h11->GetBinContent(h->FindBin(136.474/1000.))

Using your macro and your file I get:
136.474 keV, PORTHOS : 267750, bin: 546
255.134 keV, PORTHOS : 168643, bin: 1021
391.698 keV, PORTHOS : 109256, bin: 1567
661.657 keV, PORTHOS : 66989, bin: 2647
834.838 keV, PORTHOS : 54675, bin: 3340
1000 keV, PORTHOS : 44393, bin: 4001

Indeed, I made a typo. This was what I did before. I fixed my previous message.

Can it be that you have two files named “fichier1.root” (that’s what your macro opens) and “fichier2.root” (that’s what you open in an interactive session)?

Non, I have only one file fichier2.root. And indeed, I have a file named fichier1.root but I do not think that it interfers.
That’s said, could you download the root file and try to run my macro to see if you get the same artefact?

Hmmm, actually, it owrks now. I just rewrite some parts and it seems there were probably a mix among fichier1.root and fichier2.root. Sorry for the noise.