Athena Developer Guide
Athena Developer Guide
Athena Developer Guide
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Athena</strong><br />
Chapter 7 Accessing data Version/Issue: 2.0.0<br />
trackContainer.release(h1);<br />
Since the fate of a contained object is so closely tied to that of its container life would become more<br />
complex if objects could belong to more than one container. Suppose that an object belonged to two<br />
containers, one of which was deleted. Should the object be deleted and removed from the second<br />
container, or not deleted? To avoid such issues an object is allowed to belong to a single container only.<br />
If you wish to move an object from one container to another, you must first remove it from one and then<br />
add to the other. However, the first operation is done implicitly for you when you try to add an object to<br />
a second container:<br />
container1.push_back(h1); // Add to fist container<br />
container2.push_back(h1); // Move to second container<br />
// Internally invokes release().<br />
Since the object h1 has a link back to its container, the push_back() method is able to first follow<br />
this link and invoke the release() method to remove the object from the first container, before<br />
adding it into the second.<br />
In general your first exposure to object containers is likely to be when retrieving data from the event<br />
data store. The sample code in Listing 7.5 shows how, once you have retrieved an object container from<br />
the store you may iterate over its contents, just as with an STL vector.<br />
Listing 7.5 Use of the ObjectVector templated class.<br />
1: typedef ObjectVector MyTrackVector;<br />
2: MyTrackVector *tracks;<br />
3: MyTrackVector::iterator it;<br />
4:<br />
5: for( it = tracks->begin(); it != tracks->end(); it++ ) {<br />
6: // Get the energy of the track and histogram it<br />
7: double energy = (*it)->fourMomentum().e();<br />
8: m_hEnergyDist->fill( energy, 1. );<br />
9: }<br />
The variable tracks is set to point to an object in the event data store of type:<br />
ObjectVector with a dynamic cast (not shown above). An iterator (i.e. a pointer-like<br />
object for looping over the contents of the container) is defined on line 3 and this is used within the loop<br />
to point consecutively to each of the contained objects. In this case the objects contained within the<br />
ObjectVector are of type “pointer to MyTrack”. The iterator returns each object in turn and in the<br />
example, the energy of the object is used to fill a histogram.<br />
page 49