Accessing vector<vector<float> > in PyROOT

Hi all,

I’m trying to read a ROOT file containing 2d vectors. By scavenging the forum I found that if I added the following it should work:

in *.py:
ROOT.gROOT.ProcessLine(’.L Loader.C+’)

in Loader.C:
#include
#pragma link C++ class vector<vector >+;
#pragma link C++ class vector<vector >+;
#pragma link C++ class vector<vector >+;
#pragma link C++ class vector<vector >+;

Locally
When I run the code on my mac (code attached) it now works, but not first, and its not clear to me what changed that…

LxPlus
Next I tried to run it on lxplus (SLC5, ROOT 5.26.00b) resulting in:
mjoergen@lxplus305 ~> python dedxplot.py user10.MortenDamJoergensen.user10.ChristianOhm.mc09_7TeV.106417.Pythia_R-Hadron0_gluino_450GeV.100404.15.6.3.12.reco.SYNTr98_CPH2010_SMP_filtered.15.6.8.4/user10.MortenDamJoergensen.user10.ChristianOhm.mc09_7TeV.106417.Pythia_R-Hadron0_gluino_450GeV.100404.15.6.3.12.reco.SYNTr98_CPH2010_SMP_filtered.15.6.8.4.AANT._00001.root
Traceback (most recent call last):
File “dedxplot.py”, line 47, in ?
main()
File “dedxplot.py”, line 4, in main
import ROOT
File “/afs/cern.ch/sw/lcg/app/releases/ROOT/5.26.00b/x86_64-slc5-gcc44-opt/root/lib/ROOT.py”, line 86, in ?
import libPyROOT as _root
ImportError: libpython2.5.so: cannot open shared object file: No such file or directory

Since that might be specific to lxplus, I tried another SLC5 machine we have at CERN:

Generic SLC5 + ATLAS 15.6.*
mjoergen@pcnbi2 Analysis> python dedxplot.py user10.MortenDamJoergensen.user10.ChristianOhm.mc09_7TeV.106417.Pythia_R-Hadron0_gluino_450GeV.100404.15.6.3.12.reco.SYNTr98_CPH2010_SMP_filtered.15.6.8.4.AANT._00001.root
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
Warning in TEnvRec::ChangeValue: duplicate entry <Library.vector=vector.dll> for level 0; ignored
TUnixSystem::SetDisplay:0: RuntimeWarning: DISPLAY not set, setting it to lxplus305.cern.ch:0.0
<class ‘ROOT.vector<vector >’>
add user10.MortenDamJoergensen.user10.ChristianOhm.mc09_7TeV.106417.Pythia_R-Hadron0_gluino_450GeV.100404.15.6.3.12.reco.SYNTr98_CPH2010_SMP_filtered.15.6.8.4.AANT._00001.root
TClass::TClass:0: RuntimeWarning: no dictionary for class AttributeListLayout is available
TClass::TClass:0: RuntimeWarning: no dictionary for class pair<string,string> is available
Traceback (most recent call last):
File “dedxplot.py”, line 45, in
main()
File “dedxplot.py”, line 29, in main
h2fDeDxTile.Fill(chain.Trk_p[trk]/1000, chain.Trk_dEdx[trk][8])
TypeError: ‘vector<vector >’ object is unindexable

My ultimate goal is the have it running in a grid job, a long way off it seems… :frowning:

What am I doing wrong, and is there some other way to handle this in pure python?
Loader.C (212 Bytes)
dedxplot.py (1.37 KB)

Hi,

when trying out “ROOT.gROOT.ProcessLine(’.L Loader.C+’)” I get a bundle of warnings. Do you as well?

Typically, the right approach is (picking just one class to show as an example):#ifdef __CINT__ #pragma link C++ class vector<vector<int>; #else template class std::vector<std::vector<int>; #endifThe point of the second part is to make sure that all parts of the template are instantiated and linked in. The macro’s prevent the CINT specific code from being seen by gcc, thus pre-empting the warnings.

Note that std::vector is its own can of worms …

As for lxplus missing libpython2.5.so, see e.g.:http://root.cern.ch/phpBB2/viewtopic.php?t=7191

HTH,
Wim

Thanks a lot Wim, both problems solved :slight_smile:

Cheers,
Morten

This thread was very helpful, so I’m posting some additional information:

I had vectors with user-defined types in my ROOT trees. To use them, you can just add them (and the files in which they are defined) to your Loader.C file like so:

#include <vector>
#include "Track_t.hh"
#include "Hit_t.hh"
#ifdef __CINT__
#pragma link C++ class Track_t+;
#pragma link C++ class Hit_t+;
#pragma link C++ class std::vector<Track_t>+;
#pragma link C++ class std::vector<Hit_t>+;
#endif

where Track_t, for example, is defined in Track_t.hh as

#ifndef TRACK_T_HH_
#define TRACK_T_HH_ 1
#include <stdlib.h>
#include <vector>
// Track structure
typedef struct {
  double pX;
  double pY;
  double pZ;
  int    ID;
  int   PDG;
}Track_t;
#endif /* TRACK_T_HH_ */

Anyway the point is you can treat user-defined types in the same way, and then load them into python with the line

in your .py file.