Getopt module

I need to parse a command-line like string (something like “-filename run_0001.root -n 12345”).
As far as I know the TString class does not offer any functionality like that, so I was wondering if it is possible, via PyROOT, to use the getopt python module.
Thanks in advance


yes. Note that if you want to ignore your CLI options, you can set:

import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True


Thanks for your reply, but it’s not clear to me how to implement the idea.
As I understand, to bring an object back to the ROOT macro I need to use the Eval method of TPython, but to do the parsing I need more code than what can be kept in a single statement (I guess I need at least a for loop on all the options)…
Is it possible to bring an object from python to ROOT from within a python macro?


yes, Eval() can evaluate an expression that is a ROOT object. The result needs to be cast (TRealType*)(void*) to the real type. You can call TPython::Eval() from gROOT.ProcessLine in python.

Example session:

[code]root [0] TPython::Prompt()

v = ROOT.TString(“test”)

ROOT.gROOT.ProcessLine( ‘TString* v = (TString*)(void*)TPython::Eval(“v”)’ )

root [1] cout << *v << endl;
root [2] .q[/code]

I see… And is it possible to do this also from inside a compiled macro? I tried with

#include "TSystem.h"
#include "TCanvas.h"
#include "TPython.h"
void test_pyroot()
  TCanvas *c = (void*)TPython::Eval("ROOT.TCanvas()");

It works fine if I just execute “root test_pyroot.C”, but if I add the + I get

Processing test_pyroot.C+...
Info in <TUnixSystem::ACLiC>: creating shared library /home/rnicolini/test/./
/home/rnicolini/test/./test_pyroot.C: In function ‘void test_pyroot()’:
/home/rnicolini/test/./test_pyroot.C:7: error: invalid conversion from ‘void*’ to ‘TCanvas*’
/home/rnicolini/test/./test_pyroot.C:7: warning: unused variable ‘c’
g++: /home/rnicolini/test/./test_pyroot_C_ACLiC_dict.o: No such file or directory
Error in <ACLiC>: Compilation failed!
Error: Function test_pyroot() is not defined in current scope  :0:
*** Interpreter error recovered ***

I’m asking because I’m basically trying to pass command-line options to a TSelector, which of course needs to be compiled.


should be, as per above, the full syntax is (TRealType*)(void*). The compiler is complaining about the missing second cast (to a TCanvas* in your example).


Ops, I didn’t notice the second cast, sorry.
Thank you very much for the help!

you do not need necessarily need python for getopt. It exists in almost every language, including C:

I know, thanks, but the python version is much more flexible; I actually eneded up using the argparse module instead.