ATLAS Offline Software
SimHitCsvDumperAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "SimHitCsvDumperAlg.h"
6 
9 #include <GaudiKernel/SystemOfUnits.h>
10 
11 #include <fstream>
12 #include <TString.h>
13 
20  bool operator<(const TrueHitInChamb& other) const{
21  if (stationId != other.stationId) return stationId < other.stationId;
22  return simHit->genParticleLink().barcode() < other.simHit->genParticleLink().barcode();
23  }
24 };
25 
26 namespace MuonR4{
27 SimHitCsvDumperAlg::SimHitCsvDumperAlg(const std::string& name, ISvcLocator* pSvcLocator):
28  AthAlgorithm{name, pSvcLocator} {}
29 
32  ATH_CHECK(m_inSimHitKey.initialize());
33  ATH_CHECK(m_idHelperSvc.retrieve());
35  return StatusCode::SUCCESS;
36 
37 }
38 
40 
41  const EventContext & context = Gaudi::Hive::currentContext();
43  ATH_CHECK(gctxHandle.isPresent());
44  const ActsGeometryContext& gctx{*gctxHandle};
45 
46  // these are the conventions by ACTS
47  std::ofstream file{std::string(Form("event%09zu-",++m_event))+"MuonSimHit.csv"};
48  const std::string delim = ",";
49  file<<"pdgId"<<delim;
50  file<<"StationName"<<delim;
51  file<<"StationEta"<<delim;
52  file<<"StationPhi"<<delim;
53  file<<"LocalPositionExtrx"<<delim;
54  file<<"LocalPositionExtry"<<delim;
55  file<<"LocalPositionExtrz"<<delim;
56  file<<"LocalDirectionx"<<delim;
57  file<<"LocalDirectiony"<<delim;
58  file<<"LocalDirectionz"<<std::endl;
59 
60 
61  std::set<TrueHitInChamb> usedStations{};
63 
64  SG::ReadHandle<xAOD::MuonSimHitContainer> readSimHits{key, context};
65  ATH_CHECK(readSimHits.isPresent());
66 
67 
68  for (const xAOD::MuonSimHit* simHit : *readSimHits) {
69 
70  //The sim hit collection contains non-muon hits, while the fast digi only writes out muon
71  // deposits. Here remove sim hits that are not expected to be accounted for in the digi.
72 
73  if (std::abs(simHit->pdgId()) != 13) continue;
74  const Identifier ID = simHit->identify();
75 
76  TrueHitInChamb newHit{};
77  newHit.stationId = m_idHelperSvc->chamberId(ID);
78  newHit.simHit = simHit;
79  ATH_MSG_VERBOSE("Dump simulation hit for " <<m_idHelperSvc->toString(ID));
81 
82  //transform from local (w.r.t tube's frame) to global (ATLAS frame) and then to chamber's frame
83  const Amg::Transform3D toChamber = reElement->msSector()->globalToLocalTrans(gctx) *
84  reElement->localToGlobalTrans(gctx, reElement->measurementHash(ID));
85 
86  const Amg::Vector3D localPos{toChamber * xAOD::toEigen(simHit->localPosition())};
87  newHit.lDir = toChamber.linear() * xAOD::toEigen(simHit->localDirection());
88 
89  const std::optional<double> lambda = Amg::intersect<3>(localPos, newHit.lDir, Amg::Vector3D::UnitZ(), 0.);
90  newHit.lPos = localPos + (*lambda)*newHit.lDir;
91 
92  auto insert_itr = usedStations.insert(newHit);
94  if (insert_itr.second) continue;
95 
96  const TrueHitInChamb prevMuon{*insert_itr.first};
97  constexpr double tolerance = 0.1 * Gaudi::Units::millimeter;
98  double angleTolerance = std::cos(0.1*Gaudi::Units::deg);
99  if ((prevMuon.lPos - newHit.lPos).mag() > tolerance ||
100  prevMuon.lDir.dot(newHit.lDir) < angleTolerance ) {
101  ATH_MSG_WARNING("Muon "<<newHit.simHit->genParticleLink()<<" has a different ending point when it starts from "
102  <<m_idHelperSvc->toString(prevMuon.simHit->identify())<<" "<<Amg::toString(prevMuon.lPos)
103  <<" pointing to "<<Amg::toString(prevMuon.lDir)
104  <<" -- vs. "<<m_idHelperSvc->toString(newHit.simHit->identify())
105  <<" "<<Amg::toString(newHit.lPos) <<" pointing to "<<Amg::toString(newHit.lDir)<<" difference: "
106  <<(prevMuon.lPos - newHit.lPos).mag() <<" && "<< std::acos(std::min(1.,(std::max(-1.,prevMuon.lDir.dot(newHit.lDir))))) / Gaudi::Units::deg ) ;
107  }
108  }
109  }
110 
111  for (const TrueHitInChamb& hit : usedStations) {
112  file<<hit.simHit->pdgId()<<delim;
113  file<<m_idHelperSvc->stationName(hit.stationId)<<delim;
114  file<<m_idHelperSvc->stationEta(hit.stationId)<<delim;
115  file<<m_idHelperSvc->stationPhi(hit.stationId)<<delim;
116 
117  file<<hit.lPos.x()<<delim;
118  file<<hit.lPos.y()<<delim;
119  file<<hit.lPos.z()<<delim;
120 
121  file<<hit.lDir.x()<<delim;
122  file<<hit.lDir.y()<<delim;
123  file<<hit.lDir.z()<<std::endl;
124  }
125  return StatusCode::SUCCESS;
126 }
127 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
xAOD::MuonSimHit_v1
Definition: MuonSimHit_v1.h:18
MuonGMR4::MuonReadoutElement::msSector
const SpectrometerSector * msSector() const
Returns the pointer to the envelope volume enclosing all chambers in the sector.
ID
std::vector< Identifier > ID
Definition: CalibHitIDCheck.h:24
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
MuonR4::SimHitCsvDumperAlg::m_r4DetMgr
const MuonGMR4::MuonDetectorManager * m_r4DetMgr
Access to the readout geometry.
Definition: SimHitCsvDumperAlg.h:40
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
MuonR4::SimHitCsvDumperAlg::execute
StatusCode execute() override
Definition: SimHitCsvDumperAlg.cxx:39
MuonGMR4::MuonReadoutElement
The MuonReadoutElement is an abstract class representing the geometry representing the muon detector.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/MuonReadoutGeometryR4/MuonReadoutElement.h:38
deg
#define deg
Definition: SbPolyhedron.cxx:17
MuonR4::SimHitCsvDumperAlg::m_inSimHitKey
SG::ReadHandleKeyArray< xAOD::MuonSimHitContainer > m_inSimHitKey
Definition: SimHitCsvDumperAlg.h:33
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::ReadHandleKey
Property holding a SG store/key/clid from which a ReadHandle is made.
Definition: StoreGate/StoreGate/ReadHandleKey.h:39
TrueHitInChamb::simHit
const xAOD::MuonSimHit * simHit
Definition: SimHitCsvDumperAlg.cxx:19
SpectrometerSector.h
SimHitCsvDumperAlg.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
MuonR4::SimHitCsvDumperAlg::m_idHelperSvc
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
Definition: SimHitCsvDumperAlg.h:36
python.SystemOfUnits.millimeter
int millimeter
Definition: SystemOfUnits.py:53
Amg::toString
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Definition: GeoPrimitivesToStringConverter.h:40
xAOD::MuonSimHit_v1::genParticleLink
const HepMcParticleLink & genParticleLink() const
Returns the link to the HepMC particle producing this hit.
Definition: xAODMuonSimHit_V1.cxx:67
MuonR4::SimHitCsvDumperAlg::m_event
size_t m_event
Definition: SimHitCsvDumperAlg.h:42
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MuonGMR4::SpectrometerSector::globalToLocalTrans
Amg::Transform3D globalToLocalTrans(const ActsGeometryContext &gctx) const
Returns the global -> local transformation from the ATLAS global.
Definition: SpectrometerSector.cxx:54
Amg::Transform3D
Eigen::Affine3d Transform3D
Definition: GeoPrimitives.h:46
file
TFile * file
Definition: tile_monitor.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
MuonR4::SimHitCsvDumperAlg::SimHitCsvDumperAlg
SimHitCsvDumperAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: SimHitCsvDumperAlg.cxx:27
MuonR4::SimHitCsvDumperAlg::m_geoCtxKey
SG::ReadHandleKey< ActsGeometryContext > m_geoCtxKey
Definition: SimHitCsvDumperAlg.h:31
TrueHitInChamb::stationId
Identifier stationId
Definition: SimHitCsvDumperAlg.cxx:16
ActsGeometryContext
Include the GeoPrimitives which need to be put first.
Definition: ActsGeometryContext.h:27
AthAlgorithm
Definition: AthAlgorithm.h:47
tolerance
Definition: suep_shower.h:17
MuonR4::SimHitCsvDumperAlg::initialize
StatusCode initialize() override
Definition: SimHitCsvDumperAlg.cxx:30
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
TrueHitInChamb::lPos
Amg::Vector3D lPos
Definition: SimHitCsvDumperAlg.cxx:18
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
TrueHitInChamb::operator<
bool operator<(const TrueHitInChamb &other) const
Definition: SimHitCsvDumperAlg.cxx:20
MuonR4
This header ties the generic definitions in this package.
Definition: HoughEventData.h:16
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MuonGMR4::MuonReadoutElement::measurementHash
virtual IdentifierHash measurementHash(const Identifier &measId) const =0
Constructs the identifier hash from the full measurement Identifier.
MuonGMR4::MuonReadoutElement::localToGlobalTrans
const Amg::Transform3D & localToGlobalTrans(const ActsGeometryContext &ctx) const
Returns the local to global transformation into the ATLAS coordinate system.
Definition: MuonPhaseII/MuonDetDescr/MuonReadoutGeometryR4/src/MuonReadoutElement.cxx:81
TrueHitInChamb::lDir
Amg::Vector3D lDir
Definition: SimHitCsvDumperAlg.cxx:17
TrueHitInChamb
Helper struct to map all muons in the same chamber.
Definition: SimHitCsvDumperAlg.cxx:15
MuonReadoutElement.h
generate::Zero
void Zero(TH1D *hin)
Definition: generate.cxx:32
MuonGMR4::MuonDetectorManager::getReadoutElement
const MuonReadoutElement * getReadoutElement(const Identifier &id) const
Returns a generic Muon readout element.
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
Identifier
Definition: IdentifierFieldParser.cxx:14