Error while merging files with THnSparse

Dear ROOT experts,

I have one particular problem while trying to merge files. I have two files, each of them has one THnSparseD and TH1F. Those histograms in both files have same name and binning. The sizes of files are 142.5 MBytes and 75 MBytes respectively.

When I try to merge these files via hadd, it fails with std::bad_alloc error. Same happens when I try to use TFileMerger class. I also tried to add them via this simple macro, but it fails as soon as I try to add the sparses together.

[code]#include “TH1.h”
#include “THnSparse.h”

void merger1(){

TFile * in1 = TFile::Open(“test1.root”);
TFile * in2 = TFile::Open(“test2.root”);


THnSparseD * sparse1 = (THnSparseD *)in1->Get(“sparse”);
THnSparseD * sparse2 = (THnSparseD *)in2->Get(“sparse”);

TH1F * hEvents = (TH1F *)in1->Get(“hEvents”);
TH1F * hEvents2 = (TH1F *)in2->Get(“hEvents”);

//Macro fails at this step

TFile out(“test.root”, “RECREATE”);


I guess that this is probably a memory issue, however I am not sure what can be done to prevent it. I tried merging on both Linux and Windows machines, with different amount of RAM, but with no success. I was wondering if there could be a workaround for this problem. The version of the ROOT I’m using is v5-34-07@49362

You have two times in1->Get(“sparse”)
I assume the second time it should be in2->Get(“sparse”)

[quote=“Wile E. Coyote”]You have two times in1->Get(“sparse”)
I assume the second time it should be in2->Get(“sparse”)[/quote]

Nice typo catch, but problem still remains unfortunately.

I still can’t understand why handling something a bit over 200 Mb is a problem for ROOT. :frowning:

I just tried something like this instead of plain Add:

TList * list = new TList(); list->Add(sparse2); sparse1->Merge(list);

But that didn’t work either.

There is still a “chaining-like” suggestion for THnSparse in limbo at With that you should be able to at least do projections from the merged histogram.

I am a little surprised that you see problems merging these THnSparse since I naively wouldn’t have expected the merge result to use more memory then the sum of the parts. How sparse are they and what kind of axis setup do you have?