7#include "CLHEP/Units/SystemOfUnits.h"
8#include "CLHEP/Random/RandomEngine.h"
9#include "CLHEP/Random/RandFlat.h"
10#include "CLHEP/Random/RandGaussZiggurat.h"
11#include "CLHEP/Random/RandGamma.h"
14#include "Acts/Utilities/detail/Polynomials.hpp"
32 bool doPadChargeSharing)
53 return StatusCode::SUCCESS;
69 <<
" mclink " <<
hit->genParticleLink() <<
" PDG ID " <<
hit->pdgId() );
72 const double scale =
Amg::intersect<3>(locHitPos, locHitDir, Amg::Vector3D::UnitZ(), 0.).value_or(0);
73 const double halfStep = 0.5 *
hit->stepLength();
77 if (std::abs(scale) <= halfStep) {
78 hitOnWireSurf = locHitPos + scale * locHitDir;
81 hitOnWireSurf = locHitPos + Acts::copySign(halfStep, scale) * locHitDir;
86 ReadoutChannelType::Wire, 1);
98 std::pair<int, int> wireGrpWireNum = wireDesign.
wireNumber(hitOnWire2D);
101 if (wireGrpWireNum.first < 0) {
103 wireGrpWireNum = wireDesign.
wireNumber(locHitPos2D);
106 if (wireGrpWireNum.first < 0) {
107 const Amg::Vector3D preStepPos = locHitPos - halfStep * locHitDir;
109 wireGrpWireNum = wireDesign.
wireNumber(preStepPos2D);
112 if (wireGrpWireNum.first < 0) {
113 const Amg::Vector3D postStepPos = locHitPos + halfStep * locHitDir;
115 wireGrpWireNum = wireDesign.
wireNumber(postStepPos2D);
118 if (wireGrpWireNum.first < 0) {
119 ATH_MSG_WARNING(__func__<<
"() "<<__LINE__<<
" - Unable to retrieve the wire number, skipping the hit: "
126 int wireNumber = wireDesign.
numPitchesToGroup(wireGrpWireNum.first) + wireGrpWireNum.second;
127 const int numWires = wireDesign.
nAllWires();
129 if((wireNumber < 1) || (wireNumber > numWires)) {
130 ATH_MSG_WARNING(__func__<<
"() "<<__LINE__<<
" - Unable to retrieve the wire number, skipping the hit: "
137 double distToWire = ionization.
distance;
139 if(distToWire > 0.) {
144 double distToWireAdj = ionizationAdj.
distance;
146 if ((distToWireAdj > 0.) && (distToWireAdj < distToWire)) {
147 distToWire = distToWireAdj;
148 wireNumber += adjacent;
149 ionization = std::move(ionizationAdj);
158 ATH_MSG_DEBUG(
"Distance to nearest wire: " << distToWire <<
" greater than wirePitch.");
166 const double t0_par = most_prob_time - gamma_mpv;
169 double digitTime = t0_par + CLHEP::RandGamma::shoot(condContainers.
rndEngine, gamParam.
kParameter, inv_theta);
171 constexpr unsigned shoot_limit = 4;
172 unsigned shoot_counter = 0;
173 while (digitTime < 0. && ++shoot_counter <= shoot_limit) {
174 digitTime = t0_par + CLHEP::RandGamma::shoot(condContainers.
rndEngine, gamParam.
kParameter,inv_theta);
177 ionization.
time = std::max(0., digitTime);
186 const double ionized_charge = (5.65E-6) * energyDeposit / CLHEP::keV;
191 return gain * ionized_charge;
201 const double energyDeposit =
hit->energyDeposit();
202 if (energyDeposit < std::numeric_limits<float>::epsilon()) {
219 <<
" energyDeposit "<< energyDeposit );
230 digiInput.
hitId = hitId;
234 digiInput.
hitDir = xAOD::toEigen(
hit->localDirection());
243 <<
" EDep: " << energyDeposit
255 std::ranges::move(stripDigits, std::back_inserter(allDigits));
262 std::ranges::move(padDigits, std::back_inserter(allDigits));
273 std::ranges::move(wireDigits, std::back_inserter(allDigits));
287 ReadoutChannelType::Strip, 1);
299 constexpr double tolerance_length = 0.01*Gaudi::Units::mm;
300 int stripNumber = stripDesign.
stripNumber(hitOnStripSurf);
301 if( stripNumber < 0){
302 const double newPosX = hitOnStripSurf.x() - std::copysign(tolerance_length, hitOnStripSurf.x());
305 if (stripNumber < 0) {
312 const double tan_theta = digiInput.
hitDir.perp() / digiInput.
hitDir.z();
315 const double peak_position = CLHEP::RandGaussZiggurat::shoot(condContainers.
rndEngine,
317 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
" Smeared hit from "<<hitOnStripSurf.x()<<
" -> "<<
318 peak_position<<
". Assign strip charges");
327 const double peak_position,
328 const int stripNumber)
const {
333 const double tan_theta = digiInput.
hitDir.perp() / digiInput.
hitDir.z();
335 constexpr double tolerance_charge = 0.0005;
336 constexpr int max_neighbor = 10;
341 for (
int sign : {-1, 1}) {
342 for (
int iStrip = 0; iStrip <= max_neighbor; ++iStrip) {
344 if (iStrip == 0 &&
sign == +1)
continue;
345 int currentStrip = stripNumber +
sign*iStrip;
346 if (currentStrip > design.numStrips() || currentStrip < 1) {
347 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
" Breaking the upper half strip loop stripNumber: "
348 << currentStrip <<
" > " << design.numStrips());
354 gasGap, ReadoutChannelType::Strip,
360 const double x_relative = (currentStripPos.x() - peak_position);
361 const double normX = x_relative / design.stripPitch();
365 if (
charge < tolerance_charge) {
366 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
" Breaking the upper half strip loop stripCharge: "
367 <<
charge <<
" < " << tolerance_charge);
371 double strip_time = digiInput.
time;
373 const bool shift = ((iStrip > 0) && ((x_relative + (0.5*0.75 - iStrip) * design.stripPitch()) < 0));
378 ATH_MSG_VERBOSE(__func__<<
"() - "<<__LINE__<<
" Created a strip digit: strip number = "
379 << currentStrip <<
", charge = " <<
charge);
398 ATH_MSG_DEBUG(__func__<<
"() -"<<__LINE__<<
" Outside of the pad surface boundary :"
404 constexpr double tolerance_length = 0.01*Gaudi::Units::mm;
405 auto [padEta, padPhi] = padDesign.
channelNumber(hitOnPadSurf);
407 if( padEta < 1 || padPhi < 1){
408 const double newPosX = hitOnPadSurf.x() - std::copysign(tolerance_length, hitOnPadSurf.x());
409 const double newPosY = hitOnPadSurf.y() - std::copysign(tolerance_length, hitOnPadSurf.y());
411 auto [newPadEta, newPadPhi] = padDesign.
channelNumber(newPosition);
414 if( padEta < 1 || padPhi < 1) {
428 const int padPhi)
const {
434 gasGap, ReadoutChannelType::Pad, padEta, padPhi,
isValid);
447 const double halfPadHeight = 0.5 * digiInput.
reEle->
padHeight(padHitHash);
449 const std::array<Amg::Vector2D, 4> padHitCorners = padDesign.
padCorners(std::make_pair(padEta, padPhi));
450 const double padBottomBase = (padHitCorners[0] - padHitCorners[1]).norm();
451 const double padTopBase = (padHitCorners[2] - padHitCorners[3]).norm();
452 const double halfPadWidth = 0.5 * padBottomBase + 0.25 * (1 +
diff.y()/halfPadHeight) * (padTopBase - padBottomBase);
454 double deltaX = halfPadWidth - std::abs(
diff.x());
455 double deltaY = halfPadHeight - std::abs(
diff.y());
467 unsigned newPhi = padPhi - Acts::copySign(1,
diff.x());
468 unsigned newEta = padEta + Acts::copySign(1,
diff.y());
469 bool validEta = newEta > 0 && newEta <= digiInput.
reEle->
numPadEta(padHitHash);
470 bool validPhi = newPhi > 0 && newPhi <= digiInput.
reEle->
numPadPhi(padHitHash);
472 if (isNeighX && isNeighY && validEta && validPhi) {
474 gasGap, ReadoutChannelType::Pad,
477 ReadoutChannelType::Pad, newEta, padPhi);
479 ReadoutChannelType::Pad, newEta, newPhi);
486 addDigit(digits, padHitId, digiInput.
time, (1.-xQfraction-yQfraction+xQfraction*yQfraction)*0.5*digiInput.
totalCharge);
487 }
else if (isNeighX && validPhi){
489 ReadoutChannelType::Pad, padEta, newPhi);
494 else if (isNeighY && validEta){
496 ReadoutChannelType::Pad, newEta, padPhi);
516 ReadoutChannelType::Wire, 1);
532 constexpr double tolerance_length = 0.01*Gaudi::Units::mm;
533 int wiregroupNumber = (wireDesign.
wireNumber(hitOnWireSurf)).first;
534 if( wiregroupNumber < 1) {
535 const double newPosX = hitOnWireSurf.x() - std::copysign(tolerance_length, hitOnWireSurf.x());
537 wiregroupNumber = (wireDesign.
wireNumber(newPos)).first;
538 if (wiregroupNumber < 1) {
546 ReadoutChannelType::Wire, wiregroupNumber,
isValid);
563 const double stepLength)
const {
565 constexpr double angular_tolerance = 1e-3;
584 const double wirePitch = wireDesign.
stripPitch();
585 const double wirePosX = wireDesign.
firstStripPos().x() + (wireNumber - 1) * wirePitch;
590 std::optional<double> scaleHit =
Amg::intersect<3>(locHitPos, locHitDir, Amg::Vector3D::UnitZ(), 0);
591 if (!scaleHit || std::abs(std::abs(wireDir.dot(locHitDir)) - 1.0) < angular_tolerance) {
592 ATH_MSG_DEBUG(
"The track segment is parallel to the wire, position of digit is undefined");
595 ionization.
distance = std::hypot(locHitPos.y() + wirePosX, locHitPos.z());
596 if (ionization.
distance > wirePitch) {
603 Amg::Vector3D ionizationPos = locHitPos + scaleHit.value() * locHitDir;
605 if (scaleHit.value() > stepLength) {
607 const Amg::Vector3D closestPointToWirePlane = locHitPos + stepLength * locHitDir;
609 ionization.
distance = std::abs(closestPointToWirePlane.z());
614 const double scaleWire = (ionizationPos - wirePos).
dot(wireDir);
615 const Amg::Vector3D closestPointOnWire = wirePos + scaleWire * wireDir;
619 ionization.
posOnWire = closestPointOnWire;
620 ionization.
distance = (ionizationPos - closestPointOnWire).
mag();
630 const double digittime,
634 if (!std::ranges::any_of(digits, [&](std::unique_ptr<sTgcDigit>& known) {
635 return known->identify() ==
id && std::abs(digittime - known->time()) <
tolerance;
637 digits.push_back(std::make_unique<sTgcDigit>(
id, 0, digittime,
charge, 0, 0));
645 const std::string file_name =
"sTGC_Digitization_timeArrival.dat";
647 if(file_path.empty()) {
648 ATH_MSG_FATAL(
"readFileOfTimeWindowOffset(): Could not find file " << file_name );
649 return StatusCode::FAILURE;
652 std::ifstream ifs{file_path, std::ios::in};
654 ATH_MSG_FATAL(
"sTgcDigitMaker: Failed to open time of arrival file " << file_name );
655 return StatusCode::FAILURE;
661 while (std::getline(ifs, line)) {
663 std::istringstream iss(line);
668 }
else if (key ==
"mpv") {
675 return StatusCode::SUCCESS;
682 const double d = std::abs(distance);
689 if (d < par.lowEdge) {
709 double term1 = 0.25 * std::erf( M / (std::sqrt(2) *
m_clusterParams[0]));
711 double term3 = 0.25 * std::erf( M / (std::sqrt(2) *
m_clusterParams[1]));
714 return (term1 - term2 + term3 - term4);
720 const std::string file_name =
"sTGC_Digitization_timeOffsetStrip.dat";
722 if(file_path.empty()) {
723 ATH_MSG_FATAL(
"readFileOfTimeWindowOffset(): Could not find file " << file_name );
724 return StatusCode::FAILURE;
728 std::ifstream ifs{file_path, std::ios::in};
730 ATH_MSG_FATAL(
"Failed to open time of arrival file " << file_name );
731 return StatusCode::FAILURE;
741 while (std::getline(ifs, line)) {
743 std::istringstream iss(line);
745 if (key ==
"strip") {
746 iss >>
index >> value;
751 return StatusCode::SUCCESS;
772 return 0.5 * (1.0 - std::erf( distance / (std::sqrt(2) *
m_clusterParams[1])));
Scalar mag() const
mag method
#define ATH_CHECK
Evaluate an expression and check for errors.
bool isValid() const
Test to see if the link can be dereferenced.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_FATAL(x,...)
double charge(const T &p)
void diff(const TrigInDetTrack &a, const TrigInDetTrack &b, std::map< std::string, double > &variableChange)
comparison with feedback Function compares two objects and returns "semi verbose" output in the form ...
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
This is a "hash" representation of an Identifier.
std::pair< int, int > channelNumber(const Amg::Vector2D &hitPos) const
Function to retrieve the pad eta and phi given a local position coordinate.
const Amg::Vector2D & firstStripPos() const
Vector indicating the first strip position.
double stripPitch() const
Distance between two adjacent strips.
bool insideTrapezoid(const Amg::Vector2D &extPos) const
Checks whether an external point is inside the trapezoidal area.
virtual int stripNumber(const Amg::Vector2D &pos) const
Calculates the number of the strip whose center is closest to the given point.
const Amg::Vector2D & stripDir() const
Vector pointing along the strip.
The StripLayer interfaces the 2D description of the strip plane layout with the 3D description of the...
const StripDesign & design(bool phiView=false) const
Returns the underlying strip design.
Amg::Vector2D to2D(const Amg::Vector3D &vec, const bool phiView) const
Transforms a 3D vector from the strip design into a 2D vector.
Amg::Vector3D to3D(CheckVector2D &&vec, const bool phiView) const
Transforms the 2D vector from the strip design into a 3D vector If phi view is switched on,...
unsigned int numPitchesToGroup(unsigned int groupNum) const
Returns the number of wire pitches to reach the given group.
std::pair< int, int > wireNumber(const Amg::Vector2D &extPos) const
Returns a pair where the first component indicate the wire group number and the second one returns th...
unsigned int nAllWires() const
Returns the number of all wires.
IdentifierHash measurementHash(const Identifier &measId) const override final
Constructs the identifier hash from the full measurement Identifier.
const PadDesign & padDesign(const IdentifierHash &measHash) const
Retrieves the readoutElement Layer given the Identifier/Hash.
double padHeight(const IdentifierHash &measHash) const
Returns the height of all the pads that are not adjacent to the bottom edge of the trapezoid active a...
int multilayer() const
Returns the multilayer of the sTgcReadoutElement.
Identifier measurementId(const IdentifierHash &measHash) const override final
Converts the measurement hash back to the full Identifier.
const StripDesign & stripDesign(const IdentifierHash &measHash) const
Retrieves the readoutElement Layer given the Identifier/Hash.
bool isEtaZero(const IdentifierHash &measurementHash, const Amg::Vector2D &localPosition) const
unsigned numPadEta(const IdentifierHash &measHash) const
Returns the number of pads in the eta direction in the given layer.
unsigned numPadPhi(const IdentifierHash &measHash) const
Returns the number of pads in the Phi direction in the given gasGap layer.
Amg::Vector2D localChannelPosition(const IdentifierHash &measHash) const
Returns the local position of the measurement in the repsective frame.
const WireGroupDesign & wireDesign(const IdentifierHash &measHash) const
Retrieves the readoutElement Layer given the Identifier/Hash.
static IdentifierHash createHash(const unsigned gasGap, const unsigned channelType, const unsigned channel, const unsigned wireInGrp=0)
Create a measurement hash from the Identifier fields.
const StripLayer & stripLayer(const IdentifierHash &measId) const
std::vector< std::unique_ptr< sTgcDigit > > sTgcDigitVec
Digitize a given hit.
sTgcDigitVec processWireDigitization(const DigiInput &digiInput) const
Processes wire digitization for a given hit.
const Muon::IMuonIdHelperSvc * m_idHelperSvc
StatusCode readFileOfTimeOffsetStrip()
Reads strip time offset data file.
digitMode
Constructor initializing digitization parameters.
std::vector< GammaParameter > m_gammaParameter
std::array< double, 5 > m_mostProbableArrivalTime
static constexpr std::array< double, 2 > m_clusterParams
sTgcDigitVec processPadDigitization(const DigiInput &digiInput) const
Processes pad digitization for a given hit.
static double getPadChargeFraction(double distance)
Computes charge fraction shared among pads.
double getTimeOffsetStrip(size_t neighbor_index) const
Gets the time offset for a strip cluster.
bool getIonizationPoint(const TimedHit &hit, const DigiConditions &condContainers, Ionization &ionization) const
Computes the ionization point for a hit.
digitMode m_digitMode
define offsets and widths of time windows for signals from wiregroups and strips.
double chargeIntegral(double N, double M) const
GammaParameter getGammaParameter(double distance) const
Retrieves gamma distribution parameters based on distance.
static void addDigit(sTgcDigitVec &digits, const Identifier &id, double digittime, double charge)
Adds a digit to the appropriate cache.
sTgcDigitVec processStripChargeSharing(const DigiInput &digiInput, const double peak_position, const int stripNumber) const
Handles charge sharing for strip clusters.
StatusCode initialize()
Initialize digitization parameters, including reading necessary data files.
sTgcDigitMaker(const MuonGMR4::MuonDetectorManager *detMgr, digitMode mode, double meanGasGain, bool doPadChargeSharing)
sTgcDigitVec processStripDigitization(const DigiConditions &condContainers, const DigiInput &digiInput) const
Processes strip digitization for a given hit.
double getMostProbableArrivalTime(double distance) const
Computes the most probable arrival time based on the distance of closest approach.
const sTgcIdHelper & m_idHelper
double m_chargeAngularFactor
double calculateTotalCharge(double energyDeposit, CLHEP::HepRandomEngine *rndEngine) const
Calculates total charge from energy deposit, including gas gain.
Ionization pointClosestApproach(const MuonGMR4::StripLayer &stripLayer, int wireNumber, const Amg::Vector3D &locHitPos, const Amg::Vector3D &locHitDir, const double stepLength) const
Computes the closest approach between a trajectory and a wire segment.
sTgcDigitVec processPadChargeSharing(const DigiInput &digiInput, const int padEta, const int padPhi) const
Handles charge sharing for pad clusters.
sTgcDigitVec executeDigi(const DigiConditions &condContainers, const TimedHit &hit) const
TimedHitPtr< xAOD::MuonSimHit > TimedHit
std::array< double, 6 > m_timeOffsetStrip
virtual ~sTgcDigitMaker()
Destructor.
StatusCode readFileOfTimeArrival()
Reads time arrival data file.
const MuonGMR4::MuonDetectorManager * m_detMgr
double getEfficiency(const Identifier &channelId, bool isInnerQ1=false) const
Returns the signal generation efficiency of the sTgc channel.
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
void efficiency(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
std::optional< double > intersect(const AmgVector(N)&posA, const AmgVector(N)&dirA, const AmgVector(N)&posB, const AmgVector(N)&dirB)
Calculates the point B' along the line B that's closest to a second line A.
std::string toString(const Translation3D &translation, int precision=4)
GeoPrimitvesToStringConverter.
Eigen::Matrix< double, 2, 1 > Vector2D
Eigen::Matrix< double, 3, 1 > Vector3D
This header ties the generic definitions in this package.
sTgcDigitMaker::sTgcDigitVec sTgcDigitVec
Holds necessary conditions and data for digitization.
const Muon::DigitEffiData * efficiencies
CLHEP::HepRandomEngine * rndEngine
Stores gamma distribution parameters for estimating digit time.
Holds information about ionization points in the gas volume.
Amg::Vector3D posOnSegment