THStack *stack = new THStack("stack", "Stacked Histogram"); TH1D *hist1 = new TH1D("hist1", "Histogram 1", 100, 0, 10); hist1->FillRandom("gaus", 1000); hist1->SetFillColor(kRed); TH1D *hist2 = new TH1D("hist2", "Histogram 2", 100, 0, 10); hist2->FillRandom("gaus", 1000); hist2->SetFillColor(kBlue); stack->Add(hist1); stack->Add(hist2); stack->Draw("HIST");
TCanvas *c = new TCanvas("c", "Canvas", 800, 600); THStack *stack = new THStack("stack", "Stacked Histogram"); TH1D *hist1 = new TH1D("hist1", "Histogram 1", 100, 0, 10); hist1->FillRandom("gaus", 1000); hist1->SetFillColor(kRed); TH1D *hist2 = new TH1D("hist2", "Histogram 2", 100, 0, 10); hist2->FillRandom("gaus", 1000); hist2->SetFillColor(kBlue); stack->Add(hist1); stack->Add(hist2); stack->Draw("HIST"); TLegend *legend = new TLegend(0.7, 0.7, 0.9, 0.9); legend->AddEntry(hist1, "Histogram 1", "f"); legend->AddEntry(hist2, "Histogram 2", "f"); legend->Draw(); stack->GetXaxis()->SetTitle("X Axis"); stack->GetYaxis()->SetTitle("Y Axis");This example creates a THStack object and adds two histograms to it, similarly to the first example. Additionally, a TLegend object is created and positioned in the top right corner of the plot. The legend entries are added and the legend is drawn. Finally, the X and Y axes of the stack are labeled with titles. THStack is a part of the ROOT data analysis package library.