Skip to content

fix handling of case where idx is negative#8174

Closed
will-cern wants to merge 2 commits intoroot-project:masterfrom
will-cern:argListAccess
Closed

fix handling of case where idx is negative#8174
will-cern wants to merge 2 commits intoroot-project:masterfrom
will-cern:argListAccess

Conversation

@will-cern
Copy link
Copy Markdown
Contributor

@will-cern will-cern commented May 13, 2021

I noticed that RooArgList doesn't handle the case where idx is negative. But also thought it would be nice to add support for use of negative numbers to access the list from the back, so it behaves like a python list in this respect

I noticed that there is nice operator[] behavior in 6.24 (uses at and throws exception) but this isn't in master???

@phsft-bot
Copy link
Copy Markdown

Can one of the admins verify this patch?

@guitargeek
Copy link
Copy Markdown
Contributor

Hi Will!

We try to bring the behavior of the RooAbsCollections closer to the STL containers like std::vector. This means that operator[] should simply access an element as quickly as possible without any bounds checking for no overhead. That's why it changed from 6.24 to master.

Only the at() member should do the bounds checking. I still have some work to do here because its behavior is inconsistent with std::vector (it returns a nullptr if the bounds check fails).

So bringing the element-accessing behavior closer to Python behavior would unfortunately bring us further away from the STL behavior, and I would advise against the suggested change.

However, I would agree that on the pyROOT side, the RooAbsCollections should behave more pythonic and we should absolutely implement a pythonization to support element access from the back with negative indices!

Is that a fair compromise? Did you intend to use the negative indices in C++?

@will-cern
Copy link
Copy Markdown
Contributor Author

This sounds good to me, yes this is purely for the pythonization, if there's a dedicated place where that is coded up then yes supporting it there (with an exception if trying to access invalid index) would be great! Atm it's very user un-friendly that ROOT crashes if we access an index out of range in pyROOT

@guitargeek
Copy link
Copy Markdown
Contributor

Okay, very good! Just FYI, the pythonizations are implemented in these "mirror classes" that I declared here:
https://github.com/root-project/root/tree/21fbd344e60a849622991a92d3230681e257c3b9/bindings/pyroot/pythonizations/python/ROOT/pythonization/_roofit

Every function that is defined in a mirror class is then bound to the actual RooFit class, with the original cppyy function name changed to have an underscore prefix.

I have already a PR open with some more pythonizations of RooAbsCollection: #8179. I can just add one more commit with the element access.

But in general, I'm not working too much on these pythonizations because we will most likely get a Google summer of code student for that project! So if you have any further nice ideas for RooFit pythonizations, feel free to write them down in this issue and I will what can be implemented with the student.

@guitargeek
Copy link
Copy Markdown
Contributor

Thanks again for the idea! It has been implemented in #8179, so I will close this PR.

Please let us know in #7217 if you have further ideas for pythonizations!

@guitargeek guitargeek closed this May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants