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:
#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:
My minimal example is based off of this, 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?
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?