Merging two trees with different branches and shifted entries

Hi all,
I’m struggling with the merging of two different trees. I want to add a branch to an existing tree, but the entries have to be shifted by one. For instance, if Analysis is the name of the first tree, the Track tree contains the global branch, which corresponds to instances contained in the branches inside Analysis if the entries (of global) are shifted by +1. I have seen the TTree::MergeTree function, but it does not seem to do what I’m looking for.

Thanks in advance,
Luca

Dear Luca,

Thanks for posting and welcome to the forum. I propose to solve this with RDataFrame:

void CreateInput()
{
   const auto nevts = 128;
   auto fake_b1_content = 1;
   auto fake_b2_content = 123;
   auto df1 = ROOT::RDataFrame(nevts)
                 .Define("c1", [&] { return fake_b1_content++; })
                 .Define("c2", [&] { return fake_b2_content++; })
                 .Snapshot("tree1", "file1.root");
   auto fake_b3_content = 0;
   auto df2 = ROOT::RDataFrame(nevts+1).Define("c3", [&] { return fake_b3_content++; }).Snapshot("tree2", "file2.root");
}

void test()
{
   CreateInput();

   // Open the second tree and re-dump it from the second entry
   auto auxdf = ROOT::RDataFrame("tree2", "file2.root").Filter("rdfentry_ != 0").Snapshot("tree2", "file3.root");

   // Now we are ready to build a tree with 3 branches!
   TFile f3("file3.root");
   auto  tree2 = f3.Get<TTree>("tree2");   
   TFile f1("file1.root");
   auto  tree1 = f1.Get<TTree>("tree1");
   tree1->AddFriend(tree2, "myFriend");

   // We build a DF to work with this columnar dataset
   auto df = ROOT::RDataFrame(*tree1);//.Alias("c3", "myFriend.c3");
   const std::initializer_list<std::string> colList = {"c1", "c2", "c3"};
   df.Display(colList, 32)->Print();

   // And if wanted, we can dump it in a new file
   df.Snapshot("MyMergedTree", "file4.root", colList);
}

I hope it helps.

Danilo

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