Read Data from a 2D histogram

I have a 2D spectra and need to make a .dat file of the data sets. How can I proceed. I am also attaching the .C file of the 2D histogram.
spectra.C (1.5 MB)

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

What do you mean by ".dat’ file ? ASCII file ? ROOT file ?
Can you give more details ?

Sir I mean a simple text file in any format.


will do it

you can redirect the output in a file with:

root [1] .> spectra.txt

what are .> in this statement. Are they operators

Try: root [0] .?
See: ROOT User’s Guide -> The C++ Interpreter Cling -> The ROOT Prompt

This is not working. The compiler is showing error regarding this expression “.>”

Which ROOT version are you using ?

I am using ROOT 6.08

see: Redirecting output, root 6


… The compiler is showing error …

add the following at the end of your macro,
Note: there under/overflows you might want to skip,
start with bin 1 and end at PSD12->GetNbinsX() if thats the case

   ofstream outf("myspectra.txt");
   auto ax = PSD12->GetXaxis();
   auto ay = PSD12->GetYaxis();
   int n = 0;
   for (int ix=0; ix<=PSD12->GetNbinsX()+1; ix++){
      for (int iy=0; iy<=PSD12->GetNbinsY()+1; iy++){
         if (PSD12->GetBinContent(ix, iy) != 0){
            outf<< ax->GetBinCenter(ix) << " " << ay->GetBinCenter(iy)
            << " " <<PSD12->GetBinContent(ix, iy) << endl;
   cout << "lines written " << n << endl;


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.