# Save histogram one by one ata time

**URL:** <https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914>\
**Category:** Newbie\
**Created:** [January 2, 2021, 9:03pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914 "2021-01-02T21:03:28Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 2, 2021, 9:03pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/1 "2021-01-02T21:03:29Z")

</div>

Happy New year to everyone.

I have problem, trying to loop through a list of .root file, get histogram B, do calculation with histogram A, den put histograms A and B on same canvas, den save each individual canvas. Trouble is saving each individual histogram A and B for each file.

A sample of the code:

```auto
void contam_study()
{
  char const *infile = "Bgoodndst.list";
  
  TFile *file = TFile::Open("Atrigger_goodndst.root");

  TTree *tree = (TTree*)file->Get("tree");
  Int_t nevents = tree->GetEntries();
  cout << "Nevents = " << nevents << endl;
  Int_t s_stvar;
  Float_t stvar[50];
  TBranch *s_stv_branch = tree->GetBranch("s_stvar");
  s_stv_branch->SetAddress(&s_stvar);
  TBranch *ntv_branch = tree->GetBranch("stvar");
  ntv_branch->SetAddress(&stvar);

  //histogram A abd B
  TH1F *hsq = new TH1F("hsq","bcq",100,0.,2500);
  // hsq->SetBit(TH1::kCanRebin);
  TH1F *h_sq = new TH1F("h_sq","bnsq",100,0,2500);

  
// get variables from fileA
  for (Int_t ievt=0; ievt<n events; ievt++)
  {

    tree->GetEvent(ievt);
    float bsum = stvar[2] + stvar[3];
    hsq->Fill(bsum);

  }//end of for loop

  //file B files one at time, den,calculate
  ifstream runlist(infile);//file stream 
  char filepath [900];
  TFile *tfile[10000];
  int nfile = 0;

  if(runlist)
  {
    while( runlist.good() ) // continue reading file
    {
      runlist.getline(filepath,900);
      if ( runlist.eof() ) break;

      tfile[nfile] = new TFile(filepath, "read");

      if ( tfile[nfile] )
      {
        tfile[nfile]->GetObject("h_sum",h_sq);
      }

   TCanvas *bb = new TCanvas("bb","A/B_sum",550,425);
   TH1 *histo = hsq->Clone("histo");
   histo->Divide(h_sq);
   TF1 *poly0 = new TF1("poly0","pol0",1800,2000);
   histo->Fit(poly0,"R");
   gPad->SetLogy(1);
   Double_t scale = poly0->GetParameter(0);
   cout << "scale = " << scale << endl;

   THStack *hs = new THStack("hs","Stacked histogram");
   hs->Add(hsq);
   hsq->SetLineColor(4);
   hs->Add(h_sq);
   h_sq->SetLineColor(2);
   h_sq->Scale(scale);
   hs->Draw();

  tfile[nfile]->Close(); //lock di file

  nfile++;//stepwise
  }
}
else
 {
   cout << "file " << infile << " does not exist" << endl;
}
    
  //fi save indiv plots
  TFile *h = new TFile("outfile.root", "RECREATE");
  h->cd();
  hs->Write();//fi di individual files out.

  runlist.close();//close list file.

}//end of void

```

Awaits your kind assistance.  
Cheers  
Prya13

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 3, 2021, 7:53am UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/2 "2021-01-03T07:53:41Z")

</div>

Move this

```auto
TFile *h = new TFile("outfile.root", "RECREATE");

```

to just before the creation of hsq and h\_sq (TH1F \*hsq = new TH1F…), and move these two

```auto
  h->cd();
  hs->Write();

```

inside the while loop over runlist, just after `hs->Draw();`  
Since you are saving many different hs stacks, you should give them different names, e.g. something like

```auto
  hs->Write(Form("hs%d",nfile));

```

instead of Write().  
And don’t forget to close the root file at the end of your macro

```auto
  h->Close();
  delete h;

```

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [January 3, 2021, 5:16pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/3 "2021-01-03T17:16:51Z")

</div>

Hello @Prya13,

Thanks @dastudillo for contributing! If the answer provided by @dastudillo does not solve your issue, feel free to ask again.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 3, 2021, 6:54pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/4 "2021-01-03T18:54:42Z")

</div>

Thank you so much dastudillo, for your response.  
The file are been saved individually but I am seeing the two different histogram, not sure what is happening now. Here is an example:

 ![hs_plot](https://root-forum.cern.ch/uploads/default/original/3X/f/9/f931e3ea78e972fc4d82e78f5051f343d7772def.png)

Cheers  
Priya

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 3, 2021, 6:58pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/5 "2021-01-03T18:58:35Z")

</div>

Jalopezg,

Thank you for your response. Dastudillo helped marvelously, now I have another issue regarding the outcome of the plots.

Cheers,  
Prya13

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 18, 2021, 7:10am UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/6 "2021-01-18T07:10:33Z")

</div>

Dear All,

I have another concern regarding the same macro above, I am trying to reset the THStack in the loop each time I draw before moving on to the next file.

hs-\>Reset();  
but I got error: Can’t call THStack::Reset(), how can I reset the THStack each time?  
Can you kindly make me suggestions to fix this error?

Cheers  
Prya

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 18, 2021, 8:02am UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/7 "2021-01-18T08:02:39Z")

</div>

```auto
hs->GetStack()->Clear();

```

Should do it.

> [@Prya13](#):
>
> hs-\>Reset();  
> but I got error: Can’t call THStack::Reset()

Check the THStack doc. THStack doesn’t have a such method.

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 18, 2021, 9:08am UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/8 "2021-01-18T09:08:18Z")

</div>

Thank you for your response, however I got error Can’t call GetStack()

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 18, 2021, 9:23am UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/9 "2021-01-18T09:23:04Z")

</div>

[THStack::GetStack](https://root.cern/doc/master/classTHStack.html#a849ee2cd11f7780f7e96f697bf03d9af) exists.  
Can you post the script producing this error ?

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 18, 2021, 8:54pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/10 "2021-01-18T20:54:48Z")

</div>

I am using the same script at the top of this thread, only added hs-\>Reset() and the modifications from dastudilllo.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 19, 2021, 9:13am UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/11 "2021-01-19T09:13:03Z")

</div>

If you show the code people can give better help by seeing what exactly you are trying; as you know from my first answer, it’s not only the commands you use but where they are. Where are you trying to call GetStack? Maybe try doing it before closing tfile[nfile]?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 19, 2021, 9:51am UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/12 "2021-01-19T09:51:01Z")

</div>

> [@Prya13](#):
>
> I am using the same script at the top of this thread, only added hs-\>Reset() and the modifications from dastudilllo.

Reset does not exist for THStack … post your code please.

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 19, 2021, 10:42am UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/13 "2021-01-19T10:42:06Z")

</div>

Thank you so much. This is the macro using:

```auto
void contam_study()
{
  char const *infile = "Bgoodndst.list";
  
  TFile *file = TFile::Open("Atrigger_goodndst.root");

  TTree *tree = (TTree*)file->Get("tree");
  Int_t nevents = tree->GetEntries();
  cout << "Nevents = " << nevents << endl;
  Int_t s_stvar;
  Float_t stvar[50];
  TBranch *s_stv_branch = tree->GetBranch("s_stvar");
  s_stv_branch->SetAddress(&s_stvar);
  TBranch *ntv_branch = tree->GetBranch("stvar");
  ntv_branch->SetAddress(&stvar);

   TFile *h = new TFile("outfile.root", "RECREATE");
  //histogram A abd B
  TH1F *hsq = new TH1F("hsq","bcq",100,0.,2500);
  // hsq->SetBit(TH1::kCanRebin);
  TH1F *h_sq = new TH1F("h_sq","bnsq",100,0,2500);
  THStack *hs = new THStack("hs","Stacked histogram");

  
// get variables from fileA
  for (Int_t ievt=0; ievt<n events; ievt++)
  {

    tree->GetEvent(ievt);
    float bbcnq= stvar[2];
    float bbcsq= stvar[3];
    float bsum = bbcnq + bbcsq;

    hsq->Fill(bsum);
  }//end of for loop

  //file B files one at time, den,calculate
  ifstream runlist(infile);//file stream 
  char filepath [1000];
  TFile *tfile[10000];
  int nfile = 0;

  if(runlist)
  {
    while( runlist.good() ) // continue reading file
    {
      runlist.getline(filepath,1000);
      if ( runlist.eof() ) break;

      tfile[nfile] = new TFile(filepath, "read");

      if ( tfile[nfile] )
      {
        tfile[nfile]->GetObject("h_sum",h_sq);
      }

   TCanvas *bb = new TCanvas("bb","A/B_sum",550,425);
   TH1 *histo = hsq->Clone("histo");
   histo->Divide(h_sq);
   TF1 *poly0 = new TF1("poly0","pol0",1800,2000);
   histo->Fit(poly0,"R");
   gPad->SetLogy(1);
   Double_t scale = poly0->GetParameter(0);
   cout << "scale = " << scale << endl;

  //reset each histogram each time goes through the loop
   hs->GetStack()->Clear();
   hs->Add(hsq);
   hsq->SetLineColor(4);
   hs->Add(h_sq);
   h_sq->SetLineColor(2);
   h_sq->Scale(scale);
   hs->Draw();
   gPad->SetLogy(1);//plotting log
   gPad->Update();//updating camvas each time

   h->cd();
   hs->Write(Form("hs%d",nfile));//fi di individual files out.
        
        
  tfile[nfile]->Close(); //lock di file

  nfile++;//stepwise
  }
}
else
 {
   cout << "file " << infile << " does not exist" << endl;
}
    
  //fi save indiv plots
  
  runlist.close();//close list file.

}//end of void

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 19, 2021, 1:27pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/14 "2021-01-19T13:27:43Z")

</div>

Are you sure you ran this macro ? there is a typo on the line:

```auto
  for (Int_t ievt=0; ievt<n events; ievt++)

```

I removed the extra space…

Also on this line there is a mistake:

```auto
   TH1 *histo = hsq->Clone("histo");

```

it should be:

```auto
   TH1 *histo = (TH1*)hsq->Clone("histo");

```

having done that changes the macro compiles but I am missing your data files in order to run it.

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 19, 2021, 2:03pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/15 "2021-01-19T14:03:00Z")

</div>

Couet,  
Thank you so much for your assistance.  
Yes the macro runs however the line: hs-\>GetStack()-\>Clear(); gives error: Error: illegal pointer to class object GetStack() 0x0 70

If I comment it out no error but histograms is overlaying each other on each turn through the loop but I would like to reset the hs each time through the loop.

I don’t know how to reset the THStack on each turn, can you kindly suggest me?  
Cheers

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 19, 2021, 2:12pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/16 "2021-01-19T14:12:22Z")

</div>

Move that line after `hs->Write(...)`, or try

```auto
   if (nfile>0) hs->GetStack()->Clear();

```

---

<div class="post-metadata">

**Author:** ![Prya13](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/ea666f/32.png) [@Prya13](https://root-forum.cern.ch/u/Prya13)\
**Post date:** [January 19, 2021, 2:59pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/17 "2021-01-19T14:59:41Z")

</div>

dastudillo,

Thank you for your assistance, tried both way but no solution still. Plots still overlapping on each other.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 21, 2021, 1:34pm UTC](https://root-forum.cern.ch/t/save-histogram-one-by-one-ata-time/42914/18 "2021-01-21T13:34:17Z")

</div>

Ok, can you provide your data files so we run your macro ?
