How to set range in histogram with drawn by ntuple?

Hi Experts of root in here,
Bear with the newb. I know this is probably a really simple thing…
But I have tried a lot of different things and asked a lot of different ppl and it doesn’t work:
How do you set range in a histogram set up by tuple?
I tried using :frowning:(TH1F *)(gPad->FindObject(TString::Format(“h%i”, i))))->GetXaxis()->SetRange(5E+15,15E+16);
As is seen in the commented out part, but no change is made to the 64 graphs.
Attached is the .dat file i used as input file
Thanks in advance.

Attached is my code:

[code]#include “TFile.h”
#include “TNtuple.h”
#include “TH1.h”
#include “TCanvas.h”
#include “TPad.h”
#include “TString.h”

#include
#include

void histo641(void){
Float_t c[65];
Int_t i, nlines = 0;
TFile *f = new TFile(“histo.root”,“RECREATE”);
TNtuple *ntuple = new TNtuple(“ntuple”, “data from ascii file”, “c0:c1:c2:c3:c4:c5:c6:c7:c8:c9:c10:c11:c12:c13:c14:c15:c16:c17:c18:c19:c20:c21:c22:c23:c24:c25:c26:c27:c28:c29:c30:c31:c32:c33:c34:c35:c36:c37:c38:c39:c40:c41:c42:c43:c44:c45:c46:c47:c48:c49:c50:c51:c52:c53:c54:c55:c56:c57:c58:c59:c60:c61:c62:c63:c64”);

ifstream in;
in.open(“gain1.dat”);

while (1) {
in >> c[0]>>c[1]>>c[2]>>c[3]>>c[4]>>c[5]>>c[6]>>c[7]>>c[8]>>c[9]>>c[10]>>c[11]>>c[12]>>c[13]>>c[14]>>c[15]>>c[16]>>c[17]>>c[18]>>c[19]>>c[20]>>c[21]>>c[22]>>c[23]>>c[24]>>c[25]>>c[26]>>c[27]>>c[28]>>c[29]>>c[30]>>c[31]>>c[32]>>c[33]>>c[34]>>c[35]>>c[36]>>c[37]>>c[38]>>c[39]>>c[40]>>c[41]>>c[42]>>c[43]>>c[44]>>c[45]>>c[46]>>c[47]>>c[48]>>c[49]>>c[50]>>c[51]>>c[52]>>c[53]>>c[54]>>c[55]>>c[56]>>c[57]>>c[58]>>c[59]>>c[60]>>c[61]>>c[62]>>c[63]>>c[64];
if (!in.good()) break;
nlines++;

ntuple->Fill(c);

}
printf(" found %d points\n",nlines);

in.close();

TCanvas *c1 = new TCanvas(“c1”,“plots”);
c1->Divide(8,8,.001,.001);
for(i=1; i<=64; i++) {
c1->cd(i);
// c1->cd(i)->SetLogy();
ntuple->Draw(TString::Format(“c%i >> h%i”, i, i));
((TH1F *)(gPad->FindObject(TString::Format(“h%i”, i))))->Fit(“gaus”);
// ((TH1F *)(gPad->FindObject(TString::Format(“h%i”, i))))->GetYaxis()->SetRange(0,7);
// ((TH1F *)(gPad->FindObject(TString::Format(“h%i”, i))))->GetXaxis()->SetRange(5E+15,15E+16);
((TH1F *)(gPad->FindObject(TString::Format(“h%i”, i))))->DrawCopy();
// ((TH1F *)(gPad->FindObject(TString::Format(“h%i”, i))))->GetXaxis()->Rebin(7);
// ((TH1F *)(gPad->FindObject(TString::Format(“h%i”, i))))->DrawCopy();
// ntuple->GetXaxis()->SetRange(5E+15,15E+16);
// ntuple->GetYaxis()->SetRange(0,7)
// ntuple->Fit(“gaus”);
// ntuple->Rebin(5);
}
c1->Modified(); c1->Update();

f->Write();
delete f;
}[/code]
gain1.dat (11.3 KB)

[code]#include “TFile.h”
#include “TTree.h”
#include “TCanvas.h”
#include “TPad.h”
#include “TString.h”
#include “TH1.h”
#include “TAxis.h”

void histo641(void){
Int_t i;
TFile *f = new TFile(“histo.root”,“RECREATE”);
TTree *ntuple = new TTree(“ntuple”, “data from ascii file”);
ntuple->ReadFile(“gain1.dat”, “c0:c1:c2:c3:c4:c5:c6:c7:c8:c9:c10:c11:c12:c13:c14:c15:c16:c17:c18:c19:c20:c21:c22:c23:c24:c25:c26:c27:c28:c29:c30:c31:c32:c33:c34:c35:c36:c37:c38:c39:c40:c41:c42:c43:c44:c45:c46:c47:c48:c49:c50:c51:c52:c53:c54:c55:c56:c57:c58:c59:c60:c61:c62:c63:c64”);

TCanvas *c1 = new TCanvas(“c1”,“plots”);
c1->Divide(8,8,.001,.001);
for(i=1; i<=64; i++) {
c1->cd(i);
ntuple->Draw(TString::Format(“c%i >> h%i”, i, i));
TH1F *h = ((TH1F *)(gPad->FindObject(TString::Format(“h%i”, i))));
if (h) {
h->GetXaxis()->SetLimits(5E+15, 15E+16);
h->GetYaxis()->SetRangeUser(0, 7);
h->Fit(“gaus”);
}
}
c1->cd(0);
c1->Modified(); c1->Update();

f->Write();
delete f; // automatically deletes the “ntuple” and all histograms, too
}[/code]

Hi thanks for the help. but when i run your code. the histograms flashed and disappeared and i was left with a cavas of 64 blank graphs. attached is a screen shot of what happened.
Also i don’t know if this is relevant, but i have a mac machine.
Thanks

Also. if it is not too much trouble. can you explain why you set the option this way and why my method wouldn’t work? Thanks


When you close the ROOT file (i.e. “delete f;” in your case), all objects that it owns are automatically deleted, too (i.e. the “ntuple” and all histograms in your case, so they also disappear from your canvas).

Why does my code work? Well, it’s just applied ROOT sorcery.