Segmentation Falt: a variable taking random value somehow

Hi all,

I made a function given below [1]. But at line 62 it takes random value for variable binFill. I am unable to understand why? This patch is very simple and I don’t see anything which can cause this. Please let me know if you find some issue.

I am pasting part of output here [2].

Thanks in advance for your help.

with regards,
Ram

[1]

  33 TH1F* ResetTo4bins(TH1F* wjet, int nbins, double xmin, double xmax){
  34 
  35    TH1F* hOut = new TH1F("hOut",";M_{WW} (Vjet Signal Region);",nbins, xmin, xmax);
  36 
  37    double binWidth = (xmax - xmin)/nbins;
  38 
  39    int binFill = 0;
  40 
  41    double hOutBinContent[4] = {0.0, 0.0, 0.0, 0.0};
  42 
  43    for (unsigned int ibin=1; ibin<wjet->GetNbinsX()+1; ++ibin){
  44      double xlow = wjet->GetBinLowEdge(ibin);
  45      double xhigh = wjet->GetBinLowEdge(ibin+1);
  46 
  47      double xlow_New = hOut->GetBinLowEdge(binFill);
  48      double xhigh_New = hOut->GetBinLowEdge(binFill+1);
  49 
  50      cout<< "binFill = " << binFill << endl;
  51      if (xlow < xlow_New){      cout<<" Don't fill lower bins..." << endl;cout<< "Debug 1: "<< binFill << endl; }
  52      else{
  53         if (xhigh <= xhigh_New)
  54         {
  55            hOutBinContent[binFill] += wjet->GetBinContent(ibin);
  56            cout<< "Debug 2: "<< binFill << endl;
  57         }
  58         else
  59         {
  60            binFill += 1;
  61            hOutBinContent[binFill] += wjet->GetBinContent(ibin);
  62            cout<< "Debug 3: "<< binFill << endl;
  63         }
  64      }
  65 
  66      cout<< "binFill = " << binFill << endl;
  67      cout<<ibin <<  "  xlow = " <<  xlow  <<  " xhigh = " << xhigh <<  "  xlow_New = " <<  xlow_New <<  "  xhigh_New = " <<  xhigh_New <<  "  hOutBinContent[" << binFill << "]      = " << hOutBinContent[binFill] <<endl;
  68 
  69 
  70    }
  71 
  72    return hOut;
  73 
  74 }

[2]

27  xlow = 1900 xhigh = 1950  xlow_New = 1550  xhigh_New = 2025  hOutBinContent[3] = 3.69794
binFill = 3
Debug 2: 3
binFill = 3
28  xlow = 1950 xhigh = 2000  xlow_New = 1550  xhigh_New = 2025  hOutBinContent[3] = 3.86957
binFill = 3
Debug 3: 1069678820
binFill = 1069678820

 *** Break *** bus error
29  xlow = 2000 xhigh = 2050  xlow_New = 1550  xhigh_New = 2025  hOutBinContent[1069678820] = 

_ROOT Version: 6.08/05
_Platform: fnal lpc cluster
Compiler: Not Provided


Replace line 60 (i.e. binFill += 1;) with:

if (binFill < ((sizeof(hOutBinContent) / sizeof(double)) -1)) binFill++;
else { std::cout << "Ouch!" << std::endl; break; }

Dear Coyote,

Thanks for the reply.

Now, I got Ouch!.

28  xlow = 1950 xhigh = 2000  xlow_New = 1550  xhigh_New = 2025  hOutBinContent[3] = 4.15508
binFill = 3
Ouch!

But, even in next loop binFill value should be 3. If you would like to see my full code, please see the attachment. Alpha_calculation.C (60.3 KB)

with regards,
Ram

Try with:

TH1F *ResetTo4bins(TH1F *wjet, int nbins, double xmin, double xmax) {
  TH1F *h =
    new TH1F("hOut", ";M_{WW} (Vjet Signal Region);", nbins, xmin, xmax);
  if (wjet) for (Int_t i = 1; i <= wjet->GetNbinsX(); i++)
              h->Fill(wjet->GetBinCenter(i), wjet->GetBinContent(i));
  return h;
}
1 Like

Thank you very much Coyote.

It is working now. Earlier I did not realize that this can work.

But, just for information do you have an idea why I was getting the issue in my case?

It’s obvious that, when “binFill” is used as the “hOutBinContent” array index, you must make sure that it’s valid for that array.

The TH1::Fill method will automatically make sure that the appropriate bin is incremented (including the “0” underflow bin and the “nbins + 1” overflow bin, if needed).

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