ROOT37: histogram画图

void ana3(){
// read the TFile WFM_Analysis.root
TFile *file = new TFile( "WFM_Analysis.root", "read" );
TTree *tree = (TTree*)file->Get("WFM");
// get data
const int N = tree->GetEntries();
double timestamp, energy1, energy2, time1, time2;
bool flag_coin;
tree->SetBranchAddress( "TimeStamp", ×tamp );
tree->SetBranchAddress( "PeakEnergy1", &energy1 );
tree->SetBranchAddress( "PeakEnergy2", &energy2 );
tree->SetBranchAddress( "PeakTime1", &time1 );
tree->SetBranchAddress( "PeakTime2", &time2 );
tree->SetBranchAddress( "Flag_Coin", &flag_coin );
// plot
TH1D *hgamma1 = new TH1D( "hgamma1", "Energy distribution of gamma1", 200, 300, 700 );
TH1D *hlifetime = new TH1D( "hlifetime", "Lifetime of ep annihilation", 100, 0, 20 );
for( int i=0; i<N; i++ ){
tree->GetEntry( i );
hgamma1->Fill( energy1 );
if( flag_coin == 1 ){
hlifetime->Fill( timestamp );
}
}
TF1 *f1 = new TF1( "f1", "[0]*exp(-x/[1])+[2]", 0, 20 );
f1->SetParameters( 50, 3, 1 );
f1->SetLineWidth( 2 );
TCanvas *c1 = new TCanvas();
hgamma1->Draw();
TCanvas *c2 = new TCanvas("c2", "lifeitme", 1200, 500);
c2->Divide( 2, 1 );
c2->cd(1);
hlifetime->SetStats(0);
hlifetime->SetMarkerStyle( 8 );
hlifetime->SetMarkerColor( 1 );
hlifetime->SetLineWidth( 2 );
hlifetime->GetXaxis()->SetTitle( "Time [#mus]" );
hlifetime->GetYaxis()->SetTitle( "ep annihilation / 0.2#mus" );
hlifetime->GetYaxis()->SetRangeUser( 0, 200 );
hlifetime->Draw("ep");
hlifetime->Fit( "f1" );
TLegend *leg = new TLegend( 0.6, 0.7, 0.94, 0.89 );
leg->AddEntry( hlifetime, "Experiment Data" );
leg->AddEntry( f1, "Exponential Fitting" );
leg->Draw("same");
double chisq = f1->GetChisquare();
int ndf = f1->GetNDF();
TPaveText *text = new TPaveText( 0.4, 0.4, 0.7, 0.6, "NDC");
text->SetTextAlign( 12 );
text->AddText( "ep#rightarrow #gamma#gamma" );
text->AddText( Form("#chi^{2}/ndf = %.02f/%d", chisq, ndf) );
text->AddText( Form("Lifetime = %.02f #pm %.02f [#mus]", f1->GetParameter(1), f1->GetParError(1) ));
text->Draw( "same" );
c2->cd(2);
hlifetime->SetFillColor( 8 );
hlifetime->Draw();
leg->Draw("same");
text->Draw( "same" );
c2->Print( "lifetime.pdf" );
}