# TTree with class and array

**URL:** <https://root-forum.cern.ch/t/ttree-with-class-and-array/53790>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [February 26, 2023, 6:59pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790 "2023-02-26T18:59:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [February 26, 2023, 6:59pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/1 "2023-02-26T18:59:46Z")

</div>

Dear all,

I’ve crossed this problem several times. And I am not sure how to deal with it.  
Please, find attached an example script to reproduce the problem (execute `example_tree_write.C` and then `example_tree_read.C`.

To better describe the problem:  
When I create an array of length 100 but save only the first 10 points, it will save the array properly, but anything that comes after it will not be saved (in the example, `selection`)

I would like to keep the class structure as all my analysis is based on this (in my complete scripts I have several channels and need to work with something very similar to the example).

I cannot invert “selection” with “wvf” as they have different type and the data will not be saved properly.

What am I doing wrong?

Best regards,  
Henrique

* * *

_ROOT Version:_ 6.26/06  
_Platform:_ Ubuntu 24.04  
_Compiler:_ g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

* * *

[example\_tree\_read.C](https://root-forum.cern.ch/uploads/short-url/o13l7bULZw8VAbjF6Fz1FeMmdMo.C) (855 Bytes)  
[example\_tree\_write.C](https://root-forum.cern.ch/uploads/short-url/442uVbaZL5jJORvUuROSjfu8gTR.C) (985 Bytes)

---

<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:** [February 27, 2023, 9:49am UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/2 "2023-02-27T09:49:40Z")

</div>

[example\_tree\_write.cxx](https://root-forum.cern.ch/uploads/short-url/x4UA4lm2L4JrD3Xf6AZ9Sv1AS92.cxx) (1.0 KB)  
[example\_tree\_read.cxx](https://root-forum.cern.ch/uploads/short-url/ysTf2t8QrBUiGyWvRV6ryVjIJDN.cxx) (1.3 KB)

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [February 27, 2023, 2:35pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/3 "2023-02-27T14:35:44Z")

</div>

Dear Wile,

This solution is (almost) perfect! Thanks.  
The only problem is that I still need to define the size of the array during compilation. The best solution I was searching was to define the length inside some function, but I believe that is not possible at all (without using vectors). However, declaring the class with the template is of great help.

Thanks again!

---

<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:** [February 27, 2023, 5:31pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/5 "2023-02-27T17:31:08Z")

</div>

```auto
template <Int_t npts = 100>
class MY_DATA {
public:
  // note: tree branch variables must be sorted in the decreasing order of size
  Int_t actualSize = npts;
  Int_t selection;
  Double_t peak;
  Double_t wvf[npts];
  
  std::string tobranch = "actualSize/I:selection/I:peak/D:wvf[actualSize]/D";
  
  MY_DATA();
};

```

---

<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:** [February 27, 2023, 6:15pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/6 "2023-02-27T18:15:31Z")

</div>

[MY\_DATA.hxx](https://root-forum.cern.ch/uploads/short-url/gunFlhLogWJuDNpCfbcZZ0gXd45.hxx) (1.3 KB)  
[example\_tree\_write.cxx](https://root-forum.cern.ch/uploads/short-url/bmpimGbHgoBsQxvkc59H11FPwDV.cxx) (898 Bytes)  
[example\_tree\_read.cxx](https://root-forum.cern.ch/uploads/short-url/3z2PmZ0Eotsvp5UjHYicgRsigV7.cxx) (941 Bytes)

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [February 27, 2023, 6:46pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/7 "2023-02-27T18:46:28Z")

</div>

Thanks!  
Although this works to have different sizes of vector, I still have to declare the size in the time of compilation.

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [February 27, 2023, 6:50pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/8 "2023-02-27T18:50:07Z")

</div>

Thank you again Wile.

This is a great solution, however I have several channels and I keep the variables declared in the class holding the info for each channel.  
So, I prefer to use each class as a branch.  
For instance:  
The class MY\_DATA as ch0 and ch1.  
For each waveform I have saved, I have informations like charge, peak, selection, etc.  
I hope this makes sense 🙂

---

<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:** [February 27, 2023, 7:00pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/9 "2023-02-27T19:00:03Z")

</div>

So, with [my last “example”](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/6), you just need to change the `"prefix"`, e.g.:  
`MY_DATA ch0, ch1; ch0.Branch(tree, "ch0_"); ch1.Branch(tree, "ch1_");`

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [February 27, 2023, 7:04pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/10 "2023-02-27T19:04:39Z")

</div>

Alright! Sorry my bad.  
Thank very much

---

<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:** [February 28, 2023, 11:40am UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/11 "2023-02-28T11:40:37Z")

</div>

A more canonical (better?) approach …

[MY\_DATA.hxx](https://root-forum.cern.ch/uploads/short-url/pF01CPbf51A5QKsyOiK8AvOJEuL.hxx) (648 Bytes)  
[example\_tree\_write.cxx](https://root-forum.cern.ch/uploads/short-url/1by1aspApvlUuqCGFy3r3RgHzgt.cxx) (1.1 KB)  
[example\_tree\_read.cxx](https://root-forum.cern.ch/uploads/short-url/oqBPvg6om92C440MBCZaYPPNF8d.cxx) (1.4 KB)

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [March 1, 2023, 12:14pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/12 "2023-03-01T12:14:49Z")

</div>

Dear Wile,

Yes! This is even better.  
I had no idea how to work with class + pointer. This is the solution I wanted from a long time, but I could not figure out in the documentation by lack of knowledge in C++.

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [March 9, 2023, 7:01pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/13 "2023-03-09T19:01:22Z")

</div>

Dear @Wile_E_Coyote

I noticed now, in order to work, MY\_DATA.hxx needs the comment at line 11:

```auto
  Double_t *wvf; //[npts]

```

(For both, reading or writting)  
I never saw this before, do you have any explanation for that?

Sorry to keep on the topic.

Cheers,  
Henrique

---

<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:** [March 9, 2023, 7:24pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/14 "2023-03-09T19:24:13Z")

</div>

[ROOT Manual → Functional parts → I/O concepts → ROOT’s C++ object serialization: from memory to disk and back → Array data members of fixed and variable size](https://root.cern/manual/io/#array-data-members-of-fixed-and-variable-size)

---

<div class="post-metadata">

**Author:** ![hvsouza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hvsouza/32/23711_2.png) [@hvsouza](https://root-forum.cern.ch/u/hvsouza)\
**Post date:** [March 9, 2023, 8:02pm UTC](https://root-forum.cern.ch/t/ttree-with-class-and-array/53790/15 "2023-03-09T20:02:01Z")

</div>

Thank you and sorry for the silly question 🙂
