Strange binning

Hello rooters!

I am having problem with my binning and I just don’t get it som I hope someone can halp me! I am plotting an angular distribution and hence I wish to have a scale from 0 to 180 degrees. I want 100 bins. I actually do the binning already in my simulation and what I feed into root is a file with two columns, first the degrees 1.8 … 180 and then the relative fluence that I wish to put into this bin. I thought that this would be a piece of cake, but I was wrong! I don’t get the right value of the fluence into the bin. Instead of a nice rather smooth curve that the data actually represent I get cuts in my plot. What is wrong?

code:
void AngularPlot(){

int nbins = 100;
double Deg = 180;
double e0 = 0;
double np = 0;
double rseed1 = 0;
double rseed2 = 0;
double ang = 0;
double tot = 0;

// gStyle->SetOptStat(0)
namespace std;

// open file that we want to look at
ifstream in1;
in1.open(“ANGULAR.dat”);

//Canvas
TCanvas *c1 = new TCanvas(“c1”, “Angular”, 1);

//booking histogram
TH1F *h0 = new TH1F(“h0”, " Total ", nbins, 0., Deg);

//Read in from file and fill histogram
//header
in1 >> e0 >> np >> rseed1 >> rseed2;
cout<<e0<<" “<<np<<” “<<rseed1<<” “<<rseed2<<”\n";
while(in1.good()){
//data
in1 >> ang >> tot;
cout<<"ang “<<ang<<” tot “<<tot<<”\n ";
int bin = (int)(ang/(Deg/nbins));
h0->Fill(h0->GetBinCenter(bin),tot);
cout<<"BIN “<<bin<<” - “<GetBinContent(bin)<<”\n ";
ang=tot=0;
e0=np=rseed1=rseed2=0;
}
in1.close();

//Drawing histos
h0->SetXTitle("[Deg]");
h0->SetYTitle("[cm^{-2}]");
h0->Draw(“hist”);
}

Inputdata:

    1. 1016020429 1553263714
      1.8 36.1749977
      3.6 0.220671308
      5.4 0.374225713
      7.2 0.649006725
  1. 0.759265393
    10.8 0.945218395
    12.6 0.953536172
    14.4 0.774352495
    16.2 0.991271067
  2. 1.01696074
    19.8 1.0463202
    21.6 0.97895418
    23.4 0.855412477
    25.2 0.856918466
  3. 0.803985661
    28.8 0.727690239
    30.6 0.775622379
    32.4 0.798581626
    34.2 0.756413553
  4. 0.738381172
    37.8 0.778784707
    39.6 0.672177486
    41.4 0.656357204
    43.2 0.700662949
  5. 0.668758116
    46.8 0.703951172
    48.6 0.670521757
    50.4 0.682320861
    52.2 0.710157885
  6. 0.635493852
    55.8 0.759563959
    57.6 0.633255557
    59.4 0.676083148
    61.2 0.699167163
  7. 0.645641842
    64.8 0.628085077
    66.6 0.673808007
    68.4 0.660538882
    70.2 0.67871359
  8. 0.675053267
    73.8 0.631855224
    75.6 0.684026273
    77.4 0.66405067
    79.2 0.656249658
  9. 0.622882597
    82.8 0.67291118
    84.6 0.703886304
    86.4 0.652867639
    88.2 0.639902441
  10. 0.648630147
    91.8 0.62608392
    93.6 0.622929261
    95.4 0.641881615
    97.2 0.603641072
  11. 0.695439763
    100.8 0.637021556
    102.6 0.607602988
    104.4 0.61382647
    106.2 0.639009578
  12. 0.624984074
    109.8 0.625724874
    111.6 0.591332875
    113.4 0.590575956
    115.2 0.551182563
  13. 0.53337371
    118.8 0.532174326
    120.6 0.595547412
    122.4 0.501972037
    124.2 0.494610628
  14. 0.520089265
    127.8 0.485147335
    129.6 0.462185715
    131.4 0.510599498
    133.2 0.416307361
  15. 0.43541431
    136.8 0.42580101
    138.6 0.433846698
    140.4 0.44169899
    142.2 0.433890094
  16. 0.343757483
    145.8 0.367344194
    147.6 0.326351869
    149.4 0.301190767
    151.2 0.264200788
  17. 0.294439022
    154.8 0.301988895
    156.6 0.270270669
    158.4 0.237655994
    160.2 0.21798439
  18. 0.214961906
    163.8 0.203040201
    165.6 0.165975155
    167.4 0.140739394
    169.2 0.131605251
  19. 0.114239746
    172.8 0.0688049265
    174.6 0.068232885
    176.4 0.048025417
    178.2 0.0295376917
  20. 0.00210148256

Hi,
this is due to floating point precision. Your data points are right at the bin borders, so sometimes the data gets assigned to the wrong bin.

Simply shift the bins by half a bin width: TH1F *h0 = new TH1F("h0", " Total ", nbins, 0.9, Deg+0.9);
Axel.

thank you for the suggestion, unfortunately it did not change anything… the binning is still wrong.

Hi,
Sorry - I didn’t realize that you fill your hist in a very uncommon way. Why don’t you simply use h0->Fill(ang, tot)? The way you calculate your bin number provokes precision errors again.
Axel.

Ok, that does indeed sound like a simpler way to do it, but I wish to have 100 bins and I don’t understand how I will be able to put in ang>100, plus that ang is a double with decimals, do I not need an integer? Despite my skepticism, I tried this and it does looks better, but there are still a few cuts. When i check for the bincenter and the bincontent it does not mach what I would expect.
I reallly feels like I am doing this in a very aqyard way, but I cannot figure out a better way… The oint with binning already in my simulation is to keep the outputfiles handy.


Hi,

I believe you didn’t apply both my corrections :wink: Here’s the code I used, and for me it works (see attached gif).
Axel.


AngularPlot.C (928 Bytes)

Very nice, thank you Alex!