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

Issue with Cling and TTree object in memory #16730

Open
1 task
musinsky opened this issue Oct 22, 2024 · 4 comments
Open
1 task

Issue with Cling and TTree object in memory #16730

musinsky opened this issue Oct 22, 2024 · 4 comments
Assignees

Comments

@musinsky
Copy link
Contributor

Check duplicate issues.

  • Checked for duplicates

Description

Strange issue with Cling and TTree:Scan(), please see table in Reproducer.

Reproducer

file scan.C

#include "TTree.h"
#include "TRandom.h"

TTree *t = nullptr;

void scan() {
  const Int_t kMax = 10;
  Int_t n;
  Float_t a[kMax];

  t = new TTree("t", "tree"); // in memory
  t->Branch("n", &n, "n/I");
  t->Branch("a", a, "a[n]/F");

  for (Int_t i = 0; i < 5; i++) {
    n = gRandom->Rndm()*(kMax-1);
    for (Int_t j = 0; j < n; j++)
      a[j] = gRandom->Gaus(0, 1);
    t->Fill();
  }
  // t->Scan("a[0]");
}
Fedora 40, root master (2024-10-22) lxplus.cern.ch (RHEL 9.4), root v6.32.06
$ root scan.C
root [0]
Processing scan.C...
root [1] t->Scan("a[0]")
************************
*    Row   *      a[0] *
************************
*        0 * 1.255e-41 *
ERROR leaf:a, len=22 and max=8
*        1 * 1.3911939 *
ERROR leaf:a, len=22 and max=8
*        2 * 1.1125664 *
ERROR leaf:a, len=22 and max=8
*        3 * 2.0609021 *
ERROR leaf:a, len=22 and max=8
*        4 * -0.182436 *
************************
(long long) 5
root [2] t->Draw("a[0]")
root [3] // draws correct all 5 values
$ root scan.C
root [0]
Processing scan.C...
root [1] t->Scan("a[0]")
************************
*    Row   *      a[0] *
************************
*        0 * -0.434764 *
*        1 * 1.3911939 *
*        2 * 1.1125664 *
*        3 * 2.0609021 *
*        4 * -0.182436 *
************************
(long long) 5
root [2]





$ root
root [0] .L scan.C
root [1] scan()
root [2] t->Scan("a[0]")
************************
*    Row   *      a[0] *
************************
*        0 * -0.434764 *
*        1 * 1.3911939 *
*        2 * 1.1125664 *
*        3 * 2.0609021 *
*        4 * -0.182436 *
************************

 *** Break *** segmentation violation
$ root
root [0] .L scan.C
root [1] scan()
root [2] t->Scan("a[0]")
************************
*    Row   *      a[0] *
************************
*        0 * -0.434764 *
*        1 * 1.3911939 *
*        2 * 1.1125664 *
*        3 * 2.0609021 *
*        4 * -0.182436 *
************************

 *** Break *** segmentation violation

ROOT version

see in table

Installation method

build from source

Operating system

Linux

Additional context

No response

@musinsky musinsky added the bug label Oct 22, 2024
@pcanal
Copy link
Member

pcanal commented Oct 23, 2024

Try with

  Int_t n = kMax;

@musinsky
Copy link
Contributor Author

musinsky commented Oct 23, 2024

With Int_t n = kMax; on Fedora, root master version:

root [0] 
Processing scan.C...
In file included from input_line_8:1:
/home/musinsky/scan.C:10:13: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
  Float_t a[kMax];
            ^~~~
/home/musinsky/scan.C:10:13: note: read of non-const variable 'kMax' is not allowed in a constant expression
/home/musinsky/scan.C:8:9: note: declared here
  Int_t kMax = 10;
        ^
root [1] t->Scan("a[0]")
************************
*    Row   *      a[0] *
************************
ERROR leaf:a, len=22 and max=8
*        0 * -0.434764 *
ERROR leaf:a, len=22 and max=8
*        1 * 1.3911939 *
ERROR leaf:a, len=22 and max=8
*        2 * 1.1125664 *
ERROR leaf:a, len=22 and max=8
*        3 * 2.0609021 *
ERROR leaf:a, len=22 and max=8
*        4 * -0.182436 *
************************
(long long) 5
root [2] 

Also tested with constexpr Int_t kMax = 10; to same problem.

@musinsky
Copy link
Contributor Author

musinsky commented Oct 23, 2024

Other problem with Cling and tree in memory:
Fedora, root master version

$ root scan.C
root [0]
Processing scan.C...
root [1] t->Draw("a[0]")
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [2] scan()
root [3] t->Draw("a[0]")

 *** Break *** segmentation violation
$ root
root [0] .L scan.C++
Info in <TUnixSystem::ACLiC>: creating shared library /home/musinsky/./scan_C.so
root [1] scan()
root [2] t->Draw("a[0]")
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [3] scan()
root [4] t->Draw("a[0]")
root [5] scan()
root [6] t->Draw("a[0]")
root [7] scan()
root [8] t->Draw("a[0]")

 *** Break *** segmentation violation

lxplus.cern.ch (RHEL 9.4), root v6.32.06

$ root
root [0] .L scan.C++
Info in <TUnixSystem::ACLiC>: creating shared library /afs/cern.ch/user/m/musinsky/./scan_C.so
root [1] scan()
root [2] t->Draw("a[0]")
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
*** stack smashing detected ***: terminated

@musinsky
Copy link
Contributor Author

musinsky commented Oct 23, 2024

The version if the tree is saved to a file works as expected.

#include "TTree.h"
#include "TRandom.h"

TFile *f = nullptr;
TTree *t = nullptr;

void tree(const char *fname = "file.root")
{
  const Int_t kMax = 10;
  Int_t n;
  Float_t a[kMax];

  SafeDelete(t); SafeDelete(f);
  if (fname) f = new TFile(fname, "recreate");
  // else in memory
  t = new TTree("t", "tree");
  t->Branch("n", &n, "n/I");
  t->Branch("a", a, "a[n]/F");

  for (Int_t i = 0; i < 5; i++) {
    n = gRandom->Rndm()*(kMax-1);
    for (Int_t j = 0; j < n; j++)
      a[j] = gRandom->Gaus(0, 1);
    t->Fill();
  }
  if (f) {
    t->Write();
    SafeDelete(t); SafeDelete(f);
    f = new TFile(fname);
    t = (TTree*)f->Get("t");
  }
  // t->Draw("a[0]");
}
$ root
root [0] .L tree.C
root [1] tree(); t->Draw("a[0]");
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [2] tree(); t->Draw("a[0]");
root [3] tree(); t->Draw("a[0]");

@musinsky musinsky changed the title Issue with Cling and TTree:Scan() Issue with Cling and TTree in memory Oct 23, 2024
@musinsky musinsky changed the title Issue with Cling and TTree in memory Issue with Cling and TTree object in memory Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants