FilterModules

From Mu2eWiki
Jump to navigation Jump to search

Filter modules are used to control the execution of other modules in a trigger path. Recall that a trigger path is an ordered list of module labels. If one of the module labels corresponds to a filter module, and if the filter returns false, then no further modules in that trigger path will be executed.

When specifying a trigger path, one may add an exclamation point in front of a module label to negate the sense of the filter.

The pass/fail status of an entire trigger path can be used to select events for inclusion in an output file.

The source code for a filter module is structured much like a producer module with the following changes:

  • It must inherit from art::EDFilter, not art::EDProducer.
  • It's per event member function is named filter, not produce.
  • The member functions, filter, beginRun, EndRun, beginSubRun and endSubRun have a return type of bool, not void. The return value indicates whether or not the argument ( Event, Run or SubRun ) passed the filter condition.

A filter module may produce data products. We advise that a filter only produce data products that document the filter decision. For example, if you wish to produce a data product and then make a flow control decision based on the content of that data product, we advise that you create one producer module and one or more filter modules. An alternative would be to make a monolithic filter module that both produces the data produt and applies the filter criteria. We advise against this on separation of functions grounds.

The example below shows a filter module that takes a single string parameter as an argument. Legal values of the argument are "odd", "even", "all" and "none". The filter will pass either odd numbered events, even numbered events, all events or no events depending on the value of that parameter.

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

#include "cetlib/exception.h"

#include <iostream>
#include <string>

namespace mu2e {

  class OddEven : public art::EDFilter {

  public:

    explicit OddEven(fhicl::ParameterSet const& );

    bool beginRun(art::Run& run  ) override;
    bool filter(art::Event& event) override;

  private:

    enum mode_type { odd, even, all, none };

    mode_type mode_;

  };

}

mu2e::OddEven::OddEven(fhicl::ParameterSet const& pset ){

  std::string mode = pset.get<std::string>("mode");

  if ( mode == "odd" ){
    mode_= odd;
  } else if ( mode == "even" ){
    mode_ = even;
  } else if ( mode == "all" ){
    mode_ = all;
  } else if ( mode == "none" ){
    mode_ = none;
  } else{
    throw cet::exception("CONFIGURATION")
      << "Unrecognized value for the mode parameter: " << mode << "\n"
      << "Allowed values are: { odd, even, all, none }\n";
  }

}

bool mu2e::OddEven::beginRun(art::Run& ){
  if ( mode_ == none ) return false;
  return true;
}

bool mu2e::OddEven::filter(art::Event& event ){

  bool isOdd = event.id().event()%2 == 1;

  bool pass =
    ( mode_ == all )             ||
    ( mode_ == odd  &&  isOdd )  ||
    ( mode_ == even && !isOdd );

  return pass;
}

DEFINE_ART_MODULE(mu2e::OddEven)