# Example code for mass v significance using root files

**URL:** <https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947>\
**Category:** TMVA\
**Created:** [January 24, 2024, 10:57am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947 "2024-01-24T10:57:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sidra\_swalheen](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sidra_swalheen/32/35516_2.png) [@sidra\_swalheen](https://root-forum.cern.ch/u/sidra_swalheen)\
**Post date:** [January 24, 2024, 10:57am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947/1 "2024-01-24T10:57:47Z")

</div>

Hi,  
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

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 24, 2024, 11:29am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947/2 "2024-01-24T11:29:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sidra\_swalheen](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sidra_swalheen/32/35516_2.png) [@sidra\_swalheen](https://root-forum.cern.ch/u/sidra_swalheen)\
**Post date:** [January 25, 2024, 3:32am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947/3 "2024-01-25T03:32:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 25, 2024, 7:14am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947/4 "2024-01-25T07:14:32Z")

</div>

Then maybe @moneta can help you

---

<div class="post-metadata">

**Author:** ![sidra\_swalheen](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sidra_swalheen/32/35516_2.png) [@sidra\_swalheen](https://root-forum.cern.ch/u/sidra_swalheen)\
**Post date:** [January 25, 2024, 8:04am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947/5 "2024-01-25T08:04:04Z")

</div>

```auto
#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->Add(treeBackground1);
    treeBackground2->Project("histBackground", "massVariable", "backgroundSelection");
    treeBackground3->Add(treeBackground2);
    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
    histSignal->Scale(luminosity1);
    histBackground->Scale(luminosity2);

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

    // Draw histograms
    histSignificance->Draw("hist");
    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");
    legend->Draw();

    // Set axis labels
    histSignificance->GetXaxis()->SetTitle("Mass");
    histSignificance->GetYaxis()->SetTitle("Significance");

    // Save plot as an image
    canvas->SaveAs("MassVsSignificance.png");

    // Close files
    fileSignal->Close();
    fileBackground1->Close();
    fileBackground2->Close();
    fileBackground3->Close();
}

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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 25, 2024, 9:10am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947/6 "2024-01-25T09:10:24Z")

</div>

> [@sidra\_swalheen](#):
>
> I wrote this but it is not working properly

What does not work properly?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 25, 2024, 9:14am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947/7 "2024-01-25T09:14:56Z")

</div>

> [@sidra\_swalheen](#):
>
> ```auto
> treeBackground1->Project("histBackground", "massVariable", "backgroundSelection");
> treeBackground2->Add(treeBackground1);
> treeBackground2->Project("histBackground", "massVariable", "backgroundSelection");
> treeBackground3->Add(treeBackground2);
> treeBackground3->Project("histBackground", "massVariable", "backgroundSelection");
> 
> ```

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](https://root.cern/doc/master/classTTree.html#a73450649dc6e54b5b94516c468523e45) instead of Project, using “\>\>” before the histogram name and “+” between this and the histogram name for the 2nd and 3rd times; e.g.

```auto
    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.)

---

<div class="post-metadata">

**Author:** ![sidra\_swalheen](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sidra_swalheen/32/35516_2.png) [@sidra\_swalheen](https://root-forum.cern.ch/u/sidra_swalheen)\
**Post date:** [January 25, 2024, 9:32am UTC](https://root-forum.cern.ch/t/example-code-for-mass-v-significance-using-root-files/57947/8 "2024-01-25T09:32:08Z")

</div>

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