How to initialize TTreeReader within a function to pass to main?

Hi,

I want to initialize multiple TTreeReaders from various trees from various files which I can do within main as such:

  TTree* exampleTree_1;
  exampleTree_1= (TTree*)exampleFile_1->Get("exampleName_1");
  TTreeReader readerExample_1(exampleTree_1);

  TTree* exampleTree_2;
  exampleTree_2= (TTree*)exampleFile_2->Get("exampleName_2");
  TTreeReader readerExample_2(exampleTree_2);

etc and so on.

However I’d much prefer to have this as a function so I’m not repeating code, however when I try to do so I am not able to.

I have tried:

TTreeReader  initiateTTreeReaderFromFile(TFile* file, std::string nameOfTree){                                                                                                                                                                                                                                                                      
TTree* tree;                                                                                                                                                                                                                                                                                                                                      
tree = (TTree*)file->Get(nameOfTree.c_str());                                                                                                                                                                                                                                                                                                     
TreeReader reader(tree);                                                                                                                                                                                                                                                                                                                         
 return reader;                                                                                                                                                                                                                                                                                                                                    
}

However this fails compilation because of:
error: call to implicitly-deleted copy constructor of ‘TTreeReader’
return reader;
^~~~~~
note: copy constructor of ‘TTreeReader’ is implicitly deleted because field ‘fProxies’ has an inaccessible copy constructor
THashTable fProxies; ///< attached ROOT::TNamedBranchProxies; owned

I have also tried

void  initiateTTreeReaderFromFile(TTreeReader& reader,TFile* file, std::string nameOfTree){                                                                                                                                                                                                                                                         
   TTree* tree;                                                                                                                                                                                                                                                                                                                                      
   tree = (TTree*)file->Get(nameOfTree.c_str());                                                                                                                                                                                                                                                                                                     
   reader = TTreeReader(tree);
}

However this fails compilation because of:

error: object of type ‘TTreeReader’ cannot be assigned because its copy assignment operator is implicitly deleted
reader = TTreeReader(tree);
^
note: copy assignment operator of ‘TTreeReader’ is implicitly deleted because field ‘fProxies’ has an inaccessible copy assignment operator THashTable fProxies; ///< attached ROOT::TNamedBranchProxies; owned

I have also tried

void  initiateTTreeReaderFromFile(TTreeReader* readerPointer,TFile* file, std::string nameOfTree){      

  TTree* tree;
  tree = (TTree*)file->Get(nameOfTree.c_str());
  TTreeReader reader (tree);
 readerPointer = &reader;
}

And then dereferencing the pointer in main, this doesn’t fail at compilation but the memory that readerPointer points to is empty.

How can I manage to initialize TTreeReader within a function and then pass this to main?

Thank you,
Jack

Hi Jack,

you are almost there:

# note the &!
void  initiateTTreeReaderFromFile(TTreeReader*& readerPointer,TFile* file, const std::string& nameOfTree){      

  TTree* tree;
  tree = (TTree*)file->Get(nameOfTree.c_str());
  readerPointer = new TTreeReader(tree); // remember to delete this
}

The problem is that you have to satisfy, as you noted, some constraints:

  • The TTreeReader has no copy ctor
  • The memory attached to the pointer returned must stay valid

Some other solutions are available to you, for example if you decided to work with shared/smart pointers.

If you are open to alternative solutions, you can adopt a higher level interface to the dataset stored in the TTree, TDataFrame which would avoid to use raw pointers and just deal with file names and tree names.

Cheers,
D

Hi dipapro,

Oops, didn’t realise I’d passed a copy of the pointer not reference.

Thanks very much for your help,
Jack

P.S. this isn’t important but I’m just curious, what is the reason that TTreeReader doesn’t have a copy constructor?

Hi Jack,

the compiler is telling you that in your very message:

note: copy constructor of ‘TTreeReader’ is implicitly deleted because field ‘fProxies’ has an inaccessible copy constructor

Cheers,
D

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