Negative valued bin in histograms

Hi everyone and thanks in advance for your answers

I am a coding an application that read some file and fill histogram with the data

I’m simply filling the bins this way :

[quote]for(int i=0;i<13;i++){


 TH1F *histox = new TH1F("histo","h",20,0,2.0);
 TH1F *histoy = new TH1F("histo","h",20,0,2.0);
 TH1F *histoz = new TH1F("histoz","h",20,0,2.0);

  for(int j=0; j<Nphotons;j++){


And to my greatest surprise , when I draw those, I get histo with negative bin value !!!
How can it be possible ? I must say I’m totally clueless about what is going on in there.

Thank You !

Mathieu Benoit
Université de Montréal

Why do you have the histograms declaration within the “i” loop ? It looks a bit strange seems to me. Anyway, can you send to this forum a small running example reproducing your problem ?

Why not? He has the nested loop in which he fills hists.


What is really bad in his script - new expressions. He can simply create local vairables TH1F histox;TH1F histoy;TH1F histoz; BEFORE the enclosing loop, and after that, at the end of each iteration (of enclosing loop) simply clear hists.
Unfortunatly, many ROOT’s users create dynamic objects, though in many cases they can use local stack variables - IMHO it’s the result of ROOT’s tutorials (which contains a lot of new expression). Such a code that we can see in original message - well known example of “Java outperforms C++!!!”, Java programmers AFAIK can write the same code, but their Java has garbage collector and sofisticated algorithms, to make such ugly code more effective.


Ok fine, but still we need a running example reproducing the problem.

Pleas, spare me a little bit ! :blush: A couple of month ago I was not even able to telll you the difference between a pointer and teapot :wink:

And yes your right, I’ve learn to do this from the user guide. Quite frankly, last time my teachers used a language, it was probably fortran so …

So good, I have bad programming habits , I think it would be more constructive to show me what would be considered a good habit.¸ I learn all by myself so it would be good to tell me when I’m going in the wrong direction.

I join the code and the data. If I add a line to draw the histox, histoy, histoz, I get negative valued bin.

I compiled it using ACLiC through CINT.
select_output.txt (1.7 MB)
mc.cpp (6.11 KB)

When I run your example in ROOT I get:

May be you can fix that first .

Actually, while I was fixing that (wrong file name), I tried it again and it gets even more weird.

if I interpret it as you did, .x mc.cpp, it works just fine,
but then ,if I compile it with .xmc.cpp+, nothing works.
Histo are empty , but say they are filled and return a credible rms and mean value.
And the graph have the good shape, but really not the good scale!

At least I can get my graphs, but I would still lik to know how you would have done with histo declaration :slight_smile:

I join the code. I’ll still try to reproduce my bug :confused:

Thank you for taking the time to answer my question :slight_smile:

mc.cpp (6.05 KB)

Attached find a cleaned up version of your program. It is working both interpreted or using AClic.
mc.cpp (4.4 KB)

I have to take the habit of cleaning my code like that :slight_smile:

Merci Beaucoup!

Yes… you find problems more easily in “clean code” :slight_smile: . Note that the main change was in the following lines:

      histox = new TH1F(Form("histox%d",i), Form("histox%d",i), 20, 0, 2.0);
      histoy = new TH1F(Form("histoy%d",i), Form("histoy%d",i), 20, 0, 2.0);
      histoz = new TH1F(Form("histoz%d",i), Form("histoz%d",i), 20, 0, 2.0);

I used “Form” to generate different histograms’ names. In your previous version the histograms were overwritten 13 times. (cf the error messages about the memory leaks).