__I have a root file whose contents are arranged as shown. I need to access The leaf MCParticles.m_pdg and put a cut on it. I am trying as follows but I am not able to get it. Would you please help?
have you thought to a declarative approach via RDataFrame?
The syntax for your task would be:
ROOT::RDataFrame rdf("Dtorhog_sim_my20K.root","tree/MCParticles");
auto h = rdf.Filter("MCParticles.m_pdg==433").Define("first_daughter","MCParticles.m_firstDaughter").Histo1D("first_daughter");
h->GetXaxis()->SetTitle(“First Daughter”);
if (gROOT->IsBatch()) return;
new TBrowser();
Thank you so much. I had never tried this before. As per your suggestion , I am trying to use the following code:
void Dtorhog_problem()
{
TFile *f = new TFile("Dtorhog_sim_my20K.root");
TTree *tree = (TTree*)f->Get("tree/MCParticles/");
Int_t MCParticles, MCParticles.m_pdg, MCParticles.m_firstDaughter, MCParticles.m_lastDaughter ;
tree->SetBranchAddress("MCParticles",&MCParticles);
tree->SetBranchAddress("MCParticles/MCParticles.m_pdg",&MCParticles/MCParticles.m_pdg);
tree->SetBranchAddress("MCParticles/MCParticles.m_firstDaughter",&MCParticles/MCParticles.m_firstDaughter);
tree->SetBranchAddress("MCParticles/MCParticles.m_lastDaughter",&MCParticles/MCParticles.m_lastDaughter);
ROOT::RDataFrame rdf("Dtorhog_sim_my20K.root","tree/MCParticles");
auto h = rdf.Filter("MCParticles.m_pdg==433").Define("first_daughter","MCParticles.m_firstDaughter").Histo1D("first_daughter");
h->GetXaxis()->SetTitle(“First Daughter”);
if (gROOT->IsBatch()) return;
new TBrowser();
}
However, when I try to run, I get the error message:“illegal pointer to class object tree 0x0 1105 Dtorhog_problem.C:9:”. Now this is an error which is received when the path is wrong. Here, the path to the leaf is correct. What could be the reason?
This indicates that the Get command is not finding the tree.
To get some more information try:
TFile *f = new TFile("Dtorhog_sim_my20K.root");
TTree *tree = (TTree*)f->Get("tree/MCParticles/");
if (tree == nullptr) {
cerr << "Could not find the tree using the name 'tree/MCParticles/'. Let look at the file content:\n";
file->ls();
return;
}