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

**URL:** <https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [August 20, 2022, 7:09pm UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262 "2022-08-20T19:09:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 20, 2022, 7:09pm UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262/1 "2022-08-20T19:09:32Z")

</div>

#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

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 20, 2022, 8:10pm UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262/2 "2022-08-20T20:10:26Z")

</div>

Try with:

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

```

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 20, 2022, 8:30pm UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262/3 "2022-08-20T20:30:56Z")

</div>

It worked, Thanxxx

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 21, 2022, 7:57pm UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262/4 "2022-08-21T19:57:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 22, 2022, 6:56am UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262/5 "2022-08-22T06:56:52Z")

</div>

[THStack](https://root.cern/doc/master/classTHStack.html)

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 22, 2022, 3:39pm UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262/6 "2022-08-22T15:39:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 22, 2022, 4:42pm UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262/7 "2022-08-22T16:42:43Z")

</div>

```cpp
{
  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");
}

```

---

<div class="post-metadata">

**Author:** ![gurmukh\_dhindsa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gurmukh_dhindsa/32/30326_2.png) [@gurmukh\_dhindsa](https://root-forum.cern.ch/u/gurmukh_dhindsa)\
**Post date:** [August 22, 2022, 8:05pm UTC](https://root-forum.cern.ch/t/create-new-root-file-which-contains-all-histograms-after-fitting-histograms-from-old-root-file/51262/8 "2022-08-22T20:05:14Z")

</div>

it worked.Thanks
