Adding an automatic cast of a cut to a string/char*

Hi,

I have a python class, derived from ROOT.TCut, that implements a more pythonic cut.
This works great for TTree.Draw, since it can accept a [quote]const TCut& selection[/quote] argument.
But it doesn’t work for TTree.Scan, since it only accept a [quote]const char* selection = “”[/quote] argument.
The simplest workaround is to implicitly convert the object to a string when calling TTree.Scan. But I was hoping not to have to mess around with such programming concerns when I’m playing around with cuts interactively and thinking about the actual analysis.
So I’m looking for a way to tell PyROOT that objects of my class should be converted to a string, either whenever (C++ style), or specifically in TTree.Scan. Then I’ll set that up in my ipython start up and never think about it again.

Any ideas on how this may be accomplished, if at all?

thanks,
Amnon Harel

Python has stronger typing than C++, so there are fewer automatic casts like that TCut->str. There are only a few instances like between integers and floats. The only solutions I can think of would be:

  • get someone to implement a TTree::Scan that accepts a TCut for the cut string.
  • make your derived-from-TCut-class a C++ class that can automatically cast to const char * instead of a python class

Maybe there are other ways, but that’s all I’ve got.

Jean-François

Hi,

yes, as there are no implicit conversion (and even in C++ there is only 1 implicit conversion allowed, so that does not always help), you have to be explicit in Python. In the case of TCut, the “operator const char*()” method is mapped to its Python equivalent: “str”. You can do the same for the more pythonistic TCut you wrote and then use str(pyobj) when using it with Scan.

There is also: http://www.rootpy.org/reference/generated/rootpy.tree.Cut.html.

Cheers,
Wim