Outputting one figure per iteration from for loop

I am fitting a function to a histogram and looking at how the fit changes as I vary some parameters of the data.
I have a for loop, such that in every iteration the data parameters change and I fit my function.
The fit is failing for some iterations, though not always subsequent, so I wanted to be able to look at the actual graphs from every iteration to visually see what is wrong…

I was wondering whether there is a way of declaring

TCanvas * c[i] = new TCanvas(“c[i]”, “i”, etc…)

with i going from (say) 1 to 25, and then, in the for loop, including something like

c[i]->cd();
name_of_my_fit -> Draw()

in order to output a graph from each iteration.

Thanks for your help!

Hi,

this is definitively possible.
One caveat is that not only the canvases but also the drawn objects should survive out of the scope of the for loop. Alternatively, even with just one canvas and not an array, you could dump images on disk of your plots using TCanvas::Print(const char*).

Best,
Danilo

Try something like this: { // ... const UInt_t kMaxIters = 25; TCanvas *c[(kMaxIters)]; // ... for(UInt_t i = 0; i < (kMaxIters); i++) { // ... c[i] = new TCanvas(TString::Format("c%i", i), TString::Format("Iteration %i", i)); // ... MyHisto->Fit(); // automatically draws it, too gPad->Modified(); gPad->Update(); // make sure it's really drawn // ... } // ... }