TTree::Branch() with std::vector

Hi,

I’m using gcc 3.4.6 with ROOT 5.18.00d. I’m trying to create a branch to hold a std::vector, following this tutorial:
root.cern.ch/root/html/tutorials … tor.C.html

However the tutorial macro doesn’t compile for me:

$ root -l -b -q hvector.C++
root [0]
Processing hvector.C++…
Info in TUnixSystem::ACLiC: creating shared library /nfs/home/efeng/./hvector_C.so
Note: Link requested for already precompiled class vector<float,allocator > (ignore this message) :0:
In file included from /nfs/home/efeng/./fileiA66ET.h:32,
from /nfs/home/efeng/./fileiA66ET.cxx:16:
/nfs/home/efeng/./hvector.C: In function void write()': /nfs/home/efeng/./hvector.C:39: error: invalid conversion fromstd::vector<float, std::allocator >’ to Int_t' /nfs/home/efeng/./hvector.C:39: error: initializing argument 2 ofvirtual Int_t TTree::Branch(const char, Int_t, Int_t)’

It appears that it cannot find a suitable implementation of TTree::Branch() that takes these arguments. What should I do?

Thanks,
Eric

Hi,

Upgrade to v5.22. The tutorials on the web site pertains to the latest release.

Alternatively in v5.18 you will need to use something like: std::vector<float> *myp = new std::vector<float>(); .... tree->Branch("mybranch","vector<float>",&myp);

Cheers,
Philippe.

Hi Philippe,

Thanks but I’m stuck to this ROOT version which is associated to a software release for the ATLAS experiment. Is there an example of the correct syntax that I can use in this version?

I imagine something like:

std::vector *vecPt = new std::vector;
mytree->Branch(“Pt”, &vecPt);

However, while this compiles I get an empty branch.

Thanks,
Eric

[quote=“pcanal”]Hi,

Upgrade to v5.22. The tutorials on the web site pertains to the latest release.

Alternatively in v5.18 you will need to use something like: std::vector<float> *myp = new std::vector<float>(); .... tree->Branch("mybranch","vector<float>",&myp);

Cheers,
Philippe.[/quote]

[quote]I imagine something like:
std::vector *vecPt = new std::vector;
mytree->Branch(“Pt”, &vecPt); [/quote]Yes this should also work.

It should have worked, assuming that you filled the vector you created with the call to operator new.
Cheers,
Philippe.,

[quote=“pcanal”][quote]I imagine something like:
std::vector *vecPt = new std::vector;
mytree->Branch(“Pt”, &vecPt); [/quote]Yes this should also work.

It should have worked, assuming that you filled the vector you created with the call to operator new.
Cheers,
Philippe.,[/quote]

Hi Philippe,

I see now (partly) what the problem is. My branches aren’t empty, but I’m having some problems reading them. I’ve attached an example ROOT file.

If I just open a TBrowser, I can draw all the branches fine.

However, I need to create a macro. So I call:
CollectionTree->MakeClass(“analysis”)

Then edit analysis.h:
#include
using namespace std;

Then compile class and load tree:
.L analysis.C++
analysis A
tree = A.fChain

Finally I can scan any branches with primitives, and vectors of ints. But strangely I cannot scan vector of doubles:

tree->Scan(“NPhot”) //integer – works
tree->Scan(“PhotNTrack”) //vector – works
tree->Scan(“PhotPt”) //vector – FAILS

Do you have any idea why this might be? My ROOTSYS is:
root/5.18.00d/slc4_ia32_gcc34/

The machine is actually 64-bit, but have specified to run in 32-bit compatibility mode (standard for ATLAS software).

Thanks,
Eric
CBNT.root (19.5 KB)

Hi,

I can not reproduce this problem. Does the scanning works if you do not use the analysis.C?

Cheers,
Philippe

[quote=“pcanal”]Hi,

I can not reproduce this problem. Does the scanning works if you do not use the analysis.C?

Cheers,
Philippe[/quote]

Hi Phillippe,

Sorry for the late reply. No, the scanning does not work directly. I will post again if I encounter the problem later.

Thanks for your help,
Eric

Hi Philippe,

I’ve found a solution. I can scan the branches fine if I first do either:

or

I understand the first command loads the vector library. My understanding of the second one is only nominal, namely that Cintex allows CINT to understand arbitrary classes with a Reflex dictionary:
http://lcgapp.cern.ch/project/cls/workbook/cintex.html
http://root.cern.ch/drupal/content/reflex

I assume method 2 works because there are reflex dictionaries for STL classes?

Thanks,
Eric

This is plausible. Alternatively, it is possible that Cintex is inducing the loading of the vector library via the autoloader.

Cheers,
Philippe.