Setting axis range

Hi, it’s again me :frowning:

there is a way to change the axis range and set it up to the last non null bin?

Thank you
Amir

if “h” is the histogram you can do:

h->GetXaxis()->SetRange(x,y);

I’ve tried with this cycle

[code]int main(){

const Char_t *Energy[6];
Energy[0]=“0_1GeV”;
Energy[1]=“0_3GeV”;
Energy[2]=“1GeV”;
Energy[3]=“3GeV”;
Energy[4]=“10GeV”;
Energy[5]=“RanEn”;

const Char_t *Direction[2];
Direction[0]=“theta0”;
//Direction[1]=“theta30”;
//Direction[2]=“theta60”;
Direction[1]=“RanPosRanDir”;

Double_t PETotpmt1;
Double_t PETotpmt2;
Double_t PETotpmt3;
Double_t PETotb;

for(Int_t c=0; c<2; c++)
{

for(Int_t a=0; a<6; a++)
{
ostringstream title;
ostringstream titlegraph1;
ostringstream titlegraph2;
ostringstream titlegraph3;
ostringstream titlegraph4;

ostringstream endTitle;
ostringstream otherTitle;

if (c==1)
{
title<<“mu”<<Energy[a]<<Direction[c];
}
else
{
title<<“mu”<<Energy[a]<<Direction[c]<<“phi0”;
}

//title<<“el1GeVtheta0phi0”;

otherTitle<<title.str().c_str()<<“TotPmts.root”<<ends;

cout<<“Processing “<<otherTitle.str().c_str()<<”…”;
TFile *myfile =TFile::Open(otherTitle.str().c_str());

titlegraph1<<title.str()<<“pmt1”;
titlegraph2<<title.str()<<“pmt2”;
titlegraph3<<title.str()<<“pmt3”;
titlegraph4<<title.str()<<“Tot”;

TH1F primo =(TH1F)myfile->Get(titlegraph1.str().c_str());
primo->SetName(“pmt1”);
int nbins1 = primo->GetNbinsX();
int bincontent1 = 0;
int x1=0;
for (int i=0; i<nbins1; i++)
{
bincontent1 = primo -> GetBinContent(i+1);
if (bincontent1 !=0)
{
x1= primo -> GetBinCenter(i+1);
}
}
primo->SetAxisRange(0,x1,“x”);
primo->SetLineColor(2);

TH1F secondo =(TH1F)myfile->Get(titlegraph2.str().c_str());
secondo->SetName(“pmt2”);
int nbins2 = secondo->GetNbinsX();
int bincontent2 = 0;
int x2=0;
for (int l=0; l<nbins2; l++)
{
bincontent2 = secondo -> GetBinContent(l+1);
if (bincontent2 !=0)
{
x2= secondo -> GetBinCenter(l+1);
}
}
secondo->SetAxisRange(0,x2,“x”);
secondo->SetLineColor(3);

TH1F terzo =(TH1F)myfile->Get(titlegraph3.str().c_str());
terzo->SetName(“pmt3”);
int nbins3 = terzo->GetNbinsX();
int bincontent3 = 0;
int x3=0;
for (int m=0; m<nbins3; m++)
{
bincontent3 = terzo -> GetBinContent(m+1);
if (bincontent3 !=0)
{
x3= terzo -> GetBinCenter(m+1);
}
}
terzo->SetAxisRange(0,x3,“x”);
terzo->SetLineColor(4);

TH1F tot =(TH1F)myfile->Get(titlegraph4.str().c_str());
tot->SetName(“tot”);
int nbins4 = tot->GetNbinsX();
int bincontent4 = 0;
int x4=0;
for (int n=0; n<nbins4; n++)
{
bincontent4 = tot -> GetBinContent(n+1);
if (bincontent4 !=0)
{
x4= tot -> GetBinCenter(n+1);
}
}
tot->SetAxisRange(0,x4,“x”);
tot->SetLineColor(6);

THStack *hs = new THStack(“pmts”,“pmts”);

hs->Add(primo);
hs->Add(secondo);
hs->Add(terzo);
hs->Add(tot);

TH1F *chs =(TH1F *)hs->Clone();
chs->SetAxisRange(0,140,“x”);//for example, in reality i should use the maximum non null bin of the 4 histos

endTitle<<title.str()<<“Sov.root”;

TFile *prova =new TFile(endTitle.str().c_str(),“recreate”);
primo->Write();
secondo->Write();
terzo->Write();
tot->Write();
chs->Write();

cout<<“All Green!\n”;
}//fine Energy
}//fine Direction

return 0;
}
[/code]

this is because i know the maximum range of the 4 histos (2500 bins) and i want to shrink them to the last useful value (that many times is around 120).
but while i’m able to set the axis range for the histos,i cannot do the same for the stack.
ther is a way to do it?

Amir

Can you send something smaller I can run ? (here you are using data files I do not have). Or put a tar file in some public area on afs.

Here are the files :slight_smile:

Thank you very much!
filesAmir.tar.gz (6.51 KB)

I get:


pcepsft15.cern.ch> sovrapposiz.exe
Processing mu0_1GeVRanPosRanDirTotPmts.root...All Green!
pcepsft15.cern.ch>

and that’s it … no ouput, no ps file.

the output should be a file named mu0_1GeVRanPosRanDirSov.root

if you watch the this file you see that the single histos pmt1,pmt2,pmt3 and tot have the corrected range while the histo named pmts (the stack) still has the primitive range 0-2500.

today i came with an unclean solution:

i modified the three cicles

[code]TH1F primo =(TH1F)myfile->Get(titlegraph1.str().c_str());

int nbins1 = primo->GetNbinsX();
int bincontent1 = 0;
int x1=0;
for (int i=0; i<nbins1; i++)
{
bincontent1 = primo -> GetBinContent(i+1);
if (bincontent1 !=0)
{
x1= primo -> GetBinCenter(i+1);
}
}
TH1F *pmt1 =new TH1F(“pmt1”,“pmt1”,x1,0,x1);<=============added this part
Float_t newbin1 =0;
for (int o=1; o<x1; o++)
{
newbin1 =primo->GetBinContent(o);
pmt1->SetBinContent(o,newbin1);
}
pmt1->SetLineColor(2);
[/code]

so i created three new histos, and i added them to the stack instead of the old ones.

Now everithing seems working.

For the .eps file i have another script that i must run under ROOT (added in the tar file)

Thank you
Amir
filesAmir2.tar.gz (2.1 KB)