Skip to content

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

Open
@aaduszki

Description

@aaduszki

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?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions