Hi everyone,
I am trying to plot energy deposited vs. transverse radius using ROOT, but I keep getting an empty histogram. I am new to ROOT and not very experienced in C++.
My setup:
I have data on energy deposition in both the absorber and the gaps.
I calculate the transverse radius using the Pythagorean theorem:
r=x2+y2
radius_hit = sqrt(pow(Gap_X->at(nhit), 2) + pow(Gap_Y->at(nhit), 2));
The applied energy is 2 GeV, and I expect to see how the deposited energy varies with transverse radius.
Problem:
My histogram is empty despite filling it in the code.
I suspect there might be an issue with how I am filling the histogram or how ROOT handles my data.
I am attaching the code. Kindly, help me in sorting out the code. Thanks ! GeV2_NEG_PION.C (3.6 KB)
You are not providing a stand-alone macro that shows the issue, so it’s hard to tell without knowing what happens in the rest of your code, but you can try printing out some messages and see if the code is even reaching the Fill or SetBinContent line. For instance, you have
for (Long64_t jentry = 0; jentry < nentries; jentry++) {
if (jentry >= 10) break;
so that could be leaving out many entries, and so on (check all your loops and if conditions and see if there is data surviving those)…
One quick check, if you are getting a plot (you actually see the histo in a canvas, although without entries), is adding gStyle->SetOptStat(111111); before drawing, and check if the entries are in the under and/or overflow bins (meaning you are indeed filling, but the histogram lower/upper limits --in x-- are not wide enough for the data, or the bin numbers you have in SetBinContent are wrong).
I tried changing the jentry iteration condition to if (jentry > 100) and adjusting the histogram’s lower and upper limits in the x-axis, but it is still empty. I also included gStyle->SetOptStat(111111);, and I can see that the histogram remains empty. I am attaching the PDF for reference. unnamed (2).pdf (13.4 KB)
Maybe if you can help me in filling the histogram and loop adjusting?
As I wrote, add printouts at the appropriate points to check that things are going on as you expect. For example, print nentries as soon as you assign it a value and see if it’s correct, and so on at other points and variables; another example: just before or after SetBinContent, print out the bin number and the value you are trying to add to the histo and see what you have; etc.
Hi, I checked and I see the problem lies in
if (radius_hit > radius[i] && radius_hit < radius[i + 1]) {
Eabs_r[i] += EAbs->at(nhit);}. It is not storing the nhits. I introduced vector std::vector<std::vector<size_t>> nhit_store(n_radius); To store nhit indices
nhit_store[i].push_back(nhit). But still it is empty.
It will be faster if you could upload all the files one would need to run your macro and explain how to run it. I will be able to take a look at this then.
Hi Yus, GeV2_NEG_PION.C (5.2 KB) GeV2_NEG_PION.h (6.5 KB)
I attached GeV2_NEG_PION.C , GeV2_NEG_PION.h. To run the macro. you need to follow the steps below:
root GeV2_NEG_PION.C
GeV2_NEG_PION t
t.Loop
It will show the output. Thanks !! GeV2_NEG_PION.root - Google Drive. GeV2_NEG_PION.root contains the root file which is the source file. I will be looking for the response.
first, I guess the t.Loop
in your instructions should have been t.Loop()
Second, your histogram is not exactly empty. Each bin is being filled with a zero.
Third, and most importantly, is the reason why each bin is filled with a zero. You have
std::vector<double> radius(n_radius, 0.0);
for (int i = 0; i < n_radius; i++)
{
radius[i] = i*10;
...
if (radius_hit > radius[i] && radius_hit < radius[i + 1])
{
... // things where you calculate what to fill the histogram with
}
...
}
How is the code supposed to figure out radius[i+1] while you are at i in your loop?
no, that’s not what I’m suggesting. You still have
std::vector<double> radius(n_radius + 1, 0.0);
...
for (int i = 0; i <= n_radius; i++) { // Note: loop goes up to <= n_radius
radius[i] = i * 10;
...
for (...) {
...
for (...) {
...
if (...) {
...
for (...) {
...
for (...) {
...
if (radius_hit > radius[i] && radius_hit < radius[i + 1]) {
...
}
}
}
}
}
}
}
For i=0, raduis[i]=0*10=0 and radius[i+1]=0 (because it is 0 by default
and you do not assign it any other value by this iteration).
For i=1, raduis[i]=1*10=10 and radius[i+1]=0.
For i=2, raduis[i]=2*10=20 and radius[i+1]=0.
and so on
And you require that radius_hit < radius[i + 1] where radius_hit = sqrt(...), so it’s obviously a positive number. How can something positive be ever < 0?
Hi Yus,
Thanks for being so patient with me. I changed the if condition of radius hit:
radius_hit = if (radius_hit < radius[i-1] && radius_hit > radius[i]).
Secondly, I am getting very high number Y-scale such that 10^6. I am expecting it within range
(0-1). Kindly, can you have a look at the attached code as I trying to modify SetBinContent. GeV5_NEG_PION.C (3.7 KB). Thank you so much !!!
not sure why you would expect energies from 0 to 1. First, it’s unphysical (why only from 0 to 1 GeV?). Second, you fill your Edep histogram with totalEnergy[i] values
Edep->SetBinContent(i , totalEnergy[i]);
, where the totalEnergy[i] values are calculated in a loop:
for (int i = 0; i < n_radius; i++) {
double energy = (Eabs_r[i] + Egap_r[i]) / 2.0 * 1000.0;
totalEnergy[i] += energy;
}
I checked that these Eabs_r[i] values can be as large as 300. So the energy value is at least 1.5e5. And you sum it up with the existing totalEnergy[i]. No wonder you get things like 12 PeV (petaelectronvolts)!
Provided energy is just 2 GeV so it not possible to get deposited energy in that high scale. so i am confused in looping the code. it should be around (0-1) GeV.
What is the dimension of Egap and EAbs from the .root file? Is it GeV?
Also please note that you have multiple loops and an addition assignment (Eabs_r[i] += EAbs->at(nhit);) inside the innermost loop. Do you have a rough estimate on how many times this line gets executed?
Finally, you multiply every energy value by 500 with
double energy = (Eabs_r[i] + Egap_r[i]) / 2.0 * 1000.0;
If you don’t need the multiplication by 500 then please drop it from the code and upload a new version.
Also please note that your loop over TTree entries happens inside another loop over i, which runs from 0 to 80. Are you sure you need that (usually you don’t)?
Hi yus, Sorry I just checked and observed i will need the multplication of 500 to normalize. And I am attaching my code. I made a modification and trying to store the data in double energy. Also I am attaching another code which i am trying to copy. Confinement Containment.C (4.1 KB) GeV2_NEG_PION_mod.C (3.7 KB)
.C
if your GeV2_NEG_PION_mod.C is supposed to be similar to the Containment.C, please note that they do not have the outermost loop for (int i = 0; i < n_radius; i++) {...}. Also, you have up to 5 nested for loops in your code, while they have up to 3.
Anyway, I believe you will have better luck asking your colleagues and/or your supervisor about this as this is not a technical issue but merely a question on how to analyze your data.