Project

General

Profile

The art::PtrMaker utility

A common pattern is to create two collections, create art::Ptrs to the elements in each collection, and then to create associations between the objects in the two collections. The purpose of art::PtrMaker is to simplify the process of creating an art::Assns product by providing a utility that easily creates an art::Ptr. Creating an art::Ptr is a two-step process with this approach.

art::PtrMaker construction

In what is written below, art::Event can be substituted with art::SubRun, art::Run, or art::Results.

For collections produced in the same module

To construct an art::PtrMaker that creates art::Ptr objects referring to collections produced in the same module, it is necessary to supply several pieces of information:

  • the art::Ptr<T> type
  • a reference to the art::Event object,
  • for art versions older than 3.00.00, a reference to the module in question,
  • and an (optional) instance name corresponding to the collection produced in the same module

For example, if a collection of type std::vector<A> and instance name "TheAs" has been produced in the same module, an art::PtrMaker that will make art::Ptrs to that collection can be constructed via:

PtrMaker<A> make_Aptr{event, "TheAs"};
// Or for art versions older than 3.00.00
//  PtrMaker<A> make_Aptr{event, *this, "TheAs"};

If the collection is not of type std::vector<A>, then the create static function should be used instead (e.g.):

auto make_Aptr = PtrMaker<A>::create<art::PtrVector<A>>(event, "TheAs");
// Or for art versions older than 3.00.00
//  auto make_Aptr = PtrMaker<A>::create<art::PtrVector<A>>(event, *this, "TheAs");

For collections produced in a different module

To construct an art::PtrMaker that creates art::Ptr objects referring to collections produced in a different module, a similar, but smaller, set of information is required:

  • the art::Ptr<T> type
  • a reference to the art::Event object, and
  • the ProductID of the collection to which art:Ptrs will be constructed

Consider the following example:

auto const& h = event.getValidHandle<std::vector<A>>(...);
PtrMaker<A> make_Aptr{event, h.id()};

Making the art::Ptr using the maker

An art::Ptr is created by invoking the call operator on the created maker instance. This is done by specifying the "index" of the container element to which the art::Ptr refers:

 auto const a = make_Aptr(index);

Example

Create an association between a SpacePoint at index 1 and a Track at index 0. Creating a SpacePoint art::Ptr represents an example of Case I, and creating a Track art::Ptr is an example of Case II. In the following example, event is an art::Event and module is the current module type.

void MyProducer::produce(Event& event) override
{
  auto spacePoints = std::make_unique<std::vector<SpacePoint>>(...);

  art::PtrMaker<SpacePoint> makeSpacePointPtr{event};
  // Or for art versions older than 3.00.00
  //   art::PtrMaker<SpacePoint> makeSpacePointPtr{event, *this};

  auto const& h = event.getValidHandle<std::vector<Track>>(...);
  art::PtrMaker<Track> makeTrackPtr{event, h.id()};

  auto const p1 = makeSpacePointPtr(1); // SpacePoint at index 1 for collection created during this module's produce call
  auto const p2 = makeTrackPtr(0);      // Track at index 0 for collection created upstream of this module

  auto assns = std::make_unique<Assns<SpacePoint, Track>>();
  assns->addSingle(p1, p2);

  event.put(move(spacePoints));
  event.put(move(assns));
}