Module Writing Tutorial

From Mu2eWiki
Revision as of 23:37, 30 May 2019 by Edmonds (talk | contribs)
Jump to navigation Jump to search

Tutorial Session Goal

This tutorial will show how to write an art module for Mu2e. It will explain how to structure the code, define runtime parameters, produce histograms and/or TTrees, and consume and produce data products.

Session Prerequisites and Advance Preparation

This tutorial requires the user to:

Session Introduction

Mu2e uses the art framework to organize our event processing code. The art framework processes events through a configurable sequential set of modules, called a path. Modules expects certain inputs, and optionally produces certain outputs. Modules have a standard interface for interacting with the art framework, giving the user access to data at run/subrun/event transitions. Modules have a standard library for defining configuration parameters that can be changed at runtime.

In this tutorial you will learn how to create an art module from a basic template, and perform basic data operations in that module. You will learn how to configure your module in code and fcl, and how to produce various kinds of output.

Disclaimer! In this tutorial we will be copying old modules to create new modules. In general, this is not a greate idea because we can end up copying errorful code. If yuo do this in the real world make sure you trust the code you're copying!

Basic Exercises

Exercise 1: Running a simple module (Hello, Tutorial!)

In this exercise, we will run a simple module that will print a welcoming message.

  1. First create a Satelite release in the ModuleWriting tutorial
  2. > cd $TUTORIAL_BASE/ModuleWriting > /cvmfs/mu2e.opensciencegrid.org/Offline/v7_4_0/SLF6/prof/Offline/bin/createSatelliteRelease --directory . > source setup.sh
  3. This first module has already been written for you so we can compile straight away
  4. > scons -j4
  5. And run
  6. mu2e -c fcl/HelloEx01.fcl This will write "Hello, world" and the full event id for the first 3 events.

Exercise 2: Adding module configuration (Hello, Fhicl Validation!)

  • Add a configuration parameter to the module
  1. Copy last exercise's .cc file and create a new file
  2. > cp src/HelloTutorial_module.cc src/HelloFhiclValidation_module.cc
  3. Open the new file in your favourite text editor and make the following changes
    1. find and replace "HelloTutorial" with "HelloFhiclValidation"
    2. in the Config struct, below the using commands add the following:
    3. fhicl::Atom<int> number{Name("number")};
    4. add a private member variable to the class:
    5. int _number;
    6. add to the constructor initialisation list:
    7. _number(conf().number())
    8. add a second std::cout command to print the number e.g.
    9. std::cout << "My number is..." << _number << std::endl;
  4. Recompile with scons -j4 and fix any compilation errors
  5. Now create a copy of the fcl file, open it and make the following changes:
    1. find and replace "HelloTutorial" with "HelloFhiclValidation"
  6. (Optional): try playing with the fcl file and add typos to the fcl parameters
  7. (Optional): add some more parameters (try floats, strings etc.)

Exercise 3: Reading in a mu2e data product (Hello, Mu2e Data Product!)

  • Find a mu2e data product in an event

Exercise 4: Filling a histogram (Hello, Histogram!)

  • Create and fill a histogram from a data product

Exercise 5:

  • Create a subset of a data product and add it to the event
  • Create a new data product, fill it, and add it to the event
  • Create and use a persistent pointer (Ptr) to a data product
  • Create an association between 2 data products

Reference Materials

  • art workbook