#include
#include “TAttMarker.h”
#include “TCanvas.h”
#include “TLatex.h”
#include “TDirectory.h”
#include “TAxis.h”
#include //string
#include “TH1F.h”
#include “TH2F.h”
#include “TF1.h”
#include “TChain.h”
#include “TStyle.h” //for gStyle decleration
#include “TMath.h”
#include “TRandom.h”
#include “TAxis.h”
int testbinning()
{
TCanvas *c1 = new TCanvas(“c1”, “c1”,0,75,3665,2044);
c1->Range(0,0,1,1);
float pad_x1= 0.05, pad_x2= 0.5 , pad_x3= 0.95, pad_y1= 0.5, pad_y2= 0.95, pad_y3= 0.05 ;
TPad *c1_1 = new TPad(“c1_1”, “newpad”, pad_x1, pad_y1, pad_x2, pad_y2);
TPad *c1_2 = new TPad(“c1_2”, “newpad”, pad_x2, pad_y1, pad_x3, pad_y2);
TPad *c1_3 = new TPad(“c1_3”, “newpad”, pad_x1, pad_y3, pad_x2, pad_y1);
TPad *c1_4 = new TPad(“c1_4”, “newpad”, pad_x2, pad_y3, pad_x3 ,pad_y1);
c1_1->Draw();
c1_1->cd();
const char *name=“name”;
const char *title=“title”;
Int_t BinWidth= 1;
Double_t hist_lower_xlimit=5.;
Double_t hist_upper_xlimit=11. ;
int NbinsX = TMath::Ceil((hist_upper_xlimit-hist_lower_xlimit) / BinWidth );
TH1Fh1= new TH1F( name,title,NbinsX,hist_lower_xlimit,hist_upper_xlimit); //TH1F : histograms with one float per channel. Maximum precision 7 digits
TAxis xAxis = h1->GetXaxis();
/*
for(double i= hist_lower_xlimit ; i < hist_upper_xlimit ; i++){
h1->Fill(i, i+20);
}
*/
for(int i=0 ; i <= h1->GetNbinsX() ; i++){
h1->SetBinContent(i, i+20);
h1->SetBinError(i, (i+20)/100.);
}
h1->Draw(“E,histo”);
c1_1->Modified(); //retain
c1->cd();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int binx1= xAxis->FindBin(hist_lower_xlimit); //FindBin (Double_t x, Double_t y=0, Double_t z=0) Return Global bin number corresponding to x,y,z
cout << "\n\nTotal bin number ROOT finds: " << h1->GetNbinsX()<<endl;
cout << “To compare with my calculation of bin number:” << NbinsX << endl;
cout << "Bin width ROOT calculates: "<< h1->GetBinWidth(1) << endl; //GetBinWidth(bin no)
cout << "To compare with my bin width: " << BinWidth << endl;
cout << “hist_lower_xlimit= “<<hist_lower_xlimit<<” related bin number:”<< binx1<<endl;
cout << “To find the X value details from the bin INFO:”<< endl;
cout << "X Lower edge: " << xAxis->GetBinLowEdge(binx1) << endl;
cout << "X Center: " << xAxis->GetBinCenter(binx1) << endl;
cout << "X Upper edge: " << xAxis->GetBinUpEdge(binx1) << endl;
cout << "Bin content from bin no: " << h1->GetBinContent(binx1) << endl;
cout << "Bin content error from bin no: “<<binx1 <<” : " << h1->GetBinError(binx1) << endl;
cout << "Bin content error from bin no: “<< 2 <<” : " << h1->GetBinError(2) << endl;
cout << "Bin content from integral of binx1-binx1 range: "<< h1->Integral(binx1,binx1) << endl;
//GetBinContent (Int_t bin) const ,Return content of bin number whatever bin is.
c1_2->Draw();
c1_2->cd();
TH1F h2 = (TH1F)h1->Clone(“h2”);
//c1->Update(); // or create a pad and clear before drawing the 2nd one
cout<<"\nLet’s merge 2 bins into one bin."<<endl;
h2->Rebin(2.); //merge 2 bins into one bin. //https://root.cern/doc/v610/classTH1.html#aff6520fdae026334bf34fa1800946790
h2->Sumw2();
cout<< "\n\n\nTotal bin number: " << h2->GetNbinsX()<< endl;
//cout<<“Only 1st bin content is changed.”<<endl;
h2->Draw(“E,histo”);
cout << "\tContent1: " << h2->GetBinContent(1) << "\tContent2: " << h2->GetBinContent(2)<< endl;
cout << "\tContentError1: " << h2->GetBinError(1) << "\tContentError2: " << h2->GetBinError(2)<< endl;
//c1->Modified(); //not working
cout<< "integral of 1-2 bins: "<< h2->Integral(1,2) << endl;
c1_2->Modified(); // koru , kaybetme , retain
c1->cd();
/////////////////////////////////////////////////////////////
c1_3->Draw();
c1_3->cd();
TH1F h3 = (TH1F)h1->Clone(“h3”);
h3->SetBinContent(2, 10);
h3->SetBinError(2, 10./100.);
cout<<"\n\nOnly 2nd bin content is changed."<<endl;
h3->Draw(“E,histo”);
cout << "\tContent1: " << h3->GetBinContent(1) << "\tContent2: " << h3->GetBinContent(2)<<endl;
cout << "\tContentError1: " << h3->GetBinError(1) << "\tContentError2: " << h3->GetBinError(2)<< endl;
cout<< "integral of 1-2 bins: "<< h3->Integral(1,2) << " integral of 2-3 bins: "<< h3->Integral(2,3) << " integral of 3-4 bins: "<< h3->Integral(3,4) << endl;
c1_3->Modified(); // koru , kaybetme , retain
c1->cd();
/////////////////////////////////////////////////////////////
c1_4->Draw();
c1_4->cd();
TH1F h4 = (TH1F)h1->Clone(“h4”);
h4->Scale(2.);
h4->Draw(“E,histo”);
cout << "\n\nNew Scaled Content1: " << h4->GetBinContent(1) << "\tContent2: " << h4->GetBinContent(2)<< endl;
cout << "\tContentError1: " << h4->GetBinError(1) << "\tContentError2: " << h4->GetBinError(2)<< endl;
cout<< "integral of 1-2 bins: "<< h4->Integral(1,2) << endl;
c1_4->Modified(); // retain
c1->cd();
/////////////////////////////////////////////////////////////
c1->Modified(); // retain
c1->cd();
c1->SetSelected(c1);
c1->ToggleToolBar();
return 0;
}