From 0fcca120f5cf33761220ecb7ef1ea220f046ff9f Mon Sep 17 00:00:00 2001
From: Chengdong Fu <fucd@ihep.ac.cn>
Date: Fri, 24 Jul 2020 19:01:43 +0800
Subject: [PATCH] add navagation help to get data from collection

---
 Utilities/DataHelper/CMakeLists.txt          |  2 +-
 Utilities/DataHelper/DataHelper/Navagation.h | 34 ++++++++
 Utilities/DataHelper/src/Navagation.cpp      | 81 ++++++++++++++++++++
 3 files changed, 116 insertions(+), 1 deletion(-)
 create mode 100644 Utilities/DataHelper/DataHelper/Navagation.h
 create mode 100644 Utilities/DataHelper/src/Navagation.cpp

diff --git a/Utilities/DataHelper/CMakeLists.txt b/Utilities/DataHelper/CMakeLists.txt
index b8cfb6ee..7bdae2b7 100644
--- a/Utilities/DataHelper/CMakeLists.txt
+++ b/Utilities/DataHelper/CMakeLists.txt
@@ -4,7 +4,7 @@ find_package(EDM4HEP REQUIRED)
 
 gaudi_depends_on_subdirs()
 
-set(DataHelperLib_srcs src/*.cc)
+set(DataHelperLib_srcs src/*.cc src/*.cpp)
 
 #gaudi_install_headers(DataHelper)
 
diff --git a/Utilities/DataHelper/DataHelper/Navagation.h b/Utilities/DataHelper/DataHelper/Navagation.h
new file mode 100644
index 00000000..5598dda1
--- /dev/null
+++ b/Utilities/DataHelper/DataHelper/Navagation.h
@@ -0,0 +1,34 @@
+#ifndef Navagation_h
+#define Navagation_h
+
+//#include "FWCore/DataHandle.h"
+//#include "podio/CollectionBase.h"
+#include "edm4hep/MCRecoTrackerAssociationCollection.h"
+#include "edm4hep/TrackerHitCollection.h"
+#include <map>
+
+class Navagation{
+ public:
+  static Navagation* Instance();
+
+  Navagation();
+  ~Navagation();
+  
+  void Initialize();
+  //void AddDataHandle(DataHandle* hdl){if(hdl)m_hdlVec.push_back(hdl);};
+  void AddTrackerHitCollection(const edm4hep::TrackerHitCollection* col){m_hitColVec.push_back(col);};
+  void AddTrackerAssociationCollection(const edm4hep::MCRecoTrackerAssociationCollection* col){m_assColVec.push_back(col);};
+
+  edm4hep::TrackerHit* GetTrackerHit(const edm4hep::ObjectID& id, bool delete_by_caller=true);
+  std::vector<edm4hep::ConstSimTrackerHit> GetRelatedTrackerHit(const edm4hep::ObjectID& id);
+  std::vector<edm4hep::ConstSimTrackerHit> GetRelatedTrackerHit(const edm4hep::TrackerHit& hit);
+  
+  //static Navagation* m_fNavagation;
+ private:
+  static Navagation* m_fNavagation;
+  //DataHandle<edm4hep::MCRecoTrackerAssociationCollection> _inHitAssColHdl{"FTDStripTrackerHitsAssociation", Gaudi::DataHandle::Reader, this};
+  std::vector<const edm4hep::TrackerHitCollection*> m_hitColVec;
+  std::vector<const edm4hep::MCRecoTrackerAssociationCollection*> m_assColVec;
+  std::map<int, edm4hep::TrackerHit*> m_trkHits;
+};
+#endif 
diff --git a/Utilities/DataHelper/src/Navagation.cpp b/Utilities/DataHelper/src/Navagation.cpp
new file mode 100644
index 00000000..ff265a53
--- /dev/null
+++ b/Utilities/DataHelper/src/Navagation.cpp
@@ -0,0 +1,81 @@
+#include "DataHelper/Navagation.h"
+
+#include "edm4hep/SimTrackerHit.h"
+#include "edm4hep/TrackerHit.h"
+
+Navagation* Navagation::m_fNavagation = nullptr;
+
+Navagation* Navagation::Instance(){
+  if(!m_fNavagation) m_fNavagation = new Navagation();
+  return m_fNavagation;
+}
+
+Navagation::Navagation(){
+}
+
+Navagation::~Navagation(){
+}
+
+void Navagation::Initialize(){
+  m_hitColVec.clear();
+  m_assColVec.clear();
+  for(std::map<int, edm4hep::TrackerHit*>::iterator it=m_trkHits.begin();it!=m_trkHits.end();it++){
+    delete it->second;
+  }
+  m_trkHits.clear();
+}
+
+edm4hep::TrackerHit* Navagation::GetTrackerHit(const edm4hep::ObjectID& obj_id, bool delete_by_caller){
+  int id = obj_id.collectionID * 10000000 + obj_id.index;
+  if(!delete_by_caller){
+    if(m_trkHits.find(id)!=m_trkHits.end()) return m_trkHits[id];
+  }
+  /*
+  for(int i=0;i<m_assColVec.size();i++){
+    for(auto ass : *m_assColVec[i]){
+      edm4hep::ObjectID rec_id = ass.getRec().getObjectID();
+      if(rec_id.collectionID!=id.collectionID)break;
+      else if(rec_id.index==id.index){
+	m_trkHits.push_back(ass.getRec());
+	return &(m_trkHits.back());
+      }
+    }
+  }
+  */
+  for(int i=0;i<m_hitColVec.size();i++){
+    for(auto hit : *m_hitColVec[i]){
+      edm4hep::ObjectID this_id = hit.getObjectID();
+      if(this_id.collectionID!=obj_id.collectionID)break;
+      else if(this_id.index==obj_id.index){
+	edm4hep::TrackerHit* hit_copy = new edm4hep::TrackerHit(hit);
+	if(!delete_by_caller) m_trkHits[id] = hit_copy;
+	return hit_copy;//&(m_trkHits[id]);
+      }
+    }
+  }
+  
+  throw std::runtime_error("Not found TrackerHit");
+}
+
+std::vector<edm4hep::ConstSimTrackerHit> Navagation::GetRelatedTrackerHit(const edm4hep::ObjectID& id){
+  std::vector<edm4hep::ConstSimTrackerHit> hits;
+  for(int i=0;i<m_assColVec.size();i++){
+    for(auto ass : *m_assColVec[i]){
+      edm4hep::ObjectID this_id = ass.getRec().getObjectID();
+      if(this_id.collectionID!=id.collectionID)break;
+      else if(this_id.index==id.index) hits.push_back(ass.getSim()); 
+    }
+  }
+  return hits;
+}
+
+std::vector<edm4hep::ConstSimTrackerHit> Navagation::GetRelatedTrackerHit(const edm4hep::TrackerHit& hit){
+  std::vector<edm4hep::ConstSimTrackerHit> hits;
+  for(int i=0;i<m_assColVec.size();i++){
+    for(auto ass : *m_assColVec[i]){
+      if(ass.getRec().getObjectID().collectionID != hit.getObjectID().collectionID) break;
+      else if(ass.getRec()==hit) hits.push_back(ass.getSim());
+    }
+  }
+  return hits;
+}
-- 
GitLab