Problem in draw TH1D from a txt file(by tree)


Hi, rooters,

The problem is that the plot of the histogram is blank.

The txtfile have one column of data,which are scatter points.
I wang to draw a histogram. The maximum value of the data is <50;
and minimum value is 0;
Th1D *h1 = (“h1”,"",100,0,50);

txt file is imporeted and save by tree. Then the tree is drawn in hist.


void tree()
{
  TTree *t = new TTree("t", "my tree");
  Float_t x; 
  t->ReadFile("a.txt", "x/F");
  
  t->Branch("x",&x,"x/D");
	
	
	TCanvas *c = new TCanvas("c","");
  
	t->Draw("x");

  TFile *f = new TFile("data.root","RECREATE");
	t->Write();
	f->Write();
	f->Close();
}

void energyhist()
 {

    TFile *f = new TFile("data.root");
		TTree *t1 = (TTree *)f->Get("t");
    Float_t x;	  
		t1->SetBranchAddress("x",&x);
		
		//create a histogram
		TH1F *he = new TH1F("he","",300,0,50);

	  Int_t nentries = (Int_t)t1->GetEntries();
		for (Int_t i = 0; i < nentries; i++){
	     t1->GetEntry(i);
			 he->Fill(x);
			 }
	  
		he->Draw();

}

a.txt (33.5 KB)

TH1F *he = new TH1F("he", "", 300, 0., 5.e10);

1 Like

Thank you. You helped me a lot.
It turned out that my data unit was wrong… :sweat_smile:

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