# Storing TTreeReaderValue\<Float\_t\> into a tree branch

**URL:** <https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561>\
**Category:** ROOT\
**Created:** [October 12, 2017, 2:57pm UTC](https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561 "2017-10-12T14:57:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hesham\_El\_Faham](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hesham_el_faham/32/19111_2.png) [@Hesham\_El\_Faham](https://root-forum.cern.ch/u/Hesham_El_Faham)\
**Post date:** [October 12, 2017, 2:57pm UTC](https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561/1 "2017-10-12T14:57:26Z")

</div>

Hi all,

I am an absolute beginner in ROOT with fair C++ experience; please accept my naive question, and kindly try to make your answer as simple as possible.

I have a TChain from which I copy trees from its files. I then declare a new tree which is the old tree copied. I also added branches to that new tree. Now, I want to fill those branches with variables.

**Variables are declared in my script as:**  
TTreeReaderValue\<Float\_t\> tau\_0\_pt(myReader, “tau\_0\_pt”);

**The script is complaining about the process of filling the branches as follows:**  
error: member reference base type ‘float’ is not a structure or union  
tau\_0\_pt-\>Fill();

Could you please help with that?  
Thank you!

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [October 12, 2017, 3:41pm UTC](https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561/2 "2017-10-12T15:41:05Z")

</div>

Hi,

The TTreeReader is not a TTreeWriter 🙂 You will have to look at the `tutorials/tree/copytree*.C` examples which show how to do what you’re looking for.

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![Hesham\_El\_Faham](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hesham_el_faham/32/19111_2.png) [@Hesham\_El\_Faham](https://root-forum.cern.ch/u/Hesham_El_Faham)\
**Post date:** [October 12, 2017, 5:00pm UTC](https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561/3 "2017-10-12T17:00:32Z")

</div>

Thank you! I resolved the issue.

---

<div class="post-metadata">

**Author:** ![Hesham\_El\_Faham](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hesham_el_faham/32/19111_2.png) [@Hesham\_El\_Faham](https://root-forum.cern.ch/u/Hesham_El_Faham)\
**Post date:** [October 17, 2017, 4:29pm UTC](https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561/4 "2017-10-17T16:29:12Z")

</div>

Hi again,

I am actually trying to loop over files in a directory and open them one by one, I wrote the below script:

TString fname;  
TFile \*input;  
for (Int\_t i=0; i\<n; i++){  
fname = “.filename[i]”;  
input = TFile::Open(“fname”);  
}

I got the following error “failed to read type data 287 out of 300” etc.

Could you please help with that?

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [October 17, 2017, 6:42pm UTC](https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561/5 "2017-10-17T18:42:40Z")

</div>

FYI on posting Code snippets:

> [@Getting the same histograms after cuts](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/2):
>
> Remark on the way to post code snippets: When you post pieces of code on the forum enclose it with ``` . That makes your post much more readable (I systematically edit your posts that way) . So do: ``` // this is a C++ piece of code. ``` And it will display as: // this is a C++ piece of code.

Here are some comments:

```auto
TString fname;
TFile *input[n];
for (Int_t i=0; i<n; i++){
   //The following block is old code that should be removed, 
   // left to indicate what the issues were.

   //This line will not incorporate the index i
   //fname = “.filename[i]”; 
   //This line will try to open a file called "fname".
   // The pointer input will be overwritten on every loop and input will only
   // point to the last file open, while all the others will still be open.
   //input = TFile::Open(“fname”);

   //This will create a filename that is appended with index i.
   fname = Form("filename_%d", i);
   std::cout << "Opening filename: '" << fname << "'\n";
   //This will open a file with the name stored in the variable fname.
   //This will store each pointer to the files separately.
   input[n] = TFile.Open(fname);
}

```

---

<div class="post-metadata">

**Author:** ![Hesham\_El\_Faham](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hesham_el_faham/32/19111_2.png) [@Hesham\_El\_Faham](https://root-forum.cern.ch/u/Hesham_El_Faham)\
**Post date:** [October 18, 2017, 10:46am UTC](https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561/6 "2017-10-18T10:46:19Z")

</div>

Thank you! I think I managed to resolve the issue.

---

<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:** [November 1, 2017, 10:46am UTC](https://root-forum.cern.ch/t/storing-ttreereadervalue-float-t-into-a-tree-branch/26561/7 "2017-11-01T10:46:28Z")

</div>

Closed on November 1, 2017, 10:46am UTC

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