"booking" TH1F outside main loop

I’m getting segfaults, but I’m almost certain what the problem is. I just don’t know how do this in root.

I call a function (asciiread) four times. The asciiread function would then create the TH1F objects and fill them. However, because the function was called 4 times, it tried to “book” the histograms 4 times. I created a new function hbkini to book only once, but the asciiread function has no knowledge of these objects.

hbkini(){
…make arrays of histograms…
TH1F h[i] = new TH1F(…)
}
asciiread(){
…fill histograms
h[i]->Fill;
}

MyMain(){

hbkini
asciiread(i);
asciiread(j);
asciiread(k);
asciiread(l);
}

I’ll attach the file to make things clearer.

Thanks
James
mchistnew_debug.c (15.6 KB)

The simplest solution with your example is to declare the array
of pointers as global outside teh function readascii.
The clean solution is to have meaningfull names for your pointers
and declare them as data members of a class. Your code
is Fortran like (or rather hbook like) ::slight_smile:

I have also fixed several other problems in your file. Read the lines
commented with =====>

Rene
mchistnew_debug.c.gz (3.21 KB)