How to reset the range and size of Bin?

I have a root file, and now I want to read the data from it and draw a histogram. This hist data seem to have already set the range and size of Bin. How can I reset it according to the range and size I want?
Thank you for your reply.

Welcome to the ROOT forum.

See: How to set ranges on axis?

I want to divide two histograms, but the program reported an error:
Cannot divide histograms with different number of bins
One histogram is from a .root file, and the other is my own data.
This is my program. I am a new user and cannot upload files.

int bpd = 10;
    int nDec = 10;          
    int nbins = bpd * nDec; 
    float LowEdge = -1.;    
    double xbins[101];
    double step = 1./bpd;

    for(int i = 0; i <= nbins; i++){
        xbins[i] = pow(10,LowEdge + step*i);
    }

    TCanvas *c2 = new TCanvas("c2","c2",1000,500);
    TH1F *h2 = new TH1F("n_energy_P"," ",nbins,xbins); 
    
    TCanvas *c3 = new TCanvas("c3","c3",1000,500);
    TH1F *h3 = new TH1F("n_energy_p/n"," ",nbins,xbins); 
    
    string path1 = "/home/jack/Desktop/sp_part5.txt";

    TFile f("FluxDataES2-dE-100bpd-Phi60.root");
	TCanvas *c1 = new TCanvas("c1","c1",800,800);
	TH1F *h1 = (TH1F*)f.Get("hFlux2");
    
    // double tot=hFlux2->Integral(0,1000);
    // double e1=hFlux2->Integral(101,101);
    // double r = e1/tot;
    // cout<<tot<<" "<<e1<<" "<<r<<endl;

    double th_time = 0.;
    double ve = 0.83;
    double PI = 3.1415926;
    double px,py,pz,bx,by,bz,theta,energy,track_length,st,et;
    int num;
    long int trig_r;
    
    double fran = 20.;
    double l = 76.;
    double c = 29.98;
    double tr = 470.;
    double E = 0.0;
    double sca = 1e0;

    fstream in1;
    in1.open(path1,ios::in);
	while (in1>>px>>py>>pz>>bx>>by>>bz>>track_length>>energy>>num>>trig_r){
        st = pz/ve + th_time;
        E = (72.306 * l) / ((st - tr) * fran + l/c);
        E = (E * E)/sca;
        theta = (180/PI) * acos(abs(bz));
        h2->Fill(E);
    }
    in1.close();

    h3->Divide(h2,h1,1,10);

    c2->cd();h2->Draw();
    h2->SetLineWidth(2);
    h2->GetXaxis()->SetTitle("Energy(eV)"); 
    h2->GetXaxis()->CenterTitle();
    h2->GetYaxis()->SetTitle("Counts");
    h2->GetYaxis()->CenterTitle();

	c1->cd();
    h1->GetXaxis()->SetTitle("Energy(eV)");
    h1->GetXaxis()->CenterTitle();
    h1->GetYaxis()->SetTitle("Counts(neutrons/cm2/s)");
    h1->GetYaxis()->CenterTitle();
    h1->Draw();

    c3->cd();h3->Draw("E");
    h3->GetXaxis()->SetTitle("Energy/eV"); 
    h3->GetXaxis()->CenterTitle();
    h3->GetXaxis()->SetTitleOffset(1.5);
    h3->GetYaxis()->SetTitle("Counts");
    h3->GetYaxis()->CenterTitle();

In h2 histogram you are creating 100 bins. So to divide h2 by h1 they both should have same number of bins. To check number of bins in h1 histogram you can do this

cout << h1->GetNbinsX() << endl;

As @alex_HXY said you should create h2with the same number of bin as h1.

Also I see that you create h3 with the name "n_energy_p/n". That’s not a good idea to have “/” in histograms’ names. See:

Thank you for your reply. What should I do if I want to change the number and range of bin in h1? Or is it impossible to modify it?

For the range I sent you the link. For the number of bins you can rebin with less bins but you cannot increase the number of bins. If your goal is to divide h2 by h1 it will be easier to create h2 compatible with with h1 I guess.

I I got it. Thank you for your help.

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