Project

General

Profile

Using the Framework

The ART framework interface makes use of these basic ideas - all the algorithms are stored in modules and the event information is accessed using objects living in the art namespace. The I/O uses ROOT, but it is not based on TObjects. The objects that are stored in a file do not derive from TObject and it is likely that TObject derived objects will also not play nicely with the I/O. The reason for this is ROOT's way of handling pointer data members - ROOT does not store the fields for the data members of pointers, which means on read back those data members may or may not contain sensible information.

Basic Concepts

The art namespace contains the handles to information stored in an event. Objects that are stored in an event are collectively known as data products. They can either be added to an event using an art::EDProducer derived module or they can be retrieved and operated on using an art::EDAnalyzer module. Once an object has been stored in the event, its data cannot be altered.

Other namespaces to be aware of are

  • fhicl - the namespace corresponding to the ParameterSet information described below
  • cet - the namespace where the exceptions and some other utilities live.
  • mf - the namespace of the message logger facility

There is a link to the interface for each concept in the heading for that concept. The first three sections below, art::EDProducer, art::EDAnalyzer, art::Filter are the three main classes from which probably all of your classes will derive. You will become intimate with these when you want to, (1) add a data product to the event record, or (2) read a data product from the event record and do analysis but not write to the event record and (3) use the event record to determine if you want to keep this event at all to write to an output stream.

art::EDProducer

This is a type of module that makes data products and stores them in the art::Event. The module takes a fhicl::ParameterSet in the constructor and uses that to configure that module.

The module can also implement a reconfigure method to allow for run time reconfiguration, for example while running the event display. Reconfiguration during a batch job would not make sense.

The user must supply the implementation for the art::EDProducer::produce() method to create and store data products and the art::EDProducer::reconfigure() method to allow for reconfiguration from the event display.

art::EDAnalyzer

This is a type of module that analyzes data products but cannot write them in an art::Event. The module takes a fhicl::ParameterSet in the constructor and uses that to configure that module.

The module can also implement a reconfigure method to allow for run time reconfiguration, for example while running the event display. Reconfiguration during a batch job would not make sense.

The user must supply the implementation for the art::EDAnalyzer::analyze() method to analyze data products and the art::EDAnalyzer::reconfigure() method to allow for reconfiguration from the event display.

art::Filter

The object allows one to filter records based on information obtained about the record.
For more detailed description of how to correctly configure jobs to filter events see the art wiki page: Filtering Events

art::EDProducer,Analyzer,Filter::beginJob, endJob, etc

These are methods that do tasks that are needed only once a job starts, ends, or a run starts or ends, etc. For example, a beginJob method would likely contain definitions of histograms that might need to be filled during the operation of the module. A beginRun method could contain definitions of properties that can change from run to run (e.g. electron lifetime, temperature etc... ).

art::Event

The art::Event is the primary way to access products made by EDProducer type modules.

It also provides the user with information about an event such as the run, event number, etc through methods like


// assume we have an art::Event &evt

// get the run number. RunNumber_t is a typedef to unsigned int
art::RunNumber_t run = evt.run();

// get the subrun number. SubRunNumber_t is a typedef to unsigned int
art::SubRunNumber_t subRun = evt.subRun();

// get the time stamp.  art::Timestamp::value() returns a TimeValue_t which is a typedef to unsigned long long.
// The conventional use is for the upper 32 bits to have the seconds since 1970 epoch and the lower 32 bits to be
// the number of nanoseconds within the current second.
art::Timestamp ts = evt.time();

// make it into a TTimeStamp
TTimeStamp tts(ts.timeHigh(), ts.timeLow());

// get the event number - this calls a reference to art::EventID().  EventNumber_t is a typedef to unsigned int
art::EventNumber_t event = evt.id().event();

The header files for the classes mentioned above are at

art::EventID.h
art::Timestamp.h

The art::Event can also be used to access products by asking it to return an art::Handle.

art::Handle

An art::Handle is what is returned to a Module when a data product is requested. The request can either be from a art::EDProducer that is attempting to get objects stored in a previous reconstruction or analysis step, or it can be from a art::EDAnalyzer that is attempting to do some analysis task using the information in the object. For example, to get the data product mp::MyProd from the event, one should do

  art::Handle< std::vector<mp::MyProd> > mplistHandle;
  evt.getByLabel("moduleprocesslabel",mplistHandle);

where evt is an object of type art::Event discussed below. The art::Event::getByLabel method takes two arguments, the first is the name of the process associated with the module that produced the list of mp::MyProd objects, and the second is the art::Handle that is to be associated to the list.

You can also get all objects of a given type out of the event using the art::Event::getByType method:

  art::Handle< std::vector<mp::MyProd> > mplistHandle;
  evt.getByType(mplistHandle);

Notice that no module label is used because we are getting all objects of the specified type out of the event.

art::Handles look like a pointer in the code in that the data members of the object being handled are accessed using the "->". For example, to get the size of the list one can do

  mplistHandle->size();

To use the objects in the handle collection, you can

  • Use the art::Handle directly,
    // in the code below, ev is an art::Event...
    art::Handle<std::vector<mp::MyProd> > hnd;
    ev.getByLabel("...", hnd); // use the appropriate label, not "..." 
    
    for(size_t i = 0; i < hnd->size(); ++i){
        const mp::MyProd& mp = hnd->at(i);
    }
    
  • Get the collection from the art::Handle,
    // in the code below, ev is an art::Event...
    art::Handle<std::vector<mp::MyProd> > hnd;
    ev.getByLabel("...", hnd); // use the appropriate label, not "..." 
    
    std::vector<mp::MyProd>& mpvec(*hnd);
    }
    
  • Make a std::list of art::Ptr<mp::MyProd>,
    // in the code below, ev is an art::Event...
    art::Handle<std::vector<mp::MyProd> > hnd;
    ev.getByLabel("...", hnd); // use the appropriate label, not "..." 
    
    std::list<art::Ptr<mp::MyProd> > ptrs;
    art::fill_ptr_list(ptrs, hnd);
    // now ptrs contains Ptr<mp::MyProd> instances, each pointing
    // to one of the rb::Tracks in the collection to which hnd
    // refers.
    
  • Make a std::vector of art::Ptr<mp::MyProd>,
    // in the code below, ev is an art::Event...
    art::Handle<std::vector<mp::MyProd> > hnd;
    ev.getByLabel("...", hnd); // use the appropriate label, not "..." 
    
    std::vector<art::Ptr<mp::MyProd> > ptrs;
    art::fill_ptr_vector(ptrs, hnd);
    // now ptrs contains art::Ptr<mp::MyProd> instances, each pointing
    // to one of the rb::Tracks in the collection to which hnd
    // refers.
    

Use the const std::vector<mp::MyProd>, std::list< art::Ptr<mp::MyProd> >, or std::vector< art::Ptr<mp::MyProd> > if you want to be able to modify the collection of objects.

  • Make a art::PtrVector (discussed below).
    // make an art::PtrVector of the objects
    art::PtrVector<mp::MyProd> prodvec;
    for(unsigned int i = 0; i < mplistHandle->size(); ++i){
      art::Ptr<mp::MyProd> prod(mplistHandle, i);
      prodvec.push_back(prod);
    }
    

The reason for using std::list or std::vector rather than art::PtrVector<T> for this use case is that we expected people to want to modify the collection, e.g. to re-order it, or to remove elements. This is more efficient if done with the list or vector rather than the art::PtrVector.

Most code in the modules should probably use the std::list to establish the final set of art::Ptrs to mp::MyProd data objects stored in the event. The final set of art::Ptrs can then be saved in a new data product as an art::PtrVector if that is needed.

NB Only art::PtrVectors correctly save the references to objects made in other modules, so if you want to save the collection, it must be done as an art::PtrVector.

Upcast on read

The upcast on read functionality can be used to read back objects written into a file that follow a simple inheritance scheme, ie reading in objects of a derived type using the base class type. For instance, recob::Tracks which inherit from recob::Prongs. The objects are retrieved from the art::Event by passing a std::vector<const myprod::MyObject*> to the art::Event::getView() method. An example of using this functionality is

    // declare the std::vector
    std::vector<const recob::Prong*> prongs;

    // Read in the list of recob::Tracks we made in fTrackModuleLabel as recob::Prongs.
    evt.getView(fTrackModuleLabel,prongs);

Notice that the vector is of const pointers of the requested type. You shouldn't save these vectors anywhere (ie as collections in other data objects). Instead they are should only be used for information, ie in the event display.

cet::search_path

This is a utility that will search a list of predefined directories for a relative location of a file. For instance, if you want to use the geometry definition for a detector, mydet.gdml, as the input for the Geometry service, you would pass the geometry service the value "Geometry/gdml/mydet.gdml" as an std::string. The cet::search_path object then searches through the previously defined $FW_SEARCH_PATH variable to see if it can locate the specified file. It will then allow access to information about the file, including its full path. This is helpful when writing code that should search for a given file in several locations such as private and public contexts in the SRT build system. The necessary variables are set when a user sets up the environment. If a user wants the search path to include a test release, the the user should do

$srt_setup -a

from within that test release.

An example of using cet::search_path is


    // constructor decides if initialized value is a path or an environment variable
    // FW_SEARCH_PATH is set to be a cascade with SRT_PRIVATE_CONTEXT, SRT_PUBLIC_CONTEXT and
    // directory where perhaps auxiliary root files are stored 
    cet::search_path sp("FW_SEARCH_PATH");

    // taking a parameter from the parameter set passed into the geometry as the first argument,
    // the second argument is the local variable storing the full path - both are std::string objects
    sp.find_file(pset.get< std::string >("GDML"), fGDMLFile); //fGDMLFile is the relative path to a file

    // Open the GDML file and test if it is there
    struct stat sb;
    if (fGDMLFile.empty() || stat(fGDMLFile.c_str(), &sb)!=0)
      // failed to resolve the file name
      throw cet::exception("NoGDMLGeometry") 
    << "geometry gdml file " << fGDMLFile << " not found!";

fhicl::ParameterSet

This object keeps track of which parameters are to be set by the user at run time for a module or art::Service. It can interpret several data types, including those listed below. An example for how to read the parameter types from the module constructor is listed for each type. See Running Jobs for examples of the job configuration file syntax for each type.

  • bool: fMyBool (pset.get< bool >("MyBool"))
  • int: fMyInt (pset.get< int >("MyInt"))
  • unsigned int: fMyUInt (pset.get< unsigned int >("MyUInt"))
  • std::vector<int>: fMyVInt (pset.get< std::vector<int> >("MyVInt"))
  • std::vector<unsigned int>: fMyVUInt (pset.get< std::vector<unsigned int> >("MyVUInt"))
  • std::string: fMyString (pset.get< std::string >("MyString"))
  • std::vector<std::string>: fMyVString (pset.get< std::vector<std::string> >("MyVString"))
  • double: fMyDouble (pset.get< double >("MyDouble"))
  • std::vector<double>: fMyVDouble (pset.get< std::vector<double> >("MyDouble"))

Other types are available, but the above list should serve almost all a user's needs. It is also possible to pass entire parameter sets in as a single variable. Please see this quick start guide for more details.

art::ServiceHandle

The art::ServiceHandle is a templated object that behaves like a singleton, except that it is owned and managed by the framework. A service can be used within any module. Services can be configured using the job configuration file. A typical example of the use of a service is the detector Geometry. The Geometry is needed in just about every module, but you don't want to keep making instances of the Geometry. Additionally, the different detectors may have to set different parameter values that should be handled in the job configuration.

It is possible to store a service handle as a data member of a module or other object. One should do this if the handle is expected to be called many times during the processing of a single event as that is the most efficient way to access the handle.

art::TFileService

This is a specialized service that connects up to the file where histograms made by modules are to be stored. It provides a mechanism for making TObjects to be stored in that file and managing the memory for those objects. For example

    // get the geometry to figure out something for the histogram
    art::ServiceHandle<geo::Geometry> geo;

    // get access to the TFile service
    art::ServiceHandle<art::TFileService> tfs;

    // make the histogram - fHist is assumed to have been declared in the .h file
    fHist    = tfs->make<TH1D>("channels",  ";channel;# PE",  geo->Nchannels(), 0, geo->Nchannels());

    // note that for some reason ROOT does not automatically connect TGraphs to the output directory when created 
    // like it does for histograms.  So we have to do that ourselves using the ROOT global directory variable gDirectory
    fGraph   = tfs->make<TGraph>(); //default constructor, set number of points and values later
    gDirectory->Append(fGraph);

Message Facility and MessageLogger

The Message Levels

The MessageLogger provides several levels of messages that can be used to print information to an output log. The decision of which level to use is your first way to determine how frequently your message will be printed. If you choose the INFO level, then typically all such messages are printed; if instead you choose the DEBUG level, then typically those messages are not printed unless a specific environmental flag is set or the message service is configured properly.

The levels most likely to be useful are LOG_DEBUG, mf::LogInfo, mf::LogVerbatim, mf::LogWarning and mf::LogError. Note there is no mf:: in front of LOG_DEBUG because it is a macro that will include the file and line numbers of the code producing the output. These are listed in order of severity and have the following behaviors:

  • LogInfo, LogWarning, and LogError represent three levels of "severity" of the message. It is possible (see MessageLogger Parameters ) to configure the job to ignore all LogInfo messages, or all messages of severity less than LogError.
  • LogVerbatim is identical in all ways to LogInfo, except that absolutely no message formatting is performed, and no context, module, or other information is appended to the message. This is appropriate, for example, if the program has prepared a nicely-formatted table for output.
  • The category should specify what this message is about. The category will generally appear as the first part of the message, but it also plays two other roles:
    1. It is possible to set limits on how many times some specific type of message will be sent to the outputs of the logger. By "type" we mean any messages with some specific category. See MessageLogger Parameters for details.
    2. When message statistics are provided, the counts of how many times messages of a given type were issued are keyed to category, module label, and (if provided) subroutine.
    Normally a category name should be up to 20 characters long, without special characters other than underscore.
  • It is unnecessary to explicitly specify the module label or the run/event number; these are automatically provided by the logger.
  • An arbitrary number of additional objects << a << b << ... << z can be appended to the message. These can be strings, ints, doubles, or any object that has an operator<< to an ostream. (Or the message can be issued with no additional objects at all.)
  • There is no need to add spaces at the beginning or end of text items sent to the message, or to add text separators between numerical items. This spacing is taken care of by the logger. However, if any item appended to a message ends in a space, then it is assumed that the user is handling spacing explicitly, and no further automatic spaces are inserted for that message.
  • There is no need to affix any sort of endl; when the statement ends the message will be dispatched.
  • Newline characters can be included in the objects appended to the message. These will be used in formatting the message. But they are generally not necessary: Line breaks are automatically inserted if the next appended object would cause the line to exceed 80 characters.
  • LOG_DEBUG is identical to the others, except:
    • LOG_DEBUG affixes the FILE and LINE number to the message.
    • LOG_DEBUG messages are considered to be lower in severity than LogInfo messages.
    • By default, LOG_DEBUG messages will be rapidly discarded with a minimum of overhead. The user must specify in the .fcl file LOG_DEBUG messages from various modules that are to be enabled
    • Because it must get FILE and LINE from the spot issuing the message, LOG_DEBUG is implemented as a macro rather than a free function.
    • Because LOG_DEBUG is a macro, it is not prepended with the mf:: namespace designation.

Using the Message Service in Code

In order to issue messages, the module must include the MessageLogger header:

  #include "messagefacility/MessageLogger/MessageLogger.h" 

Having included the necessary MessageLogger header, when code wishes to issue a message, one of these functions can be used:

  mf::LogError    ("category") << a << b << ... << z;
  mf::LogWarning  ("category") << a << b << ... << z;
  mf::LogInfo     ("category") << a << b << ... << z;
  mf::LogVerbatim ("category") << a << b << ... << z;
  LOG_DEBUG       ("category") << a << b << ... << z;

The easiest way to produce output that is formatted to your specifications is to emply the mf::LogVerbatim level. This level has absolutely no extra formatting from the message service tacked on to it and most closely resembles what one would expect from std::cout.

You can use std formatting functions in all message service levels. For example

mf::LogVerbatim("MyMessageStream") << "define the width of the following field to be 5, " 
                                   << std::setw(5) << 999 
                                   << " and now define the precision of the following field to be 1, " 
                                   << std::setprecision(2) << 1.00012;

The above example uses mf::LogVerbatim, but the same would work in LOG_DEBUG, mf::LogInfo, mf::LogWarning, and mf::LogError.

Configuring the Message Service

Here is a tutorial for configuring the message facility. Below are some helpful tips for configuring message output.

The .fcl file controlling the job should contain the line

  message:      @local::standard_xxx

in the services block of the file, where xxx indicates the message level severity. The various predefined configurations are defined in the source:trunk/Utilities/messageservice.fcl file. Those options are

standard_debug
standard_info
standard_warning
standard_error

NB you can only use one of the above in a job .fcl file.

The MessageLogger can be configured to set the number of messages printed and to send each class of message to a different output stream. For example, see the standard_warning configuration of the source:trunk/Utilities/messageservice.fcl file, which is repeated here

standard_warning:
{ 

 destinations:  #defines the behavior of the configuration
 {  
  warningmsg:   #conveniently defined name for a destination, this is not a FHICL key word
  {
   type:      "cout"      #tells the message service to output this destination to cout
   threshold: "WARNING"   #tells the message service that this destination applies to WARNING and higher level messages
   append:    true        #says to append all messages to the output
   categories:            #these correspond to the strings in the mf::LogWarning("") calls in the code
   {
     default:             #defines a default behavior, this is a FHICL key word
     {
      limit:       1000   #limits the total number of messages in this category to 1000
      timespan:    60     #in seconds, tells the message service it can output 1000 messages from this category every minute
     }

     RecoBaseDefaultCtor: #an example of a loud category in LArSoft mf::LogWarning messages
     {
      limit: 10           #limit the total number printed to 10
      timespan: 600       #only print this category every 5 minutes
     }

   } # end categories
  } # end warningmsg
 } # end destinations
} # end standard_warning

If one wanted to add another category in the job control .fcl file, it can be done with these lines after the services block:

services.message.destinations.warningmsg.categories.YourStringHere: { limit: 100 timespan: 60 }

If instead, you want to add some message printing for info level messages you can add the following to the fcl file after the services block:

services.message.destinations.infomsg: {
   type:      "cout"      #tells the message service to output this destination to cout
   threshold: "INFO"      #tells the message service that this destination applies to INFO and higher level messages
   append:    true        #says to append all messages to the output
   categories{
     default:        {limit: 0}  #don't print anything at the infomsg level except the explicitly named categories
     YourStringHere: {limit: 100 timespan: 60 }
     YourOtherString:{limit: 1   timespan: 1000} 
  }
}

cet::exception

The cet::exception can be used to cause the framework to end a job gracefully if some predetermined bad thing happens. The use of the art::exception can be configured to skip a module, or skip to the next event, run, etc. Different exception classes can be set to do different things.

cet::exception can be used as follows:

if(x > 2) throw cet::exception("SomeUsefulDescription") << "x = " << x << " is too big";

Detailed instructions for using exceptions under the former incarnation of ART are attached to this page, CMS_SWGuideEdmExceptionUse.pdf. These instructions may no longer be accurate.

In short however, the default behavior of the framework upon catching an exception is to rethrow (except for some system exceptions, which skip the event). However, one can specify the behavior for exceptions with the following configuration fragment:

services.scheduler.XXXX: [ "SomeUsefulDescription", "SomeOtherUsefulDescription", ... ]
where XXX can be:
  • Rethrow;
  • IgnoreCompletely;
  • SkipEevent;
  • FailModule (behave as if the module returned a failure value for this event);
  • FailPath (behave as if the path rejected this event).

Note that the FailModule setting does not imply a path rejection if the module throwing the exception so configured is a filter set to "VETO" or "IGNORE."

A NOvA specific discussion of using cet::exceptions can be found here: NOVA exceptions

art::Ptr<T> and art::PtrVector<T>

The art::Ptr<T> is a template class that acts like a ROOT TRef. It provides a linkage between objects written into different areas of the event (and output file). For example, the rb::Cluster object holds an art::PtrVector<recob::Hit> pointing to the hits contained in the recob::Cluster. The art::Ptr<T> behaves like a pointer (ie you access the methods of the T using the "->"). It also has functionality to return the actual pointer to the object in question using art::Ptr<T>::get() or to check if the art::Ptr<T> is pointing to a legitimate object using art::Ptr<T>::isNull().

An art::Ptr<T> can only be made from an object that has been stored in the event record and is being fetched from the event record. Put another way, you can only make an art::Ptr<T> if you have an art::Handle to a collection of objects of type T. art::Ptr<T> cannot be instantiated like an object of type T,

// The following line will NOT work
art::Ptr<T> myt = new T(); 

or similar will not work because the object you are interested in for that code has not been first stored in the event record.

Please see this note about art::Ptrs as well.

An art::PtrVector<T> is a vector of art::Ptr<T> objects. It provides the basic functionality you would expect from a std::vector, including iterators, size(), begin() and end() methods.

This is a code snippet that iterates on a PtrVector of Hits.

    for(art::PtrVector<recob::Hit>::const_iterator ihit = hits.begin();
    ihit != hits.end(); ++ihit) {
      const art::Ptr<recob::Hit>& phit = *ihit;
      geo::View_t view = phit->View();
      //...
    }

An art::PtrVector<T> can also be sorted, viz.

art::PtrVector< mp::MyProd > mpcol;
mpcol.sort();

This example makes use of the predefined "<" operator of the mp::MyProd. If you want to sort using a function, first define a predicate in the namespace of your module

namespace mymodule {

  struct SortByTime {
    bool operator()(mp::MyProd const& a, mp::MyProd const& b) const { return a.Time() < b.Time(); }
  };

  //////--------- intervening code calling constructors, destructors, etc, now we are in the produce method ---/////////

  art::PtrVector< mp::MyProd > mpcol;
  // fill the PtrVector as in the examples in the art::Handle section above

  mpcol.sort(mymodule::SortByTime());

art::Assns

art::Assns are a way to associate (Assns is a contraction for associations) objects of one type with another. For example, you may want to associate a recob::Cluster with the recob::Hit objects comprising the recob::Cluster. The art:Assns object allows you to navigate these associations bidirectionally, that is you can ask a recob::Cluster which recob::Hits it contains, as well as as a recob::Hit to which recob::Cluster it belongs. The art::Assns also allow us to avoid storing these connections in the higher level object that is being associated. Instead the associations are stored in the event record.

A set of utility functions to perform the necessary steps to associate objects for storage in the art::Event are found in source:trunk/Utilities/AssociationUtil.h There are also methods to retrieve a collection of objects of one type that are not associated with objects of another type. Those methods allow one to retrieve, for example, all recob::Hits from an event that are not associated with recob::Tracks. The comments in that file describe how to use the functions.

To make use of the associations and retrieve objects from the file, one would do


// below trackListHandle is of type art::Handle< std::vector<recob::Track> >, 
// evt is an art::Event, and trackCreatorModule is an std::string holding the 
// label of the module making the tracks and the associations of hits to tracks
art::FindMany<recob::Hit> fmh(trackListHandle, evt, trackCreatorModule);

// loop over all tracks in the handle and get their hits
for(size_t t = 0; t < trackListHandle->size(); ++t){
   std::vector<const recob::Hit*> hits = fmh.at(t);
}

// can also get a collection of art::Ptrs instead of const pointers
art::FindManyP<recob::Hit> fmh(trackListHandle, evt, trackCreatorModule);

// loop over all tracks in the handle and get their hits
for(size_t t = 0; t < trackListHandle->size(); ++t){
   std::vector<art::Ptr<recob::Hit> > hits = fmh.at(t);
}

One can also use the art::FindOne and art::FindOneP, see the detailed description on how to use art::Assns is here. The art::FindOne returns a cet::maybe_ref, whose interface is defined here. The cet::maybe_ref can be tested for validity, allowing a user to be sure a valid association was created.

NB The art::FindMany(P) and art::FindOne(P) are smart query objects and should only be instantiated once for a given collection. If they are instantiated once for each item in a art::Handle, art::PtrVector, art::View or std::vector< art::Ptr<T> > then a heavy performance price will be paid as a lookup table is made multiple times.

If the Assn being retrieved has an instance name associated with it, in addition to a module label, the std::string label (above, trackCreatorModule) can be replaced with an art::InputTag. In this example, assnCreatorModule would be replaced with art::InputTag(trackCreatorModule, trackCreatorInstance), where trackCreatorInstance is a string with the name of the instance.

art::RandomNumberGenerator Service

A nice description of how to use this service can be found here. Note that this write up is for the older fw implementation of art, but there are only minor differences.

To store the state of the random number engines for each event one must add the RandomNumberSaver module to the list of physics producers to be run by the job. The necessary line to add to the fcl file is

physics{
 producers{
   ...
   rns: { module_type: "RandomNumberSaver" }
 }

 ...
}

A more detailed description of how to use the service in your code and store the state of the generator in the event record and retrieve it later is located here.

Making Objects to Store in the Output

Making objects to store in the art::Event is a straightforward operation. The first step is to declare a container for the objects,

std::unique_ptr<std::vector<mp::MyProd> > mpCollection(new std::vector<mp::MyProd>);

Here we used the std::auto_ptr because it handles the cleanup of the memory for the collection for us.

The mpCollection now behaves just like a std::vector, except one accesses the std::vector methods using the "->" operator. Once the collection has been filled (and it can be a collection of just one object), it is written to the event by doing

event.put(std::move(mpCollection));

Now the ownership of the collection belongs to the event and the user cannot modify the collection or the objects in the collection any more.

Schema Evolution for Data Products

Data products may change over time and we will want to do everything we can to ensure backwards compatibility between old and new versions. Please see this page on the ART wiki for information on how to evolve a data product while still maintaining backwards compatibility.

Making a Module

Below are examples of how to make both an EDProducer module and an EDAnalyzer module. The examples show a basic .cc file for each.

Important Module names may not contain underscores. Underscores are special characters used by the ART system for storing data products in an event to label the products according to what module, instance and process created them. Using underscores in module names will result in your job not running.

The artmod command is useful for generating these files required for plugging a new module (Analyzer, Producer, or Filter) into the framework. The command documents itself:

% artmod --help

Suppose I'm working in a package called "MyPackage" which uses the namespace "mp" and I want to create a producer module called "MyProducer". Moreover, assume that I want to include certain methods from the art::EDProducer base class, like art::EDProducer::beginJob() and art::EDProducer::reconfigure():

% artmod --header-loc MyPackage -e beginJob -e reconfigure producer mp::MyProducer

I can also pass it methods specific to my module using the -e tag as long as the interface is completely defined, ie

% artmod --header-loc MyPackage -e beginJob -e reconfigure -e "bool CheckSomethingOut(double d)" producer mp::MyProducer

Similarly I could pass data members like

% artmod --header-loc MyPackage -e beginJob -e reconfigure -e "bool CheckSomethingOut(double d)" -e "double fMyDouble" producer mp::MyProducer

Read the help screen for more details.

NB Objects are stored in the art::Event as collections of references, not pointers. You need to get them out of the event as collections of references, not pointers.

A word about Algorithm objects

Before describing how to use the ART module objects, it is worth discussing a complementary concept used in NOvASoft.

In NOvASoft we have the concept of an Algorithm object. Its basic purpose is to take data products in and return either new or modified data products back to the user. In almost all cases, the user will be a EDProducer or EDFilter type module. The reason to have this additional type of object, which is not defined in the ART framework, is to facilitate use of algorithms in multiple modules. That is, a user may write a module that makes use of both Algorithm objects A&B while another may only want to use Algorithm object A. If the algorithm code is written outside of the modules, then the code can be reused in many modules.

The Algorithm objects that are used in modules should have constructors that take a fhicl::ParameterSet const& p as an argument, and they must also define reconfigure(fhicl::ParameterSet const& p) methods. The constructor should call the reconfigure method in order to configure any parameters needed by the Algorithm. The modules owning the Algorithm object should call the Algorithm's reconfigure method from within the module reconfigure method.

EDProducer

#ifndef MYPRODUCER_H
#define MYPRODUCER_H

#include <iostream>

// Framework includes
#include "art/Framework/Core/ModuleMacros.h" 
#include "art/Framework/Core/EDProducer.h" 
#include "art/Framework/Principal/Event.h" 
#include "art/Framework/Principal/Handle.h" 
#include "art/Persistency/Common/Ptr.h" 
#include "art/Persistency/Common/PtrVector.h" 
#include "art/Framework/Services/Registry/ServiceHandle.h" 
#include "art/Framework/Services/Optional/TFileService.h" 
#include "art/Framework/Services/Optional/TFileDirectory.h" 
#include "fhiclcpp/ParameterSet.h" 
#include "messagefacility/MessageLogger/MessageLogger.h" 
#include "cetlib/exception.h" 
#include "cetlib/search_path.h" 

#include "Geometry/Geometry.h" 
#include "Utilities/LArProperties.h" 
#include "MyProducts/MyProduct.h" 
#include "MyProducts/MyOtherProduct.h" 

#include "TH1.h" 

namespace mp {

  ///class to produce a data product
  class MyProducer : public art::EDProducer {

  public:

    explicit MyProducer(fhicl::ParameterSet const& pset);  // the explicit tag tells the compiler that MyProducer is different from fhicl::ParameterSet
    virtual ~MyProducer();
    void produce(art::Event& evt);                         // makes the objects I want made
    void beginJob();                                      // does things like make histograms at the beginning of the job
    void beginRun(art::Run& run);                         // Happens before each run, so this is a good place to pick up run dependent variables like Lifetime etc. 
    void reconfigure(fhicl::ParameterSet const& pset);    // every module should have one of these to allow the event display to alter configurations

  private:

    double      fDouble;          ///< some data member
    double      fRunProperty;     ///< a property that changes from run to run
    std::string fPrevModuleLabel; ///< label of the module making objects that i need for this step
    TH1D*       fHist;            ///< a histogram data member

  }; // class MyProducer

  //-----------------------------------------------------
  MyProducer::MyProducer(fhicl::ParameterSet const& pset)
  {
    this->reconfigure(pset);
    produces< std::vector<mp::MyOtherProduct> >(); // this line tells the module what it is going to make, you must have one line
                                                   // for each type of collection to be made

  }

  //-----------------------------------------------------
  MyProducer::~MyProducer()
  {
  }

  //-----------------------------------------------------
  void MyProducer::reconfigure(fhicl::ParameterSet const& pset)
  {
    fDouble          = pset.get< double >     ("TheDouble");
    fPrevModuleLabel = pset.get< std::string >("PreviousModuleLabel");
    return;
  }

  //-----------------------------------------------------
  void MyProducer::beginJob()
  {
    // get access to the TFile service
    art::ServiceHandle<art::TFileService> tfs;

    // get the geometry to figure out something for the histogram
    art::ServiceHandle<geo::Geometry> geo;

    fHist    = tfs->make<TH1D>("channels",  ";channels;",  geo->NPlanes()*geo->Plane(0)->Nwires(), 0, geo->NPlanes()*geo->Plane(0)->Ncells());

    return;
  }

  //-----------------------------------------------------
  void MyProducer::beginRun(art::Run& run)
  {
   //get Access to the LArProperties service:
   art::ServiceHandle<util::LArProperties> larp;
   //get temperature from LArProperties - it will have been updated from the DB by now.
   fRunProperty = larp->Temperature();
  }

  //-----------------------------------------------------
  void MyProducer::produce(art::Event& evt)
  {

    //get a collection of electrons
    std::unique_ptr<std::vector<mp::MyOtherProduct> > mopcol(new std::vector<mp::MyOtherProduct>);

    // maybe I will need the geometry service here too
    art::ServiceHandle<geo::Geometry> geom;

    double xyz[3] = {0.};
    double xyz1[3] = {0.};
    int planes = geom->Nplanes();

    ///plane 0 is the first induction plane, every plane has a wire 0
    geom->Plane(0)->GetCenter(xyz);

    // grab the mp::MyProducts made in the last module
    art::Handle< std::vector<mp::MyProduct> > mplistHandle;
    evt.getByLabel(fPrevModuleLabel,mplistHandle);

    // loop over the list of MyProducts
    for(unsigned int i = 0; i < mplistHandle->size(); ++i){

      //get a art::Ptr to the MyProducts
      art::Ptr<mp::MyProduct> mpMyProd(mplistHandle, i);

      // do something to turn out MyOtherProducts - not shown here

      // add the new MyOtherProduct to the collection
      mopcol->push_back(myOtherProd)
    }

    evt.put(std::move(mopcol));

    // mopcol is no longer a valid pointer - don't use it any more!
    // if for some reason you still want access to the products in mopcol, then use 
    // this syntax where the return value is a "read only pointer" to the data product 
    // art::OrphanHandle< std::vector<mp::MyOtherProd> > q = event.put(mopcol);
    // mf::LogInfo("NumMyProds") << "Number of products: " << q->size();

    return;

  }

  // A macro required for a JobControl module.
  DEFINE_ART_MODULE(MyProducer);

} // namespace mp

#endif //MYPRODUCER_H

EDAnalyzer

#ifndef MYANALYZERER_H
#define MYANALYZER_H

#include <iostream>

// Framework includes
#include "art/Framework/Core/EDAnalyzer.h" 
#include "art/Framework/Core/Event.h" 
#include "fhiclcpp/ParameterSet.h" 
#include "art/Persistency/Common/Handle.h" 
#include "art/Framework/Services/Registry/ServiceHandle.h" 
#include "art/Framework/Services/Optional/TFileService.h" 
#include "art/Framework/Core/TFileDirectory.h" 
#include "messagefacility/MessageLogger/MessageLogger.h" 
#include "art/Framework/Core/ModuleMacros.h" 

#include "Geometry/Geometry.h" 
#include "Utilities/LArProperties.h" 
#include "MyProducts/MyProduct.h" 
#include "MyProducts/MyOtherProduct.h" 

#include "TH1.h" 

// My Module
namespace mp {

  // class to produce a data product
  class MyAnalyzer : public art::EDAnalyzer {

  public:

    explicit MyAnalyzer(fhicl::ParameterSet const& pset);  // the explicit tag tells the compiler that MyProducer is different from fhicl::ParameterSet
    virtual ~MyAnalyzer();
    void analyze(const art::Event& evt);                   // makes the objects I want made
    void beginJob();                                       // does things like make histograms at the beginning of the job
    void beginRun(art::Run const& run);                    // Happens before each run, so this is a good place to pick up run dependent variables like Lifetime etc. 
    void reconfigure(fhicl::ParameterSet const&pset);      // every module should have one of these

  private:

    double      fDouble;          ///< some data member
    double      fRunProperty;     ///< a property that changes from run to run
    std::string fPrevModuleLabel; ///< label of the module making objects that i now want to look at
    TH1D*       fHist;            ///< a histogram data member

  }; // class MyAnalyzer

  //-----------------------------------------------------
  MyAnalyzer::MyAnalyzer(fhicl::ParameterSet const& pset)
  {
  }

  //-----------------------------------------------------
  MyAnalyzer::~MyAnalyzer()
  {
  }

  //-----------------------------------------------------
  void MyAnalyzer::reconfigure(fhicl::ParameterSet const& pset)
  {
   fDouble          = pset.get< double >     ("TheDouble");
   fPrevModuleLabel = pset.get< std::string >("PreviousModuleLabel");

   return;
  }

  //-----------------------------------------------------
  void MyAnalyzer::beginJob()
  {
    // get access to the TFile service
    art::ServiceHandle<art::TFileService> tfs;

    // get the geometry to figure out something for the histogram
    art::ServiceHandle<geo::Geometry> geo;

    fHist    = tfs->make<TH1D>("channels",  ";channels;",  geo->NPlanes()*geo->Plane(0)->Nwires(), 0, geo->NPlanes()*geo->Plane(0)->Ncells());

    return;
  }

  //-----------------------------------------------------
  void MyAnalyzer::beginRun(art::Run const& run)
  {
   //get Access to the LArProperties service:
   art::ServiceHandle<util::LArProperties> larp;
   //get temperature from LArProperties - it will have been updated from the DB by now.
   fRunProperty = larp->Temperature();
  }

  //-----------------------------------------------------
  void MyAnalyzer::analyze(const art::Event& evt)
  {

    // maybe I will need the geometry service here too
    art::ServiceHandle<geo::Geometry> geom;

    double xyz[3] = {0.};
    double xyz1[3] = {0.};
    int planes = geom->NPlanes();

    ///plane 0 is the first induction plane, every plane has a wire 0
    geom->Plane(0)->GetCenter(xyz);

    // grab the mp::MyProducts made in a previous module
    art::Handle< std::vector<mp::MyProduct> > mplistHandle;
    evt.getByLabel(fPrevModuleLabel,mplistHandle);

    // loop over the list of MyProducts
    for(unsigned int i = 0; i < mplistHandle->size(); ++i){

      //get a edm::Ptr to the MyProducts
      art::Ptr<mp::MyProduct> mpMyProd(mplistHandle, i);

      // do something to analyze them

    }

    return;

  }

  // A macro required for a JobControl module.
  DEFINE_ART_MODULE(MyAnalyzer);

} // namespace mp

#endif // MYANALYZER_H

EDFilter

A simple example of filtering on even or odd event numbers is below.

First the header file:

// Framework includes
#include "art/Framework/Core/EDFilter.h" 
#include "art/Framework/Core/ModuleMacros.h" 
#include "art/Framework/Principal/Event.h" 

namespace filt{

 class SelectEvents : public art::EDFilter {
   public:
     explicit SelectEvents(fhicl::ParameterSet const& pset);
    virtual ~SelectEvents() { }
    virtual bool filter(art::Event& e);
    void    reconfigure(fhicl::ParameterSet const& pset);

  private:

    // Control parameter: 1 to select odd numbered events; 
    //                    else select even numbered event.
    int fkeepOddOrEven;

  };

   SelectEvents::SelectEvents(fhicl::ParameterSet const& pset)
  {
  }

  void SelectEvents::reconfigure(fhicl::ParameterSet const& pset)
  {
    fkeepOddOrEven = pset.get<int>("keepOddOrEven");
  }

  bool SelectEvents::filter(art::Event& e)
  {

    // EventSetup is a cms leftover that we do not use.

    // Get event number from the event.
    int event = e.id().event();

    // Always keep event 3.
    if ( event == 3 ) return true;

    // Always discard event 4.
    if ( event == 4 ) return false;

    // Is this an odd numbered event?
    bool isOdd = ((event % 2) != 0);

    // Keep only events with odd or even event numbers, as 
    // controled by the parameter from the ParameterSet.
    if ( fkeepOddOrEven == 1){
      return isOdd;
    } else{
      return !isOdd;
    }

  }

  // A macro required for a JobControl module.
  DEFINE_ART_MODULE(SelectEvents);

} // namespace filt

Checking the Configuration of a ROOT file

To dump the information regarding all the ParameterSets stored in a given art data file, then the program config_dumper is useful.
% config_dumper file_name.root

Configuring a Job

See the Running Jobs page.

ART exit codes

More information on the exit status codes for art can be found in the ART workbook circa page C-433 (at the time of this writing).

The exit status codes are:

  • 0 -- ART completed successfully
  • 65 -- A cet::exception was caught and processed in main
  • 66 -- An std::exception was caught and processed in main
  • 67 -- An unknown exception was caught and processed in main
  • 68 -- An std::bad_alloc was caught in main
  • 88 -- Exception from command line processing
  • 89 -- Check command line options failed
  • 90 -- Process command line options failed
  • 91 -- Failed to create a parameter set from parsed configuration with exception