# How to get the content of a TPad/TCanvas

**URL:** <https://root-forum.cern.ch/t/how-to-get-the-content-of-a-tpad-tcanvas/18526>\
**Category:** ROOT\
**Created:** [November 25, 2014, 9:49am UTC](https://root-forum.cern.ch/t/how-to-get-the-content-of-a-tpad-tcanvas/18526 "2014-11-25T09:49:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [November 25, 2014, 9:49am UTC](https://root-forum.cern.ch/t/how-to-get-the-content-of-a-tpad-tcanvas/18526/1 "2014-11-25T09:49:57Z")

</div>

Hello, if I run the cc.C subroutine, I get a TCanvas with two TPads where there is inside of each of them a TH2D.  
I would like to get the TH2D without knowing its name a priori.  
To do that, I use a piece of code like that

`
TCanvas *c = NULL;
TH2D *hh = NULL;
vector <TCanvas*> canvasV;
vector <TH2D*> histoV;
  for(int i=0 ; i<gROOT->GetListOfCanvases()->GetSize() ; i++) {
    TObject *obj = gROOT->GetListOfCanvases()->At(i);
    if ( obj->IsA()->InheritsFrom( TCanvas::Class() ) ) {
      c = (TCanvas*)obj;
      canvasV.push_back(c);
    }
  }
 c = canvasV[0]; // it is just to give an idea
 for(int i=0 ; i<c->GetListOfPrimitives()->GetSize() ; i++) {
    TObject *obj = c->GetListOfPrimitives()->At(i);
    if ( obj->IsA()->InheritsFrom( TH1::Class() ) ) {
      hh = (TH2D*)obj;
      histoV.push_back(hh);
    }
  }`  
The code above is not perfect but gives an idea of what I do.  
The problem is that if you do

`.x cc.C
gROOT->GetListOfCanvases()->At(0)->GetListOfPrimitives()->At(0)->Print()`  
You will get a TPad and I do not succed to get the content of what is inside of the TPad. Do you have any idea?

PS: sorry if my problem is not very clear. I will try to reexplain better if so.  
[cc.C](https://root-forum.cern.ch/uploads/default/original/2X/c/cd9cda56cca51b364a2035dc20b00039bf4b3295.C) (317 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 25, 2014, 11:45am UTC](https://root-forum.cern.ch/t/how-to-get-the-content-of-a-tpad-tcanvas/18526/2 "2014-11-25T11:45:47Z")

</div>

`gROOT->GetListOfCanvases()->ls()
gROOT->GetListOfCanvases()->At(0)->ls()
((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->ls()
((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->ls()
((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->At(0)->ls()
((TPad*)(((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->At(0)))->ls()
((TPad*)(((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->At(0)))->GetListOfPrimitives()->ls()
((TPad*)(((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->At(0)))->GetListOfPrimitives()->Print()
((TPad*)(((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->At(0)))->GetListOfPrimitives()->At(1)->ls()
((TPad*)(((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->At(0)))->GetListOfPrimitives()->At(1)->Print()
((TH2D*)(((TPad*)(((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->At(0)))->GetListOfPrimitives()->At(1)))->ls()
((TH2D*)(((TPad*)(((TCanvas*)(gROOT->GetListOfCanvases()->At(0)))->GetListOfPrimitives()->At(0)))->GetListOfPrimitives()->At(1)))->Print()`

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [November 25, 2014, 4:24pm UTC](https://root-forum.cern.ch/t/how-to-get-the-content-of-a-tpad-tcanvas/18526/3 "2014-11-25T16:24:38Z")

</div>

Thank you Wile. I did not use the cast step. That is why it did not work here before. 😕
