# How to reset the range and size of Bin?

**URL:** <https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597>\
**Category:** ROOT\
**Created:** [March 20, 2024, 9:23am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597 "2024-03-20T09:23:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex\_HXY](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@alex\_HXY](https://root-forum.cern.ch/u/alex_HXY)\
**Post date:** [March 20, 2024, 9:23am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/1 "2024-03-20T09:23:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 20, 2024, 10:22am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/2 "2024-03-20T10:22:28Z")

</div>

Welcome to the ROOT forum.

See: [How to set ranges on axis?](https://root-forum.cern.ch/t/how-to-set-ranges-on-axis/28254)

---

<div class="post-metadata">

**Author:** ![alex\_HXY](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@alex\_HXY](https://root-forum.cern.ch/u/alex_HXY)\
**Post date:** [March 21, 2024, 12:52am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/3 "2024-03-21T00:52:07Z")

</div>

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.

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![Shiva\_Prasad\_Nayak](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shiva_prasad_nayak/32/36391_2.png) [@Shiva\_Prasad\_Nayak](https://root-forum.cern.ch/u/Shiva_Prasad_Nayak)\
**Post date:** [March 21, 2024, 6:57am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/4 "2024-03-21T06:57:42Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 21, 2024, 7:36am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/5 "2024-03-21T07:36:23Z")

</div>

As @alex_HXY said you should create `h2`with 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:

> [@How to draw an object with special characters in its name?](https://root-forum.cern.ch/t/how-to-draw-an-object-with-special-characters-in-its-name/28248):
>
> In general it is not a good idea to have object’s name containing special character like “”, “/”, “#” etc … or even have object name starting with a number because they might be not accessible from the ROOT command line. For instance “/” is the separator for directory level in a file therefore an object having a “/” in its name cannot be access from the command line. Nevertheless it may happen that the some object can be named that way and stored in a ROOT file. The following macro show how to…

---

<div class="post-metadata">

**Author:** ![alex\_HXY](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@alex\_HXY](https://root-forum.cern.ch/u/alex_HXY)\
**Post date:** [March 21, 2024, 7:51am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/6 "2024-03-21T07:51:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 21, 2024, 8:13am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/7 "2024-03-21T08:13:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alex\_HXY](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@alex\_HXY](https://root-forum.cern.ch/u/alex_HXY)\
**Post date:** [March 21, 2024, 8:29am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/8 "2024-03-21T08:29:39Z")

</div>

I I got it. Thank you for your help.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [April 4, 2024, 8:29am UTC](https://root-forum.cern.ch/t/how-to-reset-the-range-and-size-of-bin/58597/9 "2024-04-04T08:29:45Z")

</div>

Closed on April 4, 2024, 8:29am UTC

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