Error in merging trees into one using TTree::MergeTrees

Hi everyone,

I am trying to merge 3 different ROOT files, each one containing one tree. Here is the code that I use:

#include "TH1F.h"
#include "TFile.h"
#include "TTreeReader.h"
#include "TTree.h"
#include <iostream>
#include <string>

void merge_root(){
  
  TList* treeList = new TList;

  TFile* mc16file = TFile::Open("mc16.root", "RECREATE");

  TFile* mc16a = new TFile("mc16a.root");
  TFile* mc16d = new TFile("mc16d.root");
  TFile* mc16e = new TFile("mc16e.root");

  treeList->Add(mc16a);
  treeList->Add(mc16d);
  treeList->Add(mc16e);

  TTree* outTree = TTree::MergeTrees(treeList);

  outTree->Write("", TObject::kOverwrite);

  mc16file->Close();

}

I get the following error message:

#0  0x00007f80c952946c in waitpid () from /lib64/libc.so.6
#1  0x00007f80c94a6f62 in do_system () from /lib64/libc.so.6
#2  0x00007f80ca6368f3 in TUnixSystem::StackTrace() () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBaseExternals/21.2.139/InstallArea/x86_64-centos7-gcc8-opt/lib/libCore.so
#3  0x00007f80c4bd7405 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBaseExternals/21.2.139/InstallArea/x86_64-centos7-gcc8-opt/lib/libCling.so
#4  0x00007f80c4bd6e5b in cling_runtime_internal_throwIfInvalidPointer () from /cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBaseExternals/21.2.139/InstallArea/x86_64-centos7-gcc8-opt/lib/libCling.so
#5  0x00007f80cad9c48a in ?? ()
#6  0x0000000000000000 in ?? ()
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_8:1:
/usera/martinov/ZZy-analysis/merge_root.C:25:3: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
  outTree->Write("", TObject::kOverwrite);

I suspect that it’s got something to do with pointers but it would be great if someone could help me !

Cheers !

Strangely MergeTrees seems to have return a nullptr … Maybe try:

TFile* mc16file = TFile::Open("mc16.root", "RECREATE");

  TFile* mc16a = new TFile("mc16a.root");
  TFile* mc16d = new TFile("mc16d.root");
  TFile* mc16e = new TFile("mc16e.root");

  treeList->Add(mc16a);
  treeList->Add(mc16d);
  treeList->Add(mc16e);
  
   mc16file->cd();
  TTree* outTree = TTree::MergeTrees(treeList);

  outTree->Write("", TObject::kOverwrite);

to insure the result TTree is attached to the output file (and hence its basket written there).

It might be easier to run (on the shell command line)

hadd -f mc16.root mc16?.root

Ok it fixed it ! Thanks a lot !
It still works if I remove the “mc16file->cd();” line. So, it’s really the shell command that fixes it.

The ‘cd’ is to fix your code (without having to use the hadd command).
The command line replace it completely (and use the “fast merging” method so is much faster).

Oh I see. The ‘cd’ actually didn’t change anything to be honest, the error message is still the same.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.