Problem reading file to fill a TH3F

Hello, I’ve this Root macro to read an ascii file and to fill aTH3F histogram.
scorelemma.cpp (4.6 KB)
The first time I tried it by reading data from this file (just an example file…data aren’t good!)

boxMesh_1.txt (1.8 MB)
and it worked (i.e. the macro reads the lines and I get the plot as you can see here

Now I produced the good files (i.e. good data) by Geant4, for example this one
boxMesh_1.txt (147.2 KB)
then I’ve to plot these data. I used this macro to do it, but I’ve a problem, i.e. the macro reads only the last line infinity times as you can see here

So I can’t understand the reason because of it works to read the first file and it doesn’t work to read the new ones

Thank you

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

You should modify the code reading the file as following, using that fscanf returns the number of read items:

while( fscanf(infile,"%lf %lf %lf %lf %lf %lf",&z,&phi,&R,&relen,&relenq,&ev)== 6)

Thank you @moneta it worked, but I noticed a problem in the plot.

The data in the txt file are
first culomn z axis
second culomn phi axis
third culomn Radius axis
Fourth culomn Released energy

The values of z,phi,r axis are binned, i.e. I dived the z axis in 10 bins, the R axis in 300 bins and the phi axis in 1 bin, then the phi value is only 0.
When I plot the data in the TH3F I get this image

and you see in the plots looks like that I’ve many events (i.e. the coulor is yellow) when phi=[0.9-1]…but it’s impossible, because in the file I’ve just phi=0, then I should have released energy only at phi=0.

I setted the bin in the Root macro in this way

TH3F *hscoring = new TH3F("hscoring","Released energy",300,0.,300.,1,0.,1.,10,0.,10.);

I think what you see is an effect of the projection.
But if you have only one bin in phi, it is useless to create or plot a 3D histogram, but use or convertt to a 2D histogram for plotting.
Do for example:



Thank you @moneta

Yes, yesterday I tried the TH2F, but i got this plot and it doesn’t look like nice (that’s because I tried the TH3F)

Instead, if I use this code

I get this error

I think the problem is that they are bin values…

The error is something else. Probably you are using gPad when is a nullptr. Just do :
if (gPad) gPad->Modified()

The 2D plot looks fine for me. It is what you get from the data. If you don’t like you have different options for showing 2d plots. See the THistpainter documentation


Thank you @moneta

Yes I agree that the TH2F plot is fine for the data…I mean that the view isnt’ nice…I will tray differnt options!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.