-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[treeplayer] Fix precision loss in TTree::Scan for 64-bit integers #22600
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
Merged
+78
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| #include <limits> | ||
| #include <string> | ||
| #include <vector> | ||
| #include <fstream> | ||
|
|
@@ -585,3 +586,78 @@ TEST(TTreeScan, TTreeGetBranchOfFriendTChain) | |
| throw std::runtime_error("Could not retrieve TTreePlayer from main tree!"); | ||
| } | ||
| } | ||
|
|
||
| // https://github.com/root-project/root/issues/7844 | ||
| // TTree::Scan() used to lose precision when printing 64-bit integer branches | ||
| // (e.g. ULong64_t): the "lld" column format, when given without an embedded | ||
| // column size (i.e. via "colsize=N col=lld"), was not recognized as a | ||
| // "long long" modifier because of an off-by-one in the length-modifier | ||
| // detection in TTreeFormula::PrintValue. As a consequence the value was | ||
| // evaluated and printed as a double, rounding anything above 2^53. | ||
| TEST(TTreeScan, ULong64Precision) | ||
| { | ||
| // The "long long" Scan/Draw column format is evaluated through `long double` | ||
| // (see TTreeFormula::PrintValue), so exact 64-bit integer output is only | ||
| // possible where `long double` has more mantissa bits than `double`. That is | ||
| // the case on x86-64 (80-bit, 64-bit mantissa) but not, e.g., on macOS ARM | ||
| // where `long double` is just a 64-bit `double` (53-bit mantissa). Skip the | ||
| // exactness check there, since the value genuinely cannot be represented. | ||
| if (std::numeric_limits<long double>::digits <= std::numeric_limits<double>::digits) | ||
| GTEST_SKIP() << "long double is not wider than double here; the 64-bit value " | ||
| "is genuinely unrepresentable and exactness cannot be checked"; | ||
|
|
||
|
Comment on lines
+605
to
+608
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should consider lifting this restriction by adding explicit support for |
||
| // 1617047019150033926 needs 61 bits, so it cannot be represented exactly | ||
| // by a double (53-bit mantissa). | ||
| constexpr ULong64_t value{1617047019150033926ULL}; | ||
|
|
||
| constexpr const char *treeName{"tree_7844"}; | ||
| ROOT::TestSupport::FileRaii fileGuard{"tree_7844.root"}; | ||
| { | ||
| std::unique_ptr<TFile> file{TFile::Open(fileGuard.GetPath().c_str(), "recreate")}; | ||
| auto tree = std::make_unique<TTree>(treeName, treeName); | ||
|
|
||
| ULong64_t x = value; | ||
| tree->Branch("x", &x, "x/l"); | ||
| tree->Fill(); | ||
| file->Write(); | ||
| } | ||
|
|
||
| std::unique_ptr<TFile> file{TFile::Open(fileGuard.GetPath().c_str())}; | ||
| auto tree = file->Get<TTree>(treeName); | ||
|
|
||
| auto *treePlayer = static_cast<TTreePlayer *>(tree->GetPlayer()); | ||
| ASSERT_TRUE(treePlayer) << "Could not retrieve TTreePlayer from main tree!"; | ||
|
|
||
| ROOT::TestSupport::FileRaii redirectFile{"tree_7844_regression_redirect.txt"}; | ||
| // SetScanFileName() stores the raw pointer, so keep the path string alive. | ||
| const std::string redirectPath{redirectFile.GetPath()}; | ||
| treePlayer->SetScanRedirect(true); | ||
| treePlayer->SetScanFileName(redirectPath.c_str()); | ||
|
|
||
| // Run a Scan with the given option string into the redirect file and return | ||
| // its contents. | ||
| auto scanToString = [&](const char *option) { | ||
| tree->Scan("x:x-1617047019150033925:x-1617047019150033000", "", option); | ||
| std::ifstream redirectStream(redirectPath.c_str()); | ||
| std::stringstream redirectOutput; | ||
| redirectOutput << redirectStream.rdbuf(); | ||
| return redirectOutput.str(); | ||
| }; | ||
|
|
||
| const static std::string expectedScanOut{ | ||
| R"Scan(************************************************************************************ | ||
| * Row * x * x-1617047019150033925 * x-1617047019150033000 * | ||
| ************************************************************************************ | ||
| * 0 * 1617047019150033926 * 1 * 926 * | ||
| ************************************************************************************ | ||
| )Scan"}; | ||
|
|
||
| // The "long long" column format must print the exact 64-bit value, as well as | ||
| // the exact result of integer arithmetic with large constants. The column size | ||
| // can either be given separately via "colsize=" (so the format passed to | ||
| // TTreeFormula::PrintValue is just "lld") or be embedded in the format token | ||
| // itself ("21lld"). Only the former triggered the off-by-one in the | ||
| // length-modifier detection, but both must yield the exact output. | ||
| EXPECT_EQ(scanToString("colsize=21 col=lld:lld:lld"), expectedScanOut); | ||
| EXPECT_EQ(scanToString("col=21lld:21lld:21lld"), expectedScanOut); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.