Filling a TTree from an ascii file

Hello rooters!

I’m trying to read an ascii file and create a TTree with its contents.
The ascii file looks like that:

.0969525980-65.6180501 .545141802                       0 0 0     5
.8685306199 60.8000000-.773005676                       0 0 0     5
3.399685734 65.1152300 1.23008566                       0 0 0     1
4.946205044 64.0000000 1.62558672                       0 0 0     5
6.437667164 66.0000000 .838683576                       0 0 0     5
8.047000000 66.0000000 135.000000                       0 0 0     7
8.912359432 46.4416925 1.37294993                       0 0 0     4
8.941787935 83.2448512 7.22026167                       0 0 0     5
9.193489012 69.8768942 .216064140                       0 0 0     4
12.01070514 73.9312235 1.74422309                       0 0 0     4
14.87115023 53.0793217 1.41515434                       0 0 0     4
14.91620808 74.6380281 3.92427916                       0 0 0     5
15.87138830 84.8924568 .354895013                       0 0 0     4
17.16428549 72.2366324 1.87240721                       0 0 0     5
18.35811313 63.8673117 71.6084557                       0 0 0     2
20.65000000 60.2000000 47.9000000                       0 0 0     6
23.25703865 110.743781 .928736869                       0 0 0     5
24.73100974 116.959752 .374751741                       0 0 0     5
25.28226813 99.4056610 12.4466081                       0 0 0     4
26.12186995 68.3028056 2.91040375                       0 0 0     5
27.19267323 79.0389100 6.64001041                       0 0 0     1
28.02083489 114.434666 .623057828                       0 0 0     4
29.77620315 73.0515800 13.5715532                       0 0 0     1
29.94450750-98.4298215-2.88134214                       0 0 0     4

My code is the following

#include "TH1D.h"
#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"

#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

void create_tree_from_SAMMY(TString SAMMY_par_filename_in, TString filename_out){

// (0) Define user parameters
	double target_spin   = 7/2.; // For 147Sm
	int    resonance_spins[2] = {3, 4};


//(1) Read the ascii file

	std::ifstream SAMMY_par_file_in(SAMMY_par_filename_in);
	//file_in::Open(SAMMY_par_filename_in);
	
	double Er, Gg, Gn, Gn0, TwoG, TwoGGn;
	int    Er_fit, Gg_fit, Gn_fit, spin_group, J;
	
	TTree *T_resonances = new TTree("resonances","Resonance parameters from SAMMY fits");
	
	T_resonances->Branch("Er"        , &Er        , "Resonance energy (eV)/D");
	T_resonances->Branch("Gg"        , &Gg        , "Capture width (meV)/D");
	T_resonances->Branch("Gn"        , &Gn        , "Neutron width (meV)/D");
	T_resonances->Branch("Gn0"       , &Gn0       , "Reduced Neutron width/D");
	T_resonances->Branch("TwoG"      , &TwoG      , "2g/D");
	T_resonances->Branch("TwoGGn"    , &TwoGGn    , "2gGn/D");
	T_resonances->Branch("spin_group", &spin_group, "Spin group in the SAMMY par file/I");
	T_resonances->Branch("J"         , &J         , "Resonance spin J/I");
	
	
	while(true){
		SAMMY_par_file_in >> Er >> Gg >> Gn >> Er_fit >> Gg_fit >> Gn_fit >> spin_group;
		if( SAMMY_par_file_in.eof() ) break;
    	
    	if (spin_group == 1){
    		J = 3;
    		Gn0    = abs(Gn)/sqrt(Er);
    		TwoG   = (2*J+1)/(2*target_spin+1);
    		TwoGGn = TwoG*Gn;
    		T_resonances->Fill();
    	}
    	else if (spin_group == 2){
    		J = 4;
    		Gn0    = abs(Gn)/sqrt(Er);
    		TwoG   = (2*J+1)/(2*target_spin+1);
    		TwoGGn = TwoG*abs(Gn);
    		T_resonances->Fill();
    	}
    	
    }
    
    TFile *fout = new TFile(filename_out, "RECREATE");
    	T_resonances->Write();
    fout->Close();    
    
}

I’m compiling this code using .L name.C++.

what is strange is that I can compile and execute the code without any issues. Printing the tree seems also normal


root [5] resonances->Print()
******************************************************************************
*Tree    :resonances: Resonance parameters from SAMMY fits                   *
*Entries :      163 : Total =           14421 bytes  File  Size =      10760 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Br    0 :Er        : Resonance energy (eV)/D                                *
*Entries :      163 : Total  Size=       1921 bytes  File Size  =       1379 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :Gg        : Capture width (meV)/D                                  *
*Entries :      163 : Total  Size=       1911 bytes  File Size  =       1379 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    2 :Gn        : Neutron width (meV)/D                                  *
*Entries :      163 : Total  Size=       1911 bytes  File Size  =       1379 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    3 :Gn0       : Reduced Neutron width/D                                *
*Entries :      163 : Total  Size=       1923 bytes  File Size  =       1380 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    4 :TwoG      : 2g/D                                                   *
*Entries :      163 : Total  Size=       1868 bytes  File Size  =       1381 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    5 :TwoGGn    : 2gGn/D                                                 *
*Entries :      163 : Total  Size=       1878 bytes  File Size  =       1383 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    6 :spin_group : Spin group in the SAMMY par file/I                    *
*Entries :      163 : Total  Size=       1310 bytes  File Size  =        735 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    7 :J         : Resonance spin J/I                                     *
*Entries :      163 : Total  Size=       1244 bytes  File Size  =        726 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*

The trouble is that if I scan the tree the first 3 branches (i.e. Er, Gg, Gn) are empty and I get a compile error.


root [4] resonances->Scan("*", "Er<200")
Error in <TTreeFormula::Compile>:  Bad numerical expression : "Er.Er.Resonanceenergy(eV)"
Error in <TTreeFormula::Compile>:  Bad numerical expression : "Gg.Gg.Capturewidth(meV)"
Error in <TTreeFormula::Compile>:  Bad numerical expression : "Gn.Gn.Neutronwidth(meV)"
************************************************************************************************************
*    Row   * Er.Er.Res * Gg.Gg.Cap * Gn.Gn.Neu * Gn0.Gn0.R * TwoG.TwoG * TwoGGn.Tw * spin_grou * J.J.Reson *
************************************************************************************************************
*        0 *           *           *           * 0.6671384 *     0.875 * 1.0763249 *         1 *         3 *
*        1 *           *           *           * 16.712841 *     1.125 * 80.559512 *         2 *         4 *
*        2 *           *           *           * 1.2733353 *     0.875 * 5.8100091 *         1 *         3 *
*        3 *           *           *           * 2.4871094 *     0.875 * 11.875109 *         1 *         3 *
*        4 *           *           *           * 6.7104623 *     1.125 * 42.809796 *         2 *         4 *
*        5 *           *           *           * 12.592435 *     1.125 * 89.211424 *         2 *         4 *
*        6 *           *           *           * 0.7758914 *     0.875 * 4.3321662 *         1 *         3 *
*        7 *           *           *           * 1.7429721 *     1.125 * 13.773001 *         2 *         4 *
*        8 *           *           *           * 6.9120408 *     0.875 * 46.082053 *         1 *         3 *
*        9 *           *           *           * 0.6338801 *     0.875 * 4.4682693 *         1 *         3 *
*       10 *           *           *           * 0.5489610 *     1.125 * 4.9868054 *         2 *         4 *
*       11 *           *           *           * 1.6730839 *     1.125 * 16.422273 *         2 *         4 *

The most weird thing is that if I try to draw any of those 3 branches I do get a plot without any complaints!
Any idea on what might be wrong and how to fix it?

Thanks in advance!

Looks like if I remove the parentheses from

T_resonances->Branch("Er"        , &Er        , "Resonance energy in eV/D");
T_resonances->Branch("Gg"        , &Gg        , "Capture width in meV/D");
T_resonances->Branch("Gn"        , &Gn        , "Neutron width in meV/D");

it works!

1 Like

FYI, @pcanal.

T_resonances->Branch("Er" , &Er , "Resonance energy in eV/D");

TTree itself does not impose much constraint in the name of the branches (1st string) or the leaves (2nd string). However the automatic tools (TTree::Scan, TTree::Draw, TTree::MakeSelector, RDataFrame, etc.) are much more stringent and the easiest way to provide a compliant name is to use a name that could be used as a C++ identifier; in particular do not include any spaces, dot or semi-colons.

Reminder: the 2nd string (i.e. 3rd argument) in the branch call is NOT a documentation string but is the name of the leaves (In your case you are using the “leaflist” way of creating branches).

This simplest solution is to use:

T_resonances->Branch("Er" , &Er);

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