Read the TTree is not working in root 6.11

Hi,

I’m trying to running the macro readTree_1 to read the file
SimpleTree.root

macro:

void readTree_1(const char* fileNameIn="SimpleTree.root") {

  // *******************************************************
  // Histogram
  // *******************************************************
  Int_t NBINS=100;  Double_t XMIN=0;  Double_t XMAX=10;
  TH1F *h1 = new TH1F("h1","Meu espectro de pt",NBINS,XMIN,XMAX);

  // *******************************************************
  // Open the file with the object Tree
  // *******************************************************
  TFile *fin = new TFile(fileNameIn,"READ");

  // *******************************************************
  // take the object TTree in the file .root
  // *******************************************************
  TTree *tree = (TTree*)fin->Get("tree"); 

  // *******************************************************
  // Define the address of the objects inside of the TTree
  // ******************************************************* 
  Int_t ev;
  TParticle *pa;
  tree->SetBranchAddress("Ev",&ev);
  tree->SetBranchAddress("Pa",&pa);

  // *******************************************************
  // The number of line of the TTree
  // ******************************************************* 
  Int_t nevents = tree->GetEntries();

  // *******************************************************
  // Do a loop in all  the lines of the TTree
  // ******************************************************* 
  for(Int_t i=0;i<nevents;++i) {
    tree->GetEntry(i);
    Double_t pt=(pa->Pt());
    h1->Fill(pt);
  }
  // *******************************************************
  // Windows
  // *******************************************************
  TCanvas *c1 = new TCanvas("c1","Minha Janela",10,10,700,500);
  c1->SetFillColor(18); c1->SetGrid(1,1); c1->SetLogx(0); c1->SetLogy(0);

  h1->Draw();
}

The file SimpleTree.root
SimpleTree.root (2.3 MB)

But I get the following error:

root [0] 
Processing readTree_1.C...

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007ff90ab0d07a in __GI___waitpid (pid=15698, stat_loc=stat_loc
entry=0x7ffdf85f7c80, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:29
#1  0x00007ff90aa85fbb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2  0x00007ff90b6804ed in TUnixSystem::Exec (shellcmd=<optimized out>, this=0x251f4f0) at /opt/root6/root6_src/core/unix/src/TUnixSystem.cxx:2118
#3  TUnixSystem::StackTrace (this=0x251f4f0) at /opt/root6/root6_src/core/unix/src/TUnixSystem.cxx:2412
#4  0x00007ff90b682adc in TUnixSystem::DispatchSignals (this=0x251f4f0, sig=kSigSegmentationViolation) at /opt/root6/root6_src/core/unix/src/TUnixSystem.cxx:3643
#5  <signal handler called>
#6  0x0000000000000000 in ?? ()
#7  0x00007ff8ebe49bb0 in TClass::Streamer (onfile_class=0x0, b=..., obj=<optimized out>, this=<optimized out>) at /opt/root6/include/TClass.h:537
#8  TBranchElement::ReadLeavesCustomStreamer (this=0x3eb62a0, b=...) at /opt/root6/root6_src/tree/tree/src/TBranchElement.cxx:4141
#9  0x00007ff8ebe3dea6 in TBranch::GetEntry (this=this
entry=0x3eb62a0, entry=entry
entry=0, getall=getall
entry=0) at /opt/root6/root6_src/tree/tree/src/TBranch.cxx:1317
#10 0x00007ff8ebe560c0 in TBranchElement::GetEntry (this=0x3eb62a0, entry=0, getall=0) at /opt/root6/root6_src/tree/tree/src/TBranchElement.cxx:2332
#11 0x00007ff8ebe997da in TTree::<lambda()>::operator() (__closure=<synthetic pointer>) at /opt/root6/root6_src/tree/tree/src/TTree.cxx:5342
#12 TTree::GetEntry (this=0x3ae88a0, entry=0, getall=0) at /opt/root6/root6_src/tree/tree/src/TTree.cxx:5416
#13 0x00007ff90bde81e1 in ?? ()
#14 0x00007ff900000007 in ?? ()
#15 0x00000000025d3bc0 in ?? ()
#16 0x0000000002583110 in ?? ()
#17 0x35fefdb2dd491600 in ?? ()
#18 0x0000000003ba5de0 in ?? ()
#19 0x0000000000000000 in ?? ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum.
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x0000000000000000 in ?? ()
#7  0x00007ff8ebe49bb0 in TClass::Streamer (onfile_class=0x0, b=..., obj=<optimized out>, this=<optimized out>) at /opt/root6/include/TClass.h:537
#8  TBranchElement::ReadLeavesCustomStreamer (this=0x3eb62a0, b=...) at /opt/root6/root6_src/tree/tree/src/TBranchElement.cxx:4141
#9  0x00007ff8ebe3dea6 in TBranch::GetEntry (this=this
entry=0x3eb62a0, entry=entry
entry=0, getall=getall
entry=0) at /opt/root6/root6_src/tree/tree/src/TBranch.cxx:1317
#10 0x00007ff8ebe560c0 in TBranchElement::GetEntry (this=0x3eb62a0, entry=0, getall=0) at /opt/root6/root6_src/tree/tree/src/TBranchElement.cxx:2332
#11 0x00007ff8ebe997da in TTree::<lambda()>::operator() (__closure=<synthetic pointer>) at /opt/root6/root6_src/tree/tree/src/TTree.cxx:5342
#12 TTree::GetEntry (this=0x3ae88a0, entry=0, getall=0) at /opt/root6/root6_src/tree/tree/src/TTree.cxx:5416
#13 0x00007ff90bde81e1 in ?? ()
#14 0x00007ff900000007 in ?? ()
#15 0x00000000025d3bc0 in ?? ()
#16 0x0000000002583110 in ?? ()
#17 0x35fefdb2dd491600 in ?? ()
#18 0x0000000003ba5de0 in ?? ()
#19 0x0000000000000000 in ?? ()
===========================================================

I think that error is in some place for here in the macro:

for(Int_t i=0;i<nevents;++i) {
    tree->GetEntry(i);   
    Double_t pt=(pa->Pt());
    h1->Fill(pt);
  }


Someone can help me?
Thanks!
André

void readTree_1(const char* fileNameIn="SimpleTree.root") {

  // *******************************************************
  // Histogram
  // *******************************************************
  Int_t NBINS=100;  Double_t XMIN=0;  Double_t XMAX=10;
  TH1F *h1 = new TH1F("h1","Meu espectro de pt",NBINS,XMIN,XMAX);

  // *******************************************************
  // Open the file with the object Tree
  // *******************************************************
  TFile *fin = TFile::Open(fileNameIn, "READ");
  if ((!fin) || (fin->IsZombie())) {delete fin; return;} // just a precaution

  // *******************************************************
  // take the object TTree in the file .root
  // *******************************************************
  TTree *tree; fin->GetObject("tree", tree);
  if (!tree) {delete fin; return;} // just a precaution

  // *******************************************************
  // Define the address of the objects inside of the TTree
  // ******************************************************* 
  Int_t ev;
  TParticle *pa = 0;
  tree->SetBranchAddress("Ev",&ev);
  tree->SetBranchAddress("Pa",&pa);

  // *******************************************************
  // The number of line of the TTree
  // ******************************************************* 
  Int_t nevents = tree->GetEntries();

  // *******************************************************
  // Do a loop in all  the lines of the TTree
  // ******************************************************* 
  for(Int_t i=0;i<nevents;++i) {
    tree->GetEntry(i);
    Double_t pt=(pa->Pt());
    h1->Fill(pt);
  }
  // *******************************************************
  // Windows
  // *******************************************************
  TCanvas *c1 = new TCanvas("c1","Minha Janela",10,10,700,500);
  c1->SetFillColor(18); c1->SetGrid(1,1); c1->SetLogx(0); c1->SetLogy(0);

  h1->Draw();

  // tree->ResetBranchAddresses(); // "disconnect" from local variables
  delete fin; // automatically deletes "tree", too
  delete pa; // "cleanup"
}

Hi, Wile_E_Coyote

Thanks for the quick answer! This work well!

Cheers,
André

Hi Andre,

given that you are using a recent ROOT, you may want to express your analysis with the new TDataFrame. The code would look like this:

ROOT::Experimental::TDataFrame df("tree","SimpleTree.root");
Int_t NBINS=100;  Double_t XMIN=0;  Double_t XMAX=10;
auto h1 = df.Define("pa_pt","Pa.Pt()").Histo1D(TH1D("h1","Meu espectro de pt",NBINS,XMIN,XMAX),"pa_pt");
auto c1 = new TCanvas("c1","Minha Janela",10,10,700,500);
c1->SetFillColor(18); c1->SetGrid(1,1); c1->SetLogx(0); c1->SetLogy(0);
h1->Draw();

Cheers,
D

Hi dpiparo,

Thanks for the tip, I’m trying to use the code that you suggested, but
it’s not working and I got the following error:

error: no matching member function for call to 'Histo1D'
     auto h1 = df.Define("pa_pt","Pa.Pt()").Histo1D(TH1D("h1","Meu espectro de pt",NBINS,XMIN,XMAX),"pa_pt");

/opt/root6/include/ROOT/TDFInterface.hxx:533:25: note: candidate function not viable: no known conversion from 'TH1D' to '::TH1F' for 1st argument
   TResultProxy<::TH1F> Histo1D(::TH1F &&model = ::TH1F{"", "", 128u, 0., 0.}, std::string_view vName = "")

Cheers,
André

Hi Andre,

since I saw 6.11, a not yet released version of root, in the title of the thread I assumed you were using the master of yesterday which assumes that the histograms created by the TDF are TH1Ds :sweat_smile:.
In your case, as the error is saying, TH1Fs should be used instead:

ROOT::Experimental::TDataFrame df("tree","SimpleTree.root");
Int_t NBINS=100;  Double_t XMIN=0;  Double_t XMAX=10;
auto h1 = df.Define("pa_pt","Pa.Pt()").Histo1D(TH1F("h1","Meu espectro de pt",NBINS,XMIN,XMAX),"pa_pt");
auto c1 = new TCanvas("c1","Minha Janela",10,10,700,500);
c1->SetFillColor(18); c1->SetGrid(1,1); c1->SetLogx(0); c1->SetLogy(0);
h1->Draw();

This will work also with the latest tagged version, 6.10/02.

Cheers,
D

Hi @Danilo,

I did the change TH1D to TH1F, but this isn’t working
and in the Canvas not appear the histogram.

Thanks for the help!
Cheers,

Andre

Hi,

I pasted the code in the prompt of ROOT after downloading the file you shared and it works for me. What is the problem you are facing?

Cheers,
D

Hi,

thanks for the reply, to explain the situation see the image:

Cheers,
Andre

Hi,
quick fix to try: can you try changing h1->Draw() with h1->DrawClone please?

Cheers,
Enrico

1 Like

Hi,

yes, that’s the solution. It would have been the case for any drawn primitive, irrespective from the TDataFrame of course!

D

Thanks for the help, Guys! The change h1->Draw() to h1->DrawClone() work well!

Cheers,
Andre

1 Like

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