GetBinContent() gives 0

Hi ROOT family,

I am a stupid beginner on ROOT. I was trying to find out the bin numbers (counts) in some particular channel (the horizontal axis is my channel number) and a range of channels. Then I may do some populate work of these bin counts. However, I could not make it. My script gives me zero count
pedestalhistest.C (841 Bytes)

void pedestalhistest()
{
    
    TH1F *hist = new TH1F("hist","Histogram",15000,0,300);
    
    fstream file;
    file.open("Pedestal_0_2_5_2023_002.ascii", ios::in);
    
    Double_t n;
    Int_t i;
    
    
    
    while(1)
    {
        double x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11;
        
        file >> x1 >> x2 >> x3 >> x4 >> x5 >> x6 >> x7 >> x8 >> x9 >> x10 >> x11;
        hist->Fill(x4);

        
        
        if(file.eof()) break;
      }
    
    for (i=1; i<= hist->GetNbinsX();i++){
        n = hist->GetBinContent(i);    }
    
    
    
    file.close();
    
    hist->Scale(1);
 
    hist->GetXaxis()->SetTitle("Channels");
    hist->GetYaxis()->SetTitle("Particle counts");
    
    
   
    TCanvas *c1 = new TCanvas();
    hist->Draw();
 
    
    cout << "Bin counts: "<< n << endl;
  }

. Could anyone help with my question? Thanks so much for any help.

The problem is not with ROOT but with your logic. Check how/where you are setting the value/s of n; as it is, you are only printing the last (bin) value.
Another (unrelated) thing: do you really need 15000 bins in a histo from 0 to 300?

Hi!

I’m not sure what you’re exactly trying to find weather bin number at a particular channel or bin content. But if you want bin number at a particular channel then you should do

hist->FindFixBin(x-axis label);

This will give you bin number at the X axis value.
And also notice that you haven’t provided Pedestal_0_2_5_2023_002.ascii file.

Hi Shiva, I changed my file to the txt type, and txt file can be uploaded here.
pedestalhistest.C (824 Bytes)
pedestal.txt (555.8 KB)

Hi Shiva,

Thanks for your response. I also would like to find the content of in a bin, so could you please give a sample example on how to call GetBinContent()? My scrip only shows me 0 which means it did not get any bin counts. So my logic was wrong but I could not figure out how to make GetBinContent() run. Thanks so much for your time!

Hi Dastudillo,

Sorry for I am just a beginner of ROOT. I also do not know much about coding. I readly do not know where I should put my n. Should I put n into the while loop? Could you please explain a little bit more?

The reason I claim 15000 bins is because it is safe to keep the bins greater than the my channel numbers, actually in this case, I think I only need 300 bins. Thanks so much for your time!

void pedestalhistest()
{
    
    TH1F *hist = new TH1F("hist","Histogram",300,0,300);
    
    fstream file;
    file.open("pedestal.txt", ios::in);
    
    Double_t n;
    Int_t i;
    
    
    
    while(1)
    {
        double x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11;
        
        file >> x1 >> x2 >> x3 >> x4 >> x5 >> x6 >> x7 >> x8 >> x9 >> x10 >> x11;
        hist->Fill(x4);

        
        
        if(file.eof()) break;
      }
    
    for (i=1; i<= hist->GetNbinsX();i++)
    {
        n = hist->GetBinContent(i);
        cout << "Bin content at bin number " << i << " is " << n << endl;
    }
    

    file.close();
    
    hist->Scale(1);
 
    hist->GetXaxis()->SetTitle("Channels");
    hist->GetYaxis()->SetTitle("Particle counts");
    
    
   
    TCanvas *c1 = new TCanvas();
    hist->Draw();

}
1 Like

Hi Shiva,

Thanks so much for your help! It works!

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