{ "cells": [ { "cell_type": "markdown", "id": "449bb5a4", "metadata": {}, "source": [ "# **CERN 2022 Project -- Comparing mass of Higgs particle and S particle**:\n", "## Information about the Files Used (without S particle):\n", "Files 1 $\\rightarrow$ 30 are for runs of X $\\rightarrow$ HH resonant decay for various masses and percentiles. \n", "\n", " \n", "## Files with S particle:\n", "Files S1 $\\rightarrow$ S19 are for runs of X $\\rightarrow$ SH decay, where X in this first type of ntuple has a set mass of 300 GeV. The next ntuple will have varying X masses like the first ntuple. The width in this set are all set to a narrow width (< 10%)\n", "\n", "\n", "## Breakdown of S files by Decay Mode:\n", "### X $\\rightarrow$ SttHbb\n", "\n", "\n", "### X $\\rightarrow$ SbbHtt\n", "" ] }, { "cell_type": "markdown", "id": "57238958", "metadata": {}, "source": [ "\n", " \n" ] }, { "cell_type": "markdown", "id": "c567edbe", "metadata": {}, "source": [ "# Skeleton of Code\n", "This kernel was the basis for the analysis done on my project -- it can plot MC histograms if you're given MC data to work with and smeared histograms if you need to replicate real data with your ntuples. I also mention briefly in the beginning of the kernel where you can use possible alternative modules for easier plotting since ROOT and Python syntax really don't mix, however you need to have an older version of Python (2.7 or 2.8) in order to utilize them. These are commented out in the beginning. This can run for however number of files needed, but since I had a hard time trying to get ROOT to read through my directory, I had to change the names in order for the script I had to run within the directory of the files. There is definitely a better way to do it but in the time I had, I didn't have the time to figure it out. The alternate module rootplot.matplotlib did have something to remedy that but I had Python 3.11 so it wasn't compatible." ] }, { "cell_type": "code", "execution_count": 1, "id": "c0fcd653", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.26/02\n" ] }, { "ename": "ModuleNotFoundError", "evalue": "No module named 'rootplot'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mROOT\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mrandom\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mrootplot\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmatplotlib\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mr2m\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mhistogram\u001b[39m(trees, mH, mX, mHbb, mHtt, mbb, mtt):\n\u001b[1;32m 9\u001b[0m \u001b[38;5;66;03m# To be able to get all entries in for loop, need to establish a GetEntries before the loop \u001b[39;00m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;66;03m# is made\u001b[39;00m\n", "File \u001b[0;32m~/anaconda3/envs/my_root_env/lib/python3.10/site-packages/ROOT/_facade.py:153\u001b[0m, in \u001b[0;36mROOTFacade._set_import_hook.._importhook\u001b[0;34m(name, *args, **kwds)\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[1;32m 152\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n\u001b[0;32m--> 153\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_orig_ihook\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'rootplot'" ] } ], "source": [ "# Version that creates .root hist files\n", "# If you have an older version of Python, you can use the commented module for easier\n", "# plotting manipulation and accessing files via navigating the directory\n", "# There might be a way to use ROOT.TFile.Open() while navigating the directory but it \n", "# didn't work when I made this \n", "import ROOT\n", "import random # You can use ROOT.TRandom() as well but I found this worked smoother\n", "#import rootplot.matplotlib as r2m\n", "#import matplotlib.pyplot as plt\n", "\n", "# 2 functions were made to plot 2 different types of histograms: the first being the MC\n", "# histograms pre-established in the ntuple that was given; if you already have the MC\n", "# histograms, this isn't needed. The second plots smeared histograms based off the MC \n", "# data to resemble \"real\" data that includes \"trigger behaviors\". Again, if you have\n", "# raw data, it's not needed to use the smear function, you can just use the 1st def to \n", "# plot the histograms you want.\n", "\n", "def histogram(trees, mH, mX, mHbb, mHtt, mbb, mtt):\n", " # To be able to get all entries in for loop, need to establish a GetEntries before the loop \n", " # is made\n", " all_entries = trees.GetEntries()\n", " mH.Sumw2()\n", " mX.Sumw2()\n", " mHbb.Sumw2()\n", " mHtt.Sumw2()\n", " mbb.Sumw2()\n", " mtt.Sumw2()\n", " # , mSbb, mStt\n", " #mSbb.Sumw2()\n", " #mStt.Sumw2()\n", " #type(trees)\n", " # Grabbing all events with a for loop: i helps generalize what the code needs to collect so that \n", " # all events can be grabbed without error\n", " for i in range(0, all_entries):\n", " trees.GetEntry(i)\n", " #print(i)\n", " # Higgs information\n", " H_pt = getattr(trees, \"H_pt\") # transverse momentum\n", " #print(H_pt) #{x1, x2}\n", " H_eta = getattr(trees, \"H_eta\") # angle\n", " H_phi = getattr(trees, \"H_phi\") # another angle\n", " H_E = getattr(trees, \"H_e\") # energy\n", " # Higgs bb information\n", " Hbb_pt = getattr(trees, \"Hbb_pt\") # transverse momentum\n", " #print(Hbb_pt) #{x1}\n", " Hbb_eta = getattr(trees, \"Hbb_eta\") # angle\n", " Hbb_phi = getattr(trees, \"Hbb_phi\") # another angle\n", " Hbb_E = getattr(trees, \"Hbb_e\") # energy\n", " # Higgs tt information\n", " Htt_pt = getattr(trees, \"Htautau_pt\") # transverse momentum\n", " Htt_eta = getattr(trees, \"Htautau_eta\") # angle\n", " Htt_phi = getattr(trees, \"Htautau_phi\") # another angle\n", " Htt_E = getattr(trees, \"Htautau_e\") # energy\n", " # b information \n", " b_pt = getattr(trees, 'b_pt')\n", " b_eta = getattr(trees, 'b_eta')\n", " b_phi = getattr(trees, 'b_phi')\n", " b_E = getattr(trees, 'b_e')\n", " # t information\n", " t_pt = getattr(trees, 'tau_pt')\n", " t_eta = getattr(trees, 'tau_eta')\n", " t_phi = getattr(trees, 'tau_phi')\n", " t_E = getattr(trees, 'tau_e')\n", " # S bb information\n", " Sbb_pt = getattr(trees, 'Sbb_pt')\n", " #print(Sbb_pt)\n", " Sbb_eta = getattr(trees, 'Sbb_eta')\n", " Sbb_phi = getattr(trees, 'Sbb_phi')\n", " Sbb_E = getattr(trees, 'Sbb_e')\n", " # S tautau information\n", " Stt_pt = getattr(trees, 'Stautau_pt')\n", " Stt_eta = getattr(trees, 'Stautau_eta')\n", " Stt_phi = getattr(trees, 'Stautau_phi')\n", " Stt_E = getattr(trees, 'Stautau_e')\n", " # Converting all this to a 4-vector for simplicity\n", " H_0 = ROOT.TLorentzVector()\n", " H_0.SetPtEtaPhiE(H_pt[0], H_eta[0], H_phi[0], H_E[0])\n", " H_1 = ROOT.TLorentzVector()\n", " H_1.SetPtEtaPhiE(H_pt[1], H_eta[1], H_phi[1], H_E[1])\n", " # Hbb and Htt are floats, so just call them as is\n", " Hbb = ROOT.TLorentzVector()\n", " Hbb.SetPtEtaPhiE(Hbb_pt, Hbb_eta, Hbb_phi, Hbb_E) \n", " Htt = ROOT.TLorentzVector()\n", " Htt.SetPtEtaPhiE(Htt_pt, Htt_eta, Htt_phi, Htt_E)\n", " # 4-vectors for b and t\n", " b_0 = ROOT.TLorentzVector()\n", " t_0 = ROOT.TLorentzVector()\n", " b_0.SetPtEtaPhiE(b_pt[0], b_eta[0], b_phi[0], b_E[0])\n", " t_0.SetPtEtaPhiE(t_pt[0], t_eta[0], t_phi[0], t_E[0])\n", " b_1 = ROOT.TLorentzVector()\n", " t_1 = ROOT.TLorentzVector()\n", " b_1.SetPtEtaPhiE(b_pt[0], b_eta[0], b_phi[0], b_E[0])\n", " t_1.SetPtEtaPhiE(t_pt[0], t_eta[0], t_phi[0], t_E[0])\n", " # 4-vectors for Sbb and Stt\n", " #Sbb = ROOT.TLorentzVector()\n", " #Sbb.SetPtEtaPhiE(Sbb_pt, Sbb_eta, Sbb_phi, Sbb_E)\n", " #Stt = ROOT.TLorentzVector()\n", " #Stt.SetPtEtaPhiE(Stt_pt, Stt_eta, Stt_phi, Stt_E)\n", " # Getting masses:\n", " X = H_0 + H_1 # X is just the total mass of 2 Higgs' added together\n", " X_mass = X.M()\n", " #print(X_mass)\n", " H_mass = H_0.M()\n", " Hbb_mass = Hbb.M()\n", " Htt_mass = Htt.M()\n", " bb = b_0 + b_1\n", " tt = t_0 + t_1\n", " bb_mass = bb.M()\n", " tt_mass = tt.M()\n", " #Sbb_mass = Sbb.M()\n", " #Stt_mass = Stt.M()\n", " # Fills the histogram\n", " mX.Fill(X_mass)\n", " mH.Fill(H_mass)\n", " mHbb.Fill(Hbb_mass)\n", " mHtt.Fill(Htt_mass)\n", " mbb.Fill(bb_mass)\n", " mtt.Fill(tt_mass)\n", " #mSbb.Fill(Sbb_mass)\n", " #mStt.Fill(Stt_mass)\n", " # Sets the histogram to continue to exist after closing the file\n", " mX.SetDirectory(0)\n", " mH.SetDirectory(0)\n", " mHbb.SetDirectory(0)\n", " mHtt.SetDirectory(0)\n", " mbb.SetDirectory(0)\n", " mtt.SetDirectory(0)\n", " #mSbb.SetDirectory(0)\n", " #mStt.SetDirectory(0)\n", " # Writes the histograms \n", " mH.Write()\n", " mX.Write()\n", " mHbb.Write()\n", " mHtt.Write()\n", " mbb.Write()\n", " mtt.Write()\n", " #mSbb.Write()\n", " #mStt.Write()\n", " print('All histograms grabbed')\n", " return mH, mX, mHbb, mHtt, mbb, mtt\n", "# Smearing histograms and saving them \n", "def smear(trees, mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s):\n", " all_entries = trees.GetEntries()\n", " mH_s.Sumw2()\n", " mX_s.Sumw2()\n", " mHbb_s.Sumw2()\n", " mHtt_s.Sumw2()\n", " mbb_s.Sumw2()\n", " mtt_s.Sumw2()\n", " for i in range(0, all_entries):\n", " trees.GetEntry(i)\n", " # Higgs information\n", " H_pt = getattr(trees, \"H_pt\") # transverse momentum\n", " H_eta = getattr(trees, \"H_eta\") # angle\n", " H_phi = getattr(trees, \"H_phi\") # another angle\n", " H_E = getattr(trees, \"H_e\") # energy\n", " # Higgs bb information\n", " Hbb_pt = getattr(trees, \"Hbb_pt\") # transverse momentum\n", " Hbb_eta = getattr(trees, \"Hbb_eta\") # angle\n", " Hbb_phi = getattr(trees, \"Hbb_phi\") # another angle\n", " Hbb_E = getattr(trees, \"Hbb_e\") # energy\n", " # Higgs tt information\n", " Htt_pt = getattr(trees, \"Htautau_pt\") # transverse momentum\n", " Htt_eta = getattr(trees, \"Htautau_eta\") # angle\n", " Htt_phi = getattr(trees, \"Htautau_phi\") # another angle\n", " Htt_E = getattr(trees, \"Htautau_e\") # energy\n", " # b information \n", " b_pt = getattr(trees, 'b_pt')\n", " b_eta = getattr(trees, 'b_eta')\n", " b_phi = getattr(trees, 'b_phi')\n", " b_E = getattr(trees, 'b_e')\n", " # t information\n", " t_pt = getattr(trees, 'tau_pt')\n", " t_eta = getattr(trees, 'tau_eta')\n", " t_phi = getattr(trees, 'tau_phi')\n", " t_E = getattr(trees, 'tau_e')\n", " # Converting all this to a 4-vector for simplicity\n", " H_0 = ROOT.TLorentzVector()\n", " H_0.SetPtEtaPhiE(H_pt[0], H_eta[0], H_phi[0], H_E[0])\n", " H_1 = ROOT.TLorentzVector()\n", " H_1.SetPtEtaPhiE(H_pt[1], H_eta[1], H_phi[1], H_E[1])\n", " # Hbb and Htt are floats, so just call them as is\n", " Hbb = ROOT.TLorentzVector()\n", " Hbb.SetPtEtaPhiE(Hbb_pt, Hbb_eta, Hbb_phi, Hbb_E) \n", " Htt = ROOT.TLorentzVector()\n", " Htt.SetPtEtaPhiE(Htt_pt, Htt_eta, Htt_phi, Htt_E)\n", " # 4-vectors for b and t\n", " b_0 = ROOT.TLorentzVector()\n", " t_0 = ROOT.TLorentzVector()\n", " b_0.SetPtEtaPhiE(b_pt[0], b_eta[0], b_phi[0], b_E[0])\n", " t_0.SetPtEtaPhiE(t_pt[0], t_eta[0], t_phi[0], t_E[0])\n", " b_1 = ROOT.TLorentzVector()\n", " t_1 = ROOT.TLorentzVector()\n", " b_1.SetPtEtaPhiE(b_pt[0], b_eta[0], b_phi[0], b_E[0])\n", " t_1.SetPtEtaPhiE(t_pt[0], t_eta[0], t_phi[0], t_E[0])\n", " # Getting masses:\n", " X = H_0 + H_1 # X is just the total mass of 2 Higgs' added together\n", " X_mass = X.M()\n", " H_mass = H_0.M()\n", " Hbb_mass = Hbb.M()\n", " Htt_mass = Htt.M()\n", " bb = b_0 + b_1\n", " tt = t_0 + t_1\n", " bb_mass = bb.M()\n", " tt_mass = tt.M()\n", " # Establishing sigma -- the way to smear the plots to make them more realistic\n", " sigma = random.gauss(1, 0.12)\n", " #sigma = ROOT.TRandom.Gaus(1, 0.12)\n", " # Fills the histogram, but multiply by some sigma\n", " mX_s.Fill(X_mass*sigma)\n", " mH_s.Fill(H_mass*sigma)\n", " mHbb_s.Fill(Hbb_mass*sigma)\n", " mHtt_s.Fill(Htt_mass*sigma)\n", " mbb_s.Fill(bb_mass*sigma)\n", " mtt_s.Fill(tt_mass*sigma)\n", " # Sets the histogram to continue to exist after closing the file\n", " mX_s.SetDirectory(0)\n", " mH_s.SetDirectory(0)\n", " mHbb_s.SetDirectory(0)\n", " mHtt_s.SetDirectory(0)\n", " mbb_s.SetDirectory(0)\n", " mtt_s.SetDirectory(0)\n", " # Writes the histograms \n", " mH_s.Write()\n", " mX_s.Write()\n", " mHbb_s.Write()\n", " mHtt_s.Write()\n", " mbb_s.Write()\n", " mtt_s.Write()\n", " print('All smeared histograms grabbed')\n", " return mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s\n", "# Building the histogram to save onto the histogram file that's being created\n", "# This should read all the data files and start the process of making trees for all \n", "# of them, as well as grabbing the plots made in the functions and saving them into\n", "# .root and .pdf files\n", "# This is for different X masses -- hence the large if loop nested in the for loop, but\n", "# it doesn't plot appropriate titles. I tried debugging it through making a separate \n", "# function, but it has many bugs. The script is in a subsequent kernel, whoever uses this\n", "# and figures out a fix for it, then it should make things easier!\n", "for i in range(1,31):\n", " # os allows for more direct interaction with the directory -- this was an attempt to\n", " # make the script more environment friendly but I found issues with this; another\n", " # intern (Arman) figured this out so look to his script to see how to circumvent\n", " # changing the names of all the files and having the script in the directory\n", " # that you're looking through like I had to:\n", " #env = 'my_root_env'\n", " #thee_path = os.path.join(path)\n", " filename = 'file%s.root' % i\n", " X_HH = ROOT.TFile.Open(filename, 'READ')\n", " print('File' ,i, 'is grabbed and read')\n", " trees = X_HH.Get('analysis')\n", " print('Tree', i, 'is grabbed')\n", " # Will create a hist file for each file\n", " hist = 'hist%s.root' % i\n", " new_hist_file = ROOT.TFile.Open(hist, 'RECREATE')\n", " # Establishing the histogram\n", " # Bins were set to 100, but limits were constrained based off the values obtained\n", " # and the masses of the particles that were expected to get a nice peak distribution\n", " # For the MC histograms: it really just keeps the singular plot more centered\n", " mH = ROOT.TH1D(\"Higgs\", \"m_{H}, Higgs\", 100, 100e3, 200e3)\n", " mH.GetYaxis().SetTitle(\"Number of events\")\n", " mH.GetXaxis().SetTitle(\"m_{H} [MeV]\")\n", " mH.SetLineColor(ROOT.kRed)\n", " mH.SetTitle('Higgs mass plot -- no smearing')\n", " # For X histograms, since the plots have different masses that were measured, a specific \n", " # if/elif/else loop sets the limits for each one -- there are bugs with it; it starts to\n", " # add the plots at each subsequent file: look at kernel w/ X function to see an attempt\n", " # to fix\n", " X_low = 50e3\n", " if i < 7:\n", " X_high = 400e3\n", " mX = ROOT.TH1D(\"X particle at m = 300 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 300 GeV\", \"m_{X}, X particle\", 100, X_low, X_high)\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle('X mass (at 300GeV) plot -- no smearing')\n", " X_s_title = mX_s.SetTitle('X mass (at 300GeV) plot with smearing')\n", " print('X particle histograms for files 1 --> 6 are grabbed')\n", " elif 7 <= i <= 12:\n", " X_high = 600e3\n", " mX = ROOT.TH1D(\"X particle at m = 500 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 500 GeV\", \"m_{X}, X particle\", 100, X_low, X_high)\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle('X mass (at 500GeV) plot -- no smearing')\n", " X_s_title = mX_s.SetTitle('X mass (at 500GeV) plot with smearing')\n", " print('X particle histograms for files 7 --> 12 are grabbed')\n", " elif 13 <= i <= 18:\n", " X_high = 1100e3\n", " mX = ROOT.TH1D(\"X particle at m = 1000 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 1000 GeV\", \"m_{X}, X particle\", 100, X_low, X_high)\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle('X mass (at 1000GeV) plot -- no smearing')\n", " X_s_title = mX_s.SetTitle('X mass (at 1000GeV) plot with smearing')\n", " print('X particle histograms for files 13 --> 18 are grabbed')\n", " elif 19 <= i <= 24:\n", " X_high = 1700e3\n", " mX = ROOT.TH1D(\"X particle at m = 1500 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 1500 GeV\", \"m_{X}, X particle\", 100, X_low, X_high)\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle('X mass (at 1500GeV) plot -- no smearing')\n", " X_s_title = mX_s.SetTitle('X mass (at 1500GeV) plot with smearing')\n", " print('X particle histograms for files 19 --> 24 are grabbed')\n", " else:\n", " X_high = 2100e3\n", " mX = ROOT.TH1D(\"X particle at m = 2000 GeV\", 'm_{X}, X particle', 100, X_low, X_high)\n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 2000 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle('X mass (at 2000GeV) plot -- no smearing')\n", " X_s_title = mX_s.SetTitle('X mass (at 2000GeV) plot with smearing')\n", " print('X particle histograms for files 25 --> 30 are grabbed')\n", " # Setting up axes titles, plot titles and color to help with comparisons\n", " mX = ROOT.TH1D('X particle', 'm_{X}, X particle', 100, X_low, X_high)\n", " X_title = X_title\n", " X_s_title = X_s_title\n", " mX.GetYaxis().SetTitle(\"Number of events\")\n", " mX.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mHbb = ROOT.TH1D(\"Higgs bb\", \"m_{Hbb}, Higgs bb\", 100, 50e3, 200e3)\n", " mHbb.GetYaxis().SetTitle(\"Number of events\")\n", " mHbb.GetXaxis().SetTitle(\"m_{Hbb} [MeV]\")\n", " mHbb.SetLineColor(ROOT.kOrange+9)\n", " mHbb.SetTitle('Higgs bb mass plot -- no smearing')\n", " mHtt = ROOT.TH1D(\"Higgs tau tau\", \"m_{Htt}, Higgs tt\", 100, 50e3, 200e3)\n", " mHtt.GetYaxis().SetTitle(\"Number of events\")\n", " mHtt.GetXaxis().SetTitle(\"m_{Htt} [MeV]\")\n", " mHtt.SetLineColor(ROOT.kOrange+8)\n", " mHtt.SetTitle('Higgs tau tau mass plot -- no smearing')\n", " mbb = ROOT.TH1D('bb plot', 'm_{bb}, bb', 100, 9e3, 10e3)\n", " mbb.GetYaxis().SetTitle(\"Number of events\")\n", " mbb.GetXaxis().SetTitle(\"m_{bb} [MeV]\")\n", " mbb.SetLineColor(ROOT.kMagenta+3)\n", " mbb.SetTitle('bb mass plot -- no smearing')\n", " mtt = ROOT.TH1D('tautau plot', 'm_{tt}, tt', 50, 3e3, 4e3)\n", " mtt.GetYaxis().SetTitle(\"Number of events\")\n", " mtt.GetXaxis().SetTitle(\"m_{tt} [MeV]\")\n", " mtt.SetLineColor(ROOT.kMagenta-6)\n", " mtt.SetTitle('tau tau mass plot -- no smearing')\n", " mH_s = ROOT.TH1D(\"Higgs smeared\", \"m_{H}, Higgs\", 100, 100e3, 200e3)\n", " mH_s.GetYaxis().SetTitle(\"Number of events\")\n", " mH_s.GetXaxis().SetTitle(\"m_{H_s} [MeV]\")\n", " mH_s.SetLineColor(ROOT.kRed-3)\n", " mH_s.SetTitle('Higgs mass plot with smearing')\n", " mX_s = ROOT.TH1D('X particle smeared', 'm_{X}, X particle', 100, X_low, X_high)\n", " mX_s.GetYaxis().SetTitle(\"Number of events\")\n", " mX_s.GetXaxis().SetTitle('m_{X_s} [MeV]')\n", " mHbb_s = ROOT.TH1D(\"Higgs bb smeared\", \"m_{Hbb}, Higgs bb\", 100, 50e3, 200e3)\n", " mHbb_s.GetYaxis().SetTitle(\"Number of events\")\n", " mHbb_s.GetXaxis().SetTitle(\"m_{Hbb_s} [MeV]\")\n", " mHbb_s.SetLineColor(ROOT.kOrange+4)\n", " mHbb_s.SetTitle('Higgs bb mass plot with smearing')\n", " mHtt_s = ROOT.TH1D(\"Higgs tau tau smeared\", \"m_{Htt}, Higgs tt\", 100, 50e3, 200e3)\n", " mHtt_s.GetYaxis().SetTitle(\"Number of events\")\n", " mHtt_s.GetXaxis().SetTitle(\"m_{Htt_s} [MeV]\")\n", " mHtt_s.SetLineColor(ROOT.kViolet)\n", " mHtt_s.SetTitle('Higgs tau tau mass plot with smearing')\n", " mbb_s = ROOT.TH1D('bb plot smeared', 'm_{bb}, bb', 300, 9e3, 10e3)\n", " mbb_s.GetYaxis().SetTitle(\"Number of events\")\n", " mbb_s.GetXaxis().SetTitle(\"m_{bb_s} [MeV]\")\n", " mbb_s.SetLineColor(ROOT.kViolet-3)\n", " mbb_s.SetTitle('bb mass plot with smearing')\n", " mtt_s = ROOT.TH1D('tautau plot smeared', 'm_{tt}, tt', 50, 3e3, 4e3)\n", " mtt_s.GetYaxis().SetTitle(\"Number of events\")\n", " mtt_s.GetXaxis().SetTitle(\"m_{tt_s} [MeV]\")\n", " mtt_s.SetLineColor(ROOT.kBlue+2)\n", " mtt_s.SetTitle('tau tau mass plot with smearing')\n", " # Got Sbb and Stt data but idk what the limits for the hist should be yet:\n", " #mSbb = ROOT.TH1D(\"S particle bb\", \"m_{Sbb}, S bb\", 5, 0, 20000e3)\n", " #mStt = ROOT.TH1D(\"S particle tau tau\", \"m_{Stt}, S tt\", 5, 0, 20000e3)\n", " histogram(trees, mH, mX, mHbb, mHtt, mbb, mtt)\n", " smear(trees, mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s)\n", " # Building the histogram to save onto the histogram file that's being created\n", " print('Running over data')\n", " # Closing the data file\n", " X_HH.Close() \n", " print('Done running data from file')\n", " print('Writing histograms to output file...')\n", " print('Histograms collected for file', i,)\n", " # Creates and then closes the hist file\n", " new_hist_file.cd()\n", " new_hist_file.Close()\n", " i=i+1\n" ] }, { "cell_type": "markdown", "id": "f1e8550e", "metadata": {}, "source": [ "# To Create PDFs of Plots\n", "This is a similar version of the skeleton kernel, but it can save the histograms onto .pdf files instead of .root files for convenience. " ] }, { "cell_type": "code", "execution_count": null, "id": "25825bdd", "metadata": {}, "outputs": [], "source": [ "import ROOT\n", "import os # allows for more direct interaction with the directory\n", "import random\n", "\n", "# This creates pdf files for the plots instead of .root files, although my personal \n", "# script can run both functions well (running 30 files and grabbing both types of files\n", "# takes a couple of minutes)\n", "canvas = ROOT.TCanvas(\"canvas\")\n", "def histogram(trees, mH, mX, mHbb, mHtt, mbb, mtt):\n", " # To be able to get all entries in for loop, need to establish a GetEntries before the loop \n", " # is made\n", " all_entries = trees.GetEntries()\n", " mH.Sumw2()\n", " mX.Sumw2()\n", " mHbb.Sumw2()\n", " mHtt.Sumw2()\n", " mbb.Sumw2()\n", " mtt.Sumw2()\n", " # , mSbb, mStt\n", " #mSbb.Sumw2()\n", " #mStt.Sumw2()\n", " #type(trees)\n", " # Grabbing all events with a for loop: i helps generalize what the code needs to collect so that \n", " # all events can be grabbed without error\n", " for i in range(0, all_entries):\n", " trees.GetEntry(i)\n", " #print(i)\n", " # Higgs information\n", " H_pt = getattr(trees, \"H_pt\") # transverse momentum\n", " #print(H_pt) #{x1, x2}\n", " H_eta = getattr(trees, \"H_eta\") # angle\n", " H_phi = getattr(trees, \"H_phi\") # another angle\n", " H_E = getattr(trees, \"H_e\") # energy\n", " # Higgs bb information\n", " Hbb_pt = getattr(trees, \"Hbb_pt\") # transverse momentum\n", " #print(Hbb_pt) #{x1}\n", " Hbb_eta = getattr(trees, \"Hbb_eta\") # angle\n", " Hbb_phi = getattr(trees, \"Hbb_phi\") # another angle\n", " Hbb_E = getattr(trees, \"Hbb_e\") # energy\n", " # Higgs tt information\n", " Htt_pt = getattr(trees, \"Htautau_pt\") # transverse momentum\n", " Htt_eta = getattr(trees, \"Htautau_eta\") # angle\n", " Htt_phi = getattr(trees, \"Htautau_phi\") # another angle\n", " Htt_E = getattr(trees, \"Htautau_e\") # energy\n", " # b information \n", " b_pt = getattr(trees, 'b_pt')\n", " b_eta = getattr(trees, 'b_eta')\n", " b_phi = getattr(trees, 'b_phi')\n", " b_E = getattr(trees, 'b_e')\n", " # t information\n", " t_pt = getattr(trees, 'tau_pt')\n", " t_eta = getattr(trees, 'tau_eta')\n", " t_phi = getattr(trees, 'tau_phi')\n", " t_E = getattr(trees, 'tau_e')\n", " # S bb information\n", " Sbb_pt = getattr(trees, 'Sbb_pt')\n", " #print(Sbb_pt)\n", " Sbb_eta = getattr(trees, 'Sbb_eta')\n", " Sbb_phi = getattr(trees, 'Sbb_phi')\n", " Sbb_E = getattr(trees, 'Sbb_e')\n", " # S tautau information\n", " Stt_pt = getattr(trees, 'Stautau_pt')\n", " Stt_eta = getattr(trees, 'Stautau_eta')\n", " Stt_phi = getattr(trees, 'Stautau_phi')\n", " Stt_E = getattr(trees, 'Stautau_e')\n", " # Converting all this to a 4-vector for simplicity\n", " H_0 = ROOT.TLorentzVector()\n", " H_0.SetPtEtaPhiE(H_pt[0], H_eta[0], H_phi[0], H_E[0])\n", " H_1 = ROOT.TLorentzVector()\n", " H_1.SetPtEtaPhiE(H_pt[1], H_eta[1], H_phi[1], H_E[1])\n", " # Hbb and Htt are floats, so just call them as is\n", " Hbb = ROOT.TLorentzVector()\n", " Hbb.SetPtEtaPhiE(Hbb_pt, Hbb_eta, Hbb_phi, Hbb_E) \n", " Htt = ROOT.TLorentzVector()\n", " Htt.SetPtEtaPhiE(Htt_pt, Htt_eta, Htt_phi, Htt_E)\n", " # 4-vectors for b and t\n", " b_0 = ROOT.TLorentzVector()\n", " t_0 = ROOT.TLorentzVector()\n", " b_0.SetPtEtaPhiE(b_pt[0], b_eta[0], b_phi[0], b_E[0])\n", " t_0.SetPtEtaPhiE(t_pt[0], t_eta[0], t_phi[0], t_E[0])\n", " b_1 = ROOT.TLorentzVector()\n", " t_1 = ROOT.TLorentzVector()\n", " b_1.SetPtEtaPhiE(b_pt[0], b_eta[0], b_phi[0], b_E[0])\n", " t_1.SetPtEtaPhiE(t_pt[0], t_eta[0], t_phi[0], t_E[0])\n", " # 4-vectors for Sbb and Stt\n", " #Sbb = ROOT.TLorentzVector()\n", " #Sbb.SetPtEtaPhiE(Sbb_pt, Sbb_eta, Sbb_phi, Sbb_E)\n", " #Stt = ROOT.TLorentzVector()\n", " #Stt.SetPtEtaPhiE(Stt_pt, Stt_eta, Stt_phi, Stt_E)\n", " # Getting masses:\n", " X = H_0 + H_1 # X is just the total mass of 2 Higgs' added together\n", " X_mass = X.M()\n", " #print(X_mass)\n", " H_mass = H_0.M()\n", " Hbb_mass = Hbb.M()\n", " Htt_mass = Htt.M()\n", " bb = b_0 + b_1\n", " tt = t_0 + t_1\n", " bb_mass = bb.M()\n", " tt_mass = tt.M()\n", " #Sbb_mass = Sbb.M()\n", " #Stt_mass = Stt.M()\n", " # Fills the histogram\n", " mX.Fill(X_mass)\n", " mH.Fill(H_mass)\n", " mHbb.Fill(Hbb_mass)\n", " mHtt.Fill(Htt_mass)\n", " mbb.Fill(bb_mass)\n", " mtt.Fill(tt_mass)\n", " #mSbb.Fill(Sbb_mass)\n", " #mStt.Fill(Stt_mass)\n", " # Sets the histogram to continue to exist after closing the file\n", " mX.SetDirectory(0)\n", " mH.SetDirectory(0)\n", " mHbb.SetDirectory(0)\n", " mHtt.SetDirectory(0)\n", " mbb.SetDirectory(0)\n", " mtt.SetDirectory(0)\n", " #mSbb.SetDirectory(0)\n", " #mStt.SetDirectory(0)\n", " # Writes the histograms \n", " #mSbb.Write()\n", " #mStt.Write()\n", " # Draws the histograms\n", " print('All histograms grabbed')\n", " return mH, mX, mHbb, mHtt, mbb, mtt\n", "# Smearing histograms and saving them \n", "def smear(trees, mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s):\n", " all_entries = trees.GetEntries()\n", " mH_s.Sumw2()\n", " mX_s.Sumw2()\n", " mHbb_s.Sumw2()\n", " mHtt_s.Sumw2()\n", " mbb_s.Sumw2()\n", " mtt_s.Sumw2()\n", " for i in range(0, all_entries):\n", " trees.GetEntry(i)\n", " # Higgs information\n", " H_pt = getattr(trees, \"H_pt\") # transverse momentum\n", " H_eta = getattr(trees, \"H_eta\") # angle\n", " H_phi = getattr(trees, \"H_phi\") # another angle\n", " H_E = getattr(trees, \"H_e\") # energy\n", " # Higgs bb information\n", " Hbb_pt = getattr(trees, \"Hbb_pt\") # transverse momentum\n", " Hbb_eta = getattr(trees, \"Hbb_eta\") # angle\n", " Hbb_phi = getattr(trees, \"Hbb_phi\") # another angle\n", " Hbb_E = getattr(trees, \"Hbb_e\") # energy\n", " # Higgs tt information\n", " Htt_pt = getattr(trees, \"Htautau_pt\") # transverse momentum\n", " Htt_eta = getattr(trees, \"Htautau_eta\") # angle\n", " Htt_phi = getattr(trees, \"Htautau_phi\") # another angle\n", " Htt_E = getattr(trees, \"Htautau_e\") # energy\n", " # b information \n", " b_pt = getattr(trees, 'b_pt')\n", " b_eta = getattr(trees, 'b_eta')\n", " b_phi = getattr(trees, 'b_phi')\n", " b_E = getattr(trees, 'b_e')\n", " # t information\n", " t_pt = getattr(trees, 'tau_pt')\n", " t_eta = getattr(trees, 'tau_eta')\n", " t_phi = getattr(trees, 'tau_phi')\n", " t_E = getattr(trees, 'tau_e')\n", " # Converting all this to a 4-vector for simplicity\n", " H_0 = ROOT.TLorentzVector()\n", " H_0.SetPtEtaPhiE(H_pt[0], H_eta[0], H_phi[0], H_E[0])\n", " H_1 = ROOT.TLorentzVector()\n", " H_1.SetPtEtaPhiE(H_pt[1], H_eta[1], H_phi[1], H_E[1])\n", " # Hbb and Htt are floats, so just call them as is\n", " Hbb = ROOT.TLorentzVector()\n", " Hbb.SetPtEtaPhiE(Hbb_pt, Hbb_eta, Hbb_phi, Hbb_E) \n", " Htt = ROOT.TLorentzVector()\n", " Htt.SetPtEtaPhiE(Htt_pt, Htt_eta, Htt_phi, Htt_E)\n", " # 4-vectors for b and t\n", " b_0 = ROOT.TLorentzVector()\n", " t_0 = ROOT.TLorentzVector()\n", " b_0.SetPtEtaPhiE(b_pt[0], b_eta[0], b_phi[0], b_E[0])\n", " t_0.SetPtEtaPhiE(t_pt[0], t_eta[0], t_phi[0], t_E[0])\n", " b_1 = ROOT.TLorentzVector()\n", " t_1 = ROOT.TLorentzVector()\n", " b_1.SetPtEtaPhiE(b_pt[0], b_eta[0], b_phi[0], b_E[0])\n", " t_1.SetPtEtaPhiE(t_pt[0], t_eta[0], t_phi[0], t_E[0])\n", " # Getting masses:\n", " X = H_0 + H_1 # X is just the total mass of 2 Higgs' added together\n", " X_mass = X.M()\n", " H_mass = H_0.M()\n", " Hbb_mass = Hbb.M()\n", " Htt_mass = Htt.M()\n", " bb = b_0 + b_1\n", " tt = t_0 + t_1\n", " bb_mass = bb.M()\n", " tt_mass = tt.M()\n", " # Establishing sigma -- the way to smear the plots to make them more realistic\n", " sigma = random.gauss(1, 0.12)\n", " #sigma = ROOT.TRandom.Gaus(1, 0.12)\n", " # Fills the histogram, but multiply by some sigma\n", " mX_s.Fill(X_mass*sigma)\n", " mH_s.Fill(H_mass*sigma)\n", " mHbb_s.Fill(Hbb_mass*sigma)\n", " mHtt_s.Fill(Htt_mass*sigma)\n", " mbb_s.Fill(bb_mass*sigma)\n", " mtt_s.Fill(tt_mass*sigma)\n", " # Sets the histogram to continue to exist after closing the file\n", " mX_s.SetDirectory(0)\n", " mH_s.SetDirectory(0)\n", " mHbb_s.SetDirectory(0)\n", " mHtt_s.SetDirectory(0)\n", " mbb_s.SetDirectory(0)\n", " mtt_s.SetDirectory(0)\n", " # Writes and draws the histograms \n", " print('All smeared histograms grabbed')\n", " return mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s\n", "# Building the histogram to save onto the histogram file that's being created\n", "# This should read all the data files and start the process of making trees for all of them\n", "# Axes for X plots don't work in the if loop\n", "for i in range(1,31):\n", " # os allows for more direct interaction with the directory\n", " #env = 'my_root_env'\n", " #thee_path = os.path.join(path)\n", " filename = 'file%s.root' % i\n", " X_HH = ROOT.TFile.Open(filename, 'READ')\n", " print('File' ,i, 'is grabbed and read')\n", " trees = X_HH.Get('analysis')\n", " print('Tree', i, 'is grabbed')\n", " # Will create a hist file for each file\n", " #hists = ROOT.TFile.Open('hist%s.root' % i, 'CREATE')\n", " #hist = 'hist%s.root' % i\n", " #new_hist_file = ROOT.TFile.Open(hist, 'RECREATE')\n", " # Establishing the histogram\n", " # Bins are chosen based off the values obtained for each particle\n", " mH = ROOT.TH1D(\"Higgs\", \"m_{H}, Higgs\", 100, 100e3, 200e3)\n", " mH.GetYaxis().SetTitle(\"Number of events\")\n", " mH.GetXaxis().SetTitle(\"m_{H} [MeV]\")\n", " mH.SetLineColor(ROOT.kRed)\n", " mH.SetTitle('Higgs mass plot -- no smearing')\n", " # For X histograms, since the plots have different masses that were measured, a specific if/elif/\n", " # else loop sets the limits for each one\n", " X_low = 50e3\n", " if i < 7:\n", " X_high = 400e3\n", " # Fix titles back to original setting ---> do it via canvas or something\n", " # it's repeating histograms when changing the title\n", " mX = ROOT.TH1D(\"X particle at m = 300 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 300 GeV\", \"m_{X}, X particle\", 100, X_low, X_high)\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle(\"X mass (at 300GeV) plot -- no smearing\")\n", " X_s_title = mX_s.SetTitle(\"X mass (at 300GeV) plot with smearing\")\n", " print('X particle histograms for files 1 --> 6 are grabbed')\n", " elif 7 <= i <= 12:\n", " X_high = 600e3\n", " mX = ROOT.TH1D(\"X particle at m = 500 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 500 GeV\", \"m_{X}, X particle\", 100, X_low, X_high)\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle(\"X mass (at 500GeV) plot -- no smearing\")\n", " X_s_title = mX_s.SetTitle(\"X mass (at 500GeV) plot with smearing\")\n", " print('X particle histograms for files 7 --> 12 are grabbed')\n", " elif 13 <= i <= 18:\n", " X_high = 1100e3\n", " mX = ROOT.TH1D(\"X particle at m = 1000 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 1000 GeV\", \"m_{X}, X particle\", 100, X_low, X_high)\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle(\"X mass (at 1000GeV) plot -- no smearing\")\n", " X_s_title = mX_s.SetTitle(\"X mass (at 1000GeV) plot with smearing\")\n", " print('X particle histograms for files 13 --> 18 are grabbed')\n", " elif 19 <= i <= 24:\n", " X_high = 1700e3\n", " mX = ROOT.TH1D(\"X particle at m = 1500 GeV\", \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX_s = ROOT.TH1D(\"X particle smeared at m = 1500 GeV\", \"m_{X}, X particle\", 100, X_low, X_high)\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle(\"X mass (at 1500GeV) plot -- no smearing\")\n", " X_s_title = mX_s.SetTitle(\"X mass (at 1500GeV) plot with smearing\")\n", " print('X particle histograms for files 19 --> 24 are grabbed')\n", " else:\n", " X_high = 2100e3\n", " mX = ROOT.TH1D('X particle at m = 2000 GeV', 'm_{X}, X particle', 100, X_low, X_high)\n", " mX_s = ROOT.TH1D('X particle smeared at m = 2000 GeV', \"m_{X}, X particle\", 100, X_low, X_high) \n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " X_title = mX.SetTitle(\"X mass (at 2000GeV) plot -- no smearing\")\n", " X_s_title = mX_s.SetTitle(\"X mass (at 2000GeV) plot with smearing\")\n", " print('X particle histograms for files 25 --> 30 are grabbed')\n", " mX = ROOT.TH1D('X particle', 'm_{X}, X particle', 100, X_low, X_high)\n", " X_title = X_title\n", " X_s_title = X_s_title\n", " mX.SetLineColor(ROOT.kBlue)\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " mX.GetYaxis().SetTitle(\"Number of events\")\n", " mX.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mHbb = ROOT.TH1D(\"Higgs bb\", \"m_{Hbb}, Higgs bb\", 100, 50e3, 200e3)\n", " mHbb.GetYaxis().SetTitle(\"Number of events\")\n", " mHbb.GetXaxis().SetTitle(\"m_{Hbb} [MeV]\")\n", " mHbb.SetLineColor(ROOT.kOrange+9)\n", " mHbb.SetTitle('Higgs bb mass plot -- no smearing')\n", " mHtt = ROOT.TH1D(\"Higgs tau tau\", \"m_{Htt}, Higgs tt\", 100, 50e3, 200e3)\n", " mHtt.GetYaxis().SetTitle(\"Number of events\")\n", " mHtt.GetXaxis().SetTitle(\"m_{Htt} [MeV]\")\n", " mHtt.SetLineColor(ROOT.kOrange+8)\n", " mHtt.SetTitle('Higgs tau tau mass plot -- no smearing')\n", " mbb = ROOT.TH1D('bb plot', 'm_{bb}, bb', 100, 9e3, 10e3)\n", " mbb.GetYaxis().SetTitle(\"Number of events\")\n", " mbb.GetXaxis().SetTitle(\"m_{bb} [MeV]\")\n", " mbb.SetLineColor(ROOT.kMagenta+3)\n", " mbb.SetTitle('bb mass plot -- no smearing')\n", " mtt = ROOT.TH1D('tautau plot', 'm_{tt}, tt', 50, 3e3, 4e3)\n", " mtt.GetYaxis().SetTitle(\"Number of events\")\n", " mtt.GetXaxis().SetTitle(\"m_{tt} [MeV]\")\n", " mtt.SetLineColor(ROOT.kMagenta-6)\n", " mtt.SetTitle('tau tau mass plot -- no smearing')\n", " mH_s = ROOT.TH1D(\"Higgs smeared\", \"m_{H}, Higgs\", 100, 100e3, 200e3)\n", " mH_s.GetYaxis().SetTitle(\"Number of events\")\n", " mH_s.GetXaxis().SetTitle(\"m_{H_s} [MeV]\")\n", " mH_s.SetLineColor(ROOT.kRed-3)\n", " mH_s.SetTitle('Higgs mass plot with smearing')\n", " mX_s = ROOT.TH1D('X particle smeared', 'm_{X}, X particle', 100, X_low, X_high)\n", " mX_s.GetYaxis().SetTitle(\"Number of events\")\n", " mX_s.GetXaxis().SetTitle('m_{X_s} [MeV]')\n", " mHbb_s = ROOT.TH1D(\"Higgs bb smeared\", \"m_{Hbb}, Higgs bb\", 100, 50e3, 200e3)\n", " mHbb_s.GetYaxis().SetTitle(\"Number of events\")\n", " mHbb_s.GetXaxis().SetTitle(\"m_{Hbb_s} [MeV]\")\n", " mHbb_s.SetLineColor(ROOT.kOrange+4)\n", " mHbb_s.SetTitle('Higgs bb mass plot with smearing')\n", " mHtt_s = ROOT.TH1D(\"Higgs tau tau smeared\", \"m_{Htt}, Higgs tt\", 100, 50e3, 200e3)\n", " mHtt_s.GetYaxis().SetTitle(\"Number of events\")\n", " mHtt_s.GetXaxis().SetTitle(\"m_{Htt_s} [MeV]\")\n", " mHtt_s.SetLineColor(ROOT.kViolet)\n", " mHtt_s.SetTitle('Higgs tau tau mass plot with smearing')\n", " mbb_s = ROOT.TH1D('bb plot smeared', 'm_{bb}, bb', 300, 9e3, 10e3)\n", " mbb_s.GetYaxis().SetTitle(\"Number of events\")\n", " mbb_s.GetXaxis().SetTitle(\"m_{bb_s} [MeV]\")\n", " mbb_s.SetLineColor(ROOT.kViolet-3)\n", " mbb_s.SetTitle('bb mass plot with smearing')\n", " mtt_s = ROOT.TH1D('tautau plot smeared', 'm_{tt}, tt', 50, 3e3, 4e3)\n", " mtt_s.GetYaxis().SetTitle(\"Number of events\")\n", " mtt_s.GetXaxis().SetTitle(\"m_{tt_s} [MeV]\")\n", " mtt_s.SetLineColor(ROOT.kBlue+2)\n", " mtt_s.SetTitle('tau tau mass plot with smearing')\n", " histogram(trees, mH, mX, mHbb, mHtt, mbb, mtt)\n", " smear(trees, mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s)\n", " # Building the histogram to save onto the histogram file that's being created\n", " print('Running over data')\n", " # Grabs all plots and puts onto pdfs\n", " for k in range(1,31):\n", " canvas.cd(k)\n", " canvas.Print('hist%s.pdf' % k + '[')\n", " histogram(trees, mH, mX, mHbb, mHtt, mbb, mtt)\n", " mH.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mX.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mHbb.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mHtt.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mbb.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mtt.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " smear(trees, mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s)\n", " mH_s.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mX_s.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mHbb_s.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mHtt_s.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mbb_s.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " mtt_s.Draw('h')\n", " canvas.Print('hist%s.pdf' % k)\n", " canvas.Print('hist%s.pdf' % k + ']')\n", " canvas.Update()\n", " k=k+1\n", " # Closing the data file\n", " X_HH.Close() \n", " print('Done running data from file')\n", " print('Writing histograms to output file...')\n", " print('Histograms collected for file', k,)\n", " # Creates and then closes the hist file\n", " #new_hist_file.cd()\n", " #new_hist_file.Close()\n", " i=i+1" ] }, { "cell_type": "markdown", "id": "3bf304d0", "metadata": {}, "source": [ "# X Plot Function -- Attempt\n", "This is the X plot function attempt mentioned in the 1st code kernel: it plots 2 graphs (one MC and one smeared) and while the titles and limits are accurate, for some reason, it only plots the correct MC and smeared plots for every 6 files (WRT the ntuple w/o S data) but not for the first few files per loop. I could not figure out why this error was happening, but since files can have different masses to account for, this is a good place to start if you want to have one code do it all" ] }, { "cell_type": "code", "execution_count": null, "id": "55764377", "metadata": {}, "outputs": [], "source": [ "import ROOT \n", "import random \n", "\n", "# Follows the same logic as the other functions but the histograms are split WRT to\n", "# X mass\n", "def X_plot(trees, canvas, mX_300, mX_500, mX_1000, mX_1500, mX_2000, FN):\n", " all_entries = trees.GetEntries()\n", " mX_300.Sumw2()\n", " mX_500.Sumw2()\n", " mX_1000.Sumw2()\n", " mX_1500.Sumw2()\n", " mX_2000.Sumw2()\n", " for i in range(0, all_entries):\n", " trees.GetEntry(i)\n", " H_pt = getattr(trees, \"H_pt\") # transverse momentum\n", " H_eta = getattr(trees, \"H_eta\") # angle\n", " H_phi = getattr(trees, \"H_phi\") # another angle\n", " H_E = getattr(trees, \"H_e\") # energy\n", " # Converting all this to a 4-vector for simplicity\n", " H_0 = ROOT.TLorentzVector()\n", " H_0.SetPtEtaPhiE(H_pt[0], H_eta[0], H_phi[0], H_E[0])\n", " H_1 = ROOT.TLorentzVector()\n", " H_1.SetPtEtaPhiE(H_pt[1], H_eta[1], H_phi[1], H_E[1])\n", " X = H_0 + H_1 # X is just the total mass of 2 Higgs' added together\n", " X_mass = X.M()\n", " if FN < 7:\n", " mX_300.Fill(X_mass)\n", " mX_300.GetYaxis().SetTitle(\"Number of events\")\n", " mX_300.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_300.SetLineColor(ROOT.kBlue)\n", " mX_300.SetTitle('X mass (at 300GeV) plot -- no smearing')\n", " #print('X particle histograms for files 1 --> 6 are filled')\n", " elif 7 <= FN <= 12:\n", " mX_500.Fill(X_mass)\n", " mX_500.GetYaxis().SetTitle(\"Number of events\")\n", " mX_500.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_500.SetLineColor(ROOT.kBlue)\n", " mX_500.SetTitle('X mass (at 500GeV) plot -- no smearing')\n", " #print('X particle histograms for files 7 --> 12 are filled')\n", " elif 13 <= FN <= 18:\n", " mX_1000.Fill(X_mass)\n", " mX_1000.GetYaxis().SetTitle(\"Number of events\")\n", " mX_1000.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_1000.SetLineColor(ROOT.kBlue)\n", " mX_1000.SetTitle('X mass (at 1000GeV) plot -- no smearing')\n", " #print('X particle histograms for files 13 --> 18 are filled')\n", " elif 19 <= FN <= 24:\n", " mX_1500.Fill(X_mass)\n", " mX_1500.GetYaxis().SetTitle(\"Number of events\")\n", " mX_1500.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_1500.SetLineColor(ROOT.kBlue)\n", " mX_1500.SetTitle('X mass (at 1500GeV) plot -- no smearing')\n", " #print('X particle histograms for files 19 --> 24 are filled')\n", " else:\n", " mX_2000.Fill(X_mass)\n", " mX_2000.GetYaxis().SetTitle(\"Number of events\")\n", " mX_2000.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_2000.SetLineColor(ROOT.kBlue)\n", " mX_2000.SetTitle('X mass (at 2000GeV) plot -- no smearing')\n", " #print('X particle histograms for files 25 --> 30 are filled')\n", " print('All X histograms grabbed')\n", " # All the other stuff\n", " if FN < 7:\n", " canvas.Update()\n", " mX_300.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " mX_300.SetDirectory(0)\n", " elif 7 <= FN <= 12:\n", " canvas.Update()\n", " mX_500.SetDirectory(0)\n", " mX_500.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " elif 13 <= FN <= 18:\n", " canvas.Update()\n", " mX_1000.SetDirectory(0)\n", " mX_1000.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " elif 19 <= FN <= 24:\n", " canvas.Update()\n", " mX_1500.SetDirectory(0)\n", " mX_1500.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " else:\n", " canvas.Update()\n", " mX_2000.SetDirectory(0)\n", " mX_2000.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " return mX_300, mX_500, mX_1000, mX_1500, mX_2000\n", "\n", "def X_plot_s(trees, canvas, mX_300_s, mX_500_s, mX_1000_s, mX_1500_s, mX_2000_s, FN):\n", " all_entries = trees.GetEntries()\n", " mX_300_s.Sumw2()\n", " mX_500_s.Sumw2()\n", " mX_1000_s.Sumw2()\n", " mX_1500_s.Sumw2()\n", " mX_2000_s.Sumw2()\n", " for i in range(0, all_entries):\n", " trees.GetEntry(i)\n", " H_pt = getattr(trees, \"H_pt\") # transverse momentum\n", " H_eta = getattr(trees, \"H_eta\") # angle\n", " H_phi = getattr(trees, \"H_phi\") # another angle\n", " H_E = getattr(trees, \"H_e\") # energy\n", " sigma = random.gauss(1, 0.12) # smearing factor\n", " # Converting all this to a 4-vector for simplicity\n", " H_0 = ROOT.TLorentzVector()\n", " H_0.SetPtEtaPhiE(H_pt[0]*sigma, H_eta[0], H_phi[0], H_E[0])\n", " H_1 = ROOT.TLorentzVector()\n", " H_1.SetPtEtaPhiE(H_pt[1]*sigma, H_eta[1], H_phi[1], H_E[1])\n", " X = H_0 + H_1 # X is just the total mass of 2 Higgs' added together\n", " X_mass = X.M()\n", " if FN < 7:\n", " mX_300_s.Fill(X_mass)\n", " mX_300_s.GetYaxis().SetTitle(\"Number of events\")\n", " mX_300_s.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_300_s.SetLineColor(ROOT.kBlue-7)\n", " mX_300_s.SetTitle('X mass (at 300GeV) plot with smearing')\n", " #print('X particle histograms for files 1 --> 6 are filled')\n", " elif 7 <= FN <= 12:\n", " mX_500_s.Fill(X_mass)\n", " mX_500_s.GetYaxis().SetTitle(\"Number of events\")\n", " mX_500_s.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_500_s.SetLineColor(ROOT.kBlue-7)\n", " mX_500_s.SetTitle('X mass (at 500GeV) plot with smearing')\n", " #print('X particle histograms for files 7 --> 12 are filled')\n", " elif 13 <= FN <= 18:\n", " mX_1000_s.Fill(X_mass)\n", " mX_1000_s.GetYaxis().SetTitle(\"Number of events\")\n", " mX_1000_s.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_1000_s.SetLineColor(ROOT.kBlue-7)\n", " mX_1000_s.SetTitle('X mass (at 1000GeV) plot with smearing')\n", " #print('X particle histograms for files 13 --> 18 are filled')\n", " elif 19 <= FN <= 24:\n", " mX_1500_s.Fill(X_mass)\n", " mX_1500_s.GetYaxis().SetTitle(\"Number of events\")\n", " mX_1500_s.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_1500_s.SetLineColor(ROOT.kBlue-7)\n", " mX_1500_s.SetTitle('X mass (at 1500GeV) plot with smearing')\n", " #print('X particle histograms for files 19 --> 24 are filled')\n", " else:\n", " mX_2000_s.Fill(X_mass)\n", " mX_2000_s.GetYaxis().SetTitle(\"Number of events\")\n", " mX_2000_s.GetXaxis().SetTitle('m_{X} [MeV]')\n", " mX_2000_s.SetLineColor(ROOT.kBlue-7)\n", " mX_2000_s.SetTitle('X mass (at 2000GeV) plot with smearing')\n", " #print('X particle histograms for files 25 --> 30 are filled')\n", " print('All X histograms grabbed')\n", " # All other graphing stuff\n", " if FN < 7:\n", " canvas.Update()\n", " mX_300_s.SetDirectory(0)\n", " mX_300_s.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " elif 7 <= FN <= 12:\n", " canvas.Update()\n", " mX_500_s.SetDirectory(0)\n", " mX_500_s.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " elif 13 <= FN <= 18:\n", " canvas.Update()\n", " mX_1000_s.SetDirectory(0)\n", " mX_1000_s.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN) \n", " elif 19 <= FN <= 24:\n", " canvas.Update()\n", " mX_1500_s.SetDirectory(0)\n", " mX_1500_s.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " else:\n", " canvas.Update()\n", " mX_2000_s.SetDirectory(0)\n", " mX_2000_s.Draw('h')\n", " canvas.Print('hist_X%s.pdf' % FN)\n", " return mX_300_s, mX_500_s, mX_1000_s, mX_1500_s, mX_2000_s\n", "for i in range(1,31):\n", " filename = 'file%s.root' % i\n", " X_HH = ROOT.TFile.Open(filename, 'READ')\n", " print('File' ,i, 'is grabbed and read')\n", " trees = X_HH.Get('analysis')\n", " print('Tree', i, 'is grabbed')\n", " mX_300 = ROOT.TH1D('X particle at m = 300 GeV', 'm_{X}, X particle', 100, 50e3, 400e3)\n", " mX_500 = ROOT.TH1D('X particle at m = 500 GeV', 'm_{X}, X particle', 100, 50e3, 600e3)\n", " mX_1000 = ROOT.TH1D('X particle at m = 1000 GeV', 'm_{X}, X particle', 100, 50e3, 1100e3)\n", " mX_1500 = ROOT.TH1D('X particle at m = 1500 GeV', 'm_{X}, X particle', 100, 50e3, 1700e3)\n", " mX_2000 = ROOT.TH1D('X particle at m = 2000 GeV', 'm_{X}, X particle', 100, 50e3, 2100e3)\n", " mX_300_s = ROOT.TH1D('X particle smeared at m = 300 GeV', 'm_{X}, X particle', 100, 50e3, 400e3)\n", " mX_500_s = ROOT.TH1D('X particle smeared at m = 500 GeV', 'm_{X}, X particle', 100, 50e3, 600e3)\n", " mX_1000_s = ROOT.TH1D('X particle smeared at m = 1000 GeV', 'm_{X}, X particle', 100, 50e3, 1100e3)\n", " mX_1500_s = ROOT.TH1D('X particle smeared at m = 1500 GeV', 'm_{X}, X particle', 100, 50e3, 1700e3)\n", " mX_2000_s = ROOT.TH1D('X particle smeared at m = 2000 GeV', 'm_{X}, X particle', 100, 50e3, 2100e3) \n", " # Saves files as pdfs \n", " canvas = ROOT.TCanvas(\"canvas\")\n", " canvas.cd() # Open the canvas for continuous printing\n", " canvas.Print('hist_X%s.pdf'% i +'[')\n", " FN = i\n", " X_plot(trees, canvas, mX_300, mX_500, mX_1000, mX_1500, mX_2000, FN)\n", " X_plot_s(trees, canvas, mX_300_s, mX_500_s, mX_1000_s, mX_1500_s, mX_2000_s, FN)\n", " canvas.Print('hist_X%s.pdf'% i +']')\n", " # Building the histogram to save onto the histogram file that's being created\n", " print('Running over data')\n", " # Closing the data file\n", " X_HH.Close() \n", " print('Done running data from file')\n" ] }, { "cell_type": "markdown", "id": "83b01882", "metadata": {}, "source": [ "# S Particle Kernel\n", "This is the basis of my project -- it plots Higgs, X particle, S particle and other histograms but mainly is to show the mass comparison between the Higgs boson and the S particle for various S masses. A mysterious bug showed up that didn't in the previous iterations of this code, where the separate S smeared plot won't show but it will kind of show it with a title 'Htt_pt_s' -- neither myself or my mentor were able to figure out where it came from or why or how to fix it. " ] }, { "cell_type": "code", "execution_count": null, "id": "dad1b8d5", "metadata": {}, "outputs": [], "source": [ "## This kernel is WRT S particle data -- commented in earlier kernels has the setups for\n", "## S-based histograms but it doesn't include superimposed plots. This one does:\n", "import ROOT\n", "import random \n", "\n", "# For some reason, it's not plotting the S smeared plot separately, or it is but it's\n", "# putting it under another name -- 'Htt_pt_s' and I don't know why it's doing that\n", "# Smearing histograms and saving them \n", "def smear(trees, canvas, mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s, mH_pt_s, mHbb_pt_s, mHtt_pt_s, mS_s, mSbb_s, mStt_s, hs1, hs2, FN, new_hist_file, legend):\n", " all_entries = trees.GetEntries()\n", " mH_s.Sumw2()\n", " mX_s.Sumw2()\n", " mHbb_s.Sumw2()\n", " mHtt_s.Sumw2()\n", " mbb_s.Sumw2()\n", " mtt_s.Sumw2()\n", " mH_pt_s.Sumw2()\n", " mHbb_pt_s.Sumw2()\n", " mHtt_pt_s.Sumw2()\n", " mS_s.Sumw2()\n", " mSbb_s.Sumw2()\n", " mStt_s.Sumw2()\n", " for i in range(0, all_entries):\n", " trees.GetEntry(i)\n", " # Higgs information\n", " H_pt = getattr(trees, \"H_pt\") # transverse momentum\n", " #H_pt0_s = H_pt[0]\n", " H_eta = getattr(trees, \"H_eta\") # angle\n", " H_phi = getattr(trees, \"H_phi\") # another angle\n", " H_E = getattr(trees, \"H_e\") # energy\n", " # X particle\n", " X_pt = getattr(trees, 'X_pt')\n", " X_eta = getattr(trees, 'X_eta')\n", " X_phi = getattr(trees, 'X_phi')\n", " X_E = getattr(trees, 'X_e')\n", " # Higgs bb information\n", " Hbb_pt = getattr(trees, \"Hbb_pt\") # transverse momentum\n", " Hbb_eta = getattr(trees, \"Hbb_eta\") # angle\n", " Hbb_phi = getattr(trees, \"Hbb_phi\") # another angle\n", " Hbb_E = getattr(trees, \"Hbb_e\") # energy\n", " # Higgs tt information\n", " Htt_pt = getattr(trees, \"Htautau_pt\") # transverse momentum\n", " Htt_eta = getattr(trees, \"Htautau_eta\") # angle\n", " Htt_phi = getattr(trees, \"Htautau_phi\") # another angle\n", " Htt_E = getattr(trees, \"Htautau_e\") # energy\n", " # b information \n", " b_pt = getattr(trees, 'b_pt')\n", " b_eta = getattr(trees, 'b_eta')\n", " b_phi = getattr(trees, 'b_phi')\n", " b_E = getattr(trees, 'b_e')\n", " # t information\n", " t_pt = getattr(trees, 'tau_pt')\n", " t_eta = getattr(trees, 'tau_eta')\n", " t_phi = getattr(trees, 'tau_phi')\n", " t_E = getattr(trees, 'tau_e')\n", " # S information\n", " S_pt = getattr(trees, 'S_pt')\n", " S_eta = getattr(trees, 'S_eta')\n", " S_phi = getattr(trees, 'S_phi')\n", " S_E = getattr(trees, 'S_e')\n", " # S bb information\n", " Sbb_pt = getattr(trees, 'Sbb_pt')\n", " Sbb_eta = getattr(trees, 'Sbb_eta')\n", " Sbb_phi = getattr(trees, 'Sbb_phi')\n", " Sbb_E = getattr(trees, 'Sbb_e')\n", " # S tautau information\n", " Stt_pt = getattr(trees, 'Stautau_pt')\n", " Stt_eta = getattr(trees, 'Stautau_eta')\n", " Stt_phi = getattr(trees, 'Stautau_phi')\n", " Stt_E = getattr(trees, 'Stautau_e')\n", " # Establishing sigma -- the way to smear the plots to make them more realistic\n", " sigma = random.gauss(1, 0.12)\n", " sigma_no_center = random.gauss(1.5, 0.12) # for non-centering superimposed graphing\n", " sigma_no2 = random.gauss(2, 0.12) # just for funsies\n", " # Converting all this to a 4-vector for simplicity\n", " H = ROOT.TLorentzVector()\n", " H.SetPtEtaPhiE(H_pt[0]*sigma, H_eta[0], H_phi[0], H_E[0])\n", " #H_1 = ROOT.TLorentzVector()\n", " #H_1.SetPtEtaPhiE(H_pt[1]*sigma, H_eta[1], H_phi[1], H_E[1])\n", " # For X:\n", " X = ROOT.TLorentzVector()\n", " X.SetPtEtaPhiE(X_pt[0]*sigma, X_eta[0], X_phi[0], X_E[0])\n", " # Hbb and Htt are floats, so just call them as is\n", " Hbb = ROOT.TLorentzVector()\n", " Hbb.SetPtEtaPhiE(Hbb_pt*sigma, Hbb_eta, Hbb_phi, Hbb_E) \n", " Htt = ROOT.TLorentzVector()\n", " Htt.SetPtEtaPhiE(Htt_pt*sigma, Htt_eta, Htt_phi, Htt_E)\n", " # 4-vectors for b and t\n", " b_0 = ROOT.TLorentzVector()\n", " t_0 = ROOT.TLorentzVector()\n", " b_0.SetPtEtaPhiE(b_pt[0]*sigma, b_eta[0], b_phi[0], b_E[0])\n", " t_0.SetPtEtaPhiE(t_pt[0]*sigma, t_eta[0], t_phi[0], t_E[0])\n", " b_1 = ROOT.TLorentzVector()\n", " t_1 = ROOT.TLorentzVector()\n", " b_1.SetPtEtaPhiE(b_pt[1]*sigma, b_eta[1], b_phi[1], b_E[1])\n", " t_1.SetPtEtaPhiE(t_pt[1]*sigma, t_eta[1], t_phi[1], t_E[1])\n", " # 4-vectors for S, Sbb and Stt\n", " S = ROOT.TLorentzVector()\n", " S.SetPtEtaPhiE(S_pt[0]*sigma, S_eta[0], S_phi[0], S_E[0])\n", " Sbb = ROOT.TLorentzVector()\n", " Sbb.SetPtEtaPhiE(Sbb_pt*sigma, Sbb_eta, Sbb_phi, Sbb_E)\n", " Stt = ROOT.TLorentzVector()\n", " Stt.SetPtEtaPhiE(Stt_pt*sigma, Stt_eta, Stt_phi, Stt_E)\n", " # Getting masses:\n", " #X = H_0 + H_1 # X is just the total mass of 2 Higgs' added together\n", " X_mass = X.M()\n", " H_mass = H.M()\n", " Hbb_mass = Hbb.M()\n", " Htt_mass = Htt.M()\n", " bb = b_0 + b_1\n", " tt = t_0 + t_1\n", " bb_mass = bb.M()\n", " tt_mass = tt.M()\n", " S_mass = S.M()\n", " Sbb_mass = Sbb.M()\n", " Stt_mass = Stt.M()\n", " # Fills the histogram\n", " mX_s.Fill(X_mass)\n", " mH_s.Fill(H_mass)\n", " mHbb_s.Fill(Hbb_mass)\n", " mHtt_s.Fill(Htt_mass)\n", " mbb_s.Fill(bb_mass)\n", " mtt_s.Fill(tt_mass)\n", " mS_s.Fill(S_mass)\n", " mSbb_s.Fill(Sbb_mass)\n", " mStt_s.Fill(Stt_mass)\n", " mH_pt_s.Fill(H_pt[0]*sigma)\n", " mHbb_pt_s.Fill(Hbb_pt*sigma)\n", " mHtt_pt_s.Fill(Htt_pt*sigma)\n", " # Sets the histogram to continue to exist after closing the file\n", " mX_s.SetDirectory(0)\n", " mH_s.SetDirectory(0)\n", " mHbb_s.SetDirectory(0)\n", " mHtt_s.SetDirectory(0)\n", " mbb_s.SetDirectory(0)\n", " mtt_s.SetDirectory(0)\n", " mS_s.SetDirectory(0)\n", " mSbb_s.SetDirectory(0)\n", " mStt_s.SetDirectory(0)\n", " mH_pt_s.SetDirectory(0)\n", " mHbb_pt_s.SetDirectory(0)\n", " mHtt_pt_s.SetDirectory(0)\n", "# Draws stacked histograms\n", " hs1 = ROOT.THStack('h', 'Higgs vs. S with smearing')\n", " hs1.Add(mH_s)\n", " hs1.Add(mS_s)\n", " #hs1.Add(mHtt_pt_s)\n", " hs1.Draw('nostack,e1p')\n", " # Adding a legend since the pt plot has more than one graph on it\n", " legend = ROOT.TLegend(0.7,0.6,0.85,0.75) # puts legend at right corner \n", " legend.AddEntry(mH_s,\"H smeared\") \n", " legend.AddEntry(mS_s,\"S smeared\") \n", " #legend.AddEntry(mHtt_pt_s, \"Htt_pt smeared\")\n", " legend.SetLineWidth(0) \n", " legend.Draw()\n", " canvas.Print('hist_S%s.pdf' % FN)\n", " # Superimposing masses to start comparing\n", " hs2 = ROOT.THStack('h', 'Higgs vs. Higgs bb vs. Higgs tt with Smearing')\n", " hs2.Add(mH_s)\n", " hs2.Add(mHbb_s)\n", " hs2.Add(mHtt_s)\n", " hs2.Draw('nostack,e1p')\n", " legend = ROOT.TLegend(0.7,0.6,0.85,0.75) # puts legend at right corner \n", " legend.AddEntry(mH_s,\"H smeared\") \n", " legend.AddEntry(mHbb_s,\"Hbb smeared\") \n", " legend.AddEntry(mHtt_s, \"Htt smeared\")\n", " legend.SetLineWidth(0) \n", " legend.Draw()\n", " canvas.Print('hist_S%s.pdf' % FN)\n", "# Writes the histograms to a pdf file opened outside this function \n", " mH_s.Draw('h')\n", " canvas.Print(\"hist_S%s.pdf\" % FN)\n", " mX_s.Draw('h')\n", " canvas.Print('hist_S%s.pdf' % FN)\n", " mHbb_s.Draw('h')\n", " canvas.Print(\"hist_S%s.pdf\" % FN)\n", " mHtt_s.Draw('h')\n", " canvas.Print(\"hist_S%s.pdf\" % FN)\n", " mbb_s.Draw('h')\n", " canvas.Print(\"hist_S%s.pdf\" % FN)\n", " mtt_s.Draw('h')\n", " canvas.Print(\"hist_S%s.pdf\" % FN)\n", " mS_s.Draw('h')\n", " canvas.Print(\"hist_S%s.pdf\" % FN)\n", " mSbb_s.Draw('h')\n", " canvas.Print(\"hist_S%s.pdf\" % FN)\n", " mStt_s.Draw('h')\n", " canvas.Print(\"hist_S%s.pdf\" % FN)\n", " new_hist_file.cd()\n", " mH_s.Write()\n", " mX_s.Write()\n", " mHbb_s.Write()\n", " mHtt_s.Write()\n", " mbb_s.Write()\n", " mtt_s.Write()\n", " mS_s.Write()\n", " mSbb_s.Write()\n", " mStt_s.Write()\n", " mH_pt_s.Write()\n", " mHbb_pt_s.Write()\n", " mHtt_pt_s.Write()\n", " print('All smeared histograms grabbed')\n", " return mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s, mH_pt_s, mHbb_pt_s, mHtt_pt_s, mS_s, mSbb_s, mStt_s\n", "for i in range(1,20):\n", " filename = 'Sfile%s.root' % i\n", " X_SH = ROOT.TFile.Open(filename, 'READ')\n", " print('File' ,i, 'is grabbed and read')\n", " trees = X_SH.Get('analysis')\n", " print('Tree', i, 'is grabbed')\n", " # Saves files as pdfs \n", " hs1 = ROOT.THStack('h', 'p_{T} Comparison with Smearing')\n", " hs2 = ROOT.THStack('h', 'Higgs vs. Higgs bb vs. Higgs tt with Smearing')\n", " legend = ROOT.TLegend(0.7,0.6,0.85,0.75) # puts legend at right corner\n", " canvas = ROOT.TCanvas(\"canvas\")\n", " canvas.cd() # Open the canvas for continuous printing\n", " # Establishing the histogram\n", " # Bins are chosen based off the values obtained for each particle\n", " mH_s = ROOT.TH1D(\"Higgs smeared\", \"m_{H}, Higgs\", 100, 100e3, 200e3)\n", " mH_s.GetYaxis().SetTitle(\"Number of events\")\n", " mH_s.GetXaxis().SetTitle(\"m_{H_s} [MeV]\")\n", " mH_s.SetLineColor(ROOT.kRed-3)\n", " mH_s.SetTitle('Higgs mass plot with smearing')\n", " mX_s = ROOT.TH1D('X particle at m = 300 GeV smeared', 'X particle smeared', 100, 250e3, 400e3)\n", " mX_s.GetYaxis().SetTitle(\"Number of events\")\n", " mX_s.GetXaxis().SetTitle('m_{X_s} [MeV]')\n", " mX_s.SetLineColor(ROOT.kBlue-7)\n", " mX_s.SetTitle('X mass (at 300GeV) plot with smearing')\n", " mH_pt_s = ROOT.TH1D('pt comparison H smeared', 'H_pt', 100, 50e3, 200e3)\n", " mH_pt_s.SetLineColor(ROOT.kSpring-7)\n", " mHbb_pt_s = ROOT.TH1D('pt comparison bb smeared', 'Hbb_pt', 100, 50e3, 200e3)\n", " mHbb_pt_s.SetLineColor(ROOT.kRed+4)\n", " mHtt_pt_s = ROOT.TH1D('pt comparison tt smeared', 'Htt_pt_s', 100, 50e3, 200e3)\n", " mHtt_pt_s.SetLineColor(ROOT.kAzure)\n", " mHbb_s = ROOT.TH1D(\"Higgs bb smeared\", \"m_{Hbb}, Higgs bb\", 100, 50e3, 200e3)\n", " mHbb_s.GetYaxis().SetTitle(\"Number of events\")\n", " mHbb_s.GetXaxis().SetTitle(\"m_{Hbb_s} [MeV]\")\n", " mHbb_s.SetLineColor(ROOT.kOrange+4)\n", " mHbb_s.SetTitle('Higgs bb mass plot with smearing')\n", " mHtt_s = ROOT.TH1D(\"Higgs tau tau smeared\", \"m_{Htt}, Higgs tt\", 100, 50e3, 200e3)\n", " mHtt_s.GetYaxis().SetTitle(\"Number of events\")\n", " mHtt_s.GetXaxis().SetTitle(\"m_{Htt_s} [MeV]\")\n", " mHtt_s.SetLineColor(ROOT.kViolet)\n", " mHtt_s.SetTitle('Higgs tau tau mass plot with smearing')\n", " mbb_s = ROOT.TH1D('bb plot smeared', 'm_{bb}, bb', 100, 60e3, 150e3)\n", " mbb_s.GetYaxis().SetTitle(\"Number of events\")\n", " mbb_s.GetXaxis().SetTitle(\"m_{bb_s} [MeV]\")\n", " mbb_s.SetLineColor(ROOT.kViolet-3)\n", " mbb_s.SetTitle('bb mass plot with smearing')\n", " mtt_s = ROOT.TH1D('tautau plot smeared', 'm_{tt}, tt', 100, 60e3, 150e3)\n", " mtt_s.GetYaxis().SetTitle(\"Number of events\")\n", " mtt_s.GetXaxis().SetTitle(\"m_{tt_s} [MeV]\")\n", " mtt_s.SetLineColor(ROOT.kBlue+2)\n", " mtt_s.SetTitle('tau tau mass plot with smearing')\n", " mS_s = ROOT.TH1D('S mass smeared', 'm_{S}, S particle smeared', 100, 60e3, 170e3)\n", " mS_s.GetYaxis().SetTitle(\"Number of events\")\n", " mS_s.GetXaxis().SetTitle(\"m_{S} [MeV]\")\n", " mS_s.SetLineColor(ROOT.kAzure+3)\n", " mS_s.SetTitle('S mass plot with smearing')\n", " mSbb_s = ROOT.TH1D('S bb mass smeared', 'm_{Sbb}, S particle smeared', 100, 60e3, 170e3)\n", " mSbb_s.GetYaxis().SetTitle(\"Number of events\")\n", " mSbb_s.GetXaxis().SetTitle(\"m_{Sbb} [MeV]\")\n", " mSbb_s.SetLineColor(ROOT.kBlue+2)\n", " mSbb_s.SetTitle('S bb mass plot with smearing')\n", " mStt_s = ROOT.TH1D('S tt mass smeared', 'm_{Stt}, S particle smeared', 100, 60e3, 170e3)\n", " mStt_s.GetYaxis().SetTitle(\"Number of events\")\n", " mStt_s.GetXaxis().SetTitle(\"m_{Stt} [MeV]\")\n", " mStt_s.SetLineColor(ROOT.kAzure-6)\n", " mStt_s.SetTitle('S tautau mass plot with smearing')\n", " # Saves hist.root files with all the plots\n", " hist = 'hist_S%s.root' % i\n", " new_hist_file = ROOT.TFile.Open(hist, 'RECREATE')\n", " canvas.Print('hist_S%s.pdf'% i +'[')\n", " FN = i\n", " # Call the functions so the plots are grabbed in the loop and saved in their respective files\n", " smear(trees, canvas, mH_s, mX_s, mHbb_s, mHtt_s, mbb_s, mtt_s, mH_pt_s, mHbb_pt_s, mS_s, mSbb_s, mStt_s, mHtt_pt_s, hs1, hs2, FN, new_hist_file, legend)\n", " canvas.Print('hist_S%s.pdf'% i +']')\n", " # Building the histogram to save onto the histogram file that's being created\n", " print('Running over data')\n", " # Closing the data file\n", " X_SH.Close() \n", " print('Done running data from file')\n", " print('Writing histograms to output file...')\n", " print('Histograms collected for file', i,)\n", " # Creates and then closes the hist file\n", " new_hist_file.cd()\n", " new_hist_file.Close()" ] }, { "cell_type": "markdown", "id": "12b0f4a1", "metadata": {}, "source": [ "# Transverse Momentum, Pseudorapidity, Azimuthal Angle and Energy histogram plots\n", "This just creates separate histograms for $p_{T}$, $\\eta$, $\\phi$, and E for the Higgs boson and the S particle for comparison purposes. " ] }, { "cell_type": "code", "execution_count": null, "id": "7d706373", "metadata": {}, "outputs": [], "source": [ "import ROOT\n", "import random\n", "\n", "# Plots pt, eta, phi and energy histograms for the particles specified\n", "def hist(hist_H_pt, hist_H_eta, hist_H_phi, hist_H_E, trees, canvas, hist_S_pt, hist_S_eta, hist_S_phi, hist_S_E, FN):\n", " all_entries = trees.GetEntries()\n", " hist_H_pt.Sumw2()\n", " hist_H_eta.Sumw2()\n", " hist_H_phi.Sumw2()\n", " hist_H_E.Sumw2()\n", " hist_S_pt.Sumw2()\n", " hist_S_eta.Sumw2()\n", " hist_S_phi.Sumw2()\n", " hist_S_E.Sumw2()\n", " for i in range(0, all_entries):\n", " trees.GetEntry(i)\n", " # Only plotting one entry instead of 10000 \n", " # Plots all for some files but not all\n", " # Higgs\n", " H_pt = getattr(trees, \"H_pt\") # transverse momentum\n", " H_eta = getattr(trees, \"H_eta\") # angle\n", " H_phi = getattr(trees, \"H_phi\") # another angle\n", " H_E = getattr(trees, \"H_e\") # energy\n", " # S information\n", " S_pt = getattr(trees, 'S_pt')\n", " S_eta = getattr(trees, 'S_eta')\n", " S_phi = getattr(trees, 'S_phi')\n", " S_E = getattr(trees, 'S_e')\n", " # Smearing\n", " sigma = random.gauss(1, 0.12)\n", " # Fill histograms\n", " hist_H_pt.Fill(H_pt[0]*sigma)\n", " hist_H_eta.Fill(H_eta[0]*sigma)\n", " hist_H_phi.Fill(H_phi[0]*sigma)\n", " hist_H_E.Fill(H_E[0]*sigma)\n", " hist_S_pt.Fill(S_pt[0]*sigma)\n", " hist_S_eta.Fill(S_eta[0]*sigma)\n", " hist_S_phi.Fill(S_phi[0]*sigma)\n", " hist_S_E.Fill(S_E[0]*sigma)\n", " # Sets directory\n", " hist_H_pt.SetDirectory(0)\n", " hist_H_eta.SetDirectory(0)\n", " hist_H_phi.SetDirectory(0)\n", " hist_H_E.SetDirectory(0)\n", " hist_S_pt.SetDirectory(0)\n", " hist_S_eta.SetDirectory(0)\n", " hist_S_phi.SetDirectory(0)\n", " hist_S_E.SetDirectory(0)\n", " \n", " # Draws histograms\n", " hist_H_pt.Draw('h')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % FN)\n", " hist_H_eta.Draw('h')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % FN)\n", " hist_H_phi.Draw('h')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % FN)\n", " hist_H_E.Draw('h')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % FN)\n", " hist_S_pt.Draw('h')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % FN)\n", " hist_S_eta.Draw('h')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % FN)\n", " hist_S_phi.Draw('h')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % FN)\n", " hist_S_E.Draw('h')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % FN)\n", " print('All histograms grabbed')\n", " return hist_H_pt, hist_H_eta, hist_H_phi, hist_H_E, hist_S_pt, hist_S_eta, hist_S_phi, hist_S_E\n", "\n", "for i in range(1,20):\n", " filename = 'Sfile%s.root' % i\n", " X_SH = ROOT.TFile.Open(filename, 'READ')\n", " print('File', i, 'is grabbed and read')\n", " trees = X_SH.Get('analysis')\n", " print('Tree', i, 'is grabbed')\n", " canvas = ROOT.TCanvas('canvas')\n", " canvas.cd()\n", " # Higgs plots\n", " hist_H_pt = ROOT.TH1D('Higgs pt', 'H_pt', 100, 50e3, 200e3)\n", " hist_H_pt.GetYaxis().SetTitle('Number of Events')\n", " hist_H_pt.GetXaxis().SetTitle('H_{pt} [MeV]')\n", " hist_H_pt.SetLineColor(ROOT.kRed)\n", " hist_H_pt.SetTitle('Higgs pT plot')\n", " hist_H_eta = ROOT.TH1D('Higgs eta', 'H_eta', 100, -1, 3)\n", " hist_H_eta.GetYaxis().SetTitle('Number of Events')\n", " hist_H_eta.GetXaxis().SetTitle('H_{eta} [MeV]')\n", " hist_H_eta.SetLineColor(ROOT.kRed+4)\n", " hist_H_eta.SetTitle('Higgs eta plot')\n", " hist_H_phi = ROOT.TH1D('Higgs phi', 'H_phi', 100, -3, 3)\n", " hist_H_phi.GetYaxis().SetTitle('Number of Events')\n", " hist_H_phi.GetXaxis().SetTitle('H_{phi} [MeV]')\n", " hist_H_phi.SetLineColor(ROOT.kOrange+9)\n", " hist_H_phi.SetTitle('Higgs phi plot')\n", " hist_H_E = ROOT.TH1D('Higgs Energy', 'H_E', 100, 50e3, 200e3)\n", " hist_H_E.GetYaxis().SetTitle('Sumber of Events')\n", " hist_H_E.GetXaxis().SetTitle('H_{E} [MeV]')\n", " hist_H_E.SetLineColor(ROOT.kOrange+8)\n", " hist_H_E.SetTitle('Higgs Energy plot')\n", " # S plots\n", " hist_S_pt = ROOT.TH1D('S pt', 'S_pt', 100, 50e3, 200e3)\n", " hist_S_pt.GetYaxis().SetTitle('Number of Events')\n", " hist_S_pt.GetXaxis().SetTitle('S_{pt} [MeV]')\n", " hist_S_pt.SetLineColor(ROOT.kAzure+3)\n", " hist_S_pt.SetTitle('S pT plot')\n", " hist_S_eta = ROOT.TH1D('S eta', 'S_eta', 100, -1, 3)\n", " hist_S_eta.GetYaxis().SetTitle('Number of Events')\n", " hist_S_eta.GetXaxis().SetTitle('S_{eta} [MeV]')\n", " hist_S_eta.SetLineColor(ROOT.kBlue+2)\n", " hist_S_eta.SetTitle('S eta plot')\n", " hist_S_phi = ROOT.TH1D('S phi', 'S_phi', 100, -1, 3)\n", " hist_S_phi.GetYaxis().SetTitle('Number of Events')\n", " hist_S_phi.GetXaxis().SetTitle('S_{phi} [MeV]')\n", " hist_S_phi.SetLineColor(ROOT.kAzure-6)\n", " hist_S_phi.SetTitle('S phi plot')\n", " hist_S_E = ROOT.TH1D('S Energy', 'S_E', 100, 50e3, 200e3)\n", " hist_S_E.GetYaxis().SetTitle('Number of Events')\n", " hist_S_E.GetXaxis().SetTitle('S_{E} [MeV]')\n", " hist_S_E.SetLineColor(ROOT.kBlue-7)\n", " hist_S_E.SetTitle('S Energy plot')\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % i + '[')\n", " FN = i\n", " hist(hist_H_pt, hist_H_eta, hist_H_phi, hist_H_E, trees, canvas, hist_S_pt, hist_S_eta, hist_S_phi, hist_S_E, FN)\n", " canvas.Print('hist_PtEtaPhiE%s.pdf' % i + ']')\n", " print('Running over data')\n", " print('Done running data from file')\n", " print('Histograms collected and saved for file', i,)" ] }, { "cell_type": "markdown", "id": "a8b2c428", "metadata": {}, "source": [ "# Adding Ratios\n", "This was done within the last couple days of my internship so this isn't made with mass production in mind. Using a skeleton code my mentor made, I added onto it in order to show not only the ratio of the Higgs masses for whichever files we were looking at, but the superimposed Higgs plots on top for better analysis." ] }, { "cell_type": "code", "execution_count": null, "id": "89ca73f8", "metadata": {}, "outputs": [], "source": [ "import ROOT\n", "\n", "# Fix of original ratio script -- used exercise 2 of PyROOT tutorial as a basis\n", "# Read in the files, set the histograms to be viewed and then close the files\n", "File1 = ROOT.TFile(\"hist_S2.root\",\"READ\")\n", "HHisto_1 = File1.Get('Higgs_smeared')\n", "File2 = ROOT.TFile(\"hist_S19.root\", \"READ\")\n", "HHisto_2 = File2.Get('Higgs_smeared')\n", "HHisto_1.SetDirectory(0)\n", "HHisto_2.SetDirectory(0)\n", "File1.Close()\n", "File2.Close()\n", "# Turn off the statistics box\n", "HHisto_1.SetStats(0)\n", "HHisto_2.SetStats(0)\n", "# Setting up line color differentiation\n", "HHisto_1.SetLineColor(ROOT.kRed)\n", "HHisto_2.SetLineColor(ROOT.kAzure)\n", "HHisto_1.SetLineWidth(1)\n", "HHisto_2.SetLineWidth(1)\n", "# Set axis labels\n", "HHisto_1.GetYaxis().SetTitle(\"Number of events\")\n", "HHisto_2.GetYaxis().SetTitle(\"Number of events\")\n", "HHisto_1.GetXaxis().SetTitle(\"m_{H} [MeV]\")\n", "HHisto_2.GetXaxis().SetTitle(\"m_{H} [MeV]\")\n", "# Making a canvas\n", "canvas = ROOT.TCanvas(\"canvas\")\n", "canvas.cd()\n", "canvas.Print(\"hist_Ratio_S2_S19.pdf\" + \"[\")\n", "# Calculating the ratio\n", "ratio = HHisto_1.Clone()\n", "ratio.Divide(HHisto_2)\n", "ratio.SetLineColor(ROOT.kRed)\n", "# Draw the normal plots (not the ratio)\n", "pad1 = ROOT.TPad(\"pad1\",\"pad1\",0,0.3,1,1)\n", "pad1.SetBottomMargin(0) # Upper and lower pads are joined\n", "pad1.Draw() # Draw the upper pad in the canvas\n", "pad1.cd() # pad1 becomes the current pad\n", "HHisto_1.SetTitle(\"\") # Remove the plot title\n", "HHisto_1.GetXaxis().SetLabelSize(0) # Remove x-axis labels for the top pad\n", "HHisto_1.GetXaxis().SetTitleSize(0) # Remove x-axis title for the top pad\n", "HHisto_1.GetYaxis().SetTitleSize(0.05) # Increase y-axis title size (pad is not full page)\n", "HHisto_1.Draw(\"h\") \n", "HHisto_2.Draw(\"h,same\") \n", "# Add a legend to the top pad\n", "legend = ROOT.TLegend(0.7,0.6,0.85,0.75) # Add a legend near the top right corner\n", "legend.AddEntry(HHisto_1,\"Higgs_2\") \n", "legend.AddEntry(HHisto_2,\"Higgs_19\") \n", "legend.SetLineWidth(0) # Remove the boundary on the legend\n", "legend.Draw(\"same\") # Draw the legend on the plot\n", "\n", "# Add other labels with TLatex -- optional\n", "latex = ROOT.TLatex() # Create the TLatex object\n", "latex.SetNDC() # Set the coordinates to be percent-based\n", "latex.SetTextSize(0.06) # Increase the text size\n", "latex.DrawText(0.7,0.83,\"Higgs Comparison\") \n", "latex.SetTextSize(0.04) # Reduce the text size\n", "latex.DrawText(0.7,0.77,\"S2 vs. S19\") # Add a label for the selection\n", "\n", "# Now draw the ratio\n", "canvas.cd() # Go back to the main canvas before defining pad2\n", "pad2 = ROOT.TPad(\"pad2\",\"pad2\",0,0.05,1,0.3)\n", "pad2.SetTopMargin(0) # Upper and lower pads are joined\n", "pad2.SetBottomMargin(0.25) # Expand the bottom margin for extra label space\n", "pad2.Draw() # Draw the lower pad in the canvas\n", "pad2.cd() # pad2 becomes the current pad\n", "ratio.SetTitle(\"\") # Turn off the title to avoid overlap\n", "ratio.GetXaxis().SetLabelSize(0.12) # Larger x-axis labels (pad is not full page)\n", "ratio.GetXaxis().SetTitleSize(0.12) # Larger x-axis title (pad is not full page)\n", "ratio.GetYaxis().SetLabelSize(0.1) # Larger y-axis labels (pad is not full page)\n", "ratio.GetYaxis().SetTitleSize(0.15) # Larger y-axis title (pad is not full page)\n", "ratio.GetYaxis().SetTitle(\"H_2/H_19\") # Change the y-axis title (this is the ratio)\n", "ratio.GetYaxis().SetTitleOffset(0.3) # Reduce the y-axis title spacing\n", "ratio.GetYaxis().SetRangeUser(0.5,1.5) # Set the y-axis ratio range from 0.5 to 1.5\n", "ratio.GetYaxis().SetNdivisions(207) # Change the y-axis tick-marks to work better\n", "ratio.Draw(\"pe\") # Draw the ratio in current pad with error markers\n", "\n", "# Add a line at 1 to the ratio plot\n", "line = ROOT.TLine(100e3,1,200e3,1) # Draw a line at 1 from 100 GeV to 200 GeV \n", "line.SetLineColor(ROOT.kBlack) \n", "line.SetLineWidth(1) \n", "line.Draw(\"same\") # Draw the line on the same plot as the ratio\n", "\n", "# Write the ratio plot to the output plot file\n", "canvas.Print(\"hist_Ratio_S2_S19.pdf\")\n", "# The key is the closing square-bracket after the desired file name\n", "canvas.Print(\"hist_Ratio_S2_S19.pdf\" + \"]\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" } }, "nbformat": 4, "nbformat_minor": 5 }