Multiple TCanvases

Hi,

I am trying to create multiple TCanvases. I have tried using vector format, and string manipulation, however I am unable to achieve my desired functionality.

The number of canvas is dependent on a variable nBinsX which is extract from the x bins of a 2 dimensional histogram. Since I would like to view my data in 24 bins I want 2 canvases of 12, each canvas to have 3 rows 4 columns.

Below is am attempt at using strings

[code]
int can_num = (nBinsX/12.)+1;
vector<TCanvas*> Can2;
Int_t canvasrow = 3;
Int_t canvascolu = 4;
TString CanVas[10] = {“Can1”, “Can2”, “Can3”, “Can4”, “Can5”, “Can6”, “Can7”, “Can8”, “Can9”, “Can10”};

for(int ds=0; ds<can_num; ds++){
    TCanvas *CanVas[ds].Data();
    //CanVas[ds].Data() = new TCanvas();
    CanVas[ds].Data()->Divide(canvascolu,canvasrow);
}[/code]

This gives error:

below is trying to use arrays

[code]
int can_num = (nBinsX/12.)+1;
TCanvas *Can2[can_num];
Int_t canvasrow = 3;
Int_t canvascolu = 4;

for(int ds=0; ds<can_num; ds++){

    char pt2[50];
    sprintf(pt2,"%s%i%s","Can2",ds,"\0");

    Can2[ds]=new TCanvas(pt2,pt2,800,650);
    Can2[ds]->Divide(canvascolu,canvasrow);
}[/code]

But his method will not work because the number of canvases must be know at the time of compile with using ALciC. Without direct input, this gives the normal Error: Non-static-const variable in array dimension

Below is trying to use vectors

[code]
int can_num = (nBinsX/12.)+1;
vector<TCanvas*> Can2;
Int_t canvasrow = 3;
Int_t canvascolu = 4;

for(int ds=0; ds<can_num; ds++){
  
    char pt2[50];
    sprintf(pt2,"%s","Can2");

    //Can2.emplace_back( new TCanvas(pt2,pt2,800,650));
    Can2.push_back(new TCanvas(pt2,pt2,800,650));

    Can2[ds]->Divide(canvascolu,canvasrow);

}[/code]

This does not work because of how TCanvas is stored in the vector.

Any suggestions?
Thanks

I would recommend you to read about C++ (basic syntax, declarations, expressions, etc.) - it will save you a lot of time. Instead of trying to guess which syntax/semantics is right, you have to know the right syntax/semantics, otherwise the number of invalid C++ programs is infinite, I guess it’s quite difficult to try them all (while your second and third examples are more or less reasonable (still wrong though), the first one is really weird).
I believe there are a lot of books/tutorials (free, on-line, etc.) available.

Now, back to your task, I’ll give you a working code sketch, which creates canvases (you have to fill histograms or whatever to make it more interesting):

[code]#include

std::vector<TCanvas *> canvases;

//nBinsX MUST NOT include overflow/underflow bins.
void mcnv(int nBinsX = 14)
{
if (canvases.size()) {
std::cout<<“error: mcnv called twice\n”;
return;
}

if (nBinsX <= 0) {
std::cout<<“error: invalid number of bins\n”;
return;
}

const int nRows = 3;
const int nCols = 4;
const int nPlotsPerCanvas = nRows * nCols;
const int nCanvases = (nBinsX + (nPlotsPerCanvas - 1)) / nPlotsPerCanvas;

canvases.resize(nCanvases);
for (int i = 0; i < nCanvases; ++i) {
//Let’s create a name:
const TString name = TString::Format(“Can%d”, i);
canvases[i] = new TCanvas(name.Data(), name.Data());
canvases[i]->Divide(nCols, nRows);
}
}[/code]

To “ACLiC” it you’ll have to include headers (TCanvas/TString/iostream etc.).
mcnv.C (761 Bytes)