Dear all,
I would like to merge TTrees from different files. The code snippet below works fine. In my actual script, however, I am looping over several hundred files. Therefore I would like to include a f->Close() statement. This, however, kills the TTree in the file and I get a SegFault when trying to merge the TTrees. In order to avoid this I added the tree->SetDirectory(0) statement. The script runs without any errors but the output TTree has very weird entries (they are basically all the same). What can I do to fix this?
Thank you very much in advance!
P.S.: I cannot attach the root files because they are too large, if someone is interested I can maybe find a solution in order to provide them.
// ROOT
#include <TFile.h>
#include <TTree.h>
#include <TCanvas.h>
#include <iostream>
#include <fstream>
void merge_trees() {
TFile *f;
TList * trees = new TList();
TTree *tree = new TTree();
f = new TFile("events_0.root");
tree = (TTree*)f->Get("ParTree_PMBgMaker_Off");
//tree->SetDirectory(0);
tree->GetEntry(0);
trees->AddLast(tree);
// f->Close();
f = new TFile("events_1.root");
tree = (TTree*)f->Get("ParTree_PMBgMaker_Off");
//tree->SetDirectory(0);
tree->GetEntry(0);
trees->AddLast(tree);
//f->Close();
cout << "After loop" << endl;
cout << "Found " << trees->GetSize() << " trees" << endl;
TFile *out = new TFile("out.root","RECREATE");
TTree *newtree = tree->CloneTree(0);
newtree->SetName("test");
newtree->Merge(trees);
newtree->Draw("HillasLength[4]");
return;
}