Add some print-outs to your algorithm

Last update: 19 May 2023 [History] [Edit]

Right now our algorithm doesn’t do anything, it is just an empty placeholder that shows how an algorithm looks like and what member functions it has. For the first run let’s not try to do anything meaningful or even look at the data, let’s just add some print-outs that show that we actually call the various methods. Note that for printing out we use the ANA_MSG_INFO() macro which is ATLAS specific and used by all of our tools, allowing our code to fit in there nicely.

The main function of any algorithm is execute() which gets called once per event. Find it in source/MyAnalysis/Root/MyxAODAnalysis.cxx. Let’s just add a simple print-out there:

StatusCode MyxAODAnalysis :: execute ()
  // Here you do everything that needs to be done on every single
  // events, e.g. read input variables, apply cuts, and fill
  // histograms and trees. This is where most of your actual analysis
  // code will go.

  ANA_MSG_INFO ("in execute");

  return StatusCode::SUCCESS;

The other function you usually modify is initialize() so let’s add some print-out there to show that it gets called as well:

StatusCode MyxAODAnalysis :: initialize ()
  // Here you do everything that you need to do after the first input
  // file has been connected and before the first event is processed,
  // e.g. create additional histograms based on which variables are
  // available in the input files. You can also create all of your
  // histograms and trees in here, but be aware that this method
  // doesn't get called if no events are processed. So any objects
  // you create here won't be available in the output if you have no
  // input events.

  ANA_MSG_INFO ("in initialize");

  return StatusCode::SUCCESS;

Now we should rebuild our package to pick up the changes:

cd ../build/

tip Note that we didn’t need to call cmake here, as we didn’t add or remove any files.

Now let’s run the code and look for our print-outs:

cd ../run --submission-dir=submitDir

If you are using Athena do:

cd ../run MyAnalysis/

After you are happy that your code changes are working as expected, commit and push your changes, as described in Commit Changes.

We will talk some more about messaging later on, giving you more details of the features available. Just as a basic note here that for a real job you would not want to have a printout on every single event (unless you are debugging a problem). Having a lot of useless messages can hide messages that are actually important, and in the worst case can even slow down your code execution. We use print-outs a lot in this tutorial because it allows you to see immediately what your code is doing, but for actual production level code you typically want to fill histograms and ntuples which can then be passed a lot more easily into the next stage of your analysis.

⭐️ Bonus Exercise

Add a message to the constructor and to the finalize() function. Is the order of output what you expected?