Resizing/rebinning x-axis on TH1F histograms

I have many root files and inside each is a tree with 25 variables. I have created a script to loop through all rootfiles in a specific directory. Essentially, I am trying to find the number of type of event in each exposure of a detector. So I plot the energy distribution of each exposure and take the integral of different regions on the plots. This I read into a text file. I’m finding though that using the method I am using, the histograms for each exposure are placed on different x-scales and for my method to work, I need to make sure I am taking the integral over the same region each time (I have the integral ranging from one bin to another bin and so when the ranges are different, the bins represent different values). I look at the histograms from the last exposure before deleting them and notice that their scales are still automatically defined. How do I fix this?


TTree t1 = (TTree)f.Get(“t1”); //t1 is just the name of the trees inside the files… f is the file i have loaded in root
TCut cut1 = “sqrt(rmsx18rmsx18+rmsy19rmsy19<0.65”;
t1->Draw(“fluxiso2”, cut1);
TH1F *h1 = new TH1F(“h1”,"", 100, 0, 500000);
TH1F h1 = (TH1F)gPad->GetPrimitive(“htemp”);
h1->Draw();
h1->SetBit(TH1::kCanRebin);
TAxis *Xaxis = h1->GetXAxis();
h1->RebinAxis(100,Xaxis);
h1->GetXaxis()->SetRange(0,500000);
h1->Draw();
// h1->SetMinimum(0); //I tried out the set minimum and maximum and it only changed my y axes
float lowlow = h1->Integral(0,1);
float low =h1->Integral(2, 20);
float high = h1->Integral(21,50);
float highhigh = h1->Integral(51, 100);
//x is the number of diffusion limited hits with isophotal flux values between 0 and 5000
//z is the number of diffusion limited hits with isophotal flux values between 5000 and 1000y
//z is the number of diffusion limited hits with isophotal flux values between 10000 and 25000
//w is the number of diffusion limited hits with isophotal flux values between 260000 and 500000
float difftotal = h1->Integral(0, 100);
//“q is total diffusion limited hits (rms is less than .65)“
t1->Draw(“fluxiso2”);
TH1F *h2 = new TH1F(“h2”,””,100, 0 , 500000000);
TH1F h2 = (TH1F)gPad->GetPrimitive(“htemp”);
h2->SetBit(TH1::kCanRebin);
TAxis *Xaxis2 = h2->GetXAxis();
h2->RebinAxis(100,Xaxis2);
h1->GetXaxis()->SetRange(0,500000);
h1->Draw();
// h2->SetMinimum(0);
float totaltotal= h2->Integral(0,100);
cout<< “n is the total number of events and =” << totaltotal << endl;
printf(“File: %30\n”, f);
printf(" Total Events Diffusion Limited Lowest Energy Midlow Energy Midhigh Energy Highest Energy\n");
printf(“Expected %12.1f %17.1f %13.1f %13.1f %14.1f %14.1f\n”, totaltotal, difftotal, lowlow, low, high, highhigh);
// cout<< "highhigh is = " << highhigh <<endl;
printf(“Outcome %12.1f %17.1f %13.1f %13.1f %14.1f %14.1f\n”, totaltotal, difftotal, lowlow, low, high, highhigh);

Hi,

see TTree::Draw()'s documentation at root.cern.ch/root/html/TTree#TTree:Draw especially the part starting with “In addition, the name of the histogram can be followed by up to 9 numbers between ‘(’ and ‘)’, where the numbers describe the following”.

Note that this:

TH1F *h1 = new TH1F("h1","", 100, 0, 500000);
/* WRONG: */ TH1F *h1 = (TH1F*)gPad->GetPrimitive("htemp");

is invalid C++ because you re-declare h1 (CINT lets you do it nevertheless) and causes a memory leak.

Cheers, Axel.