From 83c1c220b47486305f6dc774acaf1e2fc97c9d13 Mon Sep 17 00:00:00 2001
From: Will Leight <wleight@cern.ch>
Date: Thu, 15 Jun 2017 23:20:30 +0200
Subject: [PATCH] Fix for ATLASRECTS-3199

This fixes a bug where position and direction information was not filled for truth muon segments built from CSC hits.
CSC sim hits are treated differently than MDT ones, and it seems the code to handle them correctly was simply never written.
The fixes in MuonTruthDecorationAlg::createSegments simply calculate this information for CSC segments the same way it is done for the other segments.
The result will be FT0-violating, though only for MC, of course.
Note that this violation is not visible in the Tier0 tests as q221 happens to not reconstruct any truth CSC segments.
Tier0 test results are available at /afs/cern.ch/work/w/wleight/public/MuonSW/ATLASRECTS-3199/.


Former-commit-id: 41c73845fa7d10248228f1b5fb0e4fbc8356b6b8
---
 .../src/MuonTruthDecorationAlg.cxx            | 66 +++++++++++++------
 .../src/MuonTruthDecorationAlg.h              |  5 ++
 2 files changed, 51 insertions(+), 20 deletions(-)

diff --git a/MuonSpectrometer/MuonTruthAlgs/src/MuonTruthDecorationAlg.cxx b/MuonSpectrometer/MuonTruthAlgs/src/MuonTruthDecorationAlg.cxx
index af5864e4630b..99ce5a99c321 100644
--- a/MuonSpectrometer/MuonTruthAlgs/src/MuonTruthDecorationAlg.cxx
+++ b/MuonSpectrometer/MuonTruthAlgs/src/MuonTruthDecorationAlg.cxx
@@ -23,6 +23,7 @@
 #include "TrkGeometry/TrackingVolume.h"
 #include "TrkParameters/TrackParameters.h"
 #include "EventPrimitives/EventPrimitivesHelpers.h"
+#include "MuonReadoutGeometry/CscReadoutElement.h"
 
 namespace Muon {
 
@@ -32,7 +33,8 @@ namespace Muon {
     m_idHelper("Muon::MuonIdHelperTool/MuonIdHelperTool"),
     m_printer("Muon::MuonEDMPrinterTool/MuonEDMPrinterTool"),
     m_truthClassifier("MCTruthClassifier/MCTruthClassifier"),
-    m_extrapolator("Trk::Extrapolator/AtlasExtrapolator")
+    m_extrapolator("Trk::Extrapolator/AtlasExtrapolator"),
+    m_muonMgr(0)
   {  
     
     m_trackRecordCollectionNames.push_back("CaloEntryLayer");
@@ -81,6 +83,10 @@ namespace Muon {
     ATH_CHECK(m_printer.retrieve());
     ATH_CHECK(m_truthClassifier.retrieve());
     ATH_CHECK(m_extrapolator.retrieve());
+    if (detStore()->retrieve( m_muonMgr ).isFailure()) {
+      ATH_MSG_ERROR(" Cannot retrieve MuonGeoModel ");
+      return StatusCode::FAILURE;
+    }
 
     return StatusCode::SUCCESS;
   }
@@ -261,9 +267,9 @@ namespace Muon {
                                                const MuonTruthDecorationAlg::ChamberIdMap& ids, 
 					       xAOD::MuonSegmentContainer& segmentContainer,
 					       const std::vector<const MuonSimDataCollection*>& sdoCollections,
-					       const CscSimDataCollection* /*cscCollection*/ ) const {
+					       const CscSimDataCollection* cscCollection ) const {
 
-    bool useSDO = !sdoCollections.empty(); // || cscCollection );
+    bool useSDO =( !sdoCollections.empty() || cscCollection );
     std::map<Muon::MuonStationIndex::ChIndex,int> matchMap;
     ATH_MSG_DEBUG(" Creating Truth segments " );
     // loop over chamber layers
@@ -306,7 +312,7 @@ namespace Muon {
 	    etaLayers.insert( m_idHelper->gasGap(id) );
 	  }
 	}
-	// use SDO to look-up truth position of the hit, only CSC for now
+	// use SDO to look-up truth position of the hit
 	if( useSDO ){
 	  Amg::Vector3D gpos(0.,0.,0.);
 	  bool ok = false;
@@ -319,24 +325,44 @@ namespace Muon {
 		if( gpos.perp() > 0.1 ) ok = true; // sanity check
 	      }
 	    }
-	  } 
-	  // look up successfull, calculate 
-	  if( ok ){
-	    // small comparison function
-	    auto isSmaller = [isEndcap]( const Amg::Vector3D& p1, const Amg::Vector3D& p2 ){ 
-	      if( isEndcap ) return fabs(p1.z()) < fabs(p2.z()); 
-	      else           return p1.perp() < p2.perp(); 
-	    };
-	    if( !firstPos ) firstPos  = new Amg::Vector3D(gpos);
-            else if( !secondPos ){
-	      secondPos = new Amg::Vector3D(gpos);
-	      if( isSmaller(*secondPos,*firstPos) ) std::swap(firstPos,secondPos);
-	    }else{
-	      // update position if we can increase the distance between the two positions
-	      if( isSmaller(gpos,*firstPos) )       *firstPos  = gpos;
- 	      else if( isSmaller(*secondPos,gpos) ) *secondPos = gpos;
+	    // look up successfull, calculate 
+	    if( ok ){
+	      // small comparison function
+	      auto isSmaller = [isEndcap]( const Amg::Vector3D& p1, const Amg::Vector3D& p2 ){ 
+		if( isEndcap ) return fabs(p1.z()) < fabs(p2.z()); 
+		else           return p1.perp() < p2.perp(); 
+	      };
+	      if( !firstPos ) firstPos  = new Amg::Vector3D(gpos);
+	      else if( !secondPos ){
+		secondPos = new Amg::Vector3D(gpos);
+		if( isSmaller(*secondPos,*firstPos) ) std::swap(firstPos,secondPos);
+	      }else{
+		// update position if we can increase the distance between the two positions
+		if( isSmaller(gpos,*firstPos) )       *firstPos  = gpos;
+		else if( isSmaller(*secondPos,gpos) ) *secondPos = gpos;
+	      }
 	    }
 	  }
+	  else{
+	    auto pos = cscCollection->find(id);
+	    if( pos != cscCollection->end() ) {
+	      const MuonGM::CscReadoutElement * descriptor = m_muonMgr->getCscReadoutElement(id);
+	      ATH_MSG_DEBUG("found csc sdo with "<<pos->second.getdeposits().size()<<" deposits");
+	      Amg::Vector3D locpos(0,pos->second.getdeposits()[0].second.ypos(),pos->second.getdeposits()[0].second.zpos());
+	      gpos=descriptor->localToGlobalCoords(locpos,m_idHelper->cscIdHelper().elementID(id));
+	      ATH_MSG_DEBUG("got CSC global position "<<gpos);
+	      if( !firstPos ) firstPos  = new Amg::Vector3D(gpos);
+              else if( !secondPos ){
+                secondPos = new Amg::Vector3D(gpos);
+		if(secondPos->perp()<firstPos->perp()) std::swap(firstPos,secondPos);
+	      }
+	      else{
+		if( gpos.perp()<firstPos->perp() )       *firstPos  = gpos;
+                else if( secondPos->perp()<gpos.perp() ) *secondPos = gpos;
+              }
+	    }
+	  }
+
 	}
       }
       if( precLayers.size() > 2 ){
diff --git a/MuonSpectrometer/MuonTruthAlgs/src/MuonTruthDecorationAlg.h b/MuonSpectrometer/MuonTruthAlgs/src/MuonTruthDecorationAlg.h
index 1ea087c5fdf4..cb8e02593c96 100755
--- a/MuonSpectrometer/MuonTruthAlgs/src/MuonTruthDecorationAlg.h
+++ b/MuonSpectrometer/MuonTruthAlgs/src/MuonTruthDecorationAlg.h
@@ -34,6 +34,10 @@ class IMCTruthClassifier;
 class MuonSimDataCollection;
 class CscSimDataCollection;
 
+namespace MuonGM {
+  class MuonDetectorManager;
+}
+
 namespace Muon {
 
 class MuonTruthDecorationAlg : public AthAlgorithm  {
@@ -72,6 +76,7 @@ private:
   ToolHandle<Muon::MuonEDMPrinterTool>  m_printer;
   ToolHandle<IMCTruthClassifier>        m_truthClassifier;
   ToolHandle<Trk::IExtrapolator>        m_extrapolator;
+  const MuonGM::MuonDetectorManager * m_muonMgr;
   bool m_createTruthSegment;
   std::string m_muonTruthSegmentContainerName;
   int m_barcodeOffset;
-- 
GitLab