TChain and TTreeReader problem

Dear Rooters, I am new in Root, especially in using TTreeReader.
I am using TChain, so I have some root files, data from which should be written to different vectors.
When I use TTreereader, data from all the files are written into one vector. How should I modify my code, that entries from file0.root will be saved in vWidth[0] vector, from file1.root in vWidth[1] ?
Thank you very much in advance !

TChain *fCh = new TChain("T");

  TTreeReader *fReader = new TTreeReader(fCh);

  TTreeReaderValue<Double_t> bin_w (*fReader, "bin_w");
  TTreeReaderValue<Double_t> content(*fReader, "content");

  TObjArray *fileElements = fCh->GetListOfFiles();
  Int_t nFiles = fileElements->GetEntries();

  vector<vector<Double_t>> vWidths;
  vWidths.resize (nFiles);
  vector<vector<Double_t>> vLenghs;
  vLenghs.resize (nFiles);
  TIter next(fileElements);
  TChainElement *chEl=0;

  Int_t k = 0;

  while (  (chEl=(TChainElement*)next()) )
    TFile f (chEl->GetTitle());
    while( fReader->Next()) {     *bin_w);*content);

(vWidths[0] include all entries from all the files :disappointed:)

Hi @ritosburritos,
sorry for the delay. You built the TTreeReader passing the whole TChain as argument, so the TTreeReader will loop over all files.

What you need is an outer loop over the files, and an inner loop that builds the TTreeReader passing a single TTree coming from a single file.

Hope this helps!

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