Accessing branches

__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?

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.m_pdg",&MCParticles.m_pdg);
   tree->SetBranchAddress("MCParticles.m_firstDaughter",&MCParticles.m_firstDaughter);
   tree->SetBranchAddress("MCParticles.m_lastDaughter",&MCParticles.m_lastDaughter);


   //create  histograms
   TH1F *h1 = new TH1F("First Daughters","First Daughters",100, -500, 500);
   // TH1F *h2 = new TH1F("#pi^{0}","Invariant Mass of #pi^{0}",100, 0.124, 0.140);


   Int_t nentries = (Int_t)tree->GetEntries();
   for (Int_t i=0; i<nentries; i++) {
      tree->GetEntry(i);
      if(MCParticles.m_pdg==433)
      {
         h1->Fill(MCParticles.m_firstDaughter);
         h1->GetXaxis()->SetTitle("First Daughter");

      }

      if (gROOT->IsBatch()) return;
      new TBrowser();
   }

}

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hello,

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

Cheers,
P

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;
}
1 Like

Thanks a lot. I had tried the following code but i receive "Break Segmentation Violation "

void fixed()
{
   TFile *file=new TFile("Dtorhog_sim_my20K.root");
   
   TTree *tr=(TTree*)file->Get("tree");
   
   TLeaf *l1=(TLeaf*)tr->GetLeaf("MCParticles.m_pdg");
  
 //The histograms:
   TH1F* h1 = new TH1F("Particles", " ", 100,400,450);
 
   Int_t pdgcode;

  Int_t  nentries = (Int_t)tr->GetEntries();
 //cout<<"entries from tree"<<"        "<<nentries<<"\n";                                                                                                               
  for(Int_t i=0;i<nentries;i++){
        tr->GetEntry(i);
 
cout<<"len1"<<" "<<l1->GetLen()<<endl;
       
        Int_t len1;
       	len1 = l1->GetLen();//Return the number of effective elements of this leaf, for the current entry. 
     	cout<<len1<<"\t"<<endl;                                                            

     	for(Int_t m=0; m< len1; m++)
      		{
        	pdgcode = l1->GetValue(m); 
        //cout<<l1->GetValue(m)<<endl;
       		// if (pdgcode==431)
       		  //{
     		     h1->Fill(pdgcode);
  			//  }
			//}
		   			}
h1->Draw();	
		
  }
}

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