Linear Collider Forum



Home » Software Tools » LCIO » Daughter Collections
Re: Daughter Collections [message #1711 is a reply to message #1710] Thu, 19 March 2009 06:12 Go to previous messageGo to previous message
killenberg
Messages: 125
Registered: July 2005
Location: CERN
Hello Oliver,

I think the LCRelation is not what you want. The Idea behind this is to have a link e.g. from a TrackerHit to the SimTrackerHit it originated from (if I am not mistaken).

Let me start with your second remark:
Quote:

that the new daughter collections only contain pointers to the original objects

LCCollections always contain pointers to objects, because lcio objects are always created on the heap using new. The special thing about LCCollections is that they own the objects they are pointing to, which means the destructor of LCCollection deletes all objects from memory when the collections itself is deleted.

If you now make a copy of these pointers you have two collections pointing to the same objects, each of them holding the ownership. If you delete one of them, the other one is pointing to objects which no longer exist.

For this purpose there is the subset flag. A collection declared as subset does not take over the ownership, so it does not delete the objects if the collection is deleted. So you want to make the two split collections a subset. The original, unsplit collection just stays intact and own the objects.

Example:
  // A collection of tracks from the event.
  LCCollection *trackCollection = event->getCollection("Tracks");

  // We want to split into long an short tracks.
  // These collections are subsets.
  LCCollectionVec *longTracksCollection = new LCCollectionVec(LCIO::TRACK);
  longTracksCollection->setSubset();

  LCCollectionVec *shortTracks = new LCCollectionVec(LCIO::TRACK);
  shortTracksCollection->setSubset();

  for (int i = 0; i < trackCollection->getNumberOfElements() ; i++)
  {
    Track * mytrack = dynamic_cast<Track *>(trackCollection->getElementAt(i));

    if (mytrack->getTrackerHits().size() > 5)
    {
       longTracksCollection->addElement(mytrack);
    }
    else
    {
       shortTracksCollection->addElement(mytrack);
    }
  }

  // Add the collections to the event.
  // Make sure the original collection is not set transient, otherwise
  // the track objects are not in the data file.
  event->addCollection( longTracksCollection );
  event->addCollection( shortTracksCollection );



Cheers

Martin


Martin Killenberg

CERN
martin.killenberg@cern.ch
 
Read Message
Read Message
Read Message
Read Message
Previous Topic:easier way to test if an object is in an collection?
Next Topic:lsh: this event does not exist
Goto Forum:
  


Current Time: Sun May 27 20:21:51 Pacific Daylight Time 2018
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.1.
Copyright ©2001-2010 FUDforum Bulletin Board Software