All generated pdf's into one pdf

#include <stdio.h>
#include
#include
#include “TCanvas.h”
#include “TF1.h”
#include “TFile.h”
#include “TGraphErrors.h”
#include “TH1.h”
#include “TH2.h”
#include “TSpectrum.h”
using namespace std;
int histocounter=0;

Double_t fitpeak (TH1F *histo)
{
TF1 *fS = new TF1(“fS”,“gaus”,800,1200); //Range change for both
fS->SetParNames(“height”,“mean”,“sigma”);
fS->SetParameter(0,6000);
fS->SetParameter(1,6);
fS->SetParameter(2,0.14);
histo->Fit(fS,“R”);
histo->Draw(“”);
Double_t mean00=fS->GetParameter(1);

    return mean00;
}

int Gaus()
{
FILE *out;
TString outputName = “Sd1r_adc.txt”;
out = fopen(outputName.Data(),“w”);
Double_t mean;
TFile *f = TFile::Open(“tree066330000.root”);
TTree Iris = (TTree)f->Get(“Iris”);
Char_t histo[50],selection[100];
TString histogram = “h1”;
sprintf(histo,“TSd1rADC>>h1(%d,%d,%d)”,40,800,1200); // binning should not be high nor less (60)

// std :: string cut = “.x elasag.C”; //S3cut May not be needed, can comment
// gROOT->ProcessLine(cut.data()); // related
TObjArray Hlist(0); // created an array to store objects like histograms in this case(h2)
for(Int_t j=0;j<24; j++) // here j < 32 for sectors while j<24 for rings ,
{
//sprintf(selection,“cut && TICEnergy>450 && TICEnergy<850 && TSd1sChannel==%d”,j); //For data
sprintf(selection,“TSd1rChannel==%d && TICEnergy>194 && TICEnergy<734”,j);
//for simIris // TICEnergy>280 && TICEnergy<700 // i have to put minimum and max values of TICEnergy depends on 3Sigma values after gaussian fitting

    Iris->Draw(histo,selection,"");
    TH1F *h2 = (TH1F*)gDirectory->Get(histogram.Data());
    
    //h2->Draw("");
    h2->SetName(histogram + "_"+j);
     
    mean=fitpeak (h2);
    gPad->Update();
histocounter++;
gPad->SaveAs(Form("hist%d.pdf",histocounter)); // this will create all as many pdf for number of histograms
    cout<<"mean = "<<mean<<" j "<<j<<endl;
    //out<<"mean = "<<mean<<" j "<<j<<endl;
    fprintf(out,"%d\t%f\n",j,mean);
    Hlist.Add(h2);
}


TFile f2("demo.root","recreate");
Hlist.Write();
f2.Close();

fclose(out);
return 0;

}

In this program, I am able to generate all histograms and save them as separate pdf’s. But I want one pdf which include all histograms. Thanks

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


You need to open a multipage PDF in the first SaveAs and close it at the last one. See the example in the TPDF documentation (note the parenthesis just after “pdf”):

canvas->Print("plots.pdf("...
...
canvas->Print("plots.pdf)"...

https://root.cern/doc/v610/classTPDF.html

Or use an external program - e.g. pdfunite in-1.pdf in-2.pdf in-n.pdf out.pdf