# Get Number of Divisions in a TCanvas

**URL:** <https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495>\
**Category:** Newbie\
**Created:** [September 17, 2020, 8:51pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495 "2020-09-17T20:51:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [September 17, 2020, 8:51pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/1 "2020-09-17T20:51:51Z")

</div>

_ROOT Version:_ v6.14.06  
_Platform:_ Ubuntu 16.04.6 LTS  
_Compiler:_ g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609

* * *

I would like to have a way of obtaining the number of “divisions” in a TCanvas. I looked in the ROOT documentation, and I could not find any relevant member functions in TCanvas. I say “divisions” but I’m not sure that is the right word. But I think “pads” is not the right word either in my case.

Here is my minimal example that shows the problem:

```auto
#include <cassert>

#include "TCanvas.h"

int get_number_of_pads(TCanvas* canvas) {

  int number_of_pads = 0;

  for (const auto& object : *canvas->GetListOfPrimitives()) {
    if (object->InheritsFrom(TVirtualPad::Class())) {
      ++number_of_pads;
    }
  }

  return number_of_pads;

}

void minimal() {

  TCanvas* c1 = new TCanvas();
  c1->Divide(2, 2);

  TCanvas* c2 = new TCanvas();
  c2->Divide(4, 4);

  TCanvas* c3 = new TCanvas();

  assert(get_number_of_pads(c1) == 4);
  assert(get_number_of_pads(c2) == 16);
  assert(get_number_of_pads(c3) == 1); // This fails                                                                                                                                                                                                                                  

}

```

I know why the last assert fails: the `TList` from `GetListOfPrimitives` is empty.

If there is some ROOT functionality that already exists, please let me know. Otherwise, I basically would like a function `get_number_of_divisions` that passes all of my assert statements. For `c3` in the minimal example, it has one “division” or one “drawing space” even though it has zero “pads” (if that makes sense). Just curious if there is a way to do this without the obvious condition at the end of the function:

```auto
  if (number_of_pads == 0) {                                                                                                                                                                                                                                                          
    number_of_pads = 1;
  }

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 18, 2020, 6:56am UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/2 "2020-09-18T06:56:36Z")

</div>

Yes, you mean the number of pads…  
There is no such functionality.  
What is the use case ?

---

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [September 18, 2020, 12:53pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/3 "2020-09-18T12:53:46Z")

</div>

> [@couet](#):
>
> Yes, you mean the number of pads…

Thanks for the reply!

My minimal example is based off of [this](https://root.cern.ch/root/roottalk/roottalk02/0654.html), which is supposed to count the number of pads. Because my minimal example fails for c3, I started to think that maybe I didn’t know exactly what constitutes a pad.

My use case is wanting to loop over the pads in a canvas and do some tinkering to those pads. It would work whether the canvas was divided or not. I would have thought a simple function like TCanvas::GetNumberOfPads would exist for this.

I guess I’ll have to catch the edge case (undivided canvas) myself?

```auto
if (number_of_pads == 0) {                                                                                                                                                                                                                                                          
  number_of_pads = 1;
}

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 18, 2020, 1:20pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/4 "2020-09-18T13:20:21Z")

</div>

It works for me.

I did:

```auto
Int_t countpads(TVirtualPad *pad) {
   //count the number of pads in pad
   if (!pad) return 0;
   Int_t npads = 0;
   TObject *obj;
   TIter next(pad->GetListOfPrimitives());
   while ((obj = next())) {
      if (obj->InheritsFrom(TVirtualPad::Class())) npads++;
   }
   return npads;
}

void npads() {
   TCanvas* c1 = new TCanvas();
   c1->Divide(2, 2);
   printf("%d\n", countpads(c1));
}

```

it gives me:

```auto
% root npads.C
   ------------------------------------------------------------------
  | Welcome to ROOT 6.23/01 https://root.cern |
  | (c) 1995-2020, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Sep 17 2020, 07:58:14 |
  | From heads/master@v6-23-01-1248-gb96280beb0 |
  | With Apple clang version 12.0.0 (clang-1200.0.32.2) |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------------

root [0] 
Processing npads.C...
4
root [1] 

```

---

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [September 18, 2020, 1:30pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/5 "2020-09-18T13:30:42Z")

</div>

Correct… and if you comment out the line

```auto
c1->Divide(2, 2);

```

the function returns 0. That is the whole point of my post…

Based on your “Yes, you mean the number of pads…” in your first reply, I would think an undivided canvas would have a single pad. Is this correct or not?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 18, 2020, 1:50pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/6 "2020-09-18T13:50:02Z")

</div>

> [@Erdrick](#):
>
> Correct… and if you comment out the line
> 
> ```auto
> c1->Divide(2, 2);
> 
> ```
> 
> the function returns 0. That is the whole point of my post…

Yes … and that’s normal, isn’t it. ? … there is no pads in the Canvas in that case …

---

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [September 18, 2020, 2:07pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/7 "2020-09-18T14:07:36Z")

</div>

Then why does this work?

```auto
void minimal() {

  TCanvas* canvas = new TCanvas("canvas", "canvas");
  TVirtualPad* pad = canvas->cd();
  std::cout << pad->GetName() << std::endl;

}

```

It’s an undivided canvas, and ROOT thinks there is a pad?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 18, 2020, 2:10pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/8 "2020-09-18T14:10:55Z")

</div>

A TCanvas is a TPad … that’s why.

and `countpads` returns the number of pad inside a TCanvas.

---

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [September 18, 2020, 2:41pm UTC](https://root-forum.cern.ch/t/get-number-of-divisions-in-a-tcanvas/41495/9 "2020-09-18T14:41:25Z")

</div>

I guess it’s something I need keep clear/straight in my head. Thank you very much for your help today. It is greatly appreciated.
