How to read pointer ENTRIES from TTree into an array

Hi,

I have a tree with data that looks something like that:

root [2] t1->Show(0)
======> EVENT:0
BATStripIdMod1layer1 = (vector)0x9d83a80
BATStripIdMod1layer0 = (vector
)0x9d84400
BATStripIdMod2layer1 = (vector)0x9d84540
BATStripIdMod2layer0 = (vector
)0xa21add0
BATStripIdMod3layer1 = (vector)0xa21af58
BATStripIdMod3layer0 = (vector
)0xa21b0f8
BATStripIdMod6layer1 = (vector)0xa21b2b8
BATStripIdMod6layer0 = (vector
)0x9d859d0
BATStripQMod1layer1 = (vector)0xa37b040
BATStripQMod1layer0 = (vector
)0xa21b478
BATStripQMod2layer1 = (vector)0xa37b9a8
BATStripQMod2layer0 = (vector
)0xa3855e8
BATStripQMod3layer1 = (vector)0xa385768
BATStripQMod3layer0 = (vector
)0xa385900
BATStripQMod6layer1 = (vector)0xa37bae0
BATStripQMod6layer0 = (vector
)0xa37bdd8
root [3] t1->Scan(“BATStripIdMod1layer0”)


  • Row * Instance * BATStripI *

  •    0 *        0 *       305 *
    
  •    0 *        1 *       306 *
    
  •    0 *        2 *       307 *
    
  •    0 *        3 *       308 *
    
  •    0 *        4 *       309 *
    
  •    0 *        5 *       310 *
    
  •    0 *        6 *       311 *
    
  •    0 *        7 *       312 *
    
  •    0 *        8 *       313 *
    
  •    0 *        9 *       314 *
    
  •    0 *       10 *       315 *
    
  •    0 *       11 *       316 *
    
  •    0 *       12 *       317 *
    
  •    0 *       13 *       318 *
    
  •    1 *        0 *       240 *
    
  •    1 *        1 *       241 *
    
  •    1 *        2 *       242 *
    
  •    1 *        3 *       243 *
    
  •    1 *        4 *       244 *
    
  •    2 *        0 *       307 *
    
  •    2 *        1 *       308 *
    
  •    2 *        2 *       309 *
    
  •    2 *        3 *       310 *
    
  •    2 *        4 *       311 *
    
  •    3 *        0 *       283 *
    

Type to continue or q to quit ==> q


(Long64_t)25

I’m trying to read the values of each entry into an array of fixed length e.g Int_t Charge[650] but while using the standard branch-reading process:

root [4] Int_t charge[650]
root [5] for(int i=0;i<650;i++)charge[i]=0;
root [7] t1->SetBranchAddress(“BATStripIdMod1layer0”,&charge)
root [8] t1->GetEntry(0)
(Int_t)2112
root [9] charge[0]
(Int_t)165168128
root [10] charge[i]
(Int_t)10257496

I get those strange numbers in the array instead of what I expected (root[3]t1->Scan)…
I think the problems lies in the form of the branch data (vector*) but i cannot figure out how to change my commands to make it work…

I would be thankful for any help…

Hi,

You will need to call t1->SetMakeClass(1); or pass the address of a pointer to a vector to SetBranchAddress.

Philippe

Hi Philippe,

thank you for your time…
Do i have to use the SetMakeClass command before the SetBranchAddress or replace it? I’m sorry, but I can’t find out what this command does…

Could you please show me an example of the syntax for the second sollution cause i’m not so familiar with C commands. Should i create 2 vectors?

thanks

Kon

Hi,

The SetMakeClass call, must be issued before calling SetBranchAddres ; it tells the TTree that you intend to access the data content directly using ints and floats and do not plan to use the original object hierarchy.

As far as using objects, you simply would do #include <vector> ... vector<int> * mod1layer0_ptr = 0; t1->SetBranchAddress("BATStripIdMod1layer0",& mod1layer0_ptr);

Cheers,
Philippe.

Thanks for the help Philippe…

Following your instructions i end up with a pointer to a vector (mod1layer0_ptr). However the values in the vector cells are not the values that are held in the branch.
I suppose I have to associate this new ponter to a new vector in some way to get the real values…???

root [0] f1=new TFile(“TB09_MM3549_BAT2802.root”)
(class TFile*)0x3973428
root [1] t1=(TTree )f1->Get(“mmbatTree”)
(class TTree
)0x3bcba80
root [2] vector * mod1layer0_ptr = 0;
root [3] t1->SetBranchAddress(“BATStripIdMod1layer0”,& mod1layer0_ptr);
root [4] mod1layer0_ptr
(vector*)0x3ca3f88
root [5] mod1layer0_ptr[0]
(class vector)63586184
root [6] mod1layer0_ptr[1]
(class vector)63586200
root [7] mod1layer0_ptr[100]
(class vector)63587784
root [8] t1->GetEntry(0)
(Int_t)2112
root [9] mod1layer0_ptr[0]
(class vector)63586184
root [10] mod1layer0_ptr[100]
(class vector)63587784

root [4] mod1layer0_ptr (vector<int>*)0x3ca3f88 root [5] mod1layer0_ptr[0] I think you meant to use:root [4] mod1layer0_ptr (vector<int>*)0x3ca3f88 root [5] mod1layer0_ptr->at(0); // the variable is a _pointer_ to a vector!

Cheers,
Philippe.

I didn’t know i had to use this command to get the value from the pointer…
It works fine now!

thanks a lot Philippe

Cheers…

hi again

Although the previous instructions really work , while trying to use them in a loop root throws an exception error.

Cheking it step by step i found out that the exception is thrown when the vector cell the pointer points to is null…

root [3] strip_ptr->at(0)
(int)305
root [4] strip_ptr->at(13)
(int)318
root [5] strip_ptr->at(14)
Exception: invalid vector subscript
Error: Symbol G__exception is not defined in current scope (tmpfile)(1)
Error: type G__exception not defined FILE:(tmpfile) LINE:1
(int)0
*** Interpreter error recovered ***

In this case the 14th cell of the vector is null as I’m reading only the first event

root [3] t1->Scan(“BATStripIdMod1layer0”)


  • Row * Instance * BATStripI *

  •    0 *        0 *       305 *
    
  •    0 *        1 *       306 *
    
  •    0 *        2 *       307 *
    
  •    0 *        3 *       308 *
    
  •    0 *        4 *       309 *
    
  •    0 *        5 *       310 *
    
  •    0 *        6 *       311 *
    
  •    0 *        7 *       312 *
    
  •    0 *        8 *       313 *
    
  •    0 *        9 *       314 *
    
  •    0 *       10 *       315 *
    
  •    0 *       11 *       316 *
    
  •    0 *       12 *       317 *
    
  •    0 *       13 *       318 * //end of the first event
    
  •    1 *        0 *       240 *
    
  •    1 *        1 *       241 *
    
  •    1 *        2 *       242 *
    
  •    1 *        3 *       243 *
    
  •    1 *        4 *       244 *
    
  •    2 *        0 *       307 *
    
  •    2 *        1 *       308 *
    
  •    2 *        2 *       309 *
    
  •    2 *        3 *       310 *
    
  •    2 *        4 *       311 *
    
  •    3 *        0 *       283 *
    

Type to continue or q to quit ==>

Is there any way i can manage this exception in my script?

Thanks,

kostas

[quote]In this case the 14th cell of the vector is null as I’m reading only the first event [/quote]Actually it is not null, it does not exist! Use:root [5] if (strip_ptr->size() > 14) strip_ptr->at(14);(You may want to re-read your favorite primer/textbook on how to use stl containers).

Cheers,
Philippe.

thanks Philippe

this was really helpfull…