# TChain losing values from data file

**URL:** <https://root-forum.cern.ch/t/tchain-losing-values-from-data-file/52456>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [November 21, 2022, 6:36pm UTC](https://root-forum.cern.ch/t/tchain-losing-values-from-data-file/52456 "2022-11-21T18:36:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cowagner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@cowagner](https://root-forum.cern.ch/u/cowagner)\
**Post date:** [November 21, 2022, 6:36pm UTC](https://root-forum.cern.ch/t/tchain-losing-values-from-data-file/52456/1 "2022-11-21T18:36:12Z")

</div>

_ROOT Version:_ 6.20/06  
_Platform:_ Linux  
_Compiler:_ ROOT macro

* * *

I’ve been having an issue with reading a .root ntuple into a TChain. I had been using a TFile to get a TTree, like this:

```auto
TFile* file = new TFile();
file = TFile::Open(filename);
TTree* originalTree = (TTree*)file->Get(TString::Format("%s", tree.c_str()));

```

where “tree” is a variable passed into the code. This worked fine, but I wanted to change this to using a TChain to be able to handle adding multiple files. However, using the new line:

```auto
TChain* originalTree = new TChain(tree.c_str());
originalTree->Add("filename");

```

I add the branch with:

```auto
originalTree->SetBranchAddress("failJvt_jet_isTrueHS",&failJvt_jet_isTrueHS);

```

and I build an index to match events with truth; this eventNumber is how I know I’m looking at the same events for me to see the error. I now find that the length of one of my variables, a vector, is now 0 for a specific event where I can see there is an instance (ie, size() = 1).

```auto
* Row * Instance * eventNumb * jet_isTru * failJvt_j * failJvt_j *
***********************************************************************
* 111 * 0 * 124382 * * 0 * 23580.431 *
***********************************************************************

```

There should be the ‘0’ returned when I call (int) failJvt\_jet\_isTrueHS-\>at(i) for i=0, but instead I get a range error:

```auto
terminate called after throwing an instance of 'std::out_of_range'
  what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)

```

Is there a reason TChain would be dropping this event? Am I adding the files incorrectly? I’ve used TChain before with this method and never had an issue, so I’m somewhat confused. Any advice would be greatly appreciated.

---

<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:** [November 21, 2022, 8:49pm UTC](https://root-forum.cern.ch/t/tchain-losing-values-from-data-file/52456/2 "2022-11-21T20:49:02Z")

</div>

What does

```auto
originalTree->GetBranch("failJvt_jet_isTrueHS")->Print();

```

show?

---

<div class="post-metadata">

**Author:** ![cowagner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@cowagner](https://root-forum.cern.ch/u/cowagner)\
**Post date:** [November 21, 2022, 10:12pm UTC](https://root-forum.cern.ch/t/tchain-losing-values-from-data-file/52456/3 "2022-11-21T22:12:18Z")

</div>

Adding that line adds:

```auto
*Br 0 :failJvt_jet_isTrueHS : vector<char> *
*Entries : 308302 : Total Size= 4381401 bytes File Size = 751029 *
*Baskets : 339 : Basket Size= 25600 bytes Compression= 5.82 *
*............................................................................*

```

to the output

---

<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:** [November 21, 2022, 10:31pm UTC](https://root-forum.cern.ch/t/tchain-losing-values-from-data-file/52456/4 "2022-11-21T22:31:53Z")

</div>

and what is the type of the variable `failJvt_jet_isTrueHS` ?

---

<div class="post-metadata">

**Author:** ![cowagner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@cowagner](https://root-forum.cern.ch/u/cowagner)\
**Post date:** [November 21, 2022, 11:17pm UTC](https://root-forum.cern.ch/t/tchain-losing-values-from-data-file/52456/5 "2022-11-21T23:17:21Z")

</div>

it’s supposed to be a vector of char, defined in /athena/PhysicsAnalysis/TopPhys/xAOD/TopAnalysis/TopAnalysis/EventSaverFlatNtuple.h

```auto
0759 std::vector<char> m_failJvt_jet_isTrueHS;

```

---

<div class="post-metadata">

**Author:** ![cowagner](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/977dab/32.png) [@cowagner](https://root-forum.cern.ch/u/cowagner)\
**Post date:** [November 22, 2022, 11:32am UTC](https://root-forum.cern.ch/t/tchain-losing-values-from-data-file/52456/6 "2022-11-22T11:32:54Z")

</div>

Ok, I see what you were saying, and I found the error. In my macro there was an initialization of `failJvt_jet_isTrueHS` as a `std::vector<float>`, that hadn’t caused an issue when I was reading with the `TFile` and `TTree` but wasn’t read into the `TChain` properly. This seems to have fixed the issue. Thanks!
