Create new root file which contains all histograms , after fitting histograms from old root file

#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;

Double_t fitpeak (TH1F *histo)
{
TF1 *fS = new TF1(“fS”,“gaus”,900,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 cc()
{
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)”,60,900,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);
for(Int_t j=1;j<24; j++)
{
//sprintf(selection,“cut && TICEnergy>450 && TICEnergy<850 && TSd1sChannel==%d”,j); //For data
sprintf(selection,“TSd1rChannel==%d && TICEnergy>183 && TICEnergy<741”,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("");
    
    mean=fitpeak (h2);
    Hlist.Add(h2);
    cout<<"mean = "<<mean<<" j "<<j<<endl;
    //out<<"mean = "<<mean<<" j "<<j<<endl;
    fprintf(out,"%d\t%f\n",j,mean);
}
TFile f2("demo.root","recreate");
Hlist.Write();
f2.Close();
fclose(out);
return 0;

}

In demo.root i am getting 24 histograms but all are same, it will be a great help if my demo.root file contains all different histograms after fitting

ROOT Version: 6.24/06
Platform:
Compiler: Not Provided


Try with:

h2->SetName(histogram + "_" + j);
Hlist.Add(h2);

It worked, Thanxxx

is there any way by which all 24 histograms can be shown on the same canvas?

THStack

Can you please edit the above script or command that needed to be added inside the script to get all pdf into one?

{
  TString histogram = "h1";
  THStack *hs = new THStack();
  hs->SetName("all_my_" + histogram);
  hs->SetTitle("all my " + histogram + ";my x axis;my y axis");
  for (Int_t j = 1; j < 24; j++) {
    TH1F *h2 = new TH1F(histogram, histogram, 100, -5., 5.);
    h2->FillRandom("gaus");
    h2->SetName(histogram + "_" + j);
    h2->SetTitle(histogram + " " + j + ";my x axis;my y axis");
    h2->SetLineColor(j + (j < 10 ? 0 : 20)); // 1, ..., 9, 30, ..., 43
    hs->Add(h2);
  }
  TCanvas *c = new TCanvas("c", "c");
#if 1 /* 0 or 1 */
  hs->Draw("PADS");
#else /* 0 or 1 */
  Int_t os = gStyle->GetOptStat();
  gStyle->SetOptStat(0);
  hs->Draw("NOSTACK");
  gPad->BuildLegend(0.8, 0.3, 0.9, 0.9, "", "l");
  gStyle->SetOptStat(os);
#endif /* 0 or 1 */
  c->SaveAs("c.pdf");
}

it worked.Thanks