# Merging

**URL:** <https://root-forum.cern.ch/t/merging/2548>\
**Category:** ROOT\
**Created:** [October 25, 2005, 10:27am UTC](https://root-forum.cern.ch/t/merging/2548 "2005-10-25T10:27:16Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [October 25, 2005, 10:27am UTC](https://root-forum.cern.ch/t/merging/2548/1 "2005-10-25T10:27:16Z")

</div>

Hello!

I will take it from the beginning:  
I produce a file (file.root) with a number of branches which I then add into a TChain. The tree has e number of entries. This is no problem.  
But now I make 10 files that are identical in structure (files\*.root) and I would like to merge them so that I still in the end only have one tree. I.e I don’t wish to have 10 times the number of entries which I get from adding 10 files to the chain. I have been trying to do this using TChain::Merge(). I manage to produce a file “all.root” with this code:

//Chain  
m\_chainName = “voxels”;  
m\_chain = new TChain(m\_chainName);  
m\_chain-\>Add(files\*.root);

//Input TTree  
AnalysisTree = new FluenceNtuple(m\_chain); //Load FluenceNtuple-Object with tree "voxels"  
cout\<\<“File: “\<\<filename1\<\<” loaded”\<\<endl;  
m\_chain-\>Merge(“all.root”);

But with a few problems:

1. When I check the number of entries it is 10 times the initial number
2. all.root gets very big (larger than the sum of the initial files)

How can I achieve what I wish?

Best Regards  
Karin  
using root 4.03/02 on windows xp

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [October 25, 2005, 10:43am UTC](https://root-forum.cern.ch/t/merging/2548/2 "2005-10-25T10:43:35Z")

</div>

If your files are not too big, could you put them in some public directory?

Rene

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [October 25, 2005, 11:35am UTC](https://root-forum.cern.ch/t/merging/2548/3 "2005-10-25T11:35:06Z")

</div>

I am sorry, but I don’t have acces to do that. Could you give me some suggestion anyway?

regards,  
Karin

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [October 25, 2005, 11:59am UTC](https://root-forum.cern.ch/t/merging/2548/4 "2005-10-25T11:59:52Z")

</div>

Now I have managed to borrow some space from a friend, I have put three files at: [www.ift.uib.no/~mohn/Rene/PrimaryElectronFluence.tar.gz](http://www.ift.uib.no/~mohn/Rene/PrimaryElectronFluence.tar.gz)

/Karin

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [October 25, 2005, 12:53pm UTC](https://root-forum.cern.ch/t/merging/2548/5 "2005-10-25T12:53:18Z")

</div>

Karin,

I cannot reproduce your problem. I tried the two ways

```auto

```

`root [0] TChain ch("voxels")
root [1] ch.Add("Pri*.root")
(Int_t)3
root [2] ch.Merge("all2.root")`  
In both cases, the merged Tree has 167343 entries and is 19120175 bytes  
Could you tell me exactly what you are doing?

Rene

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [October 25, 2005, 1:49pm UTC](https://root-forum.cern.ch/t/merging/2548/6 "2005-10-25T13:49:30Z")

</div>

I have a Macro called TFluenceTool.C. One of the functions is:

void TFluenceTool::MergeFile(TString filename)  
{

//Chain  
m\_chainName = “voxels”;  
m\_chain = new TChain(m\_chainName);  
m\_chain-\>Add(filename);

//Input TTree  
AnalysisTree = new FluenceNtuple(m\_chain);

m\_chain-\>Merge(“all.root”);

}

I then run the script by writing .x run.C in the root window, fluence.C is:

void run()  
{

gROOT-\>LoadMacro( “TFluenceTool.C” );  
TFluenceTool\* test = new TFluenceTool();  
test-\>MergeFile(“Rene/Prim\*.root”);

}

but it does not work at all… ☹ The files Rene/Prim\*.root are loaded and the file all.root is created, but empty. Is it at least a bit more clear what I am doing? Maybe also where it goes wrong?

/Karin

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [October 25, 2005, 1:58pm UTC](https://root-forum.cern.ch/t/merging/2548/7 "2005-10-25T13:58:45Z")

</div>

Could you remove the lines  
//Input TTree  
AnalysisTree = new FluenceNtuple(m\_chain);

They are not necessary and may have side-effects.

Did you try the other solution  
hadd -f all.root Pri\*.root

Rene

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [October 25, 2005, 2:24pm UTC](https://root-forum.cern.ch/t/merging/2548/8 "2005-10-25T14:24:30Z")

</div>

Have I understood hadd wrong? I thought it is to add histograms only. What I wish is an NTuple Tree that I can pick nodes from. I can therefore not remove the line “AnalysisTree = new FluenceNtuple(m\_chain);”, or is there an other way to do the same thing without the Tree?

/Karin

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [October 25, 2005, 3:50pm UTC](https://root-forum.cern.ch/t/merging/2548/9 "2005-10-25T15:50:35Z")

</div>

hadd can merge files containing histograms and Trees.

Please test it.

Also run without calling your class. I have to find out the source of the problem.

Rene

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [October 26, 2005, 12:57pm UTC](https://root-forum.cern.ch/t/merging/2548/10 "2005-10-26T12:57:48Z")

</div>

Dear Rene,

I have now tried the hadd and as far as I understand it it does not do what I want to do. I think maybe I have not been clear enough when I explain what I want to do. I should not have used the word “merge” at all. I will try again.  
When I use hadd, I do indeed get a file that is a merge of two input files, but just put one after the other. I.e, when I do GetEntrise I get 2\*the number of entries in each file. I have now learned that this is what “merge” means, so it is not so strange that this is what happends.

I wish to _add_ the content of the leaves so that I in hte new tree have the same number of entries which are sums of the input files. The values will hence be about 2 times as large for 2 input files (if they have similar values stored), but the number of entries is the same.

I hope it is clearer what I want.

Best Regards  
Karin

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [October 26, 2005, 1:17pm UTC](https://root-forum.cern.ch/t/merging/2548/11 "2005-10-26T13:17:50Z")

</div>

Could you have a look at TTree::AddFriend at  
[root.cern.ch/root/htmldoc//TTree … :AddFriend](http://root.cern.ch/root/htmldoc//TTree.html#TTree:AddFriend)

Rene

---

<div class="post-metadata">

**Author:** ![karineklund](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f07891/32.png) [@karineklund](https://root-forum.cern.ch/u/karineklund)\
**Post date:** [October 26, 2005, 1:33pm UTC](https://root-forum.cern.ch/t/merging/2548/12 "2005-10-26T13:33:52Z")

</div>

It could maybe work, but seems to be a very complicated way, and would it really be faster? The main problem för me now is that it takes very long time to go through all the events when I add threes.  
As I see it I will have to construct a large tree with brute force, looping throiugh all my data files and adding values into nodes.  
/Karin

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 26, 2005, 6:23pm UTC](https://root-forum.cern.ch/t/merging/2548/13 "2005-10-26T18:23:00Z")

</div>

[quote]I wish to _add_ the content of the leaves so that I in hte new tree have the same number of entries which are sums of the input files. The values will hence be about 2 times as large for 2 input files (if they have similar values stored), but the number of entries is the same.  
[/quote]With do not provide any tool to do this automatic (the major hurdle would probably to come up with a generic meaning of ‘add’ … adding really depends on the ‘semantic’ of the content of the tree).

[quote]As I see it I will have to construct a large tree with brute force, looping throiugh all my data files and adding values into nodes. [/quote]It is still confusing to me that you use the word ‘large’. If I understand what you want the resulting tree should

- have the same number of leaf
- have the same number of entries  
and hence is the same size as any of the input files!.

And yes your best choice is to:[code]

- open one of the file, read the tree (maintree), and clone it (newtree).
- open all the other files and read the other tree (tree2-10).
- loop over each entry
  - do maintree-\>GetEntry(i)
  - add to the value in maintree the values in tree2-10)
  - call newtree-\>Fill()

- write newtree  
[/code]  
Cheers,  
Philippe.
