Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: special case for reading std::string from a TDirectory #1160

Merged
merged 4 commits into from
Mar 5, 2024

Conversation

jpivarski
Copy link
Member

@jpivarski jpivarski commented Mar 4, 2024

Fixes #1159. (That's a discussion.)

What do you think, @chrisburr? Is std::string truly exceptional, such that a special case is warranted, or is this the tip of an iceberg that should be handled in a more systematic way?

(If it's to be handled in a systematic way, I don't know when or by whom.)

@jpivarski
Copy link
Member Author

Actually, this second commit is less invasive, and it's setting up less of a precedent for special cases. The serialization of the std::string is just like a TString (not TObjString), so maybe we can put it in the lookup table under both names.

The downside of this approach is that it returns the data as an instance of a class named Model_TString. Although this is a subclass of str and you can easily get at the data, it could be misleading to call this "TString", given that it's a std::string in C++.

@jpivarski
Copy link
Member Author

Can we include your file as a test? Is https://cburr.web.cern.ch/string-example.root a stable address?

@chrisburr
Copy link
Member

Thank you for following up with this so quickly. I've tried this out and it works exactly the way I'd expect:

In [1]: import uproot
   ...: import json
   ...: uproot.classes["string"] = uproot.classes["TString"]
   ...:
   ...: fn = "https://cburr.web.cern.ch/string-example.root"
   ...:
   ...: f2 = uproot.open(fn)
   ...: json.loads(f2["FileSummaryRecord"])
   ...:
Out[1]:
{'LumiCounter.eventsByRun': {'counts': {},
  'empty': True,
  'type': 'LumiEventCounter'},
 'guid': '5FE9437E-D958-11EE-AB88-3CECEF1070AC'}

I suspect string is exceptional and at a glance there seems to be dedicated code for serialising it:

https://github.com/root-project/root/blob/e7d7ee19abd630b0df3a16d84d61bb59e2e9b357/core/meta/src/TClass.cxx#L648-L724

Can we include your file as a test? Is https://cburr.web.cern.ch/string-example.root a stable address?

It can absolutely be included as a test though I'd suggest putting it somewhere else if you can as that URL likely won't last forever. It's only ~5KB.

@jpivarski
Copy link
Member Author

Well, if it's a special case in ROOT, it can be a special case in Uproot.

I'm still not sure if I like the

            if self._fClassName == "string":
                return cursor.string(chunk, context)

or the

uproot.classes["string"] = Model_TString

better. The first option is "more hacky," or at least, more special of a special case, and the second is natural but has the downside of declaring that a std::vector is a TString.

Actually, that made my decision for me: we can't have Uproot insinuating that a std::vector is a TString. That kind of (deliberate!) confusion might mess with someone's debugging someday. At least the explicit if statement calls itself out as an obvious wart.

@jpivarski jpivarski requested a review from ioanaif March 4, 2024 22:58
@jpivarski jpivarski merged commit 83726f6 into main Mar 5, 2024
21 checks passed
@jpivarski jpivarski deleted the jpivarski/maybe-special-case-for-string branch March 5, 2024 15:34
jpivarski added a commit that referenced this pull request Mar 21, 2024
ioanaif added a commit that referenced this pull request Mar 22, 2024
* feat: add support for reading free floating std::vector

* style: pre-commit fixes

* Update test file name and ROOT import

* style: pre-commit fixes

* pytest import

* move the check for STL classes earlier and ensure at least one type matches in the regex

* style: pre-commit fixes

* changed my mind; std::vector<T> might need T to be defined by streamers

* update the fix for #1160 to be like this one, and remove the double-check for std::string

* forgot to reinstate the 'cls is None and'

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jim Pivarski <jpivarski@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants