From 91869b2bc4dfc82a3abc70e9b5dcda3f8ec76c02 Mon Sep 17 00:00:00 2001
From: Sebastien Ponce <sebastien.ponce@cern.ch>
Date: Fri, 22 Dec 2017 09:28:13 +0100
Subject: [PATCH] Adapted more algorithms to the replacement of KeyContainer
 <Track> by std::vector<Track>

Namely :
  - TrackAddLikelihood
  - TrackBestTrackCreator
  - TrackBuildCloneTable
  - TrackEraseExtraInfo
This allows some tests that are failing in the TDR branch to go further.
However, they now fail with a segfault in LinkerWithKey called by TrackBuildCloneTable.
This is due to the fact that we have broken the linking (+ lack of checking when dereferencing...). A know issue
---
 Tr/TrackUtils/src/TrackAddLikelihood.cpp    |  6 +++---
 Tr/TrackUtils/src/TrackAddLikelihood.h      |  5 +++--
 Tr/TrackUtils/src/TrackBestTrackCreator.cpp |  6 +++---
 Tr/TrackUtils/src/TrackBestTrackCreator.h   |  4 ++--
 Tr/TrackUtils/src/TrackBuildCloneTable.cpp  | 19 ++++++++-----------
 Tr/TrackUtils/src/TrackEraseExtraInfo.cpp   | 16 +++++++---------
 6 files changed, 26 insertions(+), 30 deletions(-)

diff --git a/Tr/TrackUtils/src/TrackAddLikelihood.cpp b/Tr/TrackUtils/src/TrackAddLikelihood.cpp
index 7baf8940a77..39865788560 100644
--- a/Tr/TrackUtils/src/TrackAddLikelihood.cpp
+++ b/Tr/TrackUtils/src/TrackAddLikelihood.cpp
@@ -35,15 +35,15 @@ StatusCode TrackAddLikelihood::initialize() {
 }
 
 StatusCode TrackAddLikelihood::execute(){
-  for (const auto& track : *m_input.get() ) {
-    unsigned int type = track->history();
+  for (LHCb::Track& track : m_input.get()->getData() ) {
+    unsigned int type = track.history();
     auto iter = m_toolMap.find(type);
     if (iter == m_toolMap.end()) {
       // @FIXME : do we really want to skip all other tracks?
       return Warning("Likelihood not calculated: Unknown track type",
                      StatusCode::SUCCESS );
     }
-    iter->second->execute(*track).ignore();
+    iter->second->execute(track).ignore();
   }
   return StatusCode::SUCCESS;
 }
diff --git a/Tr/TrackUtils/src/TrackAddLikelihood.h b/Tr/TrackUtils/src/TrackAddLikelihood.h
index 6fc0929ff78..a5bb4531f5c 100644
--- a/Tr/TrackUtils/src/TrackAddLikelihood.h
+++ b/Tr/TrackUtils/src/TrackAddLikelihood.h
@@ -26,8 +26,9 @@ public:
   StatusCode execute() override;
 
 private:
-
-  DataObjectHandle<LHCb::Tracks> m_input{ LHCb::TrackLocation::Default, Gaudi::DataHandle::Reader, this };
+  // This should use AnyDataHandle, but as this one imposes proper constness
+  // and this algo breaks const safety by design, it's not possible
+  DataObjectHandle<AnyDataWrapper<std::vector<LHCb::Track>>> m_input{ LHCb::TrackLocation::Default, Gaudi::DataHandle::Reader, this };
   std::vector<unsigned int> m_types;
   std::string  m_likelihoodToolName;
   std::map<unsigned int, const ITrackManipulator*> m_toolMap;
diff --git a/Tr/TrackUtils/src/TrackBestTrackCreator.cpp b/Tr/TrackUtils/src/TrackBestTrackCreator.cpp
index 454430a7c11..df07220c49c 100644
--- a/Tr/TrackUtils/src/TrackBestTrackCreator.cpp
+++ b/Tr/TrackUtils/src/TrackBestTrackCreator.cpp
@@ -91,7 +91,7 @@ StatusCode TrackBestTrackCreator::finalize()
 //=============================================================================
 // Main execution
 //=============================================================================
-LHCb::Tracks
+std::vector<LHCb::Track>
 TrackBestTrackCreator::operator()(const Gaudi::Functional::vector_of_const_<LHCb::Tracks> &ranges) const {
   if (m_debugLevel) debug() << "==> Execute" << endmsg;
   if (m_addGhostProb.value()) {
@@ -265,12 +265,12 @@ TrackBestTrackCreator::operator()(const Gaudi::Functional::vector_of_const_<LHCb
   }
 
   // create output container, and put selected tracks there
-  LHCb::Tracks tracksOutCont;
+  std::vector<LHCb::Track> tracksOutCont;
   // insert selected tracks
   tracksOutCont.reserve(successful_tracks.size());
   for (TrackData& tr: successful_tracks) {
     // make tr release ownership of track
-    tracksOutCont.add(tr.trackptr().release());
+    tracksOutCont.push_back(std::move(*tr.trackptr().release()));
   }
 
   if( m_debugLevel ) {
diff --git a/Tr/TrackUtils/src/TrackBestTrackCreator.h b/Tr/TrackUtils/src/TrackBestTrackCreator.h
index cf3833b87c5..f76d9f43d62 100644
--- a/Tr/TrackUtils/src/TrackBestTrackCreator.h
+++ b/Tr/TrackUtils/src/TrackBestTrackCreator.h
@@ -96,7 +96,7 @@ typedef GaudiAlgorithm TrackBestTrackCreatorBase;
  * - add code to use ancestor information on tracks to deal with obvious clones
  */
 class TrackBestTrackCreator final :
-  public Gaudi::Functional::MergingTransformer<LHCb::Tracks(const Gaudi::Functional::vector_of_const_<LHCb::Tracks> &ranges),
+public Gaudi::Functional::MergingTransformer<std::vector<LHCb::Track>(const Gaudi::Functional::vector_of_const_<LHCb::Tracks> &ranges),
                                                Gaudi::Functional::Traits::BaseClass_t<TrackBestTrackCreatorBase>> {
 public:
   /// Standard constructor
@@ -104,7 +104,7 @@ public:
 
   virtual StatusCode initialize() override;    ///< Algorithm initialization
   virtual StatusCode finalize  () override;    ///< Algorithm finalization
-  LHCb::Tracks
+  std::vector<LHCb::Track>
     operator()(const Gaudi::Functional::vector_of_const_<LHCb::Tracks> &ranges) const override;
 
 private:
diff --git a/Tr/TrackUtils/src/TrackBuildCloneTable.cpp b/Tr/TrackUtils/src/TrackBuildCloneTable.cpp
index 5598258905a..2ead707df44 100644
--- a/Tr/TrackUtils/src/TrackBuildCloneTable.cpp
+++ b/Tr/TrackUtils/src/TrackBuildCloneTable.cpp
@@ -1,6 +1,7 @@
 #include "TrackBuildCloneTable.h"
 
 #include "GaudiKernel/PhysicalConstants.h"
+#include "GaudiKernel/AnyDataWrapper.h"
 #include "LHCbMath/MatrixUtils.h"
 #include "Event/TrackUnitsConverters.h"
 #include "Event/State.h"
@@ -125,30 +126,26 @@ StatusCode TrackBuildCloneTable::execute()
   // loop and make working tracks
   for (const auto& loc : m_inputLocations ){
     // Tracks
-    auto inCont = getIfExists<Tracks>(loc);
-    if ( inCont == nullptr ){
-      error()<<"Container "<<loc<<" does not exist"<<endmsg;
-      continue;
-    }
+    auto& inCont = getIfExists<AnyDataWrapper<std::vector<Track>>>(loc)->getData();
     if ( msgLevel(MSG::VERBOSE) ){
-      verbose() << "Found " << inCont->size() << " Tracks at " << loc << endmsg;
+      verbose() << "Found " << inCont.size() << " Tracks at " << loc << endmsg;
     }
-    tracks.reserve(tracks.size()+inCont->size());
+    tracks.reserve(tracks.size()+inCont.size());
 
-    for ( const auto& track : *inCont )
+    for ( const auto& track : inCont )
     {
       if( UNLIKELY( msgLevel(MSG::DEBUG) ) )
-        debug() << "Track " << track->key() << " " << track->history() << endmsg;
+        debug() << "Track " << track.key() << " " << track.history() << endmsg;
 
       // make working track object, directly in the vector
-      tracks.emplace_back( track );
+      tracks.emplace_back( &track );
       CloneTrack& cloneTrack = tracks.back();
       cloneTrack.states.reserve( m_zStates.size() );
 
       // Loop over all z positions
       for ( auto z : m_zStates ) {
         // get state closest to reference z pos
-        const State & cState = track->closestState(z);
+        const State & cState = track.closestState(z);
 
         // only take ones that are close in z
         if ( fabs( cState.z() - z ) > m_maxDz ) continue ;
diff --git a/Tr/TrackUtils/src/TrackEraseExtraInfo.cpp b/Tr/TrackUtils/src/TrackEraseExtraInfo.cpp
index 07197c13f6e..55dee65a536 100644
--- a/Tr/TrackUtils/src/TrackEraseExtraInfo.cpp
+++ b/Tr/TrackUtils/src/TrackEraseExtraInfo.cpp
@@ -2,6 +2,7 @@
 #include "Event/Track.h"
 
 #include "TrackEraseExtraInfo.h"
+#include "GaudiKernel/AnyDataWrapper.h"
 
 
 using namespace LHCb;
@@ -43,16 +44,13 @@ StatusCode TrackEraseExtraInfo::initialize() {
 //=============================================================================
 StatusCode TrackEraseExtraInfo::execute(){
 
-  Tracks* inCont = getIfExists<Tracks>(m_inputLocation);
-  if( inCont == nullptr){
-    return Warning( "Input container "+m_inputLocation+" does not exist", StatusCode::SUCCESS, 20 );
-  }
+  std::vector<Track>& inCont = getIfExists<AnyDataWrapper<std::vector<Track>>>(m_inputLocation)->getData();
 
   // -- Print extra info which is on track
   if( UNLIKELY( m_printExtraInfo )){
-    for( LHCb::Track* track : *inCont){
-      info() << "ExtraInfo for track: " << track->type() << " : " << track->key() << endmsg;
-      const LHCb::Track::ExtraInfo extraInfo = track->extraInfo();
+    for( LHCb::Track& track : inCont){
+      info() << "ExtraInfo for track: " << track.type() << " : " << track.key() << endmsg;
+      const LHCb::Track::ExtraInfo extraInfo = track.extraInfo();
       for ( const auto& ei : extraInfo ) {
         const LHCb::Track::AdditionalInfo addInfo =
           static_cast<LHCb::Track::AdditionalInfo>(ei.first);
@@ -61,8 +59,8 @@ StatusCode TrackEraseExtraInfo::execute(){
     }
   }
 
-  for (auto& track : *inCont) {
-    for(int i : m_erasableInfo) track->eraseInfo( i );
+  for (auto& track : inCont) {
+    for(int i : m_erasableInfo) track.eraseInfo( i );
   }
 
   return StatusCode::SUCCESS;
-- 
GitLab