Example code for mass v significance using root files

Is there any example file to find mass v significance using root files?
I want graph for different masses at different energies but unable to do that. I am using ROOT version: 6.26 on Ubuntu 20

I think this is again a too generic question, but maybe @moneta can correct me if I’m wrong

It is definitely a generic question, but i need code for mass v significance and unable to write and find any where could you please help me with that?
I will be grateful.

Then maybe @moneta can help you

#include <iostream>
#include <TFile.h>
#include <TTree.h>
#include <TH1F.h>
#include <TCanvas.h>
#include <TLegend.h>
#include <TGraph.h>
#include <TAxis.h>
#include <TMath.h>

void massVsSignificancePlot(const char* signalFile, const char* backgroundFile1, const char* backgroundFile2, const char* backgroundFile3, double luminosity1, double luminosity2) {
    // Open files
    TFile *fileSignal = TFile::Open(signalFile);
    TFile *fileBackground1 = TFile::Open(back1);
    TFile *fileBackground2 = TFile::Open(back2);
    TFile *fileBackground3 = TFile::Open(backw3);

    // Get trees
    TTree *treeSignal = (TTree*)fileSignal->Get("DelphesS");  // Replace "treeName" with the actual name of your signal tree
    TTree *treeBackground1 = (TTree*)fileBackground1->Get("B1");
    TTree *treeBackground2 = (TTree*)fileBackground2->Get("B2");
    TTree *treeBackground3 = (TTree*)fileBackground3->Get("B3");

    // Define histograms for signal and background
    TH1F *histSignal = new TH1F("histSignal", "Signal", 100, 0, 100);
    TH1F *histBackground = new TH1F("histBackground", "Background", 100, 0, 100);

    // Fill histograms with signal and background events
    treeSignal->Project("histSignal", "massVariable", "signalSelection");
    treeBackground1->Project("histBackground", "massVariable", "backgroundSelection");
    treeBackground2->Project("histBackground", "massVariable", "backgroundSelection");
    treeBackground3->Project("histBackground", "massVariable", "backgroundSelection");

    // Calculate signal significance
    TH1F *histSignificance = new TH1F("histSignificance", "Significance", 100, 0, 100);
    for (int bin = 1; bin <= histSignal->GetNbinsX(); ++bin) {
        double signalEvents = histSignal->Integral(1, bin);
        double backgroundEvents = histBackground->Integral(1, bin);
        double significance = signalEvents / TMath::Sqrt(signalEvents + backgroundEvents + 1);  // Poisson significance
        histSignificance->SetBinContent(bin, significance);

    // Scale histograms by luminosity

    // Create canvas
    TCanvas *canvas = new TCanvas("canvas", "Mass vs Significance", 800, 600);

    // Draw histograms
    histSignal->Draw("hist same");
    histBackground->Draw("hist same");

    // Add legend
    TLegend *legend = new TLegend(0.7, 0.7, 0.9, 0.9);
    legend->AddEntry(histSignal, "Signal", "l");
    legend->AddEntry(histBackground, "Background", "l");

    // Set axis labels

    // Save plot as an image

    // Close files

int main() {
    const char* signalFile = "signal.root";
    const char* backgroundFile1 = "back1.root";
    const char* backgroundFile2 = "back2.root";
    const char* backgroundFile3 = "back3.root";
    double luminositySignal = 1000.0;  // Replace with the actual luminosity for the signal sample
    double luminosityBackground = 1000.0;  // Replace with the actual luminosity for the background sample

    massVsSignificancePlot(signalFile, backgroundFile1, backgroundFile2, backgroundFile3, luminositySignal, luminosityBackground);

    return 0;

I wrote this but it is not working properly, assist me with this.

What does not work properly?

Not sure what you are trying to achieve there, but I don’t think there is an Add method for TTrees. Supposing you want to fill the histogram histBackground with the contents of massVariable from the 3 trees (accumulated/appended in the same histogram), you can use Draw instead of Project, using “>>” before the histogram name and “+” between this and the histogram name for the 2nd and 3rd times; e.g.

    treeBackground1->Draw("massVariable>>histBackground", "backgroundSelection");
    treeBackground2->Draw("massVariable>>+histBackground", "backgroundSelection");
    treeBackground3->Draw("massVariable>>+histBackground", "backgroundSelection");

you can also add the option “goff” so the histograms are not drawn ( treeBackground1->Draw("massVariable>>histBackground", "backgroundSelection", "goff"); etc.)

1 Like

I am looking for code to draw graphs for mass v significance using signal and background root files.