root-project / root

The official repository for ROOT: analyzing, storing and visualizing big data, scientifically
https://root.cern
Other
2.65k stars 1.26k forks source link

TTree::Scan() (and TTree::Draw()) issues with ULong64_t #7844

Open aaduszki opened 3 years ago

aaduszki commented 3 years ago

I use TTree objects with branches storing ULong64_t variables. When I use Tree::Scan() to printi these variables and perform mathematical operations, I encounter unexpected results.

I wrote a following program to demonstrate the issue:

#include <TROOT.h>
#include <TFile.h>
#include <TTree.h>
#include <TString.h>

#include <iostream>
#include <iomanip>

void write(ULong64_t x) {
  TFile f("f.root", "recreate");
  TTree t("t", "t");
  t.Branch("x", &x, "x/l");
  t.Fill();
  t.Write();
  f.Close();
  std::cout<<"Written to tree:   "<<x<<"\n";
}

void read(const ULong64_t x) {
  TFile f("f.root");
  TTree* t = (TTree*)(f.Get("t"));

  ULong64_t x1;
  t->SetBranchAddress("x", &x1);
  t->GetEntry(0);
  std::cout<<"Read from tree:    "<<x1<<" ("<<((x==x1)?"correct":"incorrect")<<")\n";

  std::cout<<"\n";
  TString formula = "x";
  std::cout<<"    TTree::Scan(\""<<formula<<"\");\n";
  std::cout<<"Expected output:   "<<x<<"\n";
  t->Scan(formula, "", "colsize=25 col=lld");

  std::cout<<"\n";
  ULong64_t y = x-1;
  formula = TString::Format("x-%lld", y);
  std::cout<<"    TTree::Scan(\""<<formula<<"\");\n";
  std::cout<<"Expected output:   "<<std::setw(19)<<x-y<<"\n";
  t->Scan(formula, "", "colsize=25 col=lld");

  std::cout<<"\n";
  y = x-926;
  formula = TString::Format("x-%lld", y);
  std::cout<<"    TTree::Scan(\""<<formula<<"\");\n";
  std::cout<<"Expected output:   "<<std::setw(19)<<x-y<<"\n";
  t->Scan(formula, "", "colsize=25 col=lld");

  f.Close();
}

int main() {
  ULong64_t x = 1617047019150033926;

  write(x);
  read(x);
}

The program:

  1. Creates a tree with a single ULong64_t branch and single entry, and saves it to file f.root
  2. Reads the file and the tree:
    • Reads the value from tree with TTree::GetEntry() to confirm the variable is saved properly – this seems to work correctly
    • Displays the value with Scan() – displayed value is different on the last digit
    • Perform simple subtractions using Scan() – the results are incorrect as well

This is output I obtained with 6.22/08 in Linux:

Written to tree:   1617047019150033926
Read from tree:    1617047019150033926 (correct)

    TTree::Scan("x");
Expected output:   1617047019150033926
****************************************
*    Row   *                         x *
****************************************
*        0 *       1617047019150033920 *
****************************************

    TTree::Scan("x-1617047019150033925");
Expected output:                     1
****************************************
*    Row   *     x-1617047019150033925 *
****************************************
*        0 *                         0 *
****************************************

    TTree::Scan("x-1617047019150033000");
Expected output:                   926
****************************************
*    Row   *     x-1617047019150033000 *
****************************************
*        0 *                      1024 *
****************************************

I encountered similar issues when trying to plot the values with TTree::Draw().

It seems to me that the values are rounded... perhaps casted to double? Is there any way to avoid it?

ferdymercury commented 3 years ago

This is a duplicate of https://sft.its.cern.ch/jira/browse/ROOT-8009

ferdymercury commented 3 years ago

@pcanal does https://github.com/root-project/root/pull/6740 close the issue? I think you linked it, but as it was already merged, it does not automatically close this issue.

ferdymercury commented 3 years ago

The test @aadduszki wrote still fails on master:

   ------------------------------------------------------------------
  | Welcome to ROOT 6.25/01                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Jun 08 2021, 21:53:04                 |
  | From heads/master@v6-25-01-1189-g1ca221f010                      |
  | With                                                             |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

Processing test_tree.cpp...
Written to tree:   1617047019150033926
Read from tree:    1617047019150033926 (correct)

    TTree::Scan("x");
Expected output:   1617047019150033926
****************************************
*    Row   *                         x *
****************************************
*        0 *       1617047019150033920 *
****************************************

    TTree::Scan("x-1617047019150033925");
Expected output:                     1
****************************************
*    Row   *     x-1617047019150033925 *
****************************************
*        0 *                         0 *
****************************************

    TTree::Scan("x-1617047019150033000");
Expected output:                   926
****************************************
*    Row   *     x-1617047019150033000 *
****************************************
*        0 *                      1024 *
****************************************