Skip to content
Snippets Groups Projects
Commit 80184e89 authored by Susumu Oda's avatar Susumu Oda Committed by Vakhtang Tsulaia
Browse files

Code cleanup of TrkAmbiguitySolver

parent 9f788c17
No related branches found
No related tags found
No related merge requests found
/* /*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
*/ */
...@@ -12,16 +12,18 @@ TrkAmbiguitySolver Algorithm ...@@ -12,16 +12,18 @@ TrkAmbiguitySolver Algorithm
#define TRKAMBIGUITYSOLVER_H #define TRKAMBIGUITYSOLVER_H
#include "AthenaBaseComps/AthAlgorithm.h" #include "AthenaBaseComps/AthAlgorithm.h"
#include "GaudiKernel/ToolHandle.h"
#include "TrkTrack/TrackCollection.h"
#include "AthContainers/DataVector.h"
#include "AthContainers/ConstDataVector.h" #include "AthContainers/ConstDataVector.h"
#include "AthContainers/DataVector.h"
#include "TrkToolInterfaces/ITrackAmbiguityProcessorTool.h"
#include "TrkTrack/TrackCollection.h"
#include "GaudiKernel/ToolHandle.h"
#include <string> #include <string>
namespace Trk namespace Trk
{ {
class ITrackAmbiguityProcessorTool;
/**Algorithm does ambiguity processing /**Algorithm does ambiguity processing
This algorithm uses the TrkAmbiguityProcessorTool AlgTool to resolve ambiguities in the passed tracks. This algorithm uses the TrkAmbiguityProcessorTool AlgTool to resolve ambiguities in the passed tracks.
...@@ -33,46 +35,42 @@ namespace Trk ...@@ -33,46 +35,42 @@ namespace Trk
public: public:
TrkAmbiguitySolver(const std::string& name, ISvcLocator* pSvcLocator); TrkAmbiguitySolver(const std::string& name, ISvcLocator* pSvcLocator);
~TrkAmbiguitySolver(void); virtual ~TrkAmbiguitySolver(void) = default;
StatusCode initialize(void) override; virtual StatusCode initialize(void) override;
StatusCode execute(void) override; virtual StatusCode execute(void) override;
StatusCode finalize(void) override; virtual StatusCode finalize(void) override;
private: private:
//!<where to find tracks (set in jobOptions and can be multiple collections) //!<where to find tracks (set in jobOptions and can be multiple collections)
SG::ReadHandleKeyArray<TrackCollection> m_tracksLocation; SG::ReadHandleKeyArray<TrackCollection> m_tracksLocation;
SG::WriteHandleKey<TrackCollection> m_resolvedTracksLocation;//!<where to save the resolved tracks SG::WriteHandleKey<TrackCollection> m_resolvedTracksLocation{this, "TrackOutput", "Tracks", "where to save the resolved tracks"};
/** decides whether ambi processing actually occurs /** decides whether ambi processing actually occurs
(if not, the tracks are just resaved). (if not, the tracks are just resaved).
Default=false.*/ Default=false.*/
bool m_resolveTracks; BooleanProperty m_resolveTracks{this, "ResolveTracks", true};
/**Number of tracks input. Useful for debugging*/ /**Number of tracks input. Useful for debugging*/
long int m_trackInCount; long int m_trackInCount{0};
/**Number of tracks passing Ambi solving. Useful for debugging*/ /**Number of tracks passing Ambi solving. Useful for debugging*/
long int m_trackOutCount; long int m_trackOutCount{0};
/** responsible for actual amiguity processing*/ /** responsible for actual amiguity processing*/
ToolHandle<ITrackAmbiguityProcessorTool> m_ambiTool; ToolHandle<ITrackAmbiguityProcessorTool> m_ambiTool{this, "AmbiguityProcessor", "Trk::SimpleAmbiguityProcessorTool/TrkAmbiguityProcessor"};
/** get tracks */ /** get tracks */
StatusCode getTracks(ConstDataVector<TrackCollection> &oldTracks); StatusCode getTracks(ConstDataVector<TrackCollection>& oldTracks);
/** Save the processed tracks. If ambiguity process has not been called then they will /** Save the processed tracks. If ambiguity process has not been called then they will
still be the original converted tracks */ still be the original converted tracks */
/** Eventually this will use the TrkAmbiguityProcessorTool to resolve the track ambiguities. /** Eventually this will use the TrkAmbiguityProcessorTool to resolve the track ambiguities.
At the moment it just saves a new vector of converted tracks. */ At the moment it just saves a new vector of converted tracks. */
void resolveTracks(std::unique_ptr<TrackCollection>&, ConstDataVector<TrackCollection>&); void resolveTracks(std::unique_ptr<TrackCollection>& tracks, ConstDataVector<TrackCollection>& oldTracks);
}; };
} }
#endif #endif
/* /*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
*/ */
#include "TrkAmbiguitySolver/TrkAmbiguitySolver.h" #include "TrkAmbiguitySolver/TrkAmbiguitySolver.h"
#include "TrkToolInterfaces/ITrackAmbiguityProcessorTool.h"
#include "StoreGate/StoreGateSvc.h"
Trk::TrkAmbiguitySolver::TrkAmbiguitySolver(const std::string& name, ISvcLocator* pSvcLocator) : Trk::TrkAmbiguitySolver::TrkAmbiguitySolver(const std::string& name, ISvcLocator* pSvcLocator) :
AthAlgorithm (name, pSvcLocator), AthAlgorithm(name, pSvcLocator)
m_tracksLocation{""},
m_resolvedTracksLocation("Tracks"),
m_resolveTracks(true),
m_trackInCount(0),
m_trackOutCount(0),
m_ambiTool("Trk::SimpleAmbiguityProcessorTool/TrkAmbiguityProcessor", this)
{ {
declareProperty("TrackInput" , m_tracksLocation); declareProperty("TrackInput", m_tracksLocation={""});
declareProperty("TrackOutput" , m_resolvedTracksLocation);
declareProperty("AmbiguityProcessor", m_ambiTool);
declareProperty("ResolveTracks" , m_resolveTracks);
} }
//--------------------------------------------------------------------------
Trk::TrkAmbiguitySolver::~TrkAmbiguitySolver(void)
{}
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
StatusCode StatusCode
Trk::TrkAmbiguitySolver::initialize() Trk::TrkAmbiguitySolver::initialize()
{ {
ATH_MSG_INFO( "TrkAmbiguitySolver::initialize(). " ); ATH_MSG_INFO("TrkAmbiguitySolver::initialize(). ");
if (!m_resolveTracks) { if (not m_resolveTracks) {
ATH_MSG_INFO( "ATTENTION: Resolving tracks turned off! " ); ATH_MSG_INFO("ATTENTION: Resolving tracks turned off! ");
m_ambiTool.disable(); m_ambiTool.disable();
} else { } else {
// Get Tools sevices // Get Tools sevices
if (m_ambiTool.retrieve().isFailure()) { if (m_ambiTool.retrieve().isFailure()) {
msg(MSG::FATAL) << "Failed to retrieve tool " << m_ambiTool << endmsg; ATH_MSG_FATAL("Failed to retrieve tool " << m_ambiTool);
return StatusCode::FAILURE; return StatusCode::FAILURE;
} else } else {
ATH_MSG_INFO( "Retrieved tool " << m_ambiTool ); ATH_MSG_INFO("Retrieved tool " << m_ambiTool);
}
} }
ATH_CHECK(m_resolvedTracksLocation.initialize()); ATH_CHECK(m_resolvedTracksLocation.initialize());
...@@ -53,15 +39,14 @@ Trk::TrkAmbiguitySolver::initialize() ...@@ -53,15 +39,14 @@ Trk::TrkAmbiguitySolver::initialize()
StatusCode StatusCode
Trk::TrkAmbiguitySolver::execute() Trk::TrkAmbiguitySolver::execute()
{ {
ATH_MSG_VERBOSE ("TrkAmbiguitySolver::execute()"); ATH_MSG_VERBOSE("TrkAmbiguitySolver::execute()");
ConstDataVector<TrackCollection> oldTracks(SG::VIEW_ELEMENTS); ConstDataVector<TrackCollection> oldTracks(SG::VIEW_ELEMENTS);
StatusCode loadedTracks = getTracks(oldTracks); StatusCode loadedTracks = getTracks(oldTracks);
if ( loadedTracks.isFailure() ) if (loadedTracks.isFailure()) {
{ ATH_MSG_ERROR("TrkAmbiguitySolver could not find any tracks. Aborting.");
msg(MSG::ERROR) << "TrkAmbiguitySolver could not find any tracks. Aborting." << endmsg; return StatusCode::SUCCESS;
return StatusCode::SUCCESS; }
}
// Now pass tracks to AmbiTool // Now pass tracks to AmbiTool
std::unique_ptr<TrackCollection> tracks; std::unique_ptr<TrackCollection> tracks;
...@@ -69,8 +54,8 @@ Trk::TrkAmbiguitySolver::execute() ...@@ -69,8 +54,8 @@ Trk::TrkAmbiguitySolver::execute()
SG::WriteHandle<TrackCollection> writeh(m_resolvedTracksLocation); SG::WriteHandle<TrackCollection> writeh(m_resolvedTracksLocation);
ATH_CHECK(writeh.record(std::move(tracks))); ATH_CHECK(writeh.record(std::move(tracks)));
ATH_MSG_VERBOSE ("Saved "<<writeh->size()<<" tracks"); ATH_MSG_VERBOSE("Saved "<<writeh->size()<<" tracks");
return StatusCode::SUCCESS; return StatusCode::SUCCESS;
} }
...@@ -81,59 +66,52 @@ Trk::TrkAmbiguitySolver::finalize() ...@@ -81,59 +66,52 @@ Trk::TrkAmbiguitySolver::finalize()
{ {
m_ambiTool->statistics(); m_ambiTool->statistics();
ATH_MSG_INFO( "Finalizing with "<< m_trackInCount << " tracks input, and "<< m_trackOutCount<< " output"); ATH_MSG_INFO("Finalizing with "<< m_trackInCount << " tracks input, and "<< m_trackOutCount<< " output");
return StatusCode::SUCCESS; return StatusCode::SUCCESS;
} }
//------------------------------------------------------------------------------- //-------------------------------------------------------------------------------
StatusCode Trk::TrkAmbiguitySolver::getTracks(ConstDataVector<TrackCollection>& oldTracks)
StatusCode Trk::TrkAmbiguitySolver::getTracks(ConstDataVector<TrackCollection> &oldTracks){ {
auto handles = m_tracksLocation.makeHandles(); auto handles = m_tracksLocation.makeHandles();
size_t totalsize = 0; size_t totalsize = 0;
for (SG::ReadHandle<TrackCollection>& tmpTracks : handles) { for (SG::ReadHandle<TrackCollection>& tmpTracks : handles) {
if (!tmpTracks.isValid()) if (not tmpTracks.isValid()) {
msg(MSG::WARNING) << "Could not retrieve tracks from "<< tmpTracks.key() << endmsg; ATH_MSG_WARNING("Could not retrieve tracks from "<< tmpTracks.key());
totalsize += tmpTracks->size(); }
totalsize += tmpTracks->size();
} }
oldTracks.reserve(totalsize); oldTracks.reserve(totalsize);
for (SG::ReadHandle<TrackCollection>& tmpTracks : handles) { for (SG::ReadHandle<TrackCollection>& tmpTracks : handles) {
ATH_MSG_VERBOSE ("Successfully retrieved "<<tmpTracks->size() ATH_MSG_VERBOSE("Successfully retrieved "<<tmpTracks->size()
<<" tracks from "<< tmpTracks.key()); <<" tracks from "<< tmpTracks.key());
copy(tmpTracks->begin(),tmpTracks->end(), std::back_inserter(oldTracks)); copy(tmpTracks->begin(), tmpTracks->end(), std::back_inserter(oldTracks));
} }
return StatusCode::SUCCESS; return StatusCode::SUCCESS;
} }
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
void Trk::TrkAmbiguitySolver::resolveTracks(std::unique_ptr<TrackCollection> &tracks, ConstDataVector<TrackCollection> &oldTracks) void Trk::TrkAmbiguitySolver::resolveTracks(std::unique_ptr<TrackCollection>& tracks, ConstDataVector<TrackCollection>& oldTracks)
{ {
if (m_resolveTracks) if (m_resolveTracks) {
{ // okay, and let's call the ambiguity processor, just for a laugh.
// okay, and let's call the ambiguity processor, just for a laugh. ATH_MSG_VERBOSE("TrkAmbiguitySolver::resolveTracks() resolving "
ATH_MSG_VERBOSE ("TrkAmbiguitySolver::resolveTracks() resolving " << oldTracks.size()<<" tracks");
<< oldTracks.size()<<" tracks" ); m_trackInCount += oldTracks.size();
m_trackInCount += oldTracks.size(); tracks = std::unique_ptr<TrackCollection>(m_ambiTool->process(oldTracks.asDataVector()));
tracks = std::unique_ptr<TrackCollection>(m_ambiTool->process( oldTracks.asDataVector() )); m_trackOutCount += tracks->size();
m_trackOutCount += tracks->size(); } else {
} // copy tracks. NOT a shallow copy as it was before, since
else // this causes mem leaks when reading back in again
{ // see bug#9886 for details
// copy tracks. NOT a shallow copy as it was before, since tracks = std::make_unique<TrackCollection>();
// this causes mem leaks when reading back in again tracks->reserve(oldTracks.size());
// see bug#9886 for details for (const Track* oldTrack : oldTracks) {
tracks = std::make_unique<TrackCollection>(); tracks->push_back(new Track(*oldTrack));
tracks->reserve(oldTracks.size());
TrackCollection::const_iterator it = oldTracks.begin();
TrackCollection::const_iterator itEnd = oldTracks.end();
for (;it!=itEnd;it++) tracks->push_back( new Track(**it) );
} }
}
return; return;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment