Code does not work

I have a file as attached. I need to find the sum of edep in the file. I have attached my code here but it does not work. What am I doing wrong?

code1.C (734 Bytes) trial.root (35.4 KB)

ROOT Version: v6_18.04

Hi @Saoirse_Reids ,
and welcome to the ROOT forum!
Giving a name to the function provides better error messages, i.e. start the file with:

void code1() {

instead of just


Then one problem is that (at least with the file you shared) f.Get("t") returns a null pointer, or in other words the file does not contain a tree called “t”.

This works better:

// get the directory "radioprotection_ntuple"
TDirectory* dir = (TDirectory*)f.Get("radioprotection_ntuple");
// get tree called "102" from that directory (which has title "Edep")
TTree* ntuple = (TTree*)dir->Get("102");

A weird thing about this file is that the ntuples that contain the data (that are in the subdirectory radioprotection_ntuple subdirectory) are called 101, 102 and 103.

Then another problem is that none of the trees have a branch called “count”, which you try to retrieve.


check out for a list of learning resources

How do I get the sum then ? The count is basically the frequency of each value of edep.
I did realize there is no branch called “count” but how do it then?

if you just want the sum of all “edep” values, after removing the line that does the wrong GetBranch("count") you can do:

double sum = 0.;
for ( Int_t i=0; i<nevent; i++ ) {
     sum += edep;
std::cout << "sum: " << sum << std::endl;

If you want the frequency of each value of edep, then you probably want to fill a histogram, something like

TH1D *h = new TH1D("h", "h");
for ( Int_t i=0; i<nevent; i++ ) {

I hope this helps!

The total edep is given by multiplying edep with its frequency, this only sums up the edep. I need it to multiply as well.
If I have 3 instances of 400 MeV of energy, the total will be 3 * 400 = 1200 MeV.

These are the values of edep I see:

5.18935 MeV
115.691 MeV
0.231583 MeV
1.50597 MeV
0.974719 MeV
0.302951 MeV
0.160779 MeV
0.213786 MeV
0.549618 MeV
0.541896 MeV
0.748615 MeV
0.43873 MeV
1.54937 MeV
0.726342 MeV
0.756259 MeV
0.974569 MeV
0.733663 MeV
0.539357 MeV
0.54377 MeV
1.60516 MeV
0.1084 MeV
0.563969 MeV
0.625621 MeV

How do you decide when they are “the same” (equality on floating point values is tricky)?

Anyway once you read the values in, you can perform any arbitrary calculation on them in standard C++