faca87
September 11, 2020, 12:37pm
1
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
The cylindrical coordinates z,phi, R on the 3 axes (i.e. culomns 1,2,3);
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
couet
September 11, 2020, 12:48pm
2
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++
faca87
September 11, 2020, 1:53pm
3
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…
couet
September 11, 2020, 2:21pm
4
As i said that’s not a ROOt question. You just want to read the text file. For instance:
c, ascii, scanf
or what ever … search n the web …
faca87
September 11, 2020, 2:46pm
5
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
couet
September 11, 2020, 2:59pm
6
Then you need to fill the histogram with the value you need. have a look at TH3 doc
faca87
September 13, 2020, 10:57am
10
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
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)
I get these errors
This is the macro
scorelemma.cpp (6.1 KB)
Thank you
couet
September 14, 2020, 8:30am
11
The color scale is mapped on the bin contains. Just fill the histogram with the variable you want to show in the color map.
faca87
September 14, 2020, 9:16am
12
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)
couet
September 14, 2020, 9:28am
13
Yes you are not using the Fill correctly …check the doc.
faca87
September 14, 2020, 2:41pm
14
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…
couet
September 14, 2020, 2:57pm
15
Look at the possible Fill
signatures available. Thos are the only allowed on TH3. The message you get says explicitly that.
faca87
September 15, 2020, 8:42am
16
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
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)
I get this image
that it’s obviously wrong
scorelemma.cpp (5.4 KB)
couet
September 15, 2020, 8:57am
17
Yes but a TH3 has only 4 variables. That’s up to you to define what you meant by “taking into account”.
faca87
September 15, 2020, 1:03pm
18
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?
couet
September 15, 2020, 1:29pm
19
Your macro does not work for me. I get a crash
faca87
September 15, 2020, 5:05pm
20
It’s strange @couet … did you use this one?
scorelemma.cpp (5.4 KB)
couet
September 16, 2020, 7:42am
21
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%
faca87
September 17, 2020, 8:00am
22
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?
couet
September 17, 2020, 8:16am
23
I have changed your macro . I simply do:
FILE * infile=fopen("boxMesh_1.txt", "r");
and I get the previous error.