Merging

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

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

Rene

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

regards,
Karin

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

/Karin

Karin,

I cannot reproduce your problem. I tried the two ways

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

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… :frowning: 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

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

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

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

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

Could you have a look at TTree::AddFriend at
root.cern.ch/root/htmldoc//TTree … :AddFriend

Rene

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

[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.