Creating and Filling Histograms

Last update: 16 Nov 2022 [History] [Edit]

While the workflow we are following in this tutorial requires the outputs of your analysis code to be stored in an ntuple, it is also common to store the information in histograms.

There are several ways to manage histograms, but here we will show a way that allows you to manage histograms in the same way whether you are running your algorithm in EventLoop or Athena. Using a histogram is typically a two-step process, first you book the histogram indicating the name, binning, etc. then you fill the histogram. As an example let’s use the eventNumber that we’ve already looked at. Later in the tutorial, you will be asked to produce histograms for objects such as electrons and muons.

First, add the following include to MyxAODAnalysis.h:

#include <TH1.h>

tip Ordinarily, it would be better to put this in MyxAODAnalysis.cxx, but we will need it in the header file later, so we may as well put it there now.

Booking should happen in the initialize() function of your algorithm. Though in exceptional cases you may want to delay this to the processing of the first event. (In case the creation of the histogram depends on what sort of sample you are processing.) Either way you call:

  ANA_CHECK (book (TH1F ("eventNumber", "eventNumber", 100, 394.445e6, 394.455e6))); // event number

tip Note that the binning defined here is very specific to the input file. This is meant to simply be an example of how to book and fill histograms. In an analysis scenario, you likely wouldn’t be filling an eventNumber histogram and therefore wouldn’t need to fine tune the binning like this.

You should prefer using the initialize() function, because that method is called before processing any events and makes sure that the histogram gets created even if we don’t process any events.

And then inside execute() let’s fill the histograms with the eventNumber information we’ve already retrieved:

  hist ("eventNumber")->Fill (m_eventNumber);

That is it on the c++ side. Now let’s compile your code.

Setting up the jobs

To now successfully run your updated code, you need to do slightly different things depending on whether you are using EventLoop or Athena. In the following we describe these differences.

Outputs in EventLoop

EventLoop creates a file automatically for your output histograms, you don’t need to modify your job submission script at all to run your histogram writing algorithm.

Once your job is finished you can find the histogram(s) inside the unique directory you created at job submission (submitDir). There is a different file for each sample you submitted (hist-label.root), so in our case we have only one submitDir/hist-dataset. Please note that the second part of the histogram file name will correspond directly to the name of the sample in SampleHandler, while the first part (hist-) is set by EventLoop and can not be changed.

tip Note that there is another output file in the hist directory called hist/dataset.root. This is not the file that contains your output histograms.

Outputs in Athena

The formalism is exactly the same as for trees. You need to tell the THistSvc service which file to write for the ANALYSIS stream. You can just add the following to your jobOptions to do this (see main athena tutorial for more info):

jps.AthenaCommonFlags.HistOutputs = ["ANALYSIS:MyxAODAnalysis.outputs.root"]
svcMgr.THistSvc.MaxFileSize=-1 #speeds up jobs that output lots of histograms

tip Just as for trees, you can assign a different stream name to your algorithm, and create the output file with a different stream name. Allowing you to create multiple files from multiple algorithms.