Different SumOfWeights for histograms with different binnings

ROOT Version: 6.18.04
Platform: centos7

Dear experts,

I am trying to fill histograms from a TTree, and I encounter an unsettling issue: it seems that the sum of weights of the histograms depend on the binning…

In my code, I compare the sum of weights for one histogram with 4 bins, one with 40 bins and the sum of weights computed “by hand”. It is as follows (the input file can be found there https://cernbox.cern.ch/index.php/s/abCWpBwGftN8L9F):

  TFile *f = new TFile("simpletree.root", "r");
  TTree *tree = (TTree*)f->Get("em");

  TTreeFormula *CorrKK = new TTreeFormula("CorrKK", "CorrKK", tree);
  TTreeFormula *weight = new TTreeFormula("weight", "weight", tree);

  TH1F* h4 = new TH1F("","",4,-1,1);
  TH1F* h40 = new TH1F("","",40,-1,1);

  int i = 0;
  double sumw = 0;
  for(int i=0; i<tree->GetEntries(); i++){
    sumw += weight->EvalInstance();
    h4->Fill(CorrKK->EvalInstance(), weight->EvalInstance());
    h40->Fill(CorrKK->EvalInstance(), weight->EvalInstance());


This gives me the following output:


The sum of weights computed by hand is different from those of both histograms, which are also different from one another.
Would anyone have a clue on how to explain (and solve) this behaviour?

Use “TH1D”.

Thanks a lot, this works!
May I ask, as a follow-up question, how to do this with TTree::Draw rather than with an event loop.

For instance if I do:

  TFile *f = new TFile("simpletree.root", "r");
  TTree *tree = (TTree*)f->Get("em");


  TH1D *h4 = (TH1D*)gDirectory->Get("h4");
  TH1D *h40 = (TH1D*)gDirectory->Get("h40");


I get different sum of weights for the two histograms…

Search for “double” in the “`root-config --etcdir`/system.rootrc” file.

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