TH1F SetBinContent

Hello
I am having problems with SetBinContent()

char line[1000];
float offset = 4.0;
char *delim;
int year;
Double_t white;
char *fname ="somefile.txt";

TH1F *h_white=new TH1F("h_white","",nlines,1975-offset, 1992+offset);

for(iline =8;iline<26;iline++) {
   if(fgets(line, 1000, fp) != NULL) {
      delim = strtok(line,"\t");
      year = atoi(line);  

     delim = strtok (NULL, "\t");
     white = atof(delim);
     h_white-> SetBinContent(year,white); //my problem lies here .It returns zero if I do h_white->GetBinContent(x) where x is the year value with in the range defined in the TH1F definition, while the returned values are correct if output to screen via printf or cout
}

What did I miss?
F.

Hi,

I am not sure I have understood you problem. Can you please post a running script, including a file if needed, and tell which ROOT version are you using
Thank you

Lorenzo

char *fname = "/home/l_common/Frances/epidemiology/sect_04_zfig.04-5.csv"

int incidence () {
   int iline;
   int nlines = 16; //years covered
   float offset = 4.0;
   char line[1000];
   char *delim; 
   int year;
  Double_t white;

  TH1F *h_white=new TH1F("h_white","",nlines,1975-offset, 1992+offset);
  FILE * fp = fopen(fname, "r");
  for(iline=8;iline<25;iline++) {
     if (fgets(line,256,fp)!=NULL ) {
        printf("line %d\t", iline);
	delim = strtok(line,"\t");
	year = atoi(line);
  	printf("year %i\t", year);  //output OK

        delim = strtok (NULL, "\t"); 
  	white = atof(delim);
  	printf("white %f\t", white); //output OK
  	h_white-> SetBinContent(year,white); //output wrong if verified with GetBinContent(x); for example x = 1975. Outputs zero while I am expecting  107.300000. See below
     }
  }

outputs

root [0] .L BreastCancer.C  
** $Id: TGQt.cxx 24531 2008-06-25 05:45:47Z brun $ this=0x8839a68
root [1] int x = incidence()
Warning in <TROOT::Append>: Replacing existing TH1: h_black (Potential memory leak).
line 8  year 1975       white 107.300000
line 9  year 1976       white 104.800000
line 10 year 1977       white 103.400000
line 11 year 1978       white 103.700000
line 12 year 1979       white 104.800000
line 13 year 1980       white 105.100000
line 14 year 1981       white 109.900000
line 15 year 1982       white 110.000000
line 16 year 1983       white 114.500000
line 17 year 1984       white 119.900000
line 18 year 1985       white 127.900000
line 19 year 1986       white 130.400000
line 20 year 1987       white 140.300000
line 21 year 1988       white 136.400000
line 22 year 1989       white 132.400000
line 23 year 1990       white 136.200000
line 24 year 1991       white 138.900000


root [2] .L BreastCancer.C++
Info in <ACLiC>: script has already been loaded in interpreted mode
Info in <ACLiC>: unloading /home/l_common/Frances/macros/root/./BreastCancer.C and compiling it
Info in <TUnixSystem::ACLiC>: creating shared library /home/l_common/Frances//macros/root/./BreastCancer_C.so

Root version is 5.20
See attached file that the program reads
sect_04_zfig.04-5.txt (6.49 KB)

Instead of:
h_white->SetBinContent(year, white);
use:
h_white->Fill(year, white);
or:
h_white->SetBinContent(h_white->FindBin(year), white);

Thanks Pepe,
the correct function is

 h_white->SetBinContent(h_white->FindBin(year), white);

Actually, both my proposed solutions are “correct”, I believe.