# Code does not work

**URL:** <https://root-forum.cern.ch/t/code-does-not-work/44417>\
**Category:** Newbie\
**Created:** [April 14, 2021, 6:18pm UTC](https://root-forum.cern.ch/t/code-does-not-work/44417 "2021-04-14T18:18:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saoirse\_Reids](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saoirse_reids/32/25458_2.png) [@Saoirse\_Reids](https://root-forum.cern.ch/u/Saoirse_Reids)\
**Post date:** [April 14, 2021, 6:18pm UTC](https://root-forum.cern.ch/t/code-does-not-work/44417/1 "2021-04-14T18:18:39Z")

</div>

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](https://root-forum.cern.ch/uploads/short-url/87Mr0xvIVxKSkjCKZLwPhFLuq6N.C) (734 Bytes) [trial.root](https://root-forum.cern.ch/uploads/short-url/cziyySOSNol1zKwWqOsBcF8X48t.root) (35.4 KB)

_ROOT Version:_ v6\_18.04

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 14, 2021, 8:26pm UTC](https://root-forum.cern.ch/t/code-does-not-work/44417/2 "2021-04-14T20:26:41Z")

</div>

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:

```auto
void code1() {

```

instead of just

```auto
{

```

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:

```auto
// 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.

Cheers,  
Enrico

P.S.  
check out [https://root.cern/get\_started](https://root.cern/get_started) for a list of learning resources

---

<div class="post-metadata">

**Author:** ![Saoirse\_Reids](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saoirse_reids/32/25458_2.png) [@Saoirse\_Reids](https://root-forum.cern.ch/u/Saoirse_Reids)\
**Post date:** [April 15, 2021, 5:29am UTC](https://root-forum.cern.ch/t/code-does-not-work/44417/3 "2021-04-15T05:29:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 15, 2021, 7:38am UTC](https://root-forum.cern.ch/t/code-does-not-work/44417/4 "2021-04-15T07:38:41Z")

</div>

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

```cpp
double sum = 0.;
for ( Int_t i=0; i<nevent; i++ ) {
     ntuple->GetEvent(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

```cpp
TH1D *h = new TH1D("h", "h");
for ( Int_t i=0; i<nevent; i++ ) {
     ntuple->GetEvent(i);
     h.Fill(edep);
}
h->Draw();

```

I hope this helps!  
Enrico

---

<div class="post-metadata">

**Author:** ![Saoirse\_Reids](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saoirse_reids/32/25458_2.png) [@Saoirse\_Reids](https://root-forum.cern.ch/u/Saoirse_Reids)\
**Post date:** [April 15, 2021, 9:02am UTC](https://root-forum.cern.ch/t/code-does-not-work/44417/5 "2021-04-15T09:02:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 15, 2021, 9:15am UTC](https://root-forum.cern.ch/t/code-does-not-work/44417/6 "2021-04-15T09:15:10Z")

</div>

These are the values of `edep` I see:

```auto
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)?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 15, 2021, 9:17am UTC](https://root-forum.cern.ch/t/code-does-not-work/44417/7 "2021-04-15T09:17:51Z")

</div>

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