Skip to content

TChain::AddClone failing for sub-branches of branch of type with ClassDef #8295

@eguiraud

Description

@eguiraud
  • Checked for duplicates

To Reproduce

The following code, which emulates what happens inside a RDataFrame Snapshot, silently writes wrong data:

// xy_t.h
#pragma once
#include <Rtypes.h>

class xy_t {
public:
  double x;
  double y;

  xy_t() : x(-1), y(-1) {}
  ~xy_t(){};
  ClassDef(xy_t, 1); // remove this to remove the problem
};
// main.cpp
#include <TFile.h>
#include <TChain.h>
#include <TSystem.h>
#include <TTree.h>
#include <TTreeReader.h>
#include <iostream>
#include "xy_t.h"

void write_inputs() {
  xy_t xy;
  int i = 0;
  {
    TFile f("in1.root", "recreate");
    TTree t("t", "t");
    t.Branch("xy", &xy);
    t.Branch("i", &i);
    i = 1;
    xy.x = xy.y = 1;
    t.Fill();
    t.Write();
    f.Close();
  }
  {
    TFile f("in2.root", "recreate");
    TTree t("t", "t");
    t.Branch("xy", &xy);
    t.Branch("i", &i);
    i = 2;
    xy.x = xy.y = 2;
    t.Fill();
    t.Write();
    f.Close();
  }
}

int main() {
  write_inputs();

  TChain c("t");
  c.Add("in1.root");
  c.Add("in2.root");
  TTreeReader r(&c);
  TTreeReaderValue<int> ri(r, "i");
  TTreeReaderValue<double> rx(r, "x");
  TTreeReaderValue<xy_t> rxy(r, "xy");

  {
    r.Next();
    TFile f("out.root", "recreate");
    TTree t("t", "t");
    c.AddClone(&t);
    t.Branch("i", &(*ri));
    t.Branch("x", &(*rx));
    t.Branch("xy", &(*rxy));
    std::cout << "x: " << *rx << '\n';
    t.Fill();
    r.Next();
    *ri;
    *rx;
    *rxy;
    std::cout << "x: " << *rx << '\n';
    t.Fill();
    t.Write();
  }

  TFile f("out.root");
  f.Get<TTree>("t")->Scan();
}

Can be run with:

$ root -l -b -q xy_t.h+; g++ -o main main.cpp xy_t_h.so $(root-config --libs --cflags) && env LD_LIBRARY_PATH="$LD_LIBRARY_PATH:." ./main

Reading and writing of the xy branch can be removed, in which case a warning is printed but wrong data is still written to file.

Removing the ClassDef from xy_t removes the problem and the correct data is written out.

Additional context

The original report is https://root-forum.cern.ch/t/rdataframe-multi-file-with-class-branch-bug-report/45156, in the context of RDataFrame::Snapshot.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions