Why is my histogram changing when I fill my other Branch

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 ;

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 ;

And then when I want to tranfer it this way ;

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 :

Anyone know why it swap me x and y axis ?

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).

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.

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)

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 :

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.

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.

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)