From 9593ebf1a4eef97411e0381d5d3b4ac7e4a27f59 Mon Sep 17 00:00:00 2001
From: Alejandro Alonso Diaz <alejandro.alonso@cern.ch>
Date: Tue, 22 Nov 2016 11:43:08 +0100
Subject: [PATCH] ATLASRECTS-3738 fix (PixelDigitization-02-03-22)

	* Fixing Random Genenrators for Bichsel model: ATLASRECTS-3738
	* Tagging PixelDigitization-02-03-22.

2016-11-19  scott snyder  <snyder@bnl.gov>
	* Tagging PixelDigitization-02-03-21.
	* endreq -> endmsg.

2016-11-17  John Chapman  <John.Chapman@cern.ch>

	* python/PixelDigitizationConfig.py (PixelOverlayDigitizationTool):
	Specify output StorGate instance for RDOCollNameSPM. ATLASSIM-3026
	* Tagged as PixelDigitization-02-03-20

2016-11-02  John Chapman  <John.Chapman@cern.ch>
	* python/PixelDigitizationConfig.py (PixelOverlayDigitizationTool):
	Specify output StoreGate instance in RDOCollName and SDOCollName
	properties - necessary now that these are configuring
	WriteHandles. obo Tatyana Kharlamova. ATLASSIM-3026
	* Taggged as PixelDigitization-02-03-19


Former-commit-id: 8b93a66d5f7fcf4ab11766d8bf5684ad0bfd07a4
---
 .../python/PixelDigitizationConfig.py         |  5 +++
 .../PixelDigitization/src/BichselSimTool.cxx  | 34 +++++++++++++++----
 .../PixelDigitization/src/BichselSimTool.h    | 15 ++++++--
 .../src/PixelDigitizationTool.cxx             |  2 +-
 .../src/PixelNoisyCellGenerator.cxx           |  4 +--
 5 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/InnerDetector/InDetDigitization/PixelDigitization/python/PixelDigitizationConfig.py b/InnerDetector/InDetDigitization/PixelDigitization/python/PixelDigitizationConfig.py
index 7c7dac77ec8..47502a73776 100644
--- a/InnerDetector/InDetDigitization/PixelDigitization/python/PixelDigitizationConfig.py
+++ b/InnerDetector/InDetDigitization/PixelDigitization/python/PixelDigitizationConfig.py
@@ -54,6 +54,8 @@ def BichselSimTool(name="BichselSimTool", **kwargs):
     kwargs.setdefault("DeltaRayCut", 117.)
     kwargs.setdefault("nCols", 5)
     kwargs.setdefault("LoopLimit", 100000)
+    kwargs.setdefault("RndmSvc", digitizationFlags.rndmSvc())
+    kwargs.setdefault("RndmEngine", "PixelDigitization")
     return CfgMgr.BichselSimTool(name, **kwargs)
 
 def PixelBarrelBichselChargeTool(name="PixelBarrelBichselChargeTool", **kwargs):
@@ -286,6 +288,9 @@ def PixelDigitizationToolSplitNoMergePU(name="PixelDigitizationToolSplitNoMergeP
 def PixelOverlayDigitizationTool(name="PixelOverlayDigitizationTool",**kwargs):
     from OverlayCommonAlgs.OverlayFlags import overlayFlags
     kwargs.setdefault("EvtStore", overlayFlags.evtStore())
+    kwargs.setdefault("RDOCollName", overlayFlags.evtStore()+"/PixelRDOs")
+    kwargs.setdefault("RDOCollNameSPM", overlayFlags.evtStore()+"/PixelRDOs_SPM")
+    kwargs.setdefault("SDOCollName", overlayFlags.evtStore()+"/PixelSDO_Map")
     kwargs.setdefault("HardScatterSplittingMode", 0)
     return BasicPixelDigitizationTool(name,**kwargs)
 
diff --git a/InnerDetector/InDetDigitization/PixelDigitization/src/BichselSimTool.cxx b/InnerDetector/InDetDigitization/PixelDigitization/src/BichselSimTool.cxx
index 5f9d28bdf95..0d156fd84dd 100644
--- a/InnerDetector/InDetDigitization/PixelDigitization/src/BichselSimTool.cxx
+++ b/InnerDetector/InDetDigitization/PixelDigitization/src/BichselSimTool.cxx
@@ -15,12 +15,14 @@
 #include "TGraph.h"
 #include "TString.h"
 #include "TMath.h"
-#include "TRandom3.h"
 
 #include "PathResolver/PathResolver.h"
 #include <fstream>
 #include <cmath>
 
+#include "AtlasCLHEP_RandomGenerators/RandExpZiggurat.h"
+#include "CLHEP/Random/RandFlat.h"
+
 using namespace std;
 
 static const InterfaceID IID_IBichselSimTool("BichselSimTool", 1, 0);
@@ -28,13 +30,18 @@ const InterfaceID& BichselSimTool::interfaceID( ){ return IID_IBichselSimTool; }
 
 // Constructor with parameters:
 BichselSimTool::BichselSimTool(const std::string& type, const std::string& name,const IInterface* parent):
-  AthAlgTool(type,name,parent)
+  AthAlgTool(type,name,parent),
+  m_rndmSvc("AtDSFMTGenSvc",name),
+  m_rndmEngineName("PixelDigitization"),
+  m_rndmEngine(0)
 { 
 	declareInterface< BichselSimTool >( this );
 
   declareProperty("DeltaRayCut", m_DeltaRayCut = 117.);
   declareProperty("nCols", m_nCols = 1);
   declareProperty("LoopLimit", m_LoopLimit = 100000);
+  declareProperty("RndmSvc", m_rndmSvc, "Random Number Service used in BichselSimTool");
+  declareProperty("RndmEngine", m_rndmEngineName, "Random engine name");
 }
 
 // Destructor:
@@ -55,8 +62,23 @@ StatusCode BichselSimTool::initialize() {
   
   //** define your initialize below **//
 
-  // random seed
-  m_RandomGenerator = new TRandom3(0);
+  // random svc
+  if ( m_rndmSvc.retrieve().isFailure() ) {
+    ATH_MSG_ERROR ( " Can't get RndmSvc " );
+    return StatusCode::FAILURE;
+  } else { 
+    ATH_MSG_DEBUG ( "Retrieved RndmSvc" );
+  }
+
+  // get the random stream
+  ATH_MSG_DEBUG ( "Getting random number engine : <" << m_rndmEngineName << ">" );
+  m_rndmEngine = m_rndmSvc->GetEngine(m_rndmEngineName);
+  if (m_rndmEngine==0) {
+    ATH_MSG_ERROR ( "Could not find RndmEngine : " << m_rndmEngineName );
+    return StatusCode::FAILURE;
+  } else { 
+    ATH_MSG_DEBUG ( " Found RndmEngine : " << m_rndmEngineName ); 
+  }
 
   // clear data table
   m_BichselData.clear();
@@ -214,7 +236,7 @@ std::vector<std::pair<double,double> > BichselSimTool::BichselSim(double BetaGam
 
     // sample hit position -- exponential distribution
     double HitPosition = 0.;
-    for(int iHit = 0; iHit < m_nCols; iHit++) HitPosition += m_RandomGenerator->Exp(lambda);
+    for(int iHit = 0; iHit < m_nCols; iHit++) HitPosition += CLHEP::RandExpZiggurat::shoot(m_rndmEngine, lambda);
 
     // termination by hit position
     // yes, in case m_nCols > 1, we will loose the last m_nCols collisions. So m_nCols cannot be too big
@@ -225,7 +247,7 @@ std::vector<std::pair<double,double> > BichselSimTool::BichselSim(double BetaGam
     double TossEnergyLoss = -1.;
     // double TossIntX_record;
     while(TossEnergyLoss <= 0.){ // we have to do this because sometimes TossEnergyLoss will be negative due to too small TossIntX
-      double TossIntX = m_RandomGenerator->Uniform(0., IntXUpperBound);
+      double TossIntX = CLHEP::RandFlat::shoot(m_rndmEngine, 0., IntXUpperBound);
       TossEnergyLoss = GetColE(indices_BetaGammaLog10, TMath::Log10(TossIntX), iData);
 
       // TossIntX_record = TossIntX;
diff --git a/InnerDetector/InDetDigitization/PixelDigitization/src/BichselSimTool.h b/InnerDetector/InDetDigitization/PixelDigitization/src/BichselSimTool.h
index 988073bbd01..9af08a336bd 100644
--- a/InnerDetector/InDetDigitization/PixelDigitization/src/BichselSimTool.h
+++ b/InnerDetector/InDetDigitization/PixelDigitization/src/BichselSimTool.h
@@ -20,11 +20,15 @@
 #ifndef PIXELDIGITIZATION_BichselSimTool_H
 #define PIXELDIGITIZATION_BichselSimTool_H
 
+#include "GaudiKernel/ServiceHandle.h"
 #include "AthenaBaseComps/AthAlgTool.h"
-
+#include "CLHEP/Random/RandomEngine.h"
+#include "AthenaKernel/IAtRndmGenSvc.h"
 
 // forward class declaration
-class TRandom3;
+namespace CLHEP{
+  class HepRandomEngine;
+}
 
 // internal data structure for storage purpose
 struct BichselData
@@ -67,10 +71,15 @@ private:
   // internal private members //
   double                   m_DeltaRayCut;      // Threshold to identify a delta ray. unit in keV
   std::vector<BichselData> m_BichselData;      // vector to store Bichsel Data. Each entry is for one particle type
-  TRandom3*                m_RandomGenerator;  // Random number generator
   int                      m_nCols;            // number of collisions to simulate each time. This is mainly to save CPU time if necessary
   int                      m_LoopLimit;        // upper limit on number of loops. The default value is optimized for current configuration. People can tune this number in case of ITK upgrade (very forward barrel) or other new situation.
 
+protected:
+  ServiceHandle<IAtRndmGenSvc> m_rndmSvc;
+  std::string                  m_rndmEngineName;
+  CLHEP::HepRandomEngine*      m_rndmEngine;
+
+private:
   // internal private functions //
   std::pair<int,int> FastSearch(std::vector<double> vec, double item) const;               // A quick implementation of binary search in 2D table
   std::pair<int,int> GetBetaGammaIndices(double BetaGammaLog10, BichselData& iData) const; // get beta-gamma index. This is so commonly used by other functions that a caching would be beneficial
diff --git a/InnerDetector/InDetDigitization/PixelDigitization/src/PixelDigitizationTool.cxx b/InnerDetector/InDetDigitization/PixelDigitization/src/PixelDigitizationTool.cxx
index fdd506a846b..6d60c3d92fe 100644
--- a/InnerDetector/InDetDigitization/PixelDigitization/src/PixelDigitizationTool.cxx
+++ b/InnerDetector/InDetDigitization/PixelDigitization/src/PixelDigitizationTool.cxx
@@ -1187,7 +1187,7 @@ StatusCode PixelDigitizationTool::processBunchXing(int bunchXing,
 
     const SiHitCollection* seHitColl(0);
     if (!seStore.retrieve(seHitColl,m_inputObjectName).isSuccess()) {
-      msg(MSG::ERROR) << "SubEvent Pixel SiHitCollection not found in StoreGate " << seStore.name() << endreq;
+      msg(MSG::ERROR) << "SubEvent Pixel SiHitCollection not found in StoreGate " << seStore.name() << endmsg;
       return StatusCode::FAILURE;
     }
     ATH_MSG_DEBUG("SiHitCollection found with " << seHitColl->size() << " hits");
diff --git a/InnerDetector/InDetDigitization/PixelDigitization/src/PixelNoisyCellGenerator.cxx b/InnerDetector/InDetDigitization/PixelDigitization/src/PixelNoisyCellGenerator.cxx
index 94dc1426b51..5900fa6efbd 100644
--- a/InnerDetector/InDetDigitization/PixelDigitization/src/PixelNoisyCellGenerator.cxx
+++ b/InnerDetector/InDetDigitization/PixelDigitization/src/PixelNoisyCellGenerator.cxx
@@ -86,13 +86,13 @@ StatusCode PixelNoisyCellGenerator::initialize() {
 
   std::string pixelHelperName("PixelID");
   if ( StatusCode::SUCCESS!= detStore()->retrieve(m_pixelID,pixelHelperName) ) {
-    msg(MSG::FATAL) << "Pixel ID helper not found" << endreq;
+    msg(MSG::FATAL) << "Pixel ID helper not found" << endmsg;
     return StatusCode::FAILURE;
   }
 
   std::string managerName("Pixel");
   if ( StatusCode::SUCCESS!= detStore()->retrieve(m_pixMgr,managerName) ) {
-    msg(MSG::FATAL) << "PixelDetectorManager not found" << endreq;
+    msg(MSG::FATAL) << "PixelDetectorManager not found" << endmsg;
     return StatusCode::FAILURE;
   }
 
-- 
GitLab