TGFileDialog in PyROOT?

Hi All,

I would like to use include a TGFileDialog in the minimal GUI i’ve been developing, based on what is in tutorials/pyroot/gui_ex.py.

I’ve gotten as far as adding a button that pops up the file choose dialog, but I’m getting crashes after that point.

running the script within ipython, I get the following results:

In [1]: 
In [2]: /opt/cern/root/lib/ROOT.py:436: RuntimeWarning: creating converter for unknown type "const char**"
  attr = _root.LookupRootEntity( name )
/home/fisher/analysis/Calibrations.py

/home/fisher/analysis/Calibrations.py is the file I chose from the file open dialog.

Similarly, if I uncomment the line

	fi.fIniDir = "."

the code segfaults.

What am I doing wrong?

Thanks,
Brian
gui_ex_filedialog.py (2.12 KB)

Hi,

the message is for the public data member “fFileTypes” of TGFileInfo. That should be okay as long as you don’t try to set it (you’ll get an exception at that point), you can ignore the warning (or suppress it using a filter on module ‘warnings’).

The crash is b/c the assignment assumes there’s memory available on fIniDir, but it’s in fact a null pointer. Probably easiest is to allocate some memory on the C++ side. Something like:ROOT.gROOT.ProcessLine("fi = TGFileInfo();") ini_dir = '.' ROOT.gROOT.ProcessLine("fi.fIniDir = (char*)malloc(%d);" % len(ini_dir)+1) ROOT.fi.fIniDir = ini_dir'
Working with low-level C++ types is always a little messy, I’m afraid. It’s something that could be wrapped, though.

Cheers,
Wim

Ah cool.

One more thing: Who owns the TGFileInfo? If I’m calling and creating the TGFileDialog within a python function, and it goes out of scope once the function exits, do I need to somehow free the memory created with the

ROOT.gROOT.ProcessLine("fi.fIniDir = (char*)malloc(%d);" % len(ini_dir)+1)

line?

Hi,

with ProcessLine as written, it exists as a ROOT TGlobal object and won’t go away. The Python side is just a proxy to the global. If memory management is important (e.g. if you create lots of these info objects), then it’s best to wrap their creation and destruction in a C++ helper object. One that can take an std::string for example.

Cheers,
Wim