#include #include #include #include #include #include using namespace std; TLatex* latex_label(double x, double y, int color, string text, float size = -1., bool do_draw = true){ TLatex *lat = new TLatex(); lat->SetTextFont(42); lat->SetTextColor(color); if (size > 0) lat->SetTextSize(size); if (do_draw){ return lat->DrawLatexNDC(x, y, text.c_str()); } else { lat->SetText(x, y, text.c_str()); return lat; } } void test2() { vector labels = {"SM total", "#gamma + jets Pythia", "jet#rightarrow#gamma fake"}; vector labels_latex; vector labels_width; for (auto l : labels) { TLatex *lat = (TLatex *) latex_label(0.54, 0.7, 1, l, 0.04, false); labels_latex.push_back(lat); labels_width.push_back(lat->GetXsize()); } printf("------------------------------- before everything\n"); for (int i = 0; i < labels.size(); i++) { cout << labels[i] <<" "<< labels_width[i] << endl; } // Create histograms TH1F* hist1 = new TH1F("hist1", "Histogram 1", 100, 0, 100); TH1F* hist2 = new TH1F("hist2", "Histogram 2", 100, 0, 100); // Fill histograms with Gaussian distributions TRandom3 rand_gen; for (int i = 0; i < 1000; ++i) { double value1 = rand_gen.Gaus(30, 5); // Mean = 30, Width = 5 double value2 = rand_gen.Gaus(60, 10); // Mean = 60, Width = 10 hist1->Fill(value1); hist2->Fill(value2); } // Set histogram colors and fill styles hist1->SetLineColor(kBlue); hist1->SetFillColor(kBlue); hist1->SetFillStyle(3001); hist2->SetLineColor(kRed); hist2->SetFillColor(kRed); hist2->SetFillStyle(3001); // Stack the histograms THStack* stack = new THStack("stack", "Stacked Histograms"); stack->Add(hist1); stack->Add(hist2); TH1F * total = (TH1F *) stack->GetStack()->Last()->Clone("total"); total->SetFillColor(0); total->SetLineColor(kBlack); // Create a canvas to display the histograms TCanvas *canvas = new TCanvas("canvas", "Histogram Stacking", 800, 600); canvas->SetLogy(); labels_latex.clear(); labels_width.clear(); for (auto l : labels) { TLatex *lat = (TLatex *)latex_label(0.54, 0.7, 1, l, 0.04, false); labels_latex.push_back(lat); labels_width.push_back(lat->GetXsize()); } printf("------------------------------- after creating canvas\n"); for (int i = 0; i < labels.size(); i++) { cout << labels[i] << " " << labels_width[i] << endl; } // Draw the stacked histograms stack->Draw("hist"); labels_latex.clear(); labels_width.clear(); for (auto l : labels) { TLatex *lat = (TLatex *)latex_label(0.54, 0.7, 1, l, 0.04, false); labels_latex.push_back(lat); labels_width.push_back(lat->GetXsize()); } printf("------------------------------- after drawing stack\n"); for (int i = 0; i < labels.size(); i++) { cout << labels[i] << " " << labels_width[i] << endl; } total->GetYaxis()->SetTitle("Entries"); stack->GetYaxis()->SetTitle("Entries"); total->GetXaxis()->SetTitle("Gaussian distribution"); stack->GetXaxis()->SetTitle("Gaussian distribution"); labels_latex.clear(); labels_width.clear(); for (auto l : labels) { TLatex *lat = (TLatex *)latex_label(0.54, 0.7, 1, l, 0.04, false); labels_latex.push_back(lat); labels_width.push_back(lat->GetXsize()); } printf("------------------------------- after modified axis\n"); for (int i = 0; i < labels.size(); i++) { cout << labels[i] << " " << labels_width[i] << endl; } total->Draw("hist same"); // Update the canvas canvas->Update(); // Keep the macro running until the user closes the canvas // canvas->WaitPrimitive(); }