Merge only selected subdirectory of some root files

Hi,

I’m trying to write a script to merge only a specific sub directory of a number of identically structured root files.
I am basically trying to reproduce this example ROOT: tutorials/io/mergeSelective.C File Reference in python, but whatever I do the entire files get merged and not just the specified sub directory.

Here is a MWE:

import ROOT
with ROOT.TFile("test_input.root", "recreate") as outfile:
    outfile.mkdir("A")
    outfile.mkdir("B")

file_merger = ROOT.TFileMerger(False)
file_merger.AddFile("test_input.root")
file_merger.AddObjectNames("A")
file_merger.OutputFile("test_merged.root")
merge_mode = ROOT.TFileMerger.kOnlyListed | ROOT.TFileMerger.kAll | ROOT.TFileMerger.kIncremental
file_merger.PartialMerge(merge_mode)

Expected output: test_merged.root with only directory A
Observed output: test_merged.root with all directories from test_input.root (A and B)

Any help would be appreciated!

ROOT Version: 6.32.08


Hello @kahaeuss,

I noticed the following:

And about kAll, I read here:

kAll Merge all type of objects (default)

Did you try instead to use the following?

kOnlyListed Only the objects specified in fObjectNames list.

Hi,

thank you for the suggestion.
I did try that, but in that case I get an empty output file.
The example I linked explicitly states

Important note: the kOnlyListed and kSkipListed flags have to be bitwise OR-ed on top of the merging defaults: kAll | kIncremental (as in the example)

and the example contains the lines

Int_t default_mode = TFileMerger::kAll | TFileMerger::kIncremental;
Int_t mode = default_mode | TFileMerger::kOnlyListed;
fm->PartialMerge(mode);

so I didn’t pursue this further.

Hello again,

I believe you found a bug. I’ll investigate how to fix it. Thanks for the report!

Alright, thank you!

A small update:
While writing a test for this case, I discovered that if you wrote objects into these directories, only the ones you select get merged:

$ rootls -r selectiveMerge_output.root 
A  
  Histo_A
B

There used to be a Histo_B in the input file. So you are not really wasting space. Nevertheless, I’ll see to removing the empty directory.

You are right, the other directories are indeed empty.
That solves my problem for now, thank you!

OK, good to know! In the mean time, we are also working on a fix:

Hello @StephanH,
sorry to bother you again.
I tried the script on my data and it seems that the only thing that is being copied (apart from the directory structure) are TTrees, but not histograms.
I thought I might be doing something wrong, so I ran the tutorial example from my original post and it turned out that it has the same problem…
You seem to have been successful in merging histograms, do you have an idea what might be the issue?

I can confirm that the tutorial is broken in root 6.36, probably also in older versions. I’ll extend the unit test and the PR I linked above.