Histogram from a text file

I would like to use this code on another textfile that contain negative. Is it possible? When I did it all the negative values were plotted as zeros. Thanks Chloe.

That’s odd, it works for me fine. Could you please send me a piece of your text file with negative values?

  TTree *MyTree = new TTree("MyTree", "MyTree");
  MyTree->ReadFile("MyData.txt", "Angle:Energy");
  MyTree->SetEstimate(MyTree->GetEntries()); // not strictly necessary
  TH2F *MyHisto = new TH2F("MyHisto", ";Polar scattering angle [degrees];Energy [keV]", 100, MyTree->GetMinimum("Angle") - 1, MyTree->GetMaximum("Angle") + 1, 100, MyTree->GetMinimum("Energy") - 1, MyTree->GetMaximum("Energy") + 1);
  MyTree->Project("MyHisto", "Energy:Angle");

It’s fine now. The problem was the textfile. There was a character on one of the lines that should be there!!

Hi Yus,

I tried to work with the your ROOT macro but it doesn’t work , it shows me an error : variable length array declaration not allowed at file scope !

What should i do ?


I believe you simply need to change lines 47 and 48 to the following:

float *x = new float[numL];
float *y = new float[numL];

Note: I have not tested this.

Hello Smith,

I tried but this is happened : warning: ISO C++11 does not allow conversion from string literal to ‘char *’

Try this on line 24

const char * filename = "testfile.txt";

I tried but this happened again :confused:
2017-04-11 15:42:40.834 root.exe[2155:59829] Fatal error: requested non-existing drawable 154
2017-04-11 15:42:40.834 root.exe[2155:59829] This window not found among allocated/deleted drawables

Thanks for your attempts to help me

I’m not sure, it appeared to work for me with an empty text file. Can you post your text file?

MyData.txt (45 Bytes)

Result using your data (I changed the marker style to be easier to see.):

Looks like there may be some issue on your end. I used ROOT 6.08/06.

i have root_v6.09.02, i will test your version and see … hope it will work

Send me your ROOT macro which you have tested with !


It is the same as above with the modifications we discussed:
hist2Dfromfilev03.c (2.4 KB)

it doesn’t work :confused:
i have to change the root version i think

I am trying to use your code but keep getting the error below. Any help pls.

root [0] .L histo.cpp
/home/munirat/MCA/./histo.cpp:25:20: warning: ISO C++11 does not allow
      conversion from string literal to 'char *' [-Wwritable-strings]
        char * filename = "b.txt";
/home/munirat/MCA/./histo.cpp:48:8: error: variable length array declaration not
      allowed at file scope
        float x[numL];
              ^ ~~~~

Did you read the discussion toward the bottom of the thread? These issues were discussed and solved:

I followed your correction which clear the error and show something like this but i cant see the plooted values


What are the numbers you are plotting? Could you attach your final code (histo.cpp) and your data file?