# TTree::SetBranchAddress() for a TGraph array branch

**URL:** <https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [July 6, 2018, 2:18am UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674 "2018-07-06T02:18:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [July 6, 2018, 2:18am UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674/1 "2018-07-06T02:18:12Z")

</div>

I have a TTree “waveformGraphTree” with a branches “cohGraphs[2]” and “deconvGraphs[2]” made with the lines

```auto
	TGraph cohGraphs[2], deconvGraphs[2];
	waveformGraphTree.Branch("cohGraphs[2]", "TGraph", & cohGraphs);
	waveformGraphTree.Branch("deconvGraphs[2]", "TGraph", & deconvGraphs);

```

Later on, I would like to reference the TGraphs from these branches in another function with pointer to the TTree as input, and so I try

```auto
	TGraph cohGraphs[2], deconvGraphs[2];
	waveformGraphTree -> SetBranchAddress("cohGraphs[2]", & cohGraphs);
	waveformGraphTree -> SetBranchAddress("deconvGraphs[2]", & deconvGraphs);

```

However, I then receive the following output:

```auto
Error in <TTree::SetBranchAddress>: Unable to determine the type given for the address for "cohGraphs[2]". This is probably due to a missing dictionary, the original data class for this branch is TGraph.
Error in <TTree::SetBranchAddress>: Unable to determine the type given for the address for "deconvGraphs[2]". This is probably due to a missing dictionary, the original data class for this branch is TGraph.

```

TTree::SetBranchAddress() is overloaded, am I supposed to have another input variable? I have also tried `#include "TGraph.h"` beforehand.

* * *

\_ROOT Version:6.12/06  
_Platform:_ linuxx8664  
_Compiler:_ gcc

* * *

---

<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:** [July 6, 2018, 7:10am UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674/2 "2018-07-06T07:10:47Z")

</div>

What do you get from `waveformGraphTree->Print();`

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 6, 2018, 2:22pm UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674/3 "2018-07-06T14:22:15Z")

</div>

```auto
	TGraph cohGraphs[2], deconvGraphs[2];
	waveformGraphTree.Branch("cohGraphs[2]", "TGraph", & cohGraphs);

```

This will save only one of the 2 TGraph object. Use

```auto
	std::vector<TGraph> cohGraphs, deconvGraphs;
	waveformGraphTree.Branch("cohGraphs[2]", "TGraph", & cohGraphs);

```

SetBranchAddress expects the address of a pointer or the address a single object.

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [July 6, 2018, 8:25pm UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674/4 "2018-07-06T20:25:27Z")

</div>

Hi Philippe,

Thanks for the informative response. I tried taking it a step further and tried

```auto
vector<TGraph> cohGraphs(2), deconvGraphs(2);
waveformGraphsTree.Branch("cohGraphs[2]", "TGraph", & cohGraphs);
waveformGraphsTree.Branch("deconvGraphs[2]", "TGraph", & deconvGraphs);

```

When I call cohGraphs, I get reference address inside of the vector, in contrast to when I try cohGraphs[2], where I get an empty vector of size 2.

However, with the above lines I get

```auto
Error in <TTree::Branch>: The class requested (TGraph) for "cohGraphs[2]" is different from the type of the object passed (vector<TGraph>)
Warning in <TTree::Bronch>: Using split mode on a class: TGraph with a custom Streamer
Error in <TTree::Branch>: The class requested (TGraph) for "deconvGraphs[2]" is different from the type of the object passed (vector<TGraph>)
Warning in <TTree::Bronch>: Using split mode on a class: TGraph with a custom Streamer

```

Even if I know what the size of the vectors should be beforehand, should I not assign sizes beforehand and a vector’s “push\_back()” to add in the graphs later?

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [July 6, 2018, 8:31pm UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674/5 "2018-07-06T20:31:21Z")

</div>

Hi!

Thank you for the response. I tried implementing Philippes’ suggestion below, so that will likely change what `waveformGraphTree->Print();` returns. What it now returns is

```auto
******************************************************************************
*Tree :waveformGraphTree: TTree containing TGraphs of initial waveforms. *
*Entries : 189 : Total = 19026755 bytes File Size = 19019994 *
* : : Tree compression factor = 1.00 *
******************************************************************************
*Br 0 :run : run/I *
*Entries : 189 : Total Size= 1316 bytes File Size = 839 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 1 :eventNumber : eventNumber/i *
*Entries : 189 : Total Size= 1356 bytes File Size = 847 *
*Baskets : 1 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 2 :cohGraphs[2] : vector<TGraph> *
*Entries : 189 : Total Size= 9513996 bytes File Size = 9509882 *
*Baskets : 189 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*
*Br 3 :deconvGraphs[2] : vector<TGraph> *
*Entries : 189 : Total Size= 9509503 bytes File Size = 9505377 *
*Baskets : 189 : Basket Size= 32000 bytes Compression= 1.00 *
*............................................................................*

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 6, 2018, 8:36pm UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674/6 "2018-07-06T20:36:39Z")

</div>

> Even if I know what the size of the vectors should be beforehand, should I not assign sizes beforehand and a vector’s “push\_back()” to add in the graphs later?

Both will work.

> Error in TTree::Branch: The class requested (TGraph) for “cohGraphs[2]” is different from the type of the object passed (vector)

The message is correct. You can just skip specifying the type.

```auto
waveformGraphsTree.Branch("cohGraphs[2]", & cohGraphs);

```

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![jwruss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jwruss](https://root-forum.cern.ch/u/jwruss)\
**Post date:** [July 6, 2018, 9:46pm UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674/7 "2018-07-06T21:46:23Z")

</div>

Thanks for the help, Philippe.

I looked into your comment

> SetBranchAddress expects the address of a pointer or the address a single object.  
> and found a response you provided earlier  
> [Vector in Branch - #5 by pcanal](https://root-forum.cern.ch/t/vector-in-branch/12796/5)

So returning to the initial question, what I have tried now that the TGraphs are being properly stored is

```auto
	vector<TGraph> * cohGraphs, * deconvGraphs;
	waveformGraphTree -> SetBranchAddress("cohGraphs[2]", cohGraphs);
	waveformGraphTree -> SetBranchAddress("deconvGraphs[2]", deconvGraphs);

```

Using `TTree::SetBranchAddress()` as above doesn’t throw any errors at this point, but now a function which I call later which referenced `& cohGraphs[0]` when I had

```auto
vector<TGraph> cohGraphs(2);

```

Now returns

```auto
error: cannot initialize a parameter of type 'const TGraph *' with an rvalue of type
      'vector<TGraph> *'

```

Things appear to work if I do

```auto
vector<TGraph> * cohGraphs[2], * deconvGraphs[2];

```

and then provide the function with the argument `cohGraphs[0] -> data()`, but is that the correct thing to do? What if instead I did

```auto
vector<TGraph> * cohGraphs, * deconvGraphs;

```

and then passed as argument `cohGraphs[0].data()`? Both of these seem to work, but are they effectively doing the same thing?

Best,  
John

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 20, 2018, 9:46pm UTC](https://root-forum.cern.ch/t/ttree-setbranchaddress-for-a-tgraph-array-branch/29674/8 "2018-07-20T21:46:25Z")

</div>

Closed on July 20, 2018, 9:46pm UTC

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