# Trouble storing a plot as a .txt file

**URL:** <https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128>\
**Category:** Newbie\
**Created:** [May 25, 2022, 10:06pm UTC](https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128 "2022-05-25T22:06:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nisha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nisha/32/28801_2.png) [@Nisha](https://root-forum.cern.ch/u/Nisha)\
**Post date:** [May 25, 2022, 10:06pm UTC](https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128/1 "2022-05-25T22:06:46Z")

</div>

Dear Experts,

I have a plot with multiple peaks. I used TSpectrum to identify peaks and in a for loop, I am zooming each peak and storing the peaks as .pdf file. However, I also want to store the data as .txt files with corresponding names but I am running into two problems -

1. I am using GetBinContent() but the .txt file stores 0 for each value.
2. How can I assign different names to each txt file and store them separately?

Here is a small chunk of my code-

Double\_t min, max, sigma;

TH1F \*myhist[npeaksFound];

char \*histname = new char[npeaksFound];

TCanvas \*ch=new TCanvas(histname,“ch”);

ch-\>Print(“Peaks\_disk118.pdf[”);

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

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

   min = xpeaks[i] - 5;

   max = xpeaks[i] + 5;

   

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

   //Saving data to txt file

   ofstream finalplot;

   string plotname = "Final Plot_disk118" + std::to_string(i);

   finalplot.open("/root_v6.18.04/macros/HandOff/AlThin/Excitation Energy comparision/plotname.txt");

   Double_t xi,yi;

   for(int j=min; j<max+1; j++){

    yi = myhist[i]->GetBinContent(j);

    finalplot << j << " " << yi << endl;

   }

   ch->cd();

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

   ch->Print("Peaks_disk118.pdf");

```

}

ch-\>Print(“Peaks\_disk118.pdf]”);

This may be trivial but I am not very experienced with root. Can anyone please help. Thank you for your time and help.

_ROOT Version:_ 6.18/04  
_Platform:_ C++  
_Compiler:_ Visual Studio Code

---

<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:** [May 26, 2022, 4:39am UTC](https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128/2 "2022-05-26T04:39:09Z")

</div>

1 - Start by making sure min and max have the values you expect.  
2 - That’s more of a c++ question (for example, search on google, or whatever, how to concatenate strings). You can also use [TString::Form()](https://root.cern/doc/master/classTString.html#aeb44d6183d8b1f1b7090dbd3c93f5e39) (search this forum for examples if the documentation is not clear to you) and pass it directly to `finalplot.open()`

---

<div class="post-metadata">

**Author:** ![Nisha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nisha/32/28801_2.png) [@Nisha](https://root-forum.cern.ch/u/Nisha)\
**Post date:** [May 26, 2022, 6:18pm UTC](https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128/3 "2022-05-26T18:18:30Z")

</div>

Hi @dastudillo,

Thank you for getting back. I have the correct values for min and max. I still when trying to fill a txt file, it does not fill the y values correctly. I am using GetBinContent(i) for the histograms. I tried to do the same for the original plot which originally has all the zoomed peaks but I run into the same trouble there too. All the bin content gives 0.  
Any help is appreciated! Thank you!

Nisha

---

<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:** [May 26, 2022, 7:14pm UTC](https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128/4 "2022-05-26T19:14:02Z")

</div>

What’s xpeaks?

---

<div class="post-metadata">

**Author:** ![Nisha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nisha/32/28801_2.png) [@Nisha](https://root-forum.cern.ch/u/Nisha)\
**Post date:** [May 26, 2022, 7:27pm UTC](https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128/5 "2022-05-26T19:27:56Z")

</div>

I have a spectrum with multiple peaks. I used Tspectrum to identify those peaks. xpeaks are the peaks identified by spectrum. I have 14 peaks. May be this chunk will help you understand what I have

```
TSpectrum *mySpec = new TSpectrum();
Int_t npeaksFound = mySpec->Search(hTVsC_backsub, 20," ",0.1);
Double_t *xpeaks = mySpec->GetPositionX();
Double_t x[npeaksFound];
for(Int_t iter = 0; iter<npeaksFound; iter++ ) {
cout << xpeaks[iter] << endl;
}

```

Thank you for your time and help!

---

<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:** [May 26, 2022, 7:43pm UTC](https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128/6 "2022-05-26T19:43:05Z")

</div>

So xpeaks are doubles, and then you have

```auto
   min = xpeaks[i] - 5;
   max = xpeaks[i] + 5;

```

and

```auto
   for(int j=min; j<max+1; j++){
    yi = myhist[i]->GetBinContent(j);

```

For this to work, min and max should be (integer) bin numbers, not x coordinates.

---

<div class="post-metadata">

**Author:** ![Nisha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nisha/32/28801_2.png) [@Nisha](https://root-forum.cern.ch/u/Nisha)\
**Post date:** [May 26, 2022, 8:31pm UTC](https://root-forum.cern.ch/t/trouble-storing-a-plot-as-a-txt-file/50128/7 "2022-05-26T20:31:08Z")

</div>

Hi @dastudillo …

Oh, that makes sense. Is there any workaround you can suggest? I want to store this data so that I can read this data again in a different program and plot it. Any ideas are appreciated! Thank you for your time. You have been very helpful!

Nisha
