{ gROOT->Reset(); // Resets the ROOT environment THStack hs("hs","test stacked histograms"); // Define the histogram stack object // Setting the histograms: TH1F *h1 = new TH1F("h1","test hstack",100,-4,4); // Dynamically create a new histogram h1->FillRandom("gaus",20000); // Fill histogram h1->SetFillColor(kRed); // Set histogram fill color to red h1->SetFillStyle(3001); hs.Add(h1); // Add histogram to the histogram stack object TH1F *h2 = new TH1F("h2","test hstack",100,-4,4); h2->FillRandom("expo",15000); h2->SetFillColor(kBlue); h2->SetFillStyle(3001); hs.Add(h2); TH1F *h3 = new TH1F("h3","test hstack",100,-4,4); h3->FillRandom("landau",10000); h3->SetFillColor(kGreen); h3->SetFillStyle(3001); hs.Add(h3); /***************************************************/ // Make the legend: TLegend legend(0.1,0.55,0.45,0.85); // Dimensions of the legend block // Adds legend entries: legend.AddEntry(h1, "Gaussian"); legend.AddEntry(h2, "Exponential"); legend.AddEntry(h3, "Landau"); /***************************************************/ // Printing the histograms: TCanvas c1("c1","stacked hists", 10, 10, 1000, 500); // Create a canvas, setting the position to (10,10) and size to (1000,500) c1.Divide (3); // Divide the canvas in 2 c1.cd(1); // Go to the first pane of the canvas hs.Draw(); // Draw the histogram stack with the histograms stacked on top of each other legend.Draw(); // Draws the legend c1.cd(2); // Go to the second pane of the canvas hs.Draw("NOSTACK"); // Draw the histogram stack without stacking legend.Draw(); // Draws the legend c1.cd(3); // Go to the third pane of the canvas hs.Draw("PADS"); // Draw the histogram stack with the histograms stacked on top of each other legend.Draw(); // Draws the legend }