ROOT Tip of the Week: Weight and Plot your Data

ROOT is an object-oriented data-analysis framework, written by the physics community in C++ and freely available. We all love it, we all hate it. It’s like Windows – ubiquitous, a big target for ire, and necessary. There are lots of pretty neat things you can do in ROOT, but some of the most useful are the most basic. In occasional posts, I’ll mention some ROOT tricks and tips that help me to survive the day.

This time around, I’ll tell you how to weight a number of data sets, group them together, and then plot them to make a comparison with unweighted data events. Let’s say you have a number of simulated samples, each generated with their own level of statistics that may, or may not, reflect their actual frequency in the real data. You need to weight them, add them, plot them, and then overlay data. Here’s one way to do it, a way I really like.

Let’s say you have two simulation samples that, when added together with the proper weights, should model the real data. If their files are stored in two subdirectories, sample1/ and sample2/, and each ROOT file contains a TTree called “ntuple”, then you would do the following to quickly plot some variable, x, and draw it in a histogram:


TChain sample1("ntuple");
TChain sample2("ntuple");
sample1.Add("sample1/*.root");
sample2.Add("sample2/*.root");

TH1F hx("hx","Histogram of the variable x",100,-10,10);
hx.Sumw2(); // correctly propagate the error on weighted events

sample1.Draw("x >> hx","0.20");
sample2.Draw("x >>+ hx", "0.40");

In the above example, sample1 is weighted by 0.20 and sample2 is weighted by 0.40. Now the events in the histogram, hx, should be representative of the data. Loading the data in another chain, projecting it into another histogram, and drawing the two overlaid will reveal the level of agreement.

Hope this helps!

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *