Storing TTreeReaderValue<Float_t> into a tree branch

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!

Hi,

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

Cheers, Axel.

Thank you! I resolved the issue.

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?

FYI on posting Code snippets:

Here are some comments:

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);
}

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

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