# Merge only selected subdirectory of some root files

**URL:** <https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867>\
**Category:** Newbie\
**Tags:** python\
**Created:** [July 8, 2025, 2:09pm UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867 "2025-07-08T14:09:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kahaeuss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kahaeuss](https://root-forum.cern.ch/u/kahaeuss)\
**Post date:** [July 8, 2025, 2:09pm UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/1 "2025-07-08T14:09:39Z")

</div>

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](https://root.cern/doc/v630/mergeSelective_8C.html) in python, but whatever I do the entire files get merged and not just the specified sub directory.

Here is a MWE:

```auto
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

* * *

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 8, 2025, 2:40pm UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/2 "2025-07-08T14:40:51Z")

</div>

Hello @kahaeuss,

I noticed the following:

> [@kahaeuss](#):
>
> ```auto
> merge_mode = ROOT.TFileMerger.kOnlyListed | ROOT.TFileMerger.kAll | ROOT.TFileMerger.kIncremental
> 
> ```

And about kAll, I read [here](https://root.cern.ch/doc/master/classTFileMerger.html#a8ea43dc0722ce413c7332584d8c3ef0f):

> kAll Merge all type of objects (default)

Did you try instead to use the following?

> kOnlyListed Only the objects specified in fObjectNames list.

---

<div class="post-metadata">

**Author:** ![kahaeuss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kahaeuss](https://root-forum.cern.ch/u/kahaeuss)\
**Post date:** [July 8, 2025, 3:05pm UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/3 "2025-07-08T15:05:01Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 9, 2025, 10:52am UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/4 "2025-07-09T10:52:45Z")

</div>

Hello again,

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

---

<div class="post-metadata">

**Author:** ![kahaeuss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kahaeuss](https://root-forum.cern.ch/u/kahaeuss)\
**Post date:** [July 9, 2025, 11:17am UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/5 "2025-07-09T11:17:49Z")

</div>

Alright, thank you!

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 9, 2025, 11:20am UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/6 "2025-07-09T11:20:41Z")

</div>

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:

```auto
$ 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.

---

<div class="post-metadata">

**Author:** ![kahaeuss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kahaeuss](https://root-forum.cern.ch/u/kahaeuss)\
**Post date:** [July 9, 2025, 12:01pm UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/7 "2025-07-09T12:01:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 10, 2025, 10:36am UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/8 "2025-07-10T10:36:44Z")

</div>

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

> <https://github.com/root-project/root/pull/19331>
>
> \- Partial merging creates a full directory hierarchy in an output file,
> even if… the user didn't ask for the directories to be merged.
> \- Fix the doxygen of \`PartialMerge\`
> 
> \#19330

---

<div class="post-metadata">

**Author:** ![kahaeuss](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@kahaeuss](https://root-forum.cern.ch/u/kahaeuss)\
**Post date:** [July 10, 2025, 1:38pm UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/9 "2025-07-10T13:38:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [July 11, 2025, 8:24am UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/10 "2025-07-11T08:24:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 11, 2025, 8:52am UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/11 "2025-07-11T08:52:16Z")

</div>

Unlisted on July 11, 2025, 8:52am UTC



---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 11, 2025, 9:46am UTC](https://root-forum.cern.ch/t/merge-only-selected-subdirectory-of-some-root-files/63867/12 "2025-07-11T09:46:56Z")

</div>

Listed on July 11, 2025, 9:46am UTC


