# Printing array elements contained in branch

**URL:** <https://root-forum.cern.ch/t/printing-array-elements-contained-in-branch/55209>\
**Category:** Newbie\
**Created:** [June 6, 2023, 5:03pm UTC](https://root-forum.cern.ch/t/printing-array-elements-contained-in-branch/55209 "2023-06-06T17:03:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marse](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@marse](https://root-forum.cern.ch/u/marse)\
**Post date:** [June 6, 2023, 5:03pm UTC](https://root-forum.cern.ch/t/printing-array-elements-contained-in-branch/55209/1 "2023-06-06T17:03:14Z")

</div>

Hi,

I’ve been trying to print on .txt file the elements of an array rich\_bdt\_var contained in the homonym branch (one element in each column of the output file), but what I get printed seems random values that are not correct (I checked which are the correct one using scan() method).

This is the TTree:

```auto
******************************************************************************
*Tree :agl_sig_bdt_tree_even: Tree for training the BDT (signal AGL,even events) *
*Entries : 8887 : Total = 4638982 bytes File Size = 2102351 *
* : : Tree compression factor = 2.21 *
******************************************************************************
*Br 0 :rich_bdt_var : rich_bdt_var[130]/F *
*Entries : 8887 : Total Size= 4638519 bytes File Size = 2100532 *
*Baskets : 146 : Basket Size= 32000 bytes Compression= 2.21 *
*............................................................................*

```

and the code that I am using:

```auto
#include <fstream>
#include <iomanip>
#include <iostream>

void feat_from_bdtTree()
{
    // define features array
    double rich_bdt_var[130] = {};

    // Open the output file
    std::ofstream outputFile("features_bdtTree.txt");

    // Set the column width for each element
    int columnWidth = 40;

    //Print first row
    outputFile << std::setw(columnWidth) << "entry number" << "\t";
    outputFile << std::setw(columnWidth) << "parity" << "\t";
    outputFile << std::setw(columnWidth) << "sample" << "\t";
    for (int i = 0; i < 130; ++i) {
        outputFile << std::setw(columnWidth) << "Element_" << i+1 << "\t";
    }
    outputFile << "\n";

    //------------------------------- Even signal events
    TChain *Agl_Sig_even = new TChain("agl_sig_bdt_tree_even");
    Agl_Sig_even->Add("BDT_TTrees/RICH_TREE_BDT_20160509.root");
   Agl_Sig_even->SetBranchAddress("rich_bdt_var", &rich_bdt_var);

    // Loop over the entries and access each element of the array
    for (Long64_t entry = 0; entry < Agl_Sig_even->GetEntries(); ++entry) {
        Agl_Sig_even->GetEntry(entry);
        
        outputFile << std::setw(columnWidth) << entry << "\t";
        outputFile << std::setw(columnWidth) << "even" << "\t";
        outputFile << std::setw(columnWidth) << "signal" << "\t";

        for (int i = 0; i < 130; ++i) {
            double element = rich_bdt_var[i];
            outputFile << std::setw(columnWidth) << element << "\t";
        }

        outputFile << "\n";
    }

```

Can someone help me understand where is the mistake?  
Thank you.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 6, 2023, 5:07pm UTC](https://root-forum.cern.ch/t/printing-array-elements-contained-in-branch/55209/2 "2023-06-06T17:07:21Z")

</div>

`float rich_bdt_var[130];`

---

<div class="post-metadata">

**Author:** ![marse](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/94ad74/32.png) [@marse](https://root-forum.cern.ch/u/marse)\
**Post date:** [June 6, 2023, 5:28pm UTC](https://root-forum.cern.ch/t/printing-array-elements-contained-in-branch/55209/3 "2023-06-06T17:28:33Z")

</div>

thank you!
