Vector in Branch

The TTree documentation describes various ways to create a Branch, in particular:

So, I am trying to create a branch:

    std::vector<float> wave_model(64);
        ptree->Branch("wave_model",&wave_model);

The code compiles fine, however when I try to run I get this:

What am I doing wrong?

p.s. I am running Version 5.28/00.

1 Like

Hi,

You need to load the dictionary for vector. Add (literally):gROOT->ProcessLine("#include <vector>"); to your code before attempting to create the branches.

Cheers,
Philippe.

Aha, that worked, thanks Philippe! My problems, however, are continuing. Now I am loading the resulting .root file and am trying to set the addresses for the branches, and here’s the error that I get (I also include the tree description):

root [3] ptree->Print()
******************************************************************************
*Tree    :ptree     : event data                                             *
*Entries :    10000 : Total =        24619740 bytes  File  Size =    3623243 *
*        :          : Tree compression factor =   6.80                       *
******************************************************************************
*Br    0 :pulse     : adcoffset[1024]/s                                      *
*Entries :    10000 : Total  Size=   20542345 bytes  File Size  =     993299 *
*Baskets :      667 : Basket Size=      32000 bytes  Compression=  20.67     *
*............................................................................*
*Br    1 :wave_model : vector<float>                                         *
*Entries :    10000 : Total  Size=    2709540 bytes  File Size  =    2024254 *
*Baskets :       87 : Basket Size=      32000 bytes  Compression=   1.34     *
*............................................................................*
*Br    2 :event     : samples/i:rate/f:time/l:time_abs/D:cn/i:pattern/i:     *
*         | counter/i:min_i/i:min/i:E/F:Ec/F:Ecp/f:Ped/F:PSD/F:PSDsm/F:      *
*         | Chisquare_v0/F:Chisquare_v1/F:Chisquare_v2/F:Chisquare_v3/F:     *
*         | Chisquare_v5/F:Chisquare_v6/F:Chisquare_v7/F:Chisquare_v8/F:id/i *
*Entries :    10000 : Total  Size=    1045412 bytes  File Size  =     592936 *
*Baskets :       33 : Basket Size=      32000 bytes  Compression=   1.76     *
*............................................................................*
*Br    3 :tlv_header : beamEnergy/D:beamCurrent/D:gate/i:inputCounts/i:      *
*         | outputCounts/i:prescaleCounts/i                                  *
*Entries :    10000 : Total  Size=     321906 bytes  File Size  =       4711 *
*Baskets :       11 : Basket Size=      32000 bytes  Compression=  68.11     *
*............................................................................*
root [4]   std::vector<float> wave_model;               
root [5]   ptree->SetBranchAddress("wave_model",&wave_model);
Error in <TTree::SetBranchAddress>: The address for "wave_model" should be the address of a pointer!

Any ideas on this one?

Ok, I got it, it’s expecting a pointer to a pointer, i.e.

ptree->SetBranchAddress("wave_model",&(&wave_model));

seems to work…

[quote]ptree->SetBranchAddress(“wave_model”,&(&wave_model));
seems to work…[/quote]Unfortunately it only appears to work. This call pass the address of a transient pointer (i.e. it last only as long as the statement)) but the function keep a copy of this value … resulting in the TTree essentially using random memory (because this address will be reused).
You need:std::vector<float> * wave_model = 0; ptree->SetBranchAddress("wave_model",&wave_model);

Cheers,
Philippe.

Yep, in fact that’s what I ended up using. :slight_smile:

Thanks again!

Hi
I am having oroblem while reading vector objects from branches in tree . My branch contains data of type TObjArray. I am able to read only the events. I wish to read other branches and calculate the invariant mass. How can i do that.

Cheers
Kiran

You might need to load the library where the filled objects are defined? Or compile together with that piece of code

Hi

I am having Error : TTree::SetBranchAddress: The pointer type given (TLorentzVector) does not correspond to the class needed (TObjArray) by the branch: reco_6gvect while running the code . How to sort it out?

Thanks.