# Trying to get each evenement independently

**URL:** <https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219>\
**Category:** Newbie\
**Created:** [May 4, 2024, 11:51am UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219 "2024-05-04T11:51:23Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [May 5, 2024, 6:21pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/23 "2024-05-05T18:21:41Z")

</div>

Dear @KillianUB ,

The RDataFrame API expects you use columns available in your dataset. Are you sure you have the column named `x` in your dataset?

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![KillianUB](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/46a35a/32.png) [@KillianUB](https://root-forum.cern.ch/u/KillianUB)\
**Post date:** [May 5, 2024, 6:27pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/24 "2024-05-05T18:27:22Z")

</div>

No i don’t have column named x in my datatset. I’m sorry some problem are solved before you answered.  
Now i’m trying to get the data for one event, i’m using something like :

// Index de l’événement que vous souhaitez accéder  
Long64\_t eventIndex = 0; // Remplacez 0 par l’index de l’événement que vous souhaitez accéder

// Accédez à l’événement spécifique dans l’arbre  
tv\_\_tree-\>GetEntry(eventIndex);

// Accédez aux données du détecteur 1 pour cet événement  
Float\_t psdValue = tv\_\_tree-\>Psd\_All\_Shift[30]; // Remplacez 30 par l’indice du détecteur que vous souhaitez accéder

// Faites quelque chose avec les données du détecteur 1 pour cet événement, par exemple, affichez-le  
std::cout \<\< "Valeur PSD pour le détecteur 1 pour l’événement " \<\< eventIndex \<\< " : " \<\< psdValue \<\< std::endl;

and it returns me

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/1/5/15230add098e80094e1c32898def72144d7b9872.png)

This is my new problem to solve !

---

<div class="post-metadata">

**Author:** ![KillianUB](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/46a35a/32.png) [@KillianUB](https://root-forum.cern.ch/u/KillianUB)\
**Post date:** [May 5, 2024, 6:29pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/25 "2024-05-05T18:29:19Z")

</div>

I forgot to mention that this code ;

TH1 _histogram = (TH1_)gPad-\>GetPrimitive(“h1”);

// Vérifiez si l’histogramme existe  
if (histogram) {  
// Accédez aux données de l’histogramme  
int nbins = histogram-\>GetNbinsX();  
for (int i = 1; i \<= nbins; ++i) {  
double binContent = histogram-\>GetBinContent(i);  
// Faites quelque chose avec le contenu du bin, par exemple, l’afficher  
std::cout \<\< "Contenu du bin " \<\< i \<\< " : " \<\< binContent \<\< std::endl;  
}  
} else {  
std::cout \<\< “L’histogramme n’a pas été trouvé.” \<\< std::endl;  
}

Give me the number of event per pin, wish is a great step, but now i want the same thing but event per event.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [May 5, 2024, 6:29pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/26 "2024-05-05T18:29:31Z")

</div>

Dear @KillianUB ,

> Float\_t psdValue = tv\_\_tree-\>Psd\_All\_Shift[30];

Where have you seen this syntax? The `tv__tree` variable, which is a `TTree*` does not have a `Psd_All_Shift` method (as the very informative error suggests). What makes you think that the code you are writing is valid? Could you maybe take a further look at the TTree documentation and tutorials?

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![KillianUB](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/46a35a/32.png) [@KillianUB](https://root-forum.cern.ch/u/KillianUB)\
**Post date:** [May 5, 2024, 6:31pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/27 "2024-05-05T18:31:05Z")

</div>

I’m asking chat gpt, of cours the code given by chat gpt are not good but sometime it gives me the path to follow.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [May 5, 2024, 6:36pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/28 "2024-05-05T18:36:02Z")

</div>

Dear @KillianUB ,

You are not that far from the solution. You can traverse the entries of the tree using `GetEntries()` as the condition to stop the for loop, then calling `tree->GetEntry(i)` inside the for loop. There, you have to properly set the memory address of a certain variable, call it maybe `psdValue` as in your previous example, which needs to be populated by the value of the branch `Psd_All_Shift` with the correct index. Traverse all the indices of however many detectors your application has within the same event, you can store the values in a vector for each event.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![KillianUB](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/46a35a/32.png) [@KillianUB](https://root-forum.cern.ch/u/KillianUB)\
**Post date:** [May 5, 2024, 6:39pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/29 "2024-05-05T18:39:30Z")

</div>

That would be amazing if i could get it tonigth ! I’ll keep going, thx a lot for all your help, i think this forum topic could become an entire tutorial.

---

<div class="post-metadata">

**Author:** ![KillianUB](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/46a35a/32.png) [@KillianUB](https://root-forum.cern.ch/u/KillianUB)\
**Post date:** [May 5, 2024, 7:19pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/30 "2024-05-05T19:19:09Z")

</div>

I tryed this and the result was kinda funny :

tv\_\_tree-\>Draw(“Psd\_All\_Shift[1]\>\>h1(7500,500,8000)”,“”,“colz”);  
for (int i =1; i\<= nbins ; ++i ) {  
double binContent = GetEntry(i);  
std::cout \<\< "Contenu du bin " \<\< i \<\< " : " \<\< binContent \<\< std::endl;  
}

How ever i’d like to proced step by step, now that i can get my number of event bin per bin for one detctor, i’d like to get the same thing but divided by event like the exact same thing but for each event, i know what i want, you gave me the tools, but i don’t know how to do.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [May 5, 2024, 7:53pm UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/31 "2024-05-05T19:53:25Z")

</div>

Dear @KillianUB ,

I am glad that you are appreciating the learning value of this experience. As I previously mentioned, there are many tutorials representing already what you are doing, as well as similar questions on the forum. You have all the ingredients necessary to reach your solution, as follows:

- The name of the dataset and the name of the file, so you can get a TTree object in your application.
- The number of entries in the tree, that is `entries = tree->GetEntries()`
- Traversing the entries in the tree via a for-loop, using the number of entries as the condition and calling `tree->GetEntry(i)` inside the loop
- Populating the value of what you are trying to get from the tree at every event. You need to properly set the branch address before the loop for this to work.
- Storing N different values at each iteration of this for loop, one for every detector element you need.

You can see an example in [the manual](https://root.cern/manual/trees/#reading-a-tree). Keep in mind that using `TTree::Draw` is orthogonal to what you are trying to do, it’s not necessary in this case.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![KillianUB](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/46a35a/32.png) [@KillianUB](https://root-forum.cern.ch/u/KillianUB)\
**Post date:** [May 6, 2024, 7:06am UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/33 "2024-05-06T07:06:59Z")

</div>

Hello,

Here is what I’ve done for now :

TFile f{“RAW148OS\_treeFF01.root”};  
std::unique\_ptr tv\_\_tree{f.Get(“Analysis\_All”)};  
tv\_\_tree-\>Draw(“Psd\_All\_Shift[1]\>\>h1(7500,500,8000)”,“”,“colz”);  
TH1 _histogram = (TH1_)gPad-\>GetPrimitive(“h1”);

// Vérifiez si l’histogramme existe  
if (histogram) {  
// Accédez aux données de l’histogramme  
int nbins = histogram-\>GetNbinsX();  
int nevent = tv\_\_tree-\>GetEntries();  
for (int j = 0; j \< nevent; ++j) {  
tv\_\_tree-\>GetEntry(j); // Accédez à l’événement actuel  
for (int i = 1; i \<= nbins; ++i) {  
double binContent = histogram-\>GetBinContent(i);  
// Faites quelque chose avec le contenu du bin, par exemple, l’afficher  
std::cout \<\< "Contenu du bin " \<\< i \<\< " pour l’événement " \<\< j \<\< " : " \<\< binContent \<\< std::endl;  
}  
}  
} else {  
std::cout \<\< “L’histogramme n’a pas été trouvé.” \<\< std::endl;  
}

I just realised that the commande GetEntry() always return me the same data (int) 1690 what i don’t really understand. So when I run this code, I loop on every events for every pins but the number of events per pins is always the same and it’s not 0 or 1.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [May 6, 2024, 10:11am UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/34 "2024-05-06T10:11:54Z")

</div>

[Here](https://root.cern/doc/master/classTTree.html#a14c88179bd5fd2116228707d6addea9f) you can see what GetEntry returns; it’s not what you seem to think.  
Have a look at the [TTree tutorials](https://root.cern/doc/master/group __tutorial__ tree.html) to see how to create and read them. For example, check out [tree1.C](https://root.cern/doc/master/tree1_8C.html), in particular the function tree1r(), where you can see how to read and use variables (branches) from the tree event by event. You need to use SetBranchAddress (unless you use dataframes, as Vincenzo suggested at the beginning, but you are doing something else).

---

<div class="post-metadata">

**Author:** ![KillianUB](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/46a35a/32.png) [@KillianUB](https://root-forum.cern.ch/u/KillianUB)\
**Post date:** [May 6, 2024, 10:52am UTC](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219/35 "2024-05-06T10:52:09Z")

</div>

TFile f{“RAW148OS\_treeFF01.root”};  
std::unique\_ptr tv\_\_tree{f.Get(“Analysis\_All”)};  
tv\_\_tree-\>Draw(“Psd\_All\_Shift\>\>h1(7500,500,8000)”,“”,“colz”);  
TH1 _histogram = (TH1_)gPad-\>GetPrimitive(“h1”);

// Vérifiez si l’histogramme existe  
if (histogram) {  
// Accédez aux données de l’histogramme  
int nevent = 1; //on définit le nombre d’évenement  
int n = 0;  
for (int j = 0; j \< nevent; ++j) {  
tv\_\_tree-\>Show(j);  
tv\_\_tree-\>GetEntry(j); // Accédez à l’événement actuel  
for (int i = 1; i \<= nbins; ++i) {  
tv\_\_tree-\>SetBranchAdress(“Psd\_All\_Shift[i]”, &Psd\_All\_Shift);  
double branchContent = Psd\_All\_Shift;  
if (branchContent \< 500) {  
n = n;  
} else {  
n = n + 1 ;  
}  
return n;

```
        // Faites quelque chose avec le contenu du bin, par exemple, l'afficher
        std::cout << "Contenu du bin " << i << " pour l'événement " << j << " : " << branchContent << std::endl;
    }
}

```

} else {  
std::cout \<\< “L’histogramme n’a pas été trouvé.” \<\< std::endl;  
}

I tried to creat a code allowing me to navigate intoo event and get the result for only one branch. Her’s what i writed, it obviously dosnt work, but i hope you’ll understand the idea.

[Previous page](https://root-forum.cern.ch/t/trying-to-get-each-evenement-independently/59219.md?page=1)
