# How to read array with fixed size stored in branch from TTree in JSROOT?

**URL:** <https://root-forum.cern.ch/t/how-to-read-array-with-fixed-size-stored-in-branch-from-ttree-in-jsroot/57086>\
**Category:** Newbie\
**Tags:** tree, jsroot\
**Created:** [November 10, 2023, 3:23pm UTC](https://root-forum.cern.ch/t/how-to-read-array-with-fixed-size-stored-in-branch-from-ttree-in-jsroot/57086 "2023-11-10T15:23:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinstaky](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@kinstaky](https://root-forum.cern.ch/u/kinstaky)\
**Post date:** [November 10, 2023, 3:23pm UTC](https://root-forum.cern.ch/t/how-to-read-array-with-fixed-size-stored-in-branch-from-ttree-in-jsroot/57086/1 "2023-11-10T15:23:48Z")

</div>

I am trying to read fiexd size array from branches in TTree in jsroot. At first, I built a tree with C++ and coded like

```cpp
// in cpp
tree.Branch("px", &px, "px[3]/D");

```

So this branch is a fixed size array branch. And I am familiar to read this branch with C++, but I failed to read it with jsroot. I followed the demo in [Reading object from the ROOT file](https://github.com/root-project/jsroot/blob/master/demo/read_tree.htm), and coded like

```javascript
// in main.js script
class MySelector extends TSelector {
	constructor() {
		super();
		this.addBranch('px');
	}
	...
}

```

This works well with bracnhes that store single number. But it failed with array. To run the above js code, I build a simple local server on win10 with command `python -m http.server`, serving a webpage writring with html. In the webpage, I use

```html
# in html
<script type="module" src="main.js"></script>

```

to run the code I memtioned above. I check the webpage with firefox browser. I tried to get more information about the variable `px` using codes

```javascript
console.log(this.tgtobj.px, typeof(this.tgtobj.px));

```

The log shows that `px` is of `number` type, and I can only get `px[0]`. However, I hope to read it as `Array` type in js to get the information of `px[1]` and `px[2]`.

_ROOT Version:_ jsroot 7.5.2  
_Platform:_ win10  
_Compiler:_ firefox?

------------ update 1 -------------------  
I upload the root file (example.root), html (index.cpp) and javascript code (main.cpp). Since the limited of extensions, please change the name of the files by yorurself (index.cpp → index. html, main.cpp → main.js).

* * *

[example.root](https://root-forum.cern.ch/uploads/short-url/xQOQjg3bG8p7WHf5UCeTZ9gN9Yr.root) (5.6 KB)  
[index.cpp](https://root-forum.cern.ch/uploads/short-url/8iVJUXkZU5djGAcwebqkstqncql.cpp) (248 Bytes)  
[main.cpp](https://root-forum.cern.ch/uploads/short-url/mlyoelXM1QnSKLlWSwDmfrc8HOQ.cpp) (698 Bytes)

```cpp
#include <TFile.h>
#include <TTree.h>

int main() {
  TFile opf("example.root", "recreate");
  TTree opt("tree", "example tree");
  // output data
  double px[3];
  double p;
  // setup output branches
  opt.Branch("px", px, "px[3]/D");
  opt.Branch("p", &p, "p/D");
    
  // fill data
  for (int i = 0; i < 10; ++i) {
    px[0] = i;
    px[1] = i+1;
    px[1] = i+2;
    p = px[0] + px[1] + px[2];
    opt.Fill();
  }
  // save tree
  opt.Write();
  // close files
  opf.Close();
  return 0;
}

```

To be clear, I paste the C++ code that building this example root file.

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [November 10, 2023, 4:34pm UTC](https://root-forum.cern.ch/t/how-to-read-array-with-fixed-size-stored-in-branch-from-ttree-in-jsroot/57086/2 "2023-11-10T16:34:30Z")

</div>

Hi and welcome one the forum,

Can you upload here ROOT file with such TTree object?

---

<div class="post-metadata">

**Author:** ![kinstaky](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@kinstaky](https://root-forum.cern.ch/u/kinstaky)\
**Post date:** [November 11, 2023, 2:02am UTC](https://root-forum.cern.ch/t/how-to-read-array-with-fixed-size-stored-in-branch-from-ttree-in-jsroot/57086/3 "2023-11-11T02:02:39Z")

</div>

I upload the example root file and some code to read it.

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [November 13, 2023, 8:09am UTC](https://root-forum.cern.ch/t/how-to-read-array-with-fixed-size-stored-in-branch-from-ttree-in-jsroot/57086/4 "2023-11-13T08:09:47Z")

</div>

Thank you for providing reproducer!

JSROOT was never tested with such data and therefore was not working ☹

I apply the fix to all active branches of JSROOT. Now it should work - [here is link](https://jsroot.gsi.de/dev/?file=../files/tmp/example.root&item=tree/px&opt=dump) with dump of that branch.

Your example has small typo - last element of the array is not initialized and therefore has strange values.

You can use now `master` or `7.5` branch.  
I will provide bug-fix release later this week.

---

<div class="post-metadata">

**Author:** ![kinstaky](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@kinstaky](https://root-forum.cern.ch/u/kinstaky)\
**Post date:** [November 13, 2023, 8:44am UTC](https://root-forum.cern.ch/t/how-to-read-array-with-fixed-size-stored-in-branch-from-ttree-in-jsroot/57086/5 "2023-11-13T08:44:09Z")

</div>

Thank you. Now my program works well with `master`.
