Checking between two variables correlations with plots


I would like to see if there is a correlation between two variables (which are stored in a TTree tree).
The first thing to come to mind is to draw them both on a 2D scatter plot (tree->Draw("var1:var2")),
but it is not very intuitive to see any correlations that way.

Is there a more natural way to check between variables correlations?

The brute force way is to simply draw var1 with var2 cuts, for example

tree->Draw("var1" , "0.2 < var2 < 0.4")
tree->Draw("var1" , "0.4 < var2 < 0.6")
and so on

And if that really is the best way to check correlations, is there any built in function or a library to do so? or at least something that would make it easier?
the final goal is to plot all of the var1 plots (with different ranges for var2), on the same graph (normalized).

Thank you!

To achieve that you can produce all the histograms you need doing something like:

TH1D *h = new TH1D(.....);

tree->Draw("var1 >> h" , "0.2 < var2 < 0.4");


once you have all your histograms you can group them in a THStack or plot them on top of each other using this option SAME

Alternatively, check out a log-scale scatter plot, and profile histograms, which you can create by supplying the “prof” option to TTree::Draw (otherwise, it’s just like making a scatter plot). I find these sometimes help in quickly spotting correlations.



1 Like

It all worked out,
Thank you very much!

Running the function

void graph(TString var1 , TString var2 , float min , float max , int seg , TTree* tree)
	float* segment = new float[seg+1];
	THStack stack;
	TString	str = ">>hist(100)";
	TString cut1;
	TString cut2;

	for (int i = 0 ; i < seg+1 ; i++)
		segment[i] = min + i * (max - min) / seg;
	for (int i = 0 ; i < seg ; i++)
		cut1 = var2 + TString::Format(">%f && " , segment[i]);
		cut2 = var2 + TString::Format("<%f" , segment[i+1]);	
		tree->Draw(var1 + str, cut1 + cut2);

yields a crash… can anyone tell me why?

Which line did the stack trace point to? If none specific, rebuild ROOT and your code in debug mode.

As a side note, you can ‘speed up’ code a tad bit by using:

cut1.Form("%s>%f && " , var2.Data(), segment[i]);


I’m pretty new to root, and I’m not sure how to rebuild ROOT in debug mode.

In your ROOT build directory do:

cmake -DCMAKE_BUILD_TYPE=Debug .

and rebuild (see

Thank you pcanal, but I managed to solve the problem by avoiding histogram and changing the tree->Draw directly.
For anyone who could use it, here’s what I did

	for (int i = 1 ; i < seg ; i++)
		cut1 = var2 + TString::Format(">%f && " , segment[i]);
		cut2 = var2 + TString::Format("<%f" , segment[i+1]);
		doca->Draw(var1, cut1 + cut2,"esame");
		entry = gPad->GetListOfPrimitives()->GetEntries() - 1;
		norm = ((TH1F*)(gPad->GetListOfPrimitives()->At(entry)))->GetEntries();
		gPad->Modified(); gPad->Update();

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.