Trouble storing a plot as a .txt file

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

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() (search this forum for examples if the documentation is not clear to you) and pass it directly to finalplot.open()

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

What’s xpeaks?

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!

So xpeaks are doubles, and then you have

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

and

   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.

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