How to 2D vector of pointers in tree?

Hello all,
My root version is 5.22. I can’t shift to v5.24 as this is the version
suggested by the CMS community.
I am writitng a 2D vector of pointers as my tree variable. I writing it like this :


vector< std::vector<double> > *ptarr; ptarr = new std::vector< std::vector<double> >; myTree->Branch("pt","vector< std::vector<double> >",&ptarr); for(int i = 0; i<somevalue;i++) ptarr[i]->push_back(pt_photon[i]);

but when I compile this I get the error as :

error: base operand of ->' has non-pointer typestd::vector<std::vector<double, std::allocator >, std::allocator<std::vector<double, std::allocator > > >’

Could someone tell me where I am going wrong?
Apologies if I am asking c++ specific question.

Thanks & Regards,
Shilpi Jain

[quote]if I am asking c++ specific question. [/quote]It is :slight_smile:

ptarr[i] is illegal because if ‘assumes’ that ptarr is actually pointing to a C style array of vector<vector > (because ptarr is a pointer).

Instead use code[i].push_back(pt_photon[i]);[/code]

Also, is it your intend (it is what the code does) to fill only the ‘diagonal’ of the 2d ‘array’?

Also the code snippet your provided is lacking the ‘filling’ of the vector of vector. I.e. something like ptarr->push_back( vector() );



Thanks much for your reply. It worked :slight_smile:
Now I am facing other problem of dictionary generation on which I have seen
many posts in this forum.

I am getting error as :

I have postAnalyzer.C and .h got by doing MakeClass.
Then I make myClass.C &.h which inherits from postAnalyzer class.
I have main.C which is something like :

[code]#include “myClass.C”
#include “TROOT.h”

int main(){
gROOT->ProcessLine("#include ");
myClass m(21);

I compile this main.C and then do ./main.exe on my lxplus machine.

Following the posts, I add the following lines in myClass.C (after I have included other header files)

#include <vector> #ifdef __MAKECINT__ #pragma link C++ class vector< vector<double> >+; #endif

Still I am getting the same error!

What is it that I am missing?

Thanks & Regards,


Regarding the problem I mentioned in my previous post :

After including those lines in myClass.C which I mentioned in the previous post,
I did :
.L myClass.C+
and it compiled successfully.
Then I try running this code but this time I am getting the problem of segmentation violation because of the line :

Instead if I do :

then I do not get any problem with segmentation violation!

I could have used :

but that will not solve my problem.

I am doing something like :

for(int i=0; i<noOfPhotons;i++) { if(some condition for that particular photon) { (*ptarr)[i].push_back(pt of that photon); } }

So I want pt of 0th photon to go inside ptarr[0][0], pt of 1st photon to go inside ptarr[1][0]
If supposing 0th photon doesn’t satify that condition then still I want pt of 1st photon to go inside ptarr[1][0] and so on

Could you please suggest me something?

Thanks & Regards,

Hello again,

Sorry for the commotion.
I could get rid of that segmentation violation by adding a line (for every event ) in my code :

and then do:

And my code ran without any problems.

But now if I try drawing that 2D vector by doing :

TH1F *p = new TH1F("p","histo",100,0.0,1.0); myTree->Draw("pt[0][0]>>p");

I get something like :
Error in TObject::ReadValue: Invalid data address: result will be wrong

What is it again that I am missing?

Thanks & Regards,


You may have forgotten to call mytree->ResetBranchAddresses() when the pointer the branch point to went out of scope.