root-project / root

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

[TTreeReader] Wrong (empty) arrays silently read for complex data members of TClonesArray elements #11769

Open eguiraud opened 2 years ago

eguiraud commented 2 years ago

First reported at https://root-forum.cern.ch/t/trouble-reading-rvec-of-vector-from-branch-with-tclonesarray-of-objects-with-std-vector-member/52482 .

A reproducer:

// repro.C
#include <ROOT/RDataFrame.hxx>
#include <ROOT/RVec.hxx>
#include <TClonesArray.h>
#include <TObject.h>
#include <TTree.h>
#include <iostream>
#include <vector>

class Track : public TObject {
public:
  std::vector<double> hitEnergies;
  int id = 42;

  Track(const std::vector<double> &v = {}) : hitEnergies{v} {};

  ClassDef(Track, 1);
};
ClassImp(Track);

void repro() {
  // write file
  {
    TFile f("f.root", "recreate");
    TTree tree("Events", "events");

    TClonesArray arr("Track", 1);

    tree.Branch("Tracks", &arr);

    arr.ConstructedAt(0);
    ((Track *)arr.At(0))->hitEnergies.assign({1.0, 2.0, 3.0});
    arr.ConstructedAt(1);
    ((Track *)arr.At(1))->hitEnergies.assign({4.0, 5.0});

    tree.Fill();
    tree.Write();
  }

  TFile f("f.root");

  {
    // reading back the `id` data member works
    TTreeReader r("Events", &f);
    TTreeReaderArray<int> ids(r, "Tracks.id");
    r.Next();
    std::cout << ids.GetSize() << '\n';           // prints 2
    std::cout << ids[0] << ' ' << ids[1] << '\n'; // prints '42 42'
  }

  {
    // reading back hitEnergies data member does not work
    TTreeReader r("Events", &f);
    TTreeReaderArray<std::vector<double>> tracks(r, "Tracks.hitEnergies");
    r.Next();
    std::cout << tracks.GetSize() << '\n'; // prints 0!
  }
}
Axel-Naumann commented 2 years ago

Silently reading wrong data: critical!

eguiraud commented 1 year ago

Added 6.28 milestone as per discussion with @pcanal

pcanal commented 1 year ago

12137 Makes this setup a visible error pending the actual implementation.

Axel-Naumann commented 1 year ago

As this error is now unsilenced in 6.28 we can at least remove the blocker.