Renaming multiple plots with incremental numbers

Hello,

I have multiple plots. I want to save them with a name along with a number in increasing order using a loop. Something like:

file1.pdf
file2.pdf  
...   

Can anyone suggest how can I do this? There are many plots which makes manually saving each of them very tedious. Is there a quick way to save the plots with different names?
Thank you for your time!


ROOT Version: 6.18/04
Platform: C++
Compiler: Visual Studio Code

1 Like

If pointers to the canvases are stored in c[] you can do:

for (int plot_um=1; plot_num<=number_of_plots; I++) {
   c[plot_um]->Print(Form("file%d.pdf", plot_um));
}

I tried using an array for canvases, but it gave me an error. Here is what I have -

Double_t min, max;
   TH1F *myhist[npeaksFound];

   TCanvas *ch[npeaksFound];

   char *histname = new char[npeaksFound];

   for (int i=0;i<npeaksFound;i++) {

       sprintf(histname,"histo%d",i);

       min = xpeaks[i] - 5;

       max = xpeaks[i] + 5;

       cout << "peak , xmin, xmax = " << xpeaks[i] << " " << min << " " << max << endl;

       myhist[i] = (TH1F*)hTVsC_backsub->Clone();

       TCanvas *ch=new TCanvas(histname,"ch");

       myhist[i]->GetXaxis()->SetRangeUser(min,max);

       Double_t binmaxY = myhist[i]->GetMaximumBin();

       Double_t binmaxX = myhist[i]->GetBinCenter(binmaxY);

       cout << binmaxY << "X and Y "<< binmaxX<< endl;

       TF1* Tfit =new TF1("Tfit","gaus");

       myhist[i]->Fit("Tfit");

       ch->cd();

       myhist[i]->Draw("");

       Double_t p1 =    Tfit->GetParameter(1);

       Double_t p2 =    Tfit->GetParameter(2);

       cout << "intercept p1: " << p1 << "\tslope p2: " << p2 << endl;

If I use something like TCanvas *ch[i]=new TCanvas(histname,“ch”); this is what I get as an error-

error: variable-sized object may not be initialized
           TCanvas *ch[i]=new TCanvas(histname,"ch");

Any help is appreciated! Thank you for your time!

If you create new canvases in a loop that way you do not need an array of TCanvas.
The following is enough:

   Double_t min, max;
   TH1F *myhist[npeaksFound];
   char *histname = new char[npeaksFound];

   for (int i=0;i<npeaksFound;i++) {
       sprintf(histname,"histo%d",i);
       min = xpeaks[i] - 5;
       max = xpeaks[i] + 5;
       cout << "peak , xmin, xmax = " << xpeaks[i] << " " << min << " " << max << endl;
       myhist[i] = (TH1F*)hTVsC_backsub->Clone();

      TCanvas *ch=new TCanvas(histname,"ch");
 
       myhist[i]->GetXaxis()->SetRangeUser(min,max);
       Double_t binmaxY = myhist[i]->GetMaximumBin();
       Double_t binmaxX = myhist[i]->GetBinCenter(binmaxY);

       cout << binmaxY << "X and Y "<< binmaxX<< endl;

       TF1* Tfit =new TF1("Tfit","gaus");
       myhist[i]->Fit("Tfit");

       ch->cd();
       myhist[i]->Draw("");
       ch->Print(Form("file%d.pdf", i));

       Double_t p1 =    Tfit->GetParameter(1);
       Double_t p2 =    Tfit->GetParameter(2);
       cout << "intercept p1: " << p1 << "\tslope p2: " << p2 << endl;
   }
1 Like

That worked like a charm. Just one more question, is there a way to create a single pdf file for all these plots and save them together? I read a few comments and I am trying something like this-

ch->cd();

   myhist[i]->Draw("");

   ch->Print(Form("file%d.pdf(", i));

   ch->Print(Form("file%d.pdf", i));

   ch->Print(Form("file%d.pdf)", I));

but, it still generates n no. of separate pdf files. Any help? Thank you so much for your time and help! Really appreciate it!

   Double_t min, max;
   TH1F *myhist[npeaksFound];
   char *histname = new char[npeaksFound];

   TCanvas *ch=new TCanvas(histname,"ch");
   ch->Print("file.pdf[");

   for (int i=0;i<npeaksFound;i++) {
       sprintf(histname,"histo%d",i);
       min = xpeaks[i] - 5;
       max = xpeaks[i] + 5;
       cout << "peak , xmin, xmax = " << xpeaks[i] << " " << min << " " << max << endl;
       myhist[i] = (TH1F*)hTVsC_backsub->Clone();
     
       TCanvas *ch=new TCanvas(histname,"ch");
 
       myhist[i]->GetXaxis()->SetRangeUser(min,max);
       Double_t binmaxY = myhist[i]->GetMaximumBin();
       Double_t binmaxX = myhist[i]->GetBinCenter(binmaxY);

       cout << binmaxY << "X and Y "<< binmaxX<< endl;

       TF1* Tfit =new TF1("Tfit","gaus");
       myhist[i]->Fit("Tfit");

       ch->cd();
       myhist[i]->Draw("");
       ch->Print("file.pdf")

       Double_t p1 =    Tfit->GetParameter(1);
       Double_t p2 =    Tfit->GetParameter(2);
       cout << "intercept p1: " << p1 << "\tslope p2: " << p2 << endl;
   }
   ch->Print("file.pdf]")
1 Like

Oh, thank you so much! @couet This worked so well! You’re the best :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.