根 - 填充分支失败:内存驻留树

Root - failed filling branch : memory-resident tree

我正在尝试合并树:

string filename_;
TList *list = new TList;

///Loop on all .root output files
for (int i = 1; i < 13; ++i) {
  string filename_ = "file_";
  string file_extension = ".root";
  stringstream ss;
  ss << i;
  string str = ss.str();
  filename_ = filename_+str+file_extension;
  const char* c_filename = filename_.c_str();
  TFile *file_adress = TFile::Open(c_filename);
  TTree *tree_adress = (TTree*)file_adress->Get("pulses");
  list->Add(tree_adress);
}

// Output tree:
TTree *TPulses = TTree::MergeTrees(list);

但是我收到了这个错误,它非常明确:

Error in <TTree::Fill>: Failed filling branch:pulses.pulse_time, nbytes=-1, entry=115710
 This error is symptomatic of a Tree created as a memory-resident Tree
 Instead of doing:
    TTree *T = new TTree(...)
    TFile *f = new TFile(...)
 you should do:
    TFile *f = new TFile(...)
    TTree *T = new TTree(...)

但是我是在for循环中的TTree之前创建TFile,所以我不明白。 有人有想法吗?

您看到的错误消息来自您正在写入的 TTree。据我了解您的代码,TFile *file_adressTTree *tree_adress 已打开以供读取,而您写入 TTree *TPulses。我没有看到 TFile::Open 的默认设置(如果它是 READ 用于只读或 update 用于操作内容)但是从你的输出我声称 TPulses 没有得到在您打开的最后一个文件中创建,而不是在 RAM 中创建,而您 运行 从中创建 (see docs)。一个常见的模式是在创建 TPulses:

之前创建一个输出文件
TFile* outputfile = TFile::Open("output.root", "recreate");
TTree* TPulses = TTree::MergeTrees(list);
// ...
TPulses->Write();
outputfile->Close();
delete outputfile;