TH3F from ASCII file

Hello, I produce this file by Geant4

boxMesh_1.txt (1.8 MB)

I must do a binned TH3F plot COLZ ( I mean something like this

)

in wich I’ve

  1. The cylindrical coordinates z,phi, R on the 3 axes (i.e. culomns 1,2,3);
  2. The color scale must be done on the basis of the the released energy (culomn 4) and the event number (culomn number 6)

I’m able to plot a TH3F from a root file as for example:

 TH3F *hspacehitv1 = new TH3F("hspacehitv1", "", 40, 0., 0.,40, 0.,0.,40,0.,0.);
         TString varhspacehitv1 = TString::Format("y : x : z >> hspacehitv1");

but I don’t know how to plot a th3f from the attached file…
Does someone know how to do it please?


_ROOT Version: 6.20.04
_Platform:Windows
_Compiler:MVS2019


You need to read the file and fill the histogram accordingly.
There is many examples on the web showing how to read an ascii file in C/C++

Thank you @couet. In the past I used the Tgraph like this

 TMultiGraph *mg = new TMultiGraph();
   TGraphErrors *gamma = new TGraphErrors(data,"%lg %lg %lg %lg");

but I never filled histograms…

I was looking to find something…I found, for example this topic Histogram from a text file but I don’t understand how to adapt it to my need…

I was trying to write a macro but I don’t know how to finish it

scorelemma.cpp (5.0 KB)

I hope you can help me to finish it…

As i said that’s not a ROOt question. You just want to read the text file. For instance:

or what ever … search n the web …

Hi @couet thank you. So I read file using the fscanf like this

while( fscanf(infile,"%d %f %[^\n]s",&input[i].a,&input[i].b,&input[i].dataStr)!  =NULL)

and I fill a TH3F using input[i].a input[i].b etc? but How can I set the color scale based both on relased energy and entries number (culoms 4 and 6)?
Thank you

Then you need to fill the histogram with the value you need. have a look at TH3 doc

Hi @couet I used

	while( fscanf(infile,"%lf %lf %lf %lf %lf %lf",&z,&phi,&R,&relen,&relenq,&ev)!=NULL)
		{
			printf("z=%lf \t phi=%lf \t R=%lf \t E=%lf \t ev=%lf \n",z,phi,R,relen,ev);
			hscoring->Fill(z,phi,R);
			i++;
		}

to read the file and it worked

but currently I’m just filling the histogram using z,R,phi values…instead I must set the color scale by using the released energy and the events number (culomns 4 and 6) and I don’t know how to do it… can you help me to add this function please?

Here the ASCII file and the updated macro

boxMesh_1.txt (1.8 MB)

scorelemma.cpp (5.4 KB)

UPDATE:

I tried to fill a TTree, so that I can plot the Branches of the TTree
TString hscoring = TString::Format("relenbra:Zbra:phibra:Rbra>> hscoringTh3f");
…but

  1. IN this way I can just use the coordinates and the released energy, intestead my color scale must require also the entries (i.e. culomn 6)
  2. I get these errors

This is the macro
scorelemma.cpp (6.1 KB)

Thank you

The color scale is mapped on the bin contains. Just fill the histogram with the variable you want to show in the color map.

Hello @couet, thank you… so I started again from the first macro in my previous message ( i.e. https://root-forum.cern.ch/uploads/short-url/61wcQ8Cs5FOZNmHIe5BaHUj9MKB.cpp)

and I filled using all the variables

while( fscanf(infile,"%lf %lf %lf %lf %lf %lf",&z,&phi,&R,&relen,&relenq,&ev)!=NULL)
	{
		printf("z=%lf \t phi=%lf \t R=%lf \t E=%lf \t ev=%lf \n",z,phi,R,relen,ev);
		hscoring->Fill(ev,relen,z,phi,R);
		i++;
	}

but I get errors because there are many variebles in the TH3F

here the macro

scorelemma.cpp (5.4 KB)

Yes you are not using the Fill correctly …check the doc.

sorry @couet but I was looking this page https://root.cern.ch/doc/v608/classTH3.html , but I don’t see anything that can help me…

Look at the possible Fill signatures available. Thos are the only allowed on TH3. The message you get says explicitly that.

Hi @couet ok I understand that in the TH3F I’ve to use 4 variables x,y,z,weight

I tried, for a first step to fill in this way (i.e. just using z,R,phi and the released energy)

hscoring->Fill(z,phi,R,relen);

but

  1. I also have to take in account the entries …so, how can I take in account also the entries? for example for the line

0 0 0 83.94639398818923 3.58401786465498 3387

I’ve to say that theare 3387 events releasing energy 83.94MeV at z=0,R=0,phi=0 (not just one)

  1. I get this image

    that it’s obviously wrong
    scorelemma.cpp (5.4 KB)

Yes but a TH3 has only 4 variables. That’s up to you to define what you meant by “taking into account”.

Hi @couet I mean this:
Let’s consider, for example, the lines

z phi R Energy Entries
0 0 0 83.94639398818923 3387
0 0 1 0.03674499528515837  3
0 0 2 0 0

The first line means I’ve 3387 events at z=0,phi=0,R=0 realsing a total energy 83.95MeV
The second line means I’ve 3 events at z=0,phi=0,R=1 releasing a total energy 0.037MeV
The third line meas I’ve 0 events at z=0, phi=0,R=2 releasing a total energy 0MeV
is there a way to plot also how many events released the energy in each point?

Your macro does not work for me. I get a crash

It’s strange @couet… did you use this one?
scorelemma.cpp (5.4 KB)

Processing scorelemma.cpp...
In file included from input_line_12:1:
/Users/couet/Downloads/scorelemma.cpp:94:80: warning: comparison between NULL and non-pointer ('int' and NULL) [-Wnull-arithmetic]
                while( fscanf(infile,"%lf %lf %lf %lf %lf %lf",&z,&phi,&R,&relen,&relenq,&ev)!=NULL)
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ ~~~~
Error opening file%  

Hi @couet I read Error opening file%

the file is opening in this way

sprintf(dirin, "C:/"); 	
sprintf(inname, "boxMesh_1");
sprintf(informat, "txt");
TString myfile=TString::Format("%s%s.%s",dirin,inname,informat);

So, maybe, did you forget to repleace the sprintf(dirin, "C:/"); according to the directory into you saved the file?

I have changed your macro . I simply do:

	FILE * infile=fopen("boxMesh_1.txt", "r");

and I get the previous error.