# Why is my histogram changing when I fill my other Branch

**URL:** <https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321>\
**Category:** Newbie\
**Tags:** tree, graphics, rdataframe, root\
**Created:** [May 13, 2024, 1:57pm UTC](https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321 "2024-05-13T13:57:58Z")\
**Posts on this page:** 7\
**Page:** 1

<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 13, 2024, 1:57pm UTC](https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321/1 "2024-05-13T13:57:58Z")

</div>

Hello,  
I’m trying to transfer an hist from a tree to an other tree, but when I transfer it, it doesn’t transfer like i would like it to do. Here’s my code ;

```auto
ROOT::RDataFrame df{"Analysis_All", "RAW148OS_treeFF01.root"};

auto count_good = df.Define("count_good_pts", [](const ROOT::RVec<UShort_t> &vec) {
    return vec[0] > 0 ? 1 : 0; // Utilise seulement le premier élément du vecteur
}, {"Psd_All_Shift"});

auto hist = count_good.Histo1D({"hist", "N(n)", 10, 0, 10}, "count_good_pts");

TCanvas *canvas = new TCanvas("canvas", "Canvas", 800, 600);

// Dessine l'histogramme sur le canvas
hist->Draw();

// Affiche le canvas
canvas->Draw();

```

Wish returns me ;

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

And then when I want to tranfer it this way ;

```auto
ROOT::RDataFrame df{"Analysis_All", "RAW148OS_treeFF01.root"};

auto count_good = df.Define("count_good_pts", [](const ROOT::RVec<UShort_t> &vec) {
    return vec[0] > 0 ? 1 : 0; // Utilise seulement le premier élément du vecteur
}, {"Psd_All_Shift"});

auto hist = count_good.Histo1D({"hist", "N(n)", 10, 0, 10}, "count_good_pts");

// Obtenir les valeurs de l'histogramme dans un vecteur
std::vector<double> hist_values;
for (int i = 1; i <= hist->GetNbinsX(); ++i) {
    hist_values.push_back(hist->GetBinContent(i));
}

// Créer un nouvel arbre avec une branche contenant les données de l'histogramme
auto new_tree = std::make_unique<TTree>("new_tree", "New Tree");
new_tree->Branch("Psd_1", &hist_values); // Ajouter les valeurs de l'histogramme comme branche

// Enregistrer les données dans le nouvel arbre
new_tree->Fill();

// Écrire le nouvel arbre dans le fichier ROOT
TFile output_file("RAW148OS_treeFF01.root", "UPDATE");
new_tree->Write();
output_file.Close();

```

It returns me :

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/4/3/4375d765051509d69195a1dbbcc71b22551f28cb.png)

Anyone know why it swap me x and y axis ?

---

<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 13, 2024, 3:37pm UTC](https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321/2 "2024-05-13T15:37:55Z")

</div>

Dear @KillianUB ,

All is correct in your output. You can clearly see from the first image that there are `3000x10^3` counts in the first bin (I’m approximating the real value just by looking at the figure). Then there is I guess one count for the second bin. The other bins are empty

In the second image, you have exactly what you requested. One value at roughly `3000x10^3` which comes from the first bin of the histogram, then every other value is close to zero because all other bins of the histogram are zero (except the second bin clearly, but its value is so close to zero that you see it there in the second image).

---

<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 13, 2024, 6:02pm UTC](https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321/3 "2024-05-13T18:02:36Z")

</div>

Thanks for your answer, if I got exactly what I wanted, then I didn’t wanted what I wanted haha.  
All I would like is to get the exact same thing as the first pic but stored in a new Branch of a new tree.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 13, 2024, 7:58pm UTC](https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321/4 "2024-05-13T19:58:55Z")

</div>

In the new branch, you enter the number of entries in each of the bins of the histograms and in the second image, that is what is plotted.

What is the goal you are trying to achieve? Woudn’t just storing the histogram be the right solutions (note by definition the histograms does NOT contains the set of original values)

---

<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 14, 2024, 8:10am UTC](https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321/6 "2024-05-14T08:10:20Z")

</div>

Hello, thanks for your answer. The first histogram I sent is for only one detector. I have 34 detectors wish are disposed with different angle around the fission chamber; I would like to get the angular distributivity of neutrons. So my plan was to extract the value for each detectors in a branch, creat two new branchs with both angle phi en theta, so I can get a 3d hist of my distribution.  
But I don’t know how to export those datas from my main tree to an other one.

I tried this too :

```auto
std::unique_ptr<TFile> myFile( TFile::Open("RAW148OS_treeFF01", "RECREATE") );
auto tree = std::make_unique<TTree>("tree", "TheNewTree");

ROOT::RDataFrame df{"Analysis_All", "RAW148OS_treeFF01.root"};

auto count_good = df.Define("count_good_pts", [](const ROOT::RVec<UShort_t> &vec) {
    return vec[2] > 0 ? 1 : 0; // Utilise seulement l'élément [2] du deteceteur
}, {"Psd_All_Shift"});

tree->Branch("Psd_detect",&count_good);

```

But I’ve got an error, it’s not possible to transfer that kind of data to another branch.

---

<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 14, 2024, 8:30am UTC](https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321/7 "2024-05-14T08:30:43Z")

</div>

I just realised that what I wanted is kinda harder then I tough since I can’t just fill a new branch with my hist per detector and use it after. I don’t know how to proceed.

---

<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 14, 2024, 9:12am UTC](https://root-forum.cern.ch/t/why-is-my-histogram-changing-when-i-fill-my-other-branch/59321/8 "2024-05-14T09:12:57Z")

</div>

Dear @KillianUB ,

You don’t need to store an entire new TTree. You can directly store the histogram objects in the TFile (as well as any arbitrary C++ object for that matter)
