Reading bare variables from a tree

Hi,
I have a tree filled with an event class: the class is complicated and contains subclasses in vectors. The tree works pretty fine when read with the event itself.

For a macro* I would like to read some variable in a ntuple way, like: Int_t evnum, n_clusters; tree->SetBranchAddress ("evnum", &evnum); tree->SetBranchAddress ("v.npmt", &npmt); tree->GetEntry (100); Unfortunately when doing this way, it does not work: every variable remains empty.

Moreover, as a general comment: immagine that v (in the previous code) is a vector of classes; (if reading this way is possible), will npmt point always to variable in the first element of v? like v[0].npmt?

Thanks,
Alessandro

*I have to modify the current long macro and keeping the ntuple reading style would simplify the porting.

try the following:

Int_t evnum, n_clusters; tree->SetMakeClass(1); //<========== tree->SetBranchAddress ("evnum", &evnum); tree->SetBranchAddress ("v.npmt", &npmt); tree->GetEntry (100);

Rene

Now it crashes :slight_smile:
Have root 5.26.00 (but the same happen for 5.25)

Hints?

Hi,

Most likely the variable you use for SetBranchAddress is no longer in scope.
You can pinpoint what the problem really is by using valgrind.

[quote]Moreover, as a general comment: immagine that v (in the previous code) is a vector of classes; (if reading this way is possible), will npmt point always to variable in the first element of v? like v[0].npmt? [/quote]No,
you actually MUST pass the address of an array (of the right fundamental type) large enough to hold the largest number of element in the vector. So
something likeInt_t evnum, npmt[1000]; tree->SetBranchAddress ("evnum", &evnum); tree->SetBranchAddress ("v.npmt", &npmt); tree->GetEntry (100);

Cheers,
Philippe.

Not a variable out of scope problem:

[quote]$ root -l /bxstorage/rootfiles/cycle_12/2009/Aug_30/Run011002_c12.root
Attaching file /bxstorage/rootfiles/cycle_12/2009/Aug_30/Run011002_c12.root as _file0…
Warning in TClass::TClass: no dictionary for class BxEvent is available
[…]
Warning in TClass::TClass: no dictionary for class BxPhysTags is available
root [1] TTree bxtree = (TTree)_file0->Get (“bxtree”);
root [2] Int_t evnum, n_clusters, npmts;
root [3] bxtree->SetMakeClass(1);
root [4] bxtree->SetBranchAddress (“evnum”, &evnum);
root [5] bxtree->GetEntry (200);

*** Break *** segmentation violation
Using host libthread_db library “/lib/tls/i686/cmov/libthread_db.so.1”.
Attaching to program: /proc/20132/exe, process 20132
[Thread debugging using libthread_db enabled]
[New Thread -1226904672 (LWP 20132)]
0xffffe410 in __kernel_vsyscall ()
[…]
[/quote]
(same with other versions of root)

Hi,

What type is ‘evnum’ in the TTree (you may cross-check using bxtree->MakeSelector(“bxsel”) and look at the content of bxsel.h)?

What does

valgrind root.exe -b -l /bxstorage/rootfiles/cycle_12/2009/Aug_30/Run011002_c12.root outputs for the same example?

Cheers,
Philippe.

Hi,
it’s a Int_t.

Here the valgring output for the GetEntry command with the old root:[quote][…]
root [5] bxtree->GetEntry (200)
==13210==
==13210== Invalid write of size 4
==13210== at 0x6177AA6: TBufferFile::ReadInt(int&) (in /home/cern_etch/root_v5.20.00-1/lib/libRIO.so.5.20)
==13210== by 0x6F34D8A: TBranchElement::ReadLeaves(TBuffer&) (in /home/cern_etch/root_v5.20.00-1/lib/libTree.so.5.20)
==13210== by 0x6F2B51D: TBranch::GetEntry(long long, int) (in /home/cern_etch/root_v5.20.00-1/lib/libTree.so.5.20)
==13210== by 0x6F318B3: TBranchElement::GetEntry(long long, int) (in /home/cern_etch/root_v5.20.00-1/lib/libTree.so.5.20)
==13210== by 0x6F31765: TBranchElement::GetEntry(long long, int) (in /home/cern_etch/root_v5.20.00-1/lib/libTree.so.5.20)
==13210== by 0x6F31765: TBranchElement::GetEntry(long long, int) (in /home/cern_etch/root_v5.20.00-1/lib/libTree.so.5.20)
==13210== by 0x6F7FAE2: TTree::GetEntry(long long, int) (in /home/cern_etch/root_v5.20.00-1/lib/libTree.so.5.20)
==13210== by 0x6FFD32B: G__G__Tree_103_0_66(G__value*, char const*, G__param*, int) (in /home/cern_etch/root_v5.20.00-1/lib/libTree.so.5.20)
==13210== by 0x4710379: Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) (in /home/cern_etch/root_v5.20.00-1/lib/libCint.so.5.20)
==13210== by 0x47B10E7: G__execute_call (in /home/cern_etch/root_v5.20.00-1/lib/libCint.so.5.20)
==13210== by 0x47B2148: G__call_cppfunc (in /home/cern_etch/root_v5.20.00-1/lib/libCint.so.5.20)
==13210== by 0x4788AB8: G__interpret_func (in /home/cern_etch/root_v5.20.00-1/lib/libCint.so.5.20)
==13210== Address 0x0 is not stack’d, malloc’d or (recently) free’d
[/quote]

And here the full output of valgrind for root 5.26:[quote]
[2069:0]~$ valgrind root.exe -b -l
==19842== Memcheck, a memory error detector
==19842== Copyright © 2002-2009, and GNU GPL’d, by Julian Seward et al.
==19842== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info
==19842== Command: root.exe -b -l
==19842==
root [0] TFile f(“Run011002_c12.root”);
==19842== Conditional jump or move depends on uninitialised value(s)
==19842== at 0x5260A2E: inflateReset2 (in /usr/lib/libz.so.1.2.3.4)
==19842== by 0x5260B0C: inflateInit2_ (in /usr/lib/libz.so.1.2.3.4)
==19842== by 0x5260B82: inflateInit_ (in /usr/lib/libz.so.1.2.3.4)
==19842== by 0x425C6CC: R__unzip (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libCore.so.5.26)
==19842== by 0x6B9F55B: TKey::ReadObjWithBuffer(char*) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libRIO.so.5.26)
==19842== by 0x6B8B2DF: TFile::GetStreamerInfoList() (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libRIO.so.5.26)
==19842== by 0x6B88761: TFile::ReadStreamerInfo() (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libRIO.so.5.26)
==19842== by 0x6B8A855: TFile::Init(bool) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libRIO.so.5.26)
==19842== by 0x6B8B9C6: TFile::TFile(char const*, char const*, char const*, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libRIO.so.5.26)
==19842== by 0x6C4E11A: G__G__IO_107_0_22(G__value*, char const*, G__param*, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libRIO.so.5.26)
==19842== by 0x47EEBE6: Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libCint.so.5.26)
==19842== by 0x4899B3E: G__execute_call (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libCint.so.5.26)
==19842==
Warning in TClass::TClass: no dictionary for class BxEvent is available
[…]
root [1] Int_t evnum, n_clusters, npmts;
root [2] bxtree->SetMakeClass(1);
root [3] bxtree->SetBranchAddress (“evnum”, &evnum);
root [4] bxtree->GetEntry (200)
==19842== Invalid write of size 4
==19842== at 0x6B769D4: TBufferFile::ReadInt(int&) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libRIO.so.5.26)
==19842== by 0x89D289F: TBranchElement::ReadLeaves(TBuffer&) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libTree.so.5.26)
==19842== by 0x89C3381: TBranch::GetEntry(long long, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libTree.so.5.26)
==19842== by 0x89C9C04: TBranchElement::GetEntry(long long, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libTree.so.5.26)
==19842== by 0x89C9A94: TBranchElement::GetEntry(long long, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libTree.so.5.26)
==19842== by 0x89C9A94: TBranchElement::GetEntry(long long, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libTree.so.5.26)
==19842== by 0x8A128D3: TTree::GetEntry(long long, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libTree.so.5.26)
==19842== by 0x8A41DC1: G__G__Tree_109_0_69(G__value*, char const*, G__param*, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libTree.so.5.26)
==19842== by 0x47EEBE6: Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libCint.so.5.26)
==19842== by 0x4899B3E: G__execute_call (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libCint.so.5.26)
==19842== by 0x489B569: G__call_cppfunc (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libCint.so.5.26)
==19842== by 0x4876262: G__interpret_func (in /mnt/scratch/Software/Cern/root_v5.26.00/lib/libCint.so.5.26)
==19842== Address 0x0 is not stack’d, malloc’d or (recently) free’d
==19842==
[/quote]

The first log came from a production machine with debian old-stable, the second from my laptop with debian sid.

Thanks

And what is the result of bxtree->Print()? I suspect that evnum is kept in a vector of object rather than an object … in which case you need to pass the address of an array of int rather than the address of a single int …

Cheers,
Philippe.

[quote]root [1] bxtree->Print()


*Tree :bxtree : Borexino Events Tree (generated by echidna) *
*Entries : 51799 : Total = 505035351 bytes File Size = 141053219 *

  •    :          : Tree compression factor =   3.58                       *
    

*Branch :events *
*Entries : 51799 : BranchElement (see below) *

*Br 0 :fUniqueID : *
*Entries : 51799 : Total Size= 208002 bytes File Size = 1344 *
*Baskets : 4 : Basket Size= 64000 bytes Compression= 154.40 *

*Br 1 :fBits : *
*Entries : 51799 : Total Size= 415710 bytes File Size = 64829 *
*Baskets : 10 : Basket Size= 64000 bytes Compression= 6.40 *

*Br 2 :run : *
*Entries : 51799 : Total Size= 207954 bytes File Size = 1444 *
*Baskets : 4 : Basket Size= 64000 bytes Compression= 143.69 *

*Br 3 :evnum : *
*Entries : 51799 : Total Size= 207970 bytes File Size = 72662 *
*Baskets : 4 : Basket Size= 64000 bytes Compression= 2.86 *
[/quote]

Hi,

Quite odd ! Can you send your data file so that I can reproduce this issue?

Thanks,
Philippe.

Sure, where can i send to you? I have several root files for my data but none very small (>100MB).

Else you can download them by http, just tell me a source IP.

Thanks

Hi,

In your file there is 2 set of branches that tickles a deficiency in the SetMakeClass support when the address are not set. To work-around the problem simply disable those branches:TFile f("Run011002_c12.root"); bxtree->SetMakeClass(1); bxtree->SetBranchStatus("laben.*",0) bxtree->SetBranchStatus("muon.*",0) Int_t evnum, n_clusters, npmts; bxtree->SetBranchAddress ("evnum", &evnum); bxtree->GetEntry(200)

Cheers,
Philippe.

I have 2 branches but I did not create them on my own, they happen to be created. Do you know why? I thought it was a root stuff :neutral_face:

This is the code I use to create the file:file = TFile::Open (file_name().c_str (), "recreate"); tree = new TTree ("bxtree", "Borexino Events Tree (generated by bx_phys)"); tree->SetMaxTreeSize(Long64_t(0x2000000000ULL)); internal_ptr = new BxEvent; tree->Branch ("events", "BxEvent", &internal_ptr, 64000, 99); ... tree->Fill ()

I will try how to manage disabling the branches i do not need.

Thanks

[quote] Do you know why? I thought it was a root stuff [/quote]They are acutally sub-branches of you ‘events’ branch. They contains some of the data member of the class BxEvent.

Cheers,
Philippe.

Ok, so I can not avoid that.

If I create the following code:[code]TFile f = TFile::Open (“M4_merger/Run011002_c12.root”);
Int_t evnum, n_clusters, npmts[20];
TTree bxtree = (TTree)f->Get (“bxtree”);
bxtree->SetBranchStatus("
",0)
bxtree->SetMakeClass(1);
bxtree->SetBranchStatus(“evnum”,1)
bxtree->SetBranchAddress (“evnum”, &evnum);
bxtree->SetBranchStatus(“laben.n_clusters”,1)
bxtree->SetBranchAddress (“laben.n_clusters”, &n_clusters);
//bxtree->SetBranchStatus(“laben.clusters.npmts”,1);
//bxtree->SetBranchAddress (“laben.clusters.npmts”, &npmts);

for (int i = 2033; i < 2050; i++) { bxtree->GetEntry (i); cout << evnum << " " << n_clusters << " " << npmts[0] << endl; }
[/code]
It works untill I try uncomment the lines of npmts…

Could you post a small data file in a public space and also a small analysis script?

Rene

Hi,

If you are going to use anything in the laben data member you must set of the data members. At this point I strongly recommend that you simply set all the variables address. The simpliest way to do so is to use the result of MakeSelector (or MakeClass) … or better yet MakeProxy.

TFile *f = TFile::Open ("M4_merger/Run011002_c12.root"); bxtree->MakeSelector("bxsel"); // edit the Process member function in bxsel.C bxtree->Process("bxsel.C");

Cheers,
Philippe.