From 3e8e025cc3e5e158c8ff7225dfcef314f14fc6c1 Mon Sep 17 00:00:00 2001
From: Catrin Bernius <catrin.bernius@cern.ch>
Date: Fri, 16 Nov 2018 13:39:02 +0000
Subject: [PATCH] Merge branch '21.1-dev-user' into '21.1-dev'

Add possibility to filter Offline tracks by Roi for the ntple creation

See merge request atlas/athena!15929

(cherry picked from commit c303432281ad6fb4acf4fe01ab797a5e964f57b6)

4ea01c33 Add possibility to filter Offline tracks by Roi for the ntple creation
fd99e61b remove an unecessary commented out line
---
 .../python/iconfTool/gui/__init__.py          |  0
 .../python/iconfTool/utils/__init__.py        |  0
 .../AnalysisConfig_Ntuple.h                   |  7 +-
 .../TrigTestMonToolAC.h                       |  2 +
 .../src/AnalysisConfig_Ntuple.cxx             | 90 +++++++++++++++++++
 .../src/TrigTestMonToolAC.cxx                 |  8 +-
 6 files changed, 104 insertions(+), 3 deletions(-)
 mode change 100644 => 100755 Control/AthenaConfiguration/python/iconfTool/gui/__init__.py
 mode change 100644 => 100755 Control/AthenaConfiguration/python/iconfTool/utils/__init__.py

diff --git a/Control/AthenaConfiguration/python/iconfTool/gui/__init__.py b/Control/AthenaConfiguration/python/iconfTool/gui/__init__.py
old mode 100644
new mode 100755
diff --git a/Control/AthenaConfiguration/python/iconfTool/utils/__init__.py b/Control/AthenaConfiguration/python/iconfTool/utils/__init__.py
old mode 100644
new mode 100755
diff --git a/Trigger/TrigAnalysis/TrigInDetAnalysisExample/TrigInDetAnalysisExample/AnalysisConfig_Ntuple.h b/Trigger/TrigAnalysis/TrigInDetAnalysisExample/TrigInDetAnalysisExample/AnalysisConfig_Ntuple.h
index d9975585848..e71e6801980 100644
--- a/Trigger/TrigAnalysis/TrigInDetAnalysisExample/TrigInDetAnalysisExample/AnalysisConfig_Ntuple.h
+++ b/Trigger/TrigAnalysis/TrigInDetAnalysisExample/TrigInDetAnalysisExample/AnalysisConfig_Ntuple.h
@@ -62,7 +62,8 @@ public:
     m_tauEtCutOffline(tauEtCutOffline),
     m_TruthPdgId(TruthPdgId),
     m_finalised(true),
-    m_printInfo(true)
+    m_printInfo(true),
+    m_ptmin(0)
   {  
     //    std::cout << "AnalysisConfig_Ntuple::AnalysisConfig_Ntuple() " << chainNames.size() << std::endl;
 
@@ -116,6 +117,8 @@ public:
     delete m_event;
   }
 
+  void set_ptmin( double d=0 ) { m_ptmin=d; }
+
 protected:
 
   virtual void loop();
@@ -164,6 +167,8 @@ protected:
 
   bool m_printInfo;
 
+  double m_ptmin;
+
 };
 
 
diff --git a/Trigger/TrigAnalysis/TrigInDetAnalysisExample/TrigInDetAnalysisExample/TrigTestMonToolAC.h b/Trigger/TrigAnalysis/TrigInDetAnalysisExample/TrigInDetAnalysisExample/TrigTestMonToolAC.h
index 19a4b81992e..c0d1670df58 100644
--- a/Trigger/TrigAnalysis/TrigInDetAnalysisExample/TrigInDetAnalysisExample/TrigTestMonToolAC.h
+++ b/Trigger/TrigAnalysis/TrigInDetAnalysisExample/TrigInDetAnalysisExample/TrigTestMonToolAC.h
@@ -132,6 +132,8 @@ public:
   bool m_requireDecision;
   
   bool m_containTracks;
+
+  bool m_filter_on_roi;
 };
 
 
diff --git a/Trigger/TrigAnalysis/TrigInDetAnalysisExample/src/AnalysisConfig_Ntuple.cxx b/Trigger/TrigAnalysis/TrigInDetAnalysisExample/src/AnalysisConfig_Ntuple.cxx
index ce8d51787a6..620c092145b 100644
--- a/Trigger/TrigAnalysis/TrigInDetAnalysisExample/src/AnalysisConfig_Ntuple.cxx
+++ b/Trigger/TrigAnalysis/TrigInDetAnalysisExample/src/AnalysisConfig_Ntuple.cxx
@@ -117,6 +117,12 @@ const HepMC::GenParticle* fromParent( int pdg_id, const HepMC::GenParticle* p, b
 
 
 
+template<class T>
+void remove_duplicates(std::vector<T>& vec) {
+  std::sort(vec.begin(), vec.end());
+  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
+}
+
 
 void AnalysisConfig_Ntuple::loop() {
 
@@ -1535,6 +1541,90 @@ void AnalysisConfig_Ntuple::loop() {
 
 	}
 
+
+	{ 
+	  /// strip out the offline tracks not in any Roi ...
+
+	  if ( filterOnRoi() || m_ptmin>0 ) { 
+	    
+	    TIDA::Chain* offline = 0;
+	    
+	    std::vector<std::string> chainnames = m_event->chainnames();
+	    
+	    /// get the offline chain
+	    
+	    for ( size_t ic=chainnames.size() ; ic-- ; ) {
+	      if ( chainnames[ic] == "Offline" ) {
+		offline = &(m_event->chains()[ic]);
+		break;
+	      }
+	    }
+	    
+	    if ( offline ) { 
+	      
+	      std::vector<TIDA::Chain>& chains = m_event->chains();
+	      std::vector<TIDA::Chain>::iterator citr = chains.begin();
+	      
+	      std::vector<std::pair<double,double> > philims;
+	      
+	      for ( ; citr!=chains.end() ; citr++ ) {
+		if ( citr->name().find("HLT_")!=std::string::npos ) { 
+		  for ( size_t ir=0 ; ir<citr->size() ; ir++ ) {
+		    TIDARoiDescriptor& roi = citr->rois()[ir].roi();
+		    if ( roi.composite() ) { 
+		      for ( size_t isub=0 ; isub<roi.size() ; isub++ ) { 
+			philims.push_back( std::pair<double,double>( roi[isub]->phiMinus(), roi[isub]->phiPlus() ) ); 
+		      }
+		    }
+		    else philims.push_back( std::pair<double,double>( roi.phiMinus(), roi.phiPlus() ) ); 
+		  }
+		}
+	      }
+	      
+	      remove_duplicates( philims );
+
+	      for ( size_t iroi=0 ; iroi<offline->size() ; iroi++ ) {
+		
+		std::vector<TIDA::Track>& tracks = offline->rois()[iroi].tracks();
+		
+		size_t Noffline = tracks.size();
+
+		for ( std::vector<TIDA::Track>::iterator it=tracks.begin() ; it<tracks.end() ; ) {
+		  bool inc = true;
+		  if ( m_ptmin>0 ) { 
+		    if ( std::fabs(it->pT())<m_ptmin ) { inc=false; tracks.erase( it ); }
+		  }
+		  if ( inc && filterOnRoi() ) { 
+		    bool remove_track = true;
+		    for ( size_t isub=0 ; isub<philims.size() ; isub++ ) { 
+		      
+		      if ( philims[isub].first < philims[isub].second ) { 
+			if ( it->phi()>=philims[isub].first && it->phi()<=philims[isub].second ) { 
+			  remove_track = false; 
+			  break;
+			}
+		      }
+		      else  {
+			if ( it->phi()>=philims[isub].first || it->phi()<=philims[isub].second ) { 
+			  remove_track = false; 
+			  break;
+			}
+		      }
+		    }
+		    if ( remove_track ) { inc=false; tracks.erase( it ); }
+		  }
+		  if ( inc ) it++;
+		}
+		
+		//  m_provider->msg(MSG::INFO) << "TIDA::Roi offline track reduction: " << Noffline << " -> " << tracks.size() << endmsg;
+		
+	      }
+	     
+	    }
+	    	    
+	  }
+	}
+
 	if ( m_printInfo ) m_provider->msg(MSG::INFO) << "FILL TREE\n" << (*m_event) << endmsg;      
 	if ( mTree ) mTree->Fill();
 
diff --git a/Trigger/TrigAnalysis/TrigInDetAnalysisExample/src/TrigTestMonToolAC.cxx b/Trigger/TrigAnalysis/TrigInDetAnalysisExample/src/TrigTestMonToolAC.cxx
index 3c8cf6dd17c..49779481bab 100644
--- a/Trigger/TrigAnalysis/TrigInDetAnalysisExample/src/TrigTestMonToolAC.cxx
+++ b/Trigger/TrigAnalysis/TrigInDetAnalysisExample/src/TrigTestMonToolAC.cxx
@@ -36,7 +36,8 @@ TrigTestMonToolAC::TrigTestMonToolAC(const std::string & type, const std::string
      m_keepAllEvents(false),
      m_fileopen(false),
      m_requireDecision(false),
-     m_containTracks(false)
+     m_containTracks(false),
+     m_filter_on_roi(false)
 {
   msg(MSG::WARNING) << "TrigTestMonToolAC::TrigTestMonToolAC() compiled: " << __DATE__ << " " << __TIME__ << endmsg;
 
@@ -73,6 +74,8 @@ TrigTestMonToolAC::TrigTestMonToolAC(const std::string & type, const std::string
 
   declareProperty( "ContainTracks", m_containTracks = false );
 
+  declareProperty( "FilterOnRoi", m_filter_on_roi = false );
+
   declareProperty( "ntupleChainNames",  m_ntupleChainNames );
   declareProperty( "releaseMetaData",   m_releaseMetaData );
 
@@ -131,7 +134,8 @@ StatusCode TrigTestMonToolAC::init() {
 						      m_keepAllEvents ) );
     m_sequences.back()->releaseData(m_releaseMetaData);
     if ( m_requireDecision ) m_sequences.back()->setRequireDecision(true);
-    if ( m_mcTruth ) m_sequences.back()->setMCTruth(m_mcTruth);
+    if ( m_mcTruth )         m_sequences.back()->setMCTruth(m_mcTruth);
+    m_sequences.back()->setFilterOnRoi( m_filter_on_roi );
   }  
   
   /// NB: Do NOT create the sequences here - leave it until the book() method, since
-- 
GitLab