Impossible to fill the Root tree

Hello
I tried to create and fill a tree from .txt file.
Then I did all the code on a root session, with C++.
First I wrote a program that open .txt file and fill an static table, then I created a tree and I fill it with the data from a[size][n].
The problem that I had, Root didn’t fill the tree and it’s empty.
Here is the code.
Can you help me please ?
thank you :slight_smile:

#include <iostream>

#include<fstream>

#include<cmath>

#include<vector>

using namespace std;



void ouvrirremplir(double a[][8],int size) // Cette fonction va nous remplir un tableau statique avec des données importés d'un fihier.txt 

	{

	fstream f;

	f.open("tracks-Jorge.txt",ios::in);

	

	for(int i=0;i<size;i++)

		{

		f>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3]>>a[i][4]>>a[i][5]>>a[i][6]>>a[i][7];}

	

	f.close();

	for(int i=0;i<size;i++)

		{

		for(int j=0;j<8;j++)

			{cout<<a[i][j]<<endl;}

	}}

	

int  main (){



	int size=42548;//nombre de lignes dans notre fichier

	double a[size][8];// 8 pour le nombre de colonnes 

	ouvrirremplir(a,size);

	

TFile *f=new TFile("simulation.root","RECREATE");

 TTree *tree=new TTree("simulation","detecteur");

 

 //creation  des branches de  notre arbre

  

 double Ntracks(0),Nlayers(0),X(0),errX(0),Y(0),errY(0),Z(0),errZ(0);

 tree->Branch("Candidates of track",&Ntracks,"candidates of track");

 tree->Branch("Number of Layers",&Nlayers,"Number of Layers");

 tree->Branch("X position",&X,"X position");

 tree->Branch(" X error",&errX,"X error");

 tree->Branch(" Y position",&Y," Y position");

 tree->Branch(" Y error",&errY," Y error");

 tree->Branch(" Z position",&Z," Z position");

 tree->Branch(" Z error",&errZ," Z error");

 // Maintenant on va remplir l'arbre 

 for(int i=0;i<size;i++)

 	{

 	Ntracks=a[i][0];

 	Nlayers=a[i][1];

 	X=a[i][2];

 	errX=a[i][3];

 	Y=a[i][4];

 	errY=a[i][5];

	Z=a[i][6];

	errZ=a[i][7];

	tree->Fill();

	}

	f->Write();

	f->Close();

	

 	

 

 



	

	return 0;}

		

1 - Do not use spaces or other symbols in variables or branch names; do, e.g.

tree->Branch("Candidates_of_track",&Ntracks,"candidates_of_track");

and so on

2 - Specify the type for the branch variables (“/D” for doubles) when creating the branches. If you don’t, as you did, ROOT assumes they are floats, but you defined Ntracks, etc as double, so you should do

tree->Branch("Candidates_of_track",&Ntracks,"candidates_of_track/D");

etc.
https://root.cern/doc/master/classTTree.html#addcolumnoffundamentaltypes

1 Like

Hi @Houssem_Jeguirim ,

In addition to what @dastudillo has already said, which is correct, I will strongly suggest you use RDataFrame for your use case.

From your snippet, I assume you have the data in some kind of text format, so it should be easy for you to write it into a CSV file that could have a similar structure to the following:

ntracks,nlayers,x,errx,y,erry,z,errz
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8

Starting from there, with RDataFrame you just need

#include <ROOT/RDataFrame.hxx>
#include <ROOT/RCsvDS.hxx>

int main(){
    auto df = ROOT::RDF::FromCSV("tracks.csv");
    df.Snapshot("simulation", "simulation.root");
}

Or in Python

import ROOT

df = ROOT.RDF.FromCSV("tracks.csv")
df.Snapshot("simulation", "simulation.root")

To get your output tree:

root [0] TFile f{"simulation.root"};
root [1] std::unique_ptr<TTree> t{f.Get<TTree>("simulation")};
root [2] t->Scan("*")
************************************************************************************************************
*    Row   * ntracks.n * nlayers.n *       x.x * errx.errx *       y.y * erry.erry *       z.z * errz.errz *
************************************************************************************************************
*        0 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        1 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        2 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        3 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        4 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        5 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        6 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        7 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        8 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
*        9 *         1 *         2 *         3 *         4 *         5 *         6 *         7 *         8 *
************************************************************************************************************

Cheers,
Vincenzo

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.