Getting data from a tree

Hi,
i’m making exercises to retrieve data from a tree.

i think i followed the example in the manual, but i wasn’t able to retrieve the correct data.

i retrieve a px=4 instead of 23.

where i did wrong?

Thank you,
Amir
To create the tree

[code]#include <TFile.h>
#include <TObjArray.h>
#include <TGraph.h>
#include <TGraphErrors.h>
#include <TMultiGraph.h>
#include <TMarker.h>
#include <TTree.h>
#include <TFolder.h>
#include <TString.h>

#include <TCanvas.h>
#include

using namespace std;
Float_t px =0;

int main()
{
TFile *f =new TFile(“tree1.root”,“recreate”);
f->mkdir(“dir”);
f->cd(“dir”);
gDirectory->mkdir(“subdir”);
gDirectory->cd(“subdir”);
TTree t1(“t1”,“a simple tree”);

t1.Branch(“px”,&px,“px/F”);
px =23;

t1.Fill();
t1.Write();

return 0;
}
[/code]

To retrieve data

[code]#include <TFile.h>
#include <TObjArray.h>
#include <TGraph.h>
#include <TGraphErrors.h>
#include <TMultiGraph.h>
#include <TMarker.h>
#include <TTree.h>
#include <TFolder.h>
#include <TString.h>
#include <TH1.h>
#include <TCanvas.h>
#include
#include
#include

using namespace std;

TFile*myfile =TFile::Open(“tree1.root”);
Float_t a1 =0;

Float_t px;

int main(){

myfile->cd(“dir”);
gDirectory->cd(“subdir”);
cout<<"directory: "<pwd();

t1 =(TTree *)gDirectory->Get(“t1”);

t1->SetBranchAddress(“px”,&px);
Int_t nentries =(Int_t)t1->GetEntries();
cout<<"Entries px: "<<nentries<<endl;
for(Int_t i=0;i<nentries;i++)
{
a1 +=t1->GetEntry(i);
}
cout<<"a1: "<<a1<<endl;
return 0;
}
[/code]

where i did wrong?

Thank you,
Amir

cout<<"a1: "<<a1<<endl; prints the number of byte read.

cout<<"px: "<<px<<endl; should return 23.

Cheers,
Philippe

Thank you very much!
Now it works.

but then what is the use of GetEntry()?: i was thinking it is this method who retrieve the data stored in the branches.

Amir

[quote]but then what is the use of GetEntry()?: i was thinking it is this method who retrieve the data stored in the branches.[/quote]This is exactly it. However the data is copy into the address that you have passed to SetBranchAddress and not as the return value of GetEntry!

Cheers,
Philippe

Sorry to bother again…

i modified the tree a little to retrieve useful (for me) data.

[code]#include <TFile.h>
#include <TObjArray.h>
#include <TGraph.h>
#include <TGraphErrors.h>
#include <TMultiGraph.h>
#include <TMarker.h>
#include <TTree.h>
#include <TFolder.h>
#include <TString.h>
#include <TH1.h>
#include <TCanvas.h>
#include
#include
#include

using namespace std;

int run(){

ostringstream title;
ostringstream endTitle;
ostringstream otherTitle;

title<<“mu0_1GeVtheta0R2000”;

endTitle<<title.str()<<".root";

otherTitle<<title.str()<<“sigma.root”;
TTree *sigmaTree =new TTree(“Sigma”,“Sigma”);
TFile *myfile =TFile::Open(endTitle.str().c_str());

Double_t PETotpmt1;
Double_t PETotpmt2;
Double_t PETotpmt3;
Double_t PETotb;
Double_t sigma;

sigmaTree->Branch(“sigma”,&sigma,“sigma/D”);

for(Int_t b=1;b<2001;b++)
{
/PETotpmt1 =0;
PETotpmt2 =0;
PETotpmt3 =0;
//PETot=0;
/
const Char_t* dir;
ostringstream pippo;
pippo<<"Run "<<b;
dir =pippo.str().c_str();
myfile->cd(dir);
gDirectory->cd(“Station 119”);
cout<<"directory: "<pwd();

pmt1Tot =(TTree *)gDirectory->Get(“pmt1Tot”);
pmt2Tot =(TTree *)gDirectory->Get(“pmt2Tot”);
pmt3Tot =(TTree *)gDirectory->Get(“pmt3Tot”);

pmt1Tot->SetBranchAddress(“PETot”,&PETotpmt1);
Int_t nentries =(Int_t)pmt1Tot->GetEntries();

for(Int_t i=0;i<nentries;i++)
{
pmt1Tot->GetEntry(i);
}

pmt2Tot->SetBranchAddress(“PETot”,&PETotpmt2);
Int_t nentries =(Int_t)pmt2Tot->GetEntries();

for(Int_t i=0;i<nentries;i++)
{
pmt2Tot->GetEntry(i);
}

pmt3Tot->SetBranchAddress(“PETot”,&PETotpmt3);
Int_t nentries =(Int_t)pmt3Tot->GetEntries();

for(Int_t i=0;i<nentries;i++)
{
pmt3Tot->GetEntry(i);
}

PETotb =PETotpmt1 +PETotpmt2 +PETotpmt3;

sigma =pow(0.5*(pow(PETotpmt1-(PETotb/3.0),2) +pow(PETotpmt2-(PETotb/3.0),2) +pow(PETotpmt3-(PETotb/3.0),2)),0.5);
cout<<"sigma: "<<sigma<<endl;
sigmaTree->Fill();

myfile->cd();

}

TFile *sigmafile =new TFile(otherTitle.str().c_str(),“recreate”);
TTree *sigmaTreeNew =sigmaTree->CloneTree();
sigmaTreeNew->Write();
cout<<“All Green!”<<endl;
return 0;
}[/code]

but when i run it (under ROOT 5) i get this message:

[code]root [0] .L sigma.cc
root [1] run()
directory: mu0_1GeVtheta0R2000.root:/Run 1/Station 119
0sigma: 3.21455
directory: mu0_1GeVtheta0R2000.root:/Run 2/Station 119

*** Break *** segmentation violation
Generating stack trace…
0x413d5db8 in from /lib/libc.so.6
0x408392f1 in G__exec_asm + 0x931 from /home/amir/root5.02/lib/libCint.so
0x407c9118 in G__exec_loop + 0x535 from /home/amir/root5.02/lib/libCint.so
0x407c94dd in G__exec_for + 0x19a from /home/amir/root5.02/lib/libCint.so
0x407cb981 in G__exec_statement + 0x21cf from /home/amir/root5.02/lib/libCint.s
o
0x4079dc99 in G__interpret_func + 0x2060 from /home/amir/root5.02/lib/libCint.s
o
0x40781b09 in G__getfunction + 0x1a72 from /home/amir/root5.02/lib/libCint.so
0x40778554 in G__getitem + 0x51a from /home/amir/root5.02/lib/libCint.so
0x40777223 in G__getexpr + 0x72a6 from /home/amir/root5.02/lib/libCint.so
0x407c4e3b in G__exec_function + 0x1d5 from /home/amir/root5.02/lib/libCint.so
0x407cbc22 in G__exec_statement + 0x2470 from /home/amir/root5.02/lib/libCint.s
o
0x40760429 in G__exec_tempfile_core + 0x2f3 from /home/amir/root5.02/lib/libCin
t.so
0x407605fa in G__exec_tempfile_fp + 0x22 from /home/amir/root5.02/lib/libCint.s
o
0x407d4360 in G__process_cmd + 0x4995 from /home/amir/root5.02/lib/libCint.so
0x401e78bf in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0xaf
from /home/amir/root5.02/lib/libCore.so
0x40133197 in TApplication::ProcessLine(char const*, bool, int*) + 0x66d from /
home/amir/root5.02/lib/libCore.so
0x41257340 in TRint::HandleTermInput() + 0x1f8 from /home/amir/root5.02/lib/lib
Rint.so
0x41255f20 in TTermInputHandler::Notify() + 0x24 from /home/amir/root5.02/lib/l
ibRint.so
0x41257f2e in TTermInputHandler::ReadNotify() + 0x12 from /home/amir/root5.02/l
ib/libRint.so
0x4029b294 in TUnixSystem::CheckDescriptors() + 0x152 from /home/amir/root5.02/
lib/libCore.so
0x4029a1b7 in TUnixSystem::DispatchOneEvent(bool) + 0x17d from /home/amir/root5
.02/lib/libCore.so
0x401a4150 in TSystem::InnerLoop() + 0x18 from /home/amir/root5.02/lib/libCore.
so
0x401a40ef in TSystem::Run() + 0x7b from /home/amir/root5.02/lib/libCore.so
0x40133e1e in TApplication::Run(bool) + 0x32 from /home/amir/root5.02/lib/libCo
re.so
0x41256d8a in TRint::Run(bool) + 0x3d8 from /home/amir/root5.02/lib/libRint.so
0x08048e15 in main + 0x71 from /home/amir/root5.02/bin/root.exe
0x413c2469 in __libc_start_main + 0xb9 from /lib/libc.so.6
0x08048d01 in TApplicationImp::ShowMembers(TMemberInspector&, char*) + 0x39 fro
m /home/amir/root5.02/bin/root.exe
Root > Function run() busy flag cleared[/code]

This happens only the first time i run the script, if i reload the script and execute it again everithing works ok.

What is happening?

Thank you
Amir

[quote]This happens only the first time i run the script, if i reload the script and execute it again everithing works ok.[/quote]I did not spot what could cause the crash. I recommend that you first try to compile your script (and correct a few C++ errors like duplicated variable names):

.L sigma.cc+ run()If this works nicely, this is possibly an error in CINT (then send me a tar file to reproduce the problem). If this does not work, run your program using valgrind (valgrind.kde.org):valgrind --tool=memcheck root.exe -b -l root [] .L signa.cc+ root [] run()

Also There is something odd in your code:[code] pmt2Tot->SetBranchAddress(“PETot”,&PETotpmt2);
Int_t nentries =(Int_t)pmt2Tot->GetEntries();

for(Int_t i=0;i<nentries;i++)
{
pmt2Tot->GetEntry(i);
}
[/code]Given that GetEntry reads only one entry at the time and re-use the same memory … without doing any operation like accumulation. This is strictly equivalent to:

pmt2Tot->SetBranchAddress("PETot",&PETotpmt2); Int_t nentries =(Int_t)pmt2Tot->GetEntries(); pmt2Tot->GetEntry(nentries-1);
(except that this 2nd option does not waste time reading values that are thrown away).
My apologies if this code is there only due to simplication of you real life work :slight_smile:

Cheers,
Philippe.

Thanks for the reply.

It seems CINT doesn’t like ostringstream variables…

I corrected the code, or i think so:

[code]int run(){

ostringstream title;
ostringstream endTitle;
ostringstream otherTitle;

title<<“amirOut”;

endTitle<<title.str()<<".root";

otherTitle<<title.str()<<“sigma.root”;
TTree *sigmaTree =new TTree(“Sigma”,“Sigma”);
TFile *myfile =TFile::Open(endTitle.str().c_str());

Double_t PETotpmt1;
Double_t PETotpmt2;
Double_t PETotpmt3;
Double_t PETotb;
Double_t sigma;

sigmaTree->Branch(“sigma”,&sigma,“sigma/D”);

for(Int_t b=1;b<4;b++)
{
PETotpmt1 =0;
PETotpmt2 =0;
PETotpmt3 =0;
PETotb=0;
const Char_t* dir;
ostringstream pippo;
pippo<<"Run "<<b;
dir =pippo.str().c_str();
myfile->cd(dir);
gDirectory->cd(“Station 119”);
cout<<"directory: "<pwd();

TTree *pmt1Tot =(TTree *)gDirectory->Get(“pmt1Tot”);
TTree *pmt2Tot =(TTree *)gDirectory->Get(“pmt2Tot”);
TTree *pmt3Tot =(TTree *)gDirectory->Get(“pmt3Tot”);

pmt1Tot->SetBranchAddress(“PETot”,&PETotpmt1);
Int_t nentriesPmt1 =(Int_t)pmt1Tot->GetEntries();
pmt1Tot->GetEntry(nentriesPmt1-1);

pmt2Tot->SetBranchAddress(“PETot”,&PETotpmt2);
Int_t nentriesPmt2 =(Int_t)pmt2Tot->GetEntries();
pmt2Tot->GetEntry(nentriesPmt2-1);

pmt3Tot->SetBranchAddress(“PETot”,&PETotpmt3);
Int_t nentriesPmt3 =(Int_t)pmt3Tot->GetEntries();
pmt3Tot->GetEntry(nentriesPmt3-1);

PETotb =PETotpmt1 +PETotpmt2 +PETotpmt3;

sigma =pow(0.5*(pow(PETotpmt1-(PETotb/3.0),2) +pow(PETotpmt2-(PETotb/3.0),2) +pow(PETotpmt3-(PETotb/3.0),2)),0.5);
cout<<"sigma: "<<sigma<<endl;
sigmaTree->Fill();

myfile->cd();

}

TFile *sigmafile =new TFile(otherTitle.str().c_str(),“recreate”);
TTree *sigmaTreeNew =sigmaTree->CloneTree();
sigmaTreeNew->Write();
cout<<“All Green!”<<endl;[/code]

But still it doesnt work with the first strike.

Running .L sigma.cc+
produces

Info in <TUnixSystem::ACLiC>: creating shared library /home/amir/2/./sigma_cc.so In file included from /home/amir/2/fileWaoUoa.h:32, from /home/amir/2/fileWaoUoa.cxx:16: /home/amir/2/sigma.cc: In function `int run()': /home/amir/2/sigma.cc:22: error: aggregate `std::ostringstream title' has incomplete type and cannot be defined /home/amir/2/sigma.cc:23: error: aggregate `std::ostringstream endTitle' has incomplete type and cannot be defined /home/amir/2/sigma.cc:24: error: aggregate `std::ostringstream otherTitle' has incomplete type and cannot be defined /home/amir/2/sigma.cc:50: error: aggregate `std::ostringstream pippo' has incomplete type and cannot be defined /home/amir/2/sigma.cc:55: error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>]((&std::cout), "directory: ") << (*(gDirectory->TDirectory::_vptr.TObject + 336))(gDirectory)' /usr/include/c++/3.3.4/bits/ostream.tcc:63: error: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:74: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:86: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:122: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:156: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:98: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:178: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:189: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:193: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:204: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:181: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:216: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:241: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:219: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:265: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:289: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:313: error: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>] /home/amir/root5.02/include/TBuffer.h:514: error: TBuffer& operator<<(TBuffer&, const TObject*) /home/amir/root5.02/include/TString.h:232: error: TBuffer& operator<<(TBuffer&, const TString*) /home/amir/root5.02/include/TString.h:368: error: std::ostream& operator<<(std::ostream&, const TString&) /home/amir/root5.02/include/TString.h:370: error: TBuffer& operator<<(TBuffer&, const TString&) /home/amir/root5.02/include/TUUID.h:100: error: TBuffer& operator<<(TBuffer&, const TUUID&) /home/amir/root5.02/include/TArray.h:74: error: TBuffer& operator<<(TBuffer&, const TArray*) /home/amir/root5.02/include/TArrayD.h:74: error: TBuffer& operator<<(TBuffer&, const TArrayD*) /home/amir/root5.02/include/TArrayC.h:73: error: TBuffer& operator<<(TBuffer&, const TArrayC*) /home/amir/root5.02/include/TArrayS.h:73: error: TBuffer& operator<<(TBuffer&, const TArrayS*) /home/amir/root5.02/include/TArrayI.h:74: error: TBuffer& operator<<(TBuffer&, const TArrayI*) /home/amir/root5.02/include/TArrayF.h:73: error: TBuffer& operator<<(TBuffer&, const TArrayF*) /usr/include/c++/3.3.4/ostream:491: error: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:486: error: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:616: error: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:572: error: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:452: error: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:447: error: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/bits/ostream.tcc:512: error: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>] /usr/include/c++/3.3.4/ostream:436: error: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>] /home/amir/2/sigma.cc:89: warning: unused variable `TFile*sigmafile' g++: /home/amir/2/./fileWaoUoa.o: No such file or directory Error in <ACLiC>: Compilation failed!

While doing it with valgrind activated

==10808== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 69 from 4) ==10808== malloc/free: in use at exit: 15832496 bytes in 23767 blocks. ==10808== malloc/free: 119385 allocs, 95618 frees, 26887964 bytes allocated. ==10808== For counts of detected errors, rerun with: -v ==10808== searching for pointers to 23767 not-freed blocks. ==10808== checked 18529536 bytes. ==10808== ==10808== LEAK SUMMARY: ==10808== definitely lost: 11979 bytes in 86 blocks. ==10808== possibly lost: 112505 bytes in 2579 blocks. ==10808== still reachable: 15708012 bytes in 21102 blocks. ==10808== suppressed: 0 bytes in 0 blocks. ==10808== Use --leak-check=full to see details of leaked memory.

Do you think i should run .L sigma.cc ?
It is useful to post valgrind --tool=memcheck --leak-check=full root.exe -b -l ?

Sorry for the long post
Amir

Hi,
the “.L …+” tells root to compile your code, and then it. It calls your compiler, and that complains about the unknown ostringstream. As with any regular c++ code, you have to #include the necessary headers for all the types you use. For ostringstream that’s #include - but you need more includes than just that one (TTree.h, TFile.h,…).
Axel.

Thank you, i’ ve added sstream.h to my include list ( i was using fstream.h)

.L sigma.cc+ didn’t like the line

(iostream.h was already included)
it seems strange to me

but after commenting it everithing worked fine.

So thank you all for the great help!