#include // For printing messages to the terminal #include // For strings #include // ROOT histograms #include // ROOT files #include // ROOT canvas for plotting #include // ROOT pads for advanced plotting #include // ROOT legends for plots #include // ROOT latex for flexible text drawing #include // ROOT support for drawing lines #include // ROOT support for drawing lines void ReadHisto(){ // Retrieve the histograms from the file // Open the file in read-only mode TFile* histFileData = new TFile("histodata.root"); TFile* histFileMC = new TFile("histoMC.root"); // Get the data histogram TH1D* dataHisto = (TH1D*)histFileData->Get("DATA"); // Get the MC histogram TH1D* mcHisto = (TH1D*)histFileMC->Get("DATA"); // Make sure you got the histograms if (!dataHisto) { return; } if (!mcHisto) { return; } // Basic style // Turn off the statistics box mcHisto->SetStats(0); dataHisto->SetStats(0); // Set the line color to red for MC and black for data mcHisto->SetLineColor(kRed); dataHisto->SetLineColor(kBlack); // Set the line width to 2 for MC and data mcHisto->SetLineWidth(2); dataHisto->SetLineWidth(2); // Set axis labels mcHisto->GetYaxis()->SetTitle("Number of events"); dataHisto->GetYaxis()->SetTitle("Number of events"); mcHisto->GetXaxis()->SetTitle("m_{ll} [MeV]"); dataHisto->GetXaxis()->SetTitle("m_{ll} [MeV]"); // Prepare the canvas for plotting // Make a canvas TCanvas *c1 = new TCanvas("c1","c1"); // Move into the canvas (so anything drawn is part of this canvas) c1->Divide(2,1); c1->cd(1);gPad->SetLogy(); mcHisto->DrawCopy(); c1->cd(2);gPad->SetLogy(); dataHisto->DrawCopy(); TH1F *hMCscaled = (TH1F*)mcHisto->Clone("hMCscaled"); hMCscaled->Scale(dataHisto->Integral()/mcHisto->Integral()); TCanvas *c2 = new TCanvas("c2","c2"); c2->cd(1); hMCscaled->Draw("h"); dataHisto->Draw("pe,same"); // Calculate the ratio // First clone the current data points TH1D* ratio = (TH1D*)dataHisto->Clone("ratio"); // Divide the data points by the MC histogram ratio->Divide(hMCscaled); ratio->SetLineColor(kRed); //------------------------------------------------------ TCanvas *c3 = new TCanvas("c3","c3",600,800); // c3->cd(); TPad *pad1 = new TPad("pad1", "pad1", 0, 0.3, 1, 1.0); pad1->SetBottomMargin(0); // Upper and lower plot are joined pad1->SetGridx(); // Vertical grid pad1->SetLogy(); // Vertical grid pad1->Draw(); // Draw the upper pad: pad1 pad1->cd(); // pad1 becomes the current pad hMCscaled->SetTitle(""); // Remove the plot title hMCscaled->GetXaxis()->SetLabelSize(0); // Remove x-axis labels for the top pad hMCscaled->GetXaxis()->SetTitleSize(0); // Remove x-axis title for t he top pad hMCscaled->GetYaxis()->SetTitleSize(0.04);// Increase y-axis title size (pad is not full page) hMCscaled->Draw("h"); // Draw the MC histo in the current pad dataHisto->Draw("pe,same"); // Draw the data points on top of the MC histo // Add a legend to the top pad TLegend *legend = new TLegend(0.48,0.56,0.64,0.71); // Add a legend near the top right corner legend->AddEntry(hMCscaled,"MC"); // Add the MC histogram, labelled as "MC" legend->AddEntry(dataHisto,"Data"); // Add the data points, labelled as "Data" legend->SetLineWidth(0); // Remove the boundary on the legend legend->Draw("same"); // Draw the legend on the plot // Add other labels with TLatex // Note that TLatex is global, not pad-related // As such, the percent-based coordinates for TLatex and the above legend are not the same TLatex latex; // Create the TLatex object latex.SetNDC(); // Set the coordintes to be percent-based latex.SetTextSize(0.06); // Increase the text size latex.DrawText(0.48,0.83,"Exarcise 10"); // Write a label for HASCO latex.SetTextSize(0.04); // Reduce the text size latex.DrawText(0.48,0.77,"Di-muon events"); // Add a label for the selection // Now draw the ratio c3->cd(); // Go back to the main canvas before defining pad2 TPad *pad2 = new TPad("pad2", "pad2", 0, 0.05, 1, 0.3); pad2->SetTopMargin(0); pad2->SetBottomMargin(0.2); pad2->SetGridx(); // vertical grid pad2->Draw(); pad2->cd(); // pad2 becomes the current pad ratio->SetTitle(""); // Turn off the title to avoid overlap ratio->GetXaxis()->SetLabelSize(0.12); // Larger x-axis labels (pad is not full page) ratio->GetXaxis()->SetTitleSize(0.09); // Larger x-axis title (pad is not full page) ratio->GetYaxis()->SetLabelSize(0.1); // Larger y-axis labels (pad is not full page) ratio->GetYaxis()->SetTitleSize(0.15); // Larger y-axis title (pad is not full page) ratio->GetYaxis()->SetTitle("Data/MC"); // Change the y-axis title (this is the ratio) ratio->GetYaxis()->SetTitleOffset(0.3); // Reduce the y-axis title spacing ratio->GetYaxis()->SetRangeUser(0.5,1.5); // Set the y-axis ratio range from 0.5 to 1.5 ratio->GetYaxis()->SetNdivisions(207); // Change the y-axis tick-marks to work better ratio->Draw("pe"); // Draw the ratio in the current pad // Add a line at 1 to the ratio plot TLine line(50.e3,1,200.e3,1); // Draw a line at 1 from 50 GeV to 200 GeV (full plot) line.SetLineColor(kBlack); // Set the line colour to black line.SetLineWidth(2); // Set the line width to 2 line.Draw("same"); // Draw the line on the same plot as the ratio // // Write the ratio plot to the output plot file c3->SaveAs("finalPlot.pdf"); c3->SaveAs("finalPlot.png"); c3->cd(); }