# TTreeFormula support only 2 level of variables size collections

**URL:** <https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645>\
**Category:** Newbie\
**Tags:** rdataframe, root\
**Created:** [August 29, 2024, 9:36am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645 "2024-08-29T09:36:59Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [August 29, 2024, 9:36am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/1 "2024-08-29T09:36:59Z")

</div>

Hi all.  
This is regarding RDataFrame. However, if another way like TTreeReader is a way to solve, I will appreciate it.

I have following structure in my ROOT file.  
 ![Screenshot 2024-08-29 183313](https://root-forum.cern.ch/uploads/default/original/3X/0/b/0b7e93667b75a0917bb27077ec6dffba0de336a2.png)

I wanted to draw the ‘energy\_’ histogram just simply by double click using mouse. However, I ended up knowing that root throws a following error:

```auto
Warning in <TTreeFormula::DefinedVariable>: TTreeFormula support only 2 level of variables size collections. Assuming '@' notation for the collection singles_.

```

So, now I want to print (and eventually fill a histogram) the values stored inside ‘energy\_’.

I have seen the following topics, but haven’t actually got anything to work for me.  
[year2012](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collecti/15219) [year2020](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712)

I tried to write the following script using RDataFrame (which is not correct).  
What are the modifications that are needed?

```auto
void beta_rdataframe(){

        auto fileName = "beta_3120.root";
        auto treeName = "OutputTree";

        ROOT::RDataFrame d(treeName, fileName, {"output_vec_"});

        auto columnNames = d.GetColumnNames();

        std::cout << "Column Names are: " << std::endl;
        for (const auto &col : columnNames){
                std::cout << col << std::endl;
        }

//FOLLOWING LINES OF CODE DOESN'T WORK
        TH1F henergy("energy","energy",3000,0,3000);
        const auto fillenergy = [&henergy](auto output_vec_)
        {
                for (const auto &imp : output_vec_)
                {
                        const auto &gam_vec = imp.input_.output_vec_;
                        for (const auto &gam : gam_vec)
                        {
                                for (const auto &single : gam.singles_)
                                {
                                std::cout << "Energies are: " << std::endl;
                                std::cout << single.energy_ << std::endl;
                                henergy.Fill(single.energy_);
                                }
                        }
                }
        };

        d.Foreach(fillenergy, {"output_vec_"});
        auto c1 = new TCanvas();
        henergy.DrawClone();
}

```

The ‘partial’ output for the command ‘GetColumnNames’ is the following:

```auto
root -l beta_rdataframe.C
Processing beta_rdataframe.C...

Column Names are:
Beta
Beta.input_
Beta.output_vec_
Beta.output_vec_.input_.output_vec_
input_
output_vec_
output_vec_.input_.output_vec_
... (many more)

```

The root file is large (in tens of GB), so I cannot attach. Sorry for that.  
Any help will be appreciated.

ROOT Version: 6.26/08  
Platform: Ubuntu 22.04.2 LTS

Thanks.

* * *

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [August 29, 2024, 11:43am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/2 "2024-08-29T11:43:23Z")

</div>

Hi @Mukul,  
thanks for reaching out!  
What if you run the following?

```auto
treeName = "OutputTree"                                                                                                                                  
df = ROOT.RDataFrame(treeName, fileName)                                                                                                                                      
                                                                                                                                                                              
df.Histo1D("Beta.output_vec_.input_output_vec.singles_.energy_").Draw()
input() 

```

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [August 29, 2024, 1:13pm UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/3 "2024-08-29T13:13:27Z")

</div>

Thanks for the reply.

> [@mdessole](#):
>
> ```auto
> treeName = "OutputTree"                                                                                                                                  
> df = ROOT.RDataFrame(treeName, fileName)                                                                                                                                      
>                                                                                                                                                                               
> df.Histo1D("Beta.output_vec_.input_output_vec.singles_.energy_").Draw()
> input() 
> 
> ```

Is this a snippet for C language? I can’t run command line ROOT.RDataFrame !  
In my previous example I used ROOT::RDataFrame

Thanks.

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [August 29, 2024, 2:11pm UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/4 "2024-08-29T14:11:20Z")

</div>

Yeah, that’s python code, the C++ equivalent is the following

```auto
   auto fileName = "beta_3120.root";
   auto treeName = "OutputTree";
   ROOT::RDataFrame df(treeName, fileName);
   auto h = df.Histo1D("Beta.output_vec_.input_output_vec.singles_.energy_");
   h->DrawClone();

```

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [August 29, 2024, 2:28pm UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/5 "2024-08-29T14:28:01Z")

</div>

So, I did the following:

```auto
void beta_rdataframe(){

        auto fileName = "beta_3120.root";
        auto treeName = "OutputTree";

        ROOT::RDataFrame df(treeName, fileName);

        auto h = df.Histo1D("Beta.output_vec_.input_.output_vec_.singles_.energy_");
        h->DrawClone();
        }

```

I get the following error:

```auto
root -l beta_rdataframe.C

terminate called after throwing an instance of 'std::runtime_error'
  what(): Unknown column: Beta.output_vec_.input_.output_vec_.singles_.energy_

```

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [August 29, 2024, 3:57pm UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/6 "2024-08-29T15:57:08Z")

</div>

Maybe you could upload a snapshot of a few events, so I can reproduce your error:

```auto
 auto d_0_30 = df.Range(30);
 d_0_30.Snapshot("outputTree", "outputFile.root"); 

```

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [August 29, 2024, 4:53pm UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/7 "2024-08-29T16:53:21Z")

</div>

Thank you for the reply.  
I did the following:

```auto
void trial_beta_rdataframe(){

        auto fileName = "beta_3120.root";
        auto treeName = "OutputTree";

        ROOT::RDataFrame df(treeName, fileName);

        auto d_0_3000 = df.Range(3000);
        d_0_3000.Snapshot("OutputTree","beta_test.root");
}

```

The error in terminal:

```auto
terminate called after throwing an instance of 'std::runtime_error'
  what():
An error occurred during just-in-time compilation in RLoopManager::Run. The lines above might indicate the cause of the crash
All RDF objects that have not run their event loop yet should be considered in an invalid state.

```

Apologies for the inconvenience caused.

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [August 30, 2024, 6:07am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/8 "2024-08-30T06:07:47Z")

</div>

Just to give another try,  
I also tried following script and ended up with some error.

```auto
void beta_rdataframe(){

        auto fileName = "beta_3120.root";
        auto treeName = "OutputTree";

        ROOT::RDataFrame df(treeName, fileName);

        TH1F henergy("energy","energy",3000,0,3000);
        auto fillenergy = [&henergy](const auto output_vec_)
        {
                const auto &imp_vec = output_vec_;
                for (const auto &imp : imp_vec)
                {
                        const auto &gam_vec = imp.input_.output_vec_;
                        for (const auto &gam : &gam_vec)
                        {
                                for (const auto &single : gam.singles_)
                                {
                                        henergy.Fill(single.energy_);
                                }
                        }
                }
        return;
        };

       df.Foreach(fillenergy, {"output_vec_"});
       auto c1 = new TCanvas();
       henergy.DrawClone();

```

However, this returned error:

```auto
[-bash ~/data/output/ana]$ root -l beta_rdataframe.C
root [0]
Processing beta_rdataframe.C...
In module 'ROOTDataFrame':
/home/opt/root_v6.26.08/include/ROOT/RDF/RInterface.hxx:1390:72: error: no type named 'arg_types_nodecay' in 'ROOT::Detail::CallableTraitsImpl<(lambda at /output/ana/beta_rdataframe.C:32:20), false>'
      using arg_types = typename TTraits::CallableTraits<decltype(f)>::arg_types_nodecay;
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/output/ana/beta_rdataframe.C:72:12: note: in instantiation of function template specialization 'ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager, void>::Foreach<(lambda at /output/ana/beta_rdataframe.C:32:20)>' requested here
        df.Foreach(fillenergy, {"output_vec_"});
           ^
In module 'ROOTDataFrame':
/home/opt/root_v6.26.08/include/ROOT/RDF/RInterface.hxx:1391:71: error: no type named 'ret_type' in 'ROOT::Detail::CallableTraitsImpl<(lambda at /output/ana/beta_rdataframe.C:32:20), false>'
      using ret_type = typename TTraits::CallableTraits<decltype(f)>::ret_type;
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/home/opt/root_v6.26.08/include/ROOT/RDF/RInterface.hxx:1392:19: error: no matching function for call to 'AddSlotParameter'
      ForeachSlot(RDFInternal::AddSlotParameter<ret_type>(f, arg_types()), columns);
                  ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/opt/root_v6.26.08/include/ROOT/RDF/InterfaceUtils.hxx:628:41: note: candidate template ignored: could not match 'TypeList<type-parameter-0-2...>' against 'int'
std::function<R(unsigned int, Args...)> AddSlotParameter(F &f, TypeList<Args...>)
                                        ^

```

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [August 30, 2024, 9:11am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/9 "2024-08-30T09:11:03Z")

</div>

This looks like a nasty case to deal with, I add our IO expert @pcanal

As a general recommendation, I would suggest to use the latest version ROOT 6.32!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [August 30, 2024, 10:55am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/10 "2024-08-30T10:55:28Z")

</div>

Hi,

As @mdessole proposes, updating to the latest stable is always a good measure to take, especially because 6.26 is a rather old cycle.

In absence of the actual input file, some guesswork needs to be carried out. In your case, I would suggest to specify the type of the parameter in input to the function which is passed to the `ForEach` action:

```auto
auto fillenergy = [&henergy](const TheTypeOfOutputVec output_vec_)

```

About the `Snapshot`, I cannot say why the problem is occurring due to lack of context and ability to reproduce.

About this attempt

```auto
auto h = df.Histo1D("Beta.output_vec_.input_.output_vec_.singles_.energy_");

```

I also cannot comment given the lack of context.

Best,  
D

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [August 30, 2024, 11:16am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/11 "2024-08-30T11:16:49Z")

</div>

Thanks for the reply.

> [@Danilo](#):
>
> I would suggest to specify the type of the parameter in input to the function which is passed to the `ForEach` action:

If I do, Print(), then I have following.

```auto
root [1] OutputTree->Print()
******************************************************************************
*Tree :OutputTree: OutputTree *
*Entries : 38186 : Total = 12225561479 bytes File Size = 7853177624 *
* : : Tree compression factor = 1.56 *
******************************************************************************
*Branch :Beta *
*Entries : 38186 : BranchElement (see below) *
*............................................................................*
*Br 0 :input_ : *
*Entries : 38186 : Total Size= 117237 bytes One basket in memory *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 32 :output_vec_ : Int_t output_vec__ *
*Entries : 38186 : Total Size= 376846 bytes File Size = 135915 *
*Baskets : 161 : Basket Size= 32000 bytes Compression= 2.36 *
*............................................................................*
*Br 38 :output_vec_.input_.output_vec_ : vector<exp: *
* | :ClusterData> output_vec_[output_vec__] *
*Entries : 38186 : Total Size= 1191894790 bytes File Size = 673050551 *
*Baskets : 323 : Basket Size= 8095232 bytes Compression= 1.77 *
*............................................................................*

```

Do you mean I should mention _Int\_t_ or _exp::ClusterData_ ?  
I did use both but ended up with nothing.

Also just to mention: What I am dealing is with three levels of std::vectors i.e.,  
Beta.output\_vec\_ ,  
input.output\_vec\_ , and  
singles\_

Actually, the file isn’t created by me. So, I can’t really make a minimal reproducer.  
Apologies again!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [September 3, 2024, 7:25am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/12 "2024-09-03T07:25:34Z")

</div>

Hi,

Could you provide the requested information via these RDF methods ? [ROOT: ROOT::RDF::RInterfaceBase Class Reference](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterfaceBase.html#a623ceb5f16226c43f8bc8cf934d76fd8)

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [September 4, 2024, 7:14am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/13 "2024-09-04T07:14:01Z")

</div>

Thanks for the reply.

> [@Danilo](#):
>
> Could you provide the requested information via these RDF methods ? [ROOT: ROOT::RDF::RInterfaceBase Class Reference](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterfaceBase.html#a623ceb5f16226c43f8bc8cf934d76fd8)

So, I did this:

```auto
void beta_root_forum(){

        auto fileName = "beta_3120.root";
        auto treeName = "OutputTree";

        ROOT::RDataFrame d(treeName, fileName);

        auto columnNames = d.GetColumnNames();
        std::cout << "Column Names and their types are: " << std::endl;
        for (const auto &colName : columnNames){
                std::cout << colName << " has type " << d.GetColumnType(colName) << std::endl;
        }
}

```

and got the following:

```auto
[] root -l beta_root_forum.C
Processing beta_root_forum.C...
Column Names and their types are:
**Beta** has type OutputTreeData<OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData>,OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData> >
**Beta.input_** has type OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData>
**Beta.output_vec_** has type ROOT::VecOps::RVec<OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData>>
**Beta.output_vec_.input_.output_vec_** has type ROOT::VecOps::RVec<vector<exp::ClusterData>>
**input_** has type OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData>
**output_vec_** has type ROOT::VecOps::RVec<vector<exp::ClusterData>>
**output_vec_.input_.output_vec_** has type ROOT::VecOps::RVec<vector<exp::ClusterData>>

... (many more)

```

PS: I didn’t understand how to use: GetColumnTypeNamesList() and GetDefinedColumnNames()  
I hope you don’t require these?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [September 10, 2024, 6:46pm UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/14 "2024-09-10T18:46:32Z")

</div>

Hi,

According to the information you reported, you will need to specify the type of the branch `Beta`, which would be `OutputTreeData<OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData>,OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData> >`

Therefore,

```auto
using myType = OutputTreeData<OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData>,OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData> >;

auto fillenergy = [&henergy](const myType &output_vec_) {/*the body*/}

```

In case the type is not available to you, you can resort to [ROOT: TFile Class Reference](https://root.cern.ch/doc/master/classTFile.html#a5fdd58dba517dd7b70b43332295e529d) .

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Mukul](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/f4b2a3/32.png) [@Mukul](https://root-forum.cern.ch/u/Mukul)\
**Post date:** [September 11, 2024, 4:39am UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/15 "2024-09-11T04:39:03Z")

</div>

Thanks for the reply.  
I will summarize the solution that worked for me.

> [@Danilo](#):
>
> In case the type is not available to you, you can resort to [ROOT: TFile Class Reference](https://root.cern.ch/doc/master/classTFile.html#a5fdd58dba517dd7b70b43332295e529d) .

I did the following:

```auto
void beta_root_forum(){

        TFile *file = TFile::Open("beta_3120.root");
        TTree *tree = (TTree*)file->Get("OutputTree");
        file->MakeProject("MyProject","*","new++");
        file->Close();
}

```

and it generated a directory with many files in it.  
I then created a ‘rootlogon.C’ file in which I write a following line to load those generated files:

```auto
{
gSystem->Load("MyProject/MyProject");
}

```

Now, I proceed to write a script to plot the said branch by using RDataFrame method and your suggested line as follows:

> [@Danilo](#):
>
> `using myType = OutputTreeData<OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData>,OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData> >;`

```auto
void beta_rdataframe(){

        auto fileName = "beta_3120.root";
        auto treeName = "OutputTree";

        ROOT::RDataFrame d(treeName, fileName);

        TH1F henergy("energy","energy",3000,0,3000);
        using myType = OutputTreeData<OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData>,OutputTreeData<OutputTreeData<exp::WData,exp::ClusterData>,exp::BData> >;
        const auto fillenergy = [&henergy](const myType &start)
        {
                const auto &imp_vec = start.output_vec_;
                for (const auto &imp : imp_vec)
                {
                        const auto &gam_vec = imp.input_.output_vec_;
                        for (const auto &gam : gam_vec)
                        {
                                for (const auto &single : gam.singles_)
                                {
                                //std::cout << single.energy_ << std::endl;
                                henergy.Fill(single.energy_);
                                }
                        }
                }
                return;
        };

        d.Foreach(fillenergy, {"Beta"});
        auto c1 = new TCanvas();
        henergy.DrawClone();
}

```

This indeed returns me with the histogram for ‘energy\_’ branch as follows:  
 ![Beta.output_vec_.input_.output_vec_.singles_.energy_](https://root-forum.cern.ch/uploads/default/original/3X/f/7/f7fc867de622dbc3fac2eef67c1d87024edd747f.png)  
.

Thank you so much for your assistance.  
I’ve been struggling for days and you have been a savior!

Newbiee!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [September 11, 2024, 12:28pm UTC](https://root-forum.cern.ch/t/ttreeformula-support-only-2-level-of-variables-size-collections/60645/16 "2024-09-11T12:28:37Z")

</div>

Thanks for your perseverance! I am sure this thread will be useful to others.

Cheers,  
Danilo
