# Merging multiple Histogramms

**URL:** <https://root-forum.cern.ch/t/merging-multiple-histogramms/49669>\
**Category:** Newbie\
**Created:** [April 21, 2022, 11:35am UTC](https://root-forum.cern.ch/t/merging-multiple-histogramms/49669 "2022-04-21T11:35:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wfelix](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/a8b319/32.png) [@wfelix](https://root-forum.cern.ch/u/wfelix)\
**Post date:** [April 21, 2022, 11:35am UTC](https://root-forum.cern.ch/t/merging-multiple-histogramms/49669/1 "2022-04-21T11:35:42Z")

</div>

Hello,

I am trying to merge multiple diagrams. To do this, I created a bunch of diagrams in a for loop and now I want to add each of these diagrams to a list, and then merge them. Like this:

```auto
TList *individualetaphi = new TList;
for (Int_t i = 6; i < 10; i++) {
  trackscanv->cd(i-5);
   // defining graphs, 0 = pt, 1 = nsigma, 2,3 = eta,phi
   TH1D* projection_p0 = (TH1D*) tracks[0]->Projection(0); // pt
   projection_p2_p3[i] = (TH2D*) tracks[0]->Projection(3,2); // eta vs phi - mb
   projection_p2_p3_trdmb[i] = (TH2D*) tracks[1]->Projection(3,2); // eta vs phi - trdmb

   //efficiency
   efficiency[i] = (TH2D*) projection_p2_p3[i]->Clone(Form("Efficiency EtaPhi %i Gev/c",i));
   efficiency[i]->Divide(projection_p2_p3_trdmb[i], projection_p2_p3[i]);

   //now add the individual pt bins again
   individualetaphi->Add(efficiency[i]);

}

TH2D *efficiency_corrected = (TH2D*) efficiency[6]->Clone("f");
efficiency_corrected->Reset();
efficiency_corrected->Merge(individualetaphi);

//drawing
TCanvas *trackscanvmerged = new TCanvas("trackscanvmerged","trackscanvmerged",900,900);
trackscanvmerged->Divide(2,1);
trackscanvmerged->cd(1);
efficiency_corrected->Draw();

```

However, when I do this I get the error message:

```auto
**warning:**  **invalid memory pointer passed to a callee:**

TH2D *efficiency_corrected = (TH2D*) efficiency[6]->Clone("f");

```

There has to be a simple mistake that I am making. I would be very appreciative for help!

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [April 22, 2022, 7:53am UTC](https://root-forum.cern.ch/t/merging-multiple-histogramms/49669/2 "2022-04-22T07:53:19Z")

</div>

Hi @wfelix ,  
the error seems to imply that `efficiency[6]` is not a valid object, but it’s impossible to say from just that snippet whether that’s the case or not.

You can debug this e.g. by running your macro as `gdb --args root.exe -l -q macro.C+g` and putting a breakpoint at `macro.C:<linewitherror>`.

Cheers,  
Enrico
