Problem in the Reading the TTree

void read_root(const char* fileNameIn="crmc_eposlhc_294709180_n_C_3500.root") 
{

  Int_t NBINS=100;   Double_t XMIN=0;  Double_t XMAX=2;
  TH1F *h1 = new TH1F("h1","pt",NBINS,XMIN,XMAX);

  TFile *fin = new TFile(fileNameIn,"READ"); 
  TTree *tree = (TTree *)fin->Get("Particle"); // função Get numero de linhas

  Double_t px,py,pz,ImpactParameter,E,m;
  Int_t nPart, pdgid, status;
  
  tree->SetBranchAddress("nPart", &nPart);
  tree->SetBranchAddress("ImpactParameter", &ImpactParameter);
  tree->SetBranchAddress("pdgid", &pdgid);
  tree->SetBranchAddress("status", &status);
  tree->SetBranchAddress("px", &px);
  tree->SetBranchAddress("py", &py);
  tree->SetBranchAddress("pz", &pz);
  tree->SetBranchAddress("E", &E);
  tree->SetBranchAddress("m", &m);
 
  Int_t nevents = tree->GetEntries(); // numero de linhas

  for(Int_t i=0;i<nevents;++i) {
    
    //tree->GetEntry(i);
    cout<<" px = "<<px<<"  py = "<<py<<endl;
    //cout<<" i = "<<i<<endl;
    
    Double_t pt=px*px+py*py;
    
    //cout<<" pt = "<<pt<<endl;
    
    h1->Fill(pt); // preenche um histograma
    //cout<<" ----> "<<h1->GetBinContent(i)<<endl;
  }
    cout<<" nevents = "<<nevents<<endl;

  TCanvas *c1 = new TCanvas("c1","Minha Janela",10,10,700,500);
  c1->SetFillColor(18); c1->SetGrid(1,1);  c1->SetLogx(0);   c1->SetLogy(1);

  h1->Draw();
}

output : px = 0 and py =0;

I think the file (crmc_eposlhc_294709180_n_C_3500.root) is fine, because I can open this file in the TBrowser,

someone can help?

thanks!

Hi Andre,

perhaps you can treat the problem using the TDataFrame.
Here you have a snippet to get you started:

  Int_t NBINS=100;   Double_t XMIN=0;  Double_t XMAX=2;
  ROOT::Experimental::TDataFrame tdf("Particle", fileNameIn);
  auto h1 = tdf.AddColumn("pt2", [](double px, double py){ return px*px+py*py;} {"px","py"})
                      .Histo1D(TH1F("h1","pt",NBINS,XMIN,XMAX), "pt2");
  auto c1 = new TCanvas("c1","Minha Janela",10,10,700,500);
  c1->SetFillColor(18); c1->SetGrid(1,1);  c1->SetLogx(0);   c1->SetLogy(1);
  h1->Draw();

Cheers,
D

Hi dpiparo,

your sugesstion did the reading very well, but result is the same as before, the variables continue be zero.

The TTree in the file .root there are diferents variables with diferents entries, por example, nPart (entries=1000) and px (entries~280000). I think in this reading the macro is not acess the right leaf from the TTree.

What i need are the variable px, py,pz and E.

I think that px, py, pz and E is not Receiving the data from the TTree.

The attachament (The figure) show the results

Thanks,

André

#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include "TBrowser.h"
#include "TH2.h"
#include "TRandom.h"


void read_root(const char* fileNameIn="crmc_eposlhc_294960099_Pb_p_1600.root")
{

  // *******************************************************
  // open the file that have the object Tree
  // *******************************************************
  TFile *fin = new TFile(fileNameIn,"READ");

  // *******************************************************
  //Take the object TTree that is inside of the file .root
  // *******************************************************
  TTree *tree = (TTree*)fin->Get("Particle"); 

  // *******************************************************
  // Created a windows for show the graph
  // *******************************************************
  TCanvas *c1 = new TCanvas("c1","Minha Janela",10,10,700,500);
  c1->SetFillColor(18);
  c1->SetGrid(1,1);
  c1->SetLogx(0);
  c1->SetLogy(1);
  
  tree->Draw("TMath::Sqrt(px*px+py*py)"); 
  //tree->Print("all");
  ((TTreePlayer*)(tree->GetPlayer()))->SetScanRedirect(true);
  ((TTreePlayer*)(tree->GetPlayer()))->SetScanFileName("crmc_eposlhc_294960099_Pb_p_1600.txt");
  tree->Scan();
  

}

Now, It is working very well,

Thanks,

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