No matching member function for call to 'Get' (TTree)

ROOT Version: 6.20/00
Platform: ubuntu 20.04
Compiler: g++ 9.3.0


In my macro, I am writing a function that should copy the content of one branch to the histogram.
Part of my code that involved in this:

#include <TFile.h>
#include <TRandom3.h>
#include <TTree.h>
#include <TStopwatch.h>
#include <TH1.h>
#include <TF1.h>
#include <TCanvas.h>
#include <TCut.h>
#include <TStyle.h>
#include <TPaveStats.h>
#include <TMath.h>

#include <string.h>

using namespace std;

vector<double> FindROI(const TFile* fTracks_ideal, const double& mu_potential){   
    vector<double> roi;
    auto t_ana_uncut = fTracks_ideal->Get<TTree>("AnalysisTree");
    TH1F *hspectrum_uncut = new TH1F("hspectrum_uncut", "Energy spectrum of a reconstructed events without cut strips.", 100,1800,2620);
    //filling the histogram
    t_ana_uncut->Project("hspectrum_uncut", "tckAna_trackEnergy");

    The rest of my code

    return roi;

void cut_ROI_Xe136()
    Double_t xpeak = 2420.; 

    TFile *f_tracks2 = new TFile("my_file.root");
    vector<double> ROI_vec = FindROI(f_tracks2, xpeak);
    return 0;

When I execute my code I receive such an error:

cut_ROI_Xe136.C:55:39: error: no matching member function for call to 'Get'
    auto t_ana_uncut = fTracks_ideal->Get<TTree>("AnalysisTree");
note: candidate function not viable: no known conversion from 'const TFile' to 'TDirectoryFile' for object argument
   template <class T> inline T* Get(const char* namecycle)

Which is confusing as if I am just adding a line TTree *t_ana_uncut = (TTree*)f_tracks2->Get("AnalysisTree"); in my main macro after opening my file and not using FindROI function, everything is working fine.

Do you have an idea what am I doing wrong?
Thanks in advance.

Hi @alobasenko,
the important part of the error message is:

no known conversion from 'const TFile' to 'TDirectoryFile'

and the const in particular: you can’t call Get on a const TFile because the method is not const (it modifies the file’s state).

Changing the declaration of FindROI to accept a pointer to non-const TFile should fix the issue.

Also note that to use the templated Get<TTree> (as opposed to the other invocation you mention at the end of your post) you need a recent ROOT version (but if the templated version is not present the compiler error will say so).


1 Like

Hi @eguiraud ,

thank you very much for your help!