Module Writing Tutorial: Difference between revisions

From Mu2eWiki
Jump to navigation Jump to search
No edit summary
Line 20: Line 20:
various kinds of output.
various kinds of output.


* Compile and run HelloWorld in art
'''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.
<ol style="list-style-type:lower-alpha">
<li>First create a Satelite release in the ModuleWriting tutorial</li>
<nowiki>> cd $TUTORIAL_BASE/ModuleWriting
> /cvmfs/mu2e.opensciencegrid.org/Offline/v7_4_0/SLF6/prof/Offline/bin/createSatelliteRelease --directory .
> source setup.sh</nowiki>
<li>This first module has already been written for you so we can compile straight away</li>
<nowiki>> scons -j4</nowiki>
<li>And run</li>
<nowiki>mu2e -c fcl/HelloEx01.fcl</nowiki>
This will write "Hello, world" and the full event id for the first 3 events.
</ol>
 
=== Exercise 2: Adding module configuration (Hello, Fhicl Validation!) ===
* Add a configuration parameter to the module
* Add a configuration parameter to the module
<ol style="list-style-type:lower-alpha">
<li>Copy last exercise's .cc file and create a new file</li>
<nowiki>> cp src/HelloTutorial_module.cc src/HelloFhiclValidation_module.cc</nowiki>
<li>Open the new file in your favourite text editor and make the following changes</li>
<ol style="list-style-type:lower-roman">
  <li>find and replace "HelloTutorial" with "HelloFhiclValidation"</li>
  <li>in the <code>Config</code> struct, below the <code>using</code> commands add the following:</li>
<nowiki>fhicl::Atom<int> number{Name("number")};</nowiki>
  <li>add a private member variable to the class:</li>
<nowiki>int _number;</nowiki></li>
  <li>add to the constructor initialisation list:</li>
<nowiki>_number(conf().number())</nowiki>
  <li>add a second <code>std::cout</code> command to print the number e.g.</li>
<nowiki>std::cout << "My number is..." << _number << std::endl;</nowiki>
</ol>
<li>Recompile with <code>scons -j4</code> and fix any compilation errors
<li>Now create a copy of the fcl file, open it and make the following changes:</li>
<ol style="list-style-type:lower-roman">
  <li>find and replace "HelloTutorial" with "HelloFhiclValidation"</li>
</ol>
<li>(Optional): try playing with the fcl file and add typos to the fcl parameters</li>
<li>(Optional): add some more parameters (try floats, strings etc.)</li>
</ol>
=== Exercise 3: Reading in a mu2e data product (Hello, Mu2e Data Product!) ===
* Find a mu2e data product in an event
* Find a mu2e data product in an event
=== Exercise 4: Filling a histogram (Hello, Histogram!) ===
* Create and fill a histogram from a data product
* 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 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 a new data product, fill it, and add it to the event

Revision as of 23:37, 30 May 2019

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