Plotting two variable Histograms

I am interested in plotting two variables defined in a prior analysis file I have named “muon_eta” and “MCAngleBW” against one another. I looked into doing this with ROOT and ended up with a general format:

// Define the variables to plot
TString xvar = “muon_eta”;
TString yvar = “MCAngleBW”;

// Create a canvas and a 2D histogram
TCanvas *c1 = new TCanvas(“c1”, “2D Histogram”, 800, 600);
TH2D *h2d = new TH2D(“h2d”, “”, 100, 0, 5, 100, 0, 5);

// Set the axis titles
h2d->GetXaxis()->SetTitle(“Eta”);
h2d->GetYaxis()->SetTitle(“Angle Between Muons”);

// Fill the histogram with data from a TTree
TFile *file = TFile::Open(“filename.root”);
TTree tree = (TTree) file->Get(“treename”);
tree->Draw(yvar+“:”+xvar+“>>h2d”);

// Draw the histogram and save to a file
h2d->Draw(“COLZ”);
c1->SaveAs(“output.png”);

But this throws a syntax error. My assumption was that I’d simply need to define the histogram in analysis_final.py, but it seems like I am misunderstanding where to make an addition such as this. Conversely, would there be a simpler way to create a two-variable histogram writing a line similar to examples in the github like:

“mz”:{“name”:“Zcand_m”,“title”:“m_{Z} [GeV]”,“bin”:125,“xmin”:0,“xmax”:250},

but by adding another comma with something like “xaxis”:“muon_eta” and “yaxis”:“MCdeltaR”?

Thanks,
Hayden Shaddix

Dear Hayden,

The way you draw a 2D histogram from a Tree looks fine to me, could you provide us with the syntax error? Ideally, you can also share the input file with us.

Currently, there is no way of directly creating a 2D/3D histogram in the analysis_final.py stage. It is rather easy to implement, I will make a PR soon and let you know.

Best,
Jan

Jan,

The syntax error is:
line 66
TString xvar = “muon_eta”;
^
SyntaxError: invalid syntax

Maybe I’m misinterpreting where specifically to implement the code itself. I assumed I could do so directly into analysis_final.py but I also could be misunderstanding. As is, I have it at the end of the analysis_final.py file after the histoList. Should I have this elsewhere in the file or in a separate file?

Thanks,
Hayden Shaddix

Hi Hayden,

Adding extra code to the analysis_final.py will not work. Moreover, the syntax error you get is because you are using C++ in a python script.

My suggestion is to write a separate python script that produces the 2D histogram. It can be something like this:

import ROOT
fIn = ROOT.TFile("filename.root")
tree = fIn.Get("treename")

c = ROOT.TCanvas("c1", "2D Histogram", 800, 600)
h2d = ROOT.TH2D("h2d", "", 100, 0, 5, 100, 0, 5)
tree.Draw("yvar:xvar>>h2d")

h2d.Draw("HIST COLZ")

Or, if you can wait a couple of days, I will implement the 2D histogramming option directly in the analysis_final.py.

Best,
Jan

Jan,

Ahhhh I see, my apologies. I appreciate the help!

Best,
Hayden

Hi Hayden,

We have support now for 2 or 3D histograms that can be created directly in the analysis_final.py stage. See an example here:

Best,
Jan

Jan,

That’s awesome, exactly what I needed thank you very much. I appreciate your patience.

Best,
Hayden

Jan,

Is there a specific setup.sh version I need to use that has the capabilities implemented as well or is one necessary? I defined a few functions as the example states:

“eta_MCdeltaR”:{“cols”:[“muon_eta”, “MCAngleBW”],“title”:“eta - MCdeltaR”, “bins”: [(0,5,100), (100,0,5)]}, # 2D histogram
“eta_RCdeltaR”:{“cols”:[“muon_eta”, “RCAngleBW”],“title”:“eta - RCdeltaR”, “bins”: [(0,5,100), (100,0,5)]}

But it throws errors with the use of “bins” and spits out the general model formatting of:

model = ROOT.RDF.TH1DModel(v, “;{};”.format(histoList[v][“title”]), histoList[v][“bin”], histoList[v][“xmin”], histoList[v][“xmax”])

As it was before. Am I still missing something?

Thanks,
Hayden

Hi Hayden,

Did you update your local code (git pull) with the latest changes?

Jan

Jan,

I have pulled from the latest changes and when I try to run the final stage of the analysis it now gives me an error that ‘Namespace’ object has no attribute ‘final’. This happens even when I use the repository’s version of analysis_final.py.

Best,
Hayden

Hi Hayden,

There must be something wrong when pulling the code from github then. Can you quickly try with a fresh workspace to run the example analysis, and let us know whether it runs?
Maybe git diff can also reveal some differences that can cause the problem.

Best,
Jan

Hey Jan,

I git clone-d the repository and ran a test run of the first stage of the analysis using analysis_stage1_batch.py and it gives the same error without any changes to the repository.

Cheers,
Hayden

Hello @hshaddix,

it looks like the problem is again in the routing of the FCCAnalyses sub-commands.
I will try to reproduce the error.

Best,
Juraj

Hello @hshaddix,

I went through the whole example analysis in

examples/FCCee/higgs/mH-recoil/mumu/

I was not able to reproduce the error.
Can you share a bit more details, how do you run the analysis?
Which machine, shell, OS, exact location of the script, …

Also, sometimes clean build can help. Now we have helper sub-command for it:

fccanalyses build --clean-build

Best,
Juraj

I managed to get most of the code to run properly after starting from scratch and pulling the main branch in a few places. I am trying to plot my histograms with:

“MCAngleBW”:{“name”:“MCdeltaR”,“title”:“Angle Between Muons”,“bin”:100,“xmin”:0,“xmax”:5},
“RCAngleBW”:{“name”:“RCdeltaR”,“title”:“Angle Between Muons”,“bin”:100,“xmin”:0,“xmax”:5},
“muon_eta”:{“name”:“muon_eta”,“title”:“Eta”,“bin”:100,“xmin”:0,“xmax”:5},
“eta_MCdeltaR”:{“cols”:[“muon_eta”, “MCdeltaR”],“title”:“eta - MCdeltaR”, “bins”: [(0,5,100), (100,0,5)]}, # 2D
histogram
“eta_RCdeltaR”:{“cols”:[“muon_eta”, “RCdeltaR”],“title”:“eta - RCdeltaR”, “bins”: [(0,5,100), (100,0,5)]}

but I am getting the error:

Error: During the execution of the plots file:
/afs/cern.ch/user/h/hshaddix/LocallyRun/FCCAnalyses/examples/FCCee/higgs/mH-recoil/mumu/plots.py
exception occurred:
none of the 3 overloaded methods succeeded. Full details:
double TH2::Integral(const char* option = “”) =>
TypeError: takes at most 1 arguments (2 given)
double TH2::Integral(int binx1, int binx2, int biny1, int biny2, const char* option = “”) =>
TypeError: takes at least 4 arguments (2 given)
double TH2::Integral(int, int, int, int, int, int, const char* = “”) =>
TypeError: takes at least 6 arguments (2 given)

Is there an error in the way I am trying to define variables or something? I assume there must be some kind of information necessary to the plots file to define the axes as their own variables or something further overlooked.

Thanks,
Hayden

Hi Hayden,

Currently, the plotting tool does not support plotting 2-dimensional histograms. We’ll implement it asap, but for now, you can easily make a simple python script to plot the 2D histogram.

Best,
Jan