ATLAS Offline Software
TRTActiveCondAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <cmath>
6 
7 
8 
9 #include "TRTActiveCondAlg.h"
12 
14  , ISvcLocator* pSvcLocator )
15  : ::AthReentrantAlgorithm(name,pSvcLocator)
16 { }
18 
20 {
21  // Straw status
22  ATH_CHECK ( m_strawStatus.retrieve() );
23 
24  // Read key
25  ATH_CHECK( m_strawReadKey.initialize() );
26 
27  // Register write handle
29 
30  // Initialize readCondHandle key
32 
33  // TRT ID helper
34  ATH_CHECK(detStore()->retrieve(m_trtId,"TRT_ID"));
35 
36  return StatusCode::SUCCESS;
37 }
38 
39 StatusCode TRTActiveCondAlg::execute(const EventContext& ctx) const
40 {
41  ATH_MSG_DEBUG("execute " << name());
42 
43  // ____________ Construct Write Cond Handle and check its validity ____________
44 
46 
47  // Do we have a valid Write Cond Handle for current time?
48  if(writeHandle.isValid()) {
49  ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
50  << ". In theory this should not be called, but may happen"
51  << " if multiple concurrent events are being processed out of order.");
52 
53  return StatusCode::SUCCESS;
54  }
55 
56 
57 
58  // ____________ Construct new Write Cond Object ____________
59  std::unique_ptr<TRTCond::ActiveFraction> writeCdo{std::make_unique<TRTCond::ActiveFraction>()};
60 
61 
62  // ____________ Compute number of alive straws for Write Cond object ____________
63 
64 
65 
66  ATH_MSG_INFO(" Initialize TRTCond::ActiveFraction table with number of phi, eta bins: "
67  << writeCdo->getPhiBins().size() << ", " << writeCdo->getEtaBins().size() );
68 
69  for (unsigned int i=0; i<writeCdo->getEtaBins().size(); i++) {
70  float etaMin = writeCdo->getEtaBins()[i].first;
71  float etaMax = writeCdo->getEtaBins()[i].second;
72  int bin = writeCdo->findEtaBin( (etaMin+etaMax)/2. );
73  ATH_MSG_DEBUG("TRTCond::ActiveFraction: findEtaBin( " << etaMin << ", " << etaMax << " ] = " << bin );
74 
75  for (unsigned int j=0; j<writeCdo->getPhiBins().size(); j++) {
76  float phiMin = writeCdo->getPhiBins()[j].first;
77  float phiMax = writeCdo->getPhiBins()[j].second;
78  int bin = writeCdo->findPhiBin( (phiMin+phiMax)/2. );
79  ATH_MSG_DEBUG("TRTCond::ActiveFraction: findPhiBin( " << phiMin << ", " << phiMax << " ] = " << bin );
80  }
81  }
82 
83  std::vector<int> dummyPhiVec( writeCdo->getPhiBins().size(), 0 );
84  std::vector<std::vector<int> > dummyTableCountAll( writeCdo->getEtaBins().size(), dummyPhiVec );
85  std::vector<std::vector<int> > dummyTableCountDead( writeCdo->getEtaBins().size(), dummyPhiVec );
86 
88  const InDetDD::TRT_DetElementCollection* elements(trtDetEleHandle->getElements());
89  if (not trtDetEleHandle.isValid() or elements==nullptr) {
90  ATH_MSG_FATAL(m_trtDetEleContKey.fullKey() << " is not available.");
91  return StatusCode::FAILURE;
92  }
93 
94  float rMinEndcap = 617.;
95  float rMaxEndcap = 1106.;
96  int countAll(0), countDead(0), countPhiSkipped(0), countEtaSkipped(0), countInvalidEtaValues(0);
97  for (std::vector<Identifier>::const_iterator it = m_trtId->straw_layer_begin(); it != m_trtId->straw_layer_end(); ++it ) {
98  int nStrawsInLayer = m_trtId->straw_max(*it);
99  for (int i=0; i<=nStrawsInLayer; i++) {
100 
101  Identifier id = m_trtId->straw_id(*it, i);
102  // Make sure it is a straw_layer id
103  Identifier strawLayerId = m_trtId->layer_id(id);
104  //Get hash Id
105  IdentifierHash hashId = m_trtId->straw_layer_hash(strawLayerId);
106 
107  bool status = m_strawStatus->get_status(id, ctx);
108  countAll++; if (status) countDead++;
109 
110  const Amg::Vector3D &strawPosition = elements->getDetectorElement(hashId)->center(id);
111  double phi = std::atan2( strawPosition.y(), strawPosition.x() );
112  int phiBin = writeCdo->findPhiBin( phi );
113  if (phiBin<0) {
114  ATH_MSG_DEBUG("TRTCond::ActiveFraction phiBin<0: " << phi << " " << phiBin);
115  countPhiSkipped++;
116  continue;
117  }
118 
119  // calculate etaMin, etaMax
120  int side = m_trtId->barrel_ec(id);
121  float z = std::abs( strawPosition.z() );
122  float thetaMin(0.), thetaMax(0.);
123  if (abs(side)==1) { // barrel
124  float zRange = 360.; // straw length / 2
125  if ( m_trtId->layer_or_wheel(id) == 0 && m_trtId->straw_layer(id) < 9 ) zRange = 160.; // short straws
126  float r = std::sqrt( std::pow(strawPosition.x(), 2) + std::pow(strawPosition.y(), 2) );
127  thetaMin = std::atan( r / (z+zRange) );
128  thetaMax = ((z-zRange)>0.) ? std::atan( r / (z-zRange) ) : 1.57; // M_PI_2 - epsilon
129  } else { // endcap
130  thetaMin = std::atan( rMinEndcap / z );
131  thetaMax = std::atan( rMaxEndcap / z );
132  }
133  if (side<0) { // theta -> M_PI - theta
134  float thetaDummy = thetaMin;
135  thetaMin = M_PI - thetaMax;
136  thetaMax = M_PI - thetaDummy;
137  }
138 
139  float thetaCheck[] = {thetaMax, thetaMin};
140  float etaCheck[] = {0., 0.};
141  for (int ti=0; ti<2; ti++) {
142  if (thetaCheck[ti]<=0.||thetaCheck[ti]>=M_PI) ATH_MSG_DEBUG("TRTCond::ActiveFraction: theta " << ti << " " << thetaCheck[ti]);
143  float tanTheta = std::tan(thetaCheck[ti]/2.);
144  if (tanTheta<=0.) {
145  ATH_MSG_DEBUG("TRTCond::ActiveFraction: theta tan " << ti << " " << tanTheta );
146  countInvalidEtaValues++;
147  continue;
148  }
149  etaCheck[ti] = -std::log( tanTheta );
150  }
151  float etaMin = etaCheck[0];
152  float etaMax = etaCheck[1];
153  int etaMinBin = writeCdo->findEtaBin( etaMin );
154  int etaMaxBin = writeCdo->findEtaBin( etaMax );
155  if (etaMin>=etaMax) ATH_MSG_WARNING("TRTCond::ActiveFraction: etaMaxBin<etaMinBin " << etaMin << " " << etaMax << " " << thetaMin << " " << thetaMax);
156  if (etaMinBin<0 && etaMaxBin<0) {
157  ATH_MSG_WARNING("TRTCond::ActiveFraction: etaMaxBin<etaMinBin " << thetaMin << " " << thetaMax
158  << " " << etaMin << " " << etaMax << " " << etaMinBin << " " << etaMaxBin << ", side: " << side);
159  countEtaSkipped++;
160  continue;
161  }
162  if (etaMinBin<0) etaMinBin = 0;
163  if (etaMaxBin<0) etaMaxBin = writeCdo->getEtaBins().size() - 1;
164  if (etaMaxBin<etaMinBin) ATH_MSG_WARNING( "TRTCond::ActiveFraction: etaMaxBin<etaMinBin " << etaMinBin << " " << etaMaxBin << ", side: " << side);
165 
166  for (int iEta = etaMinBin; iEta <= etaMaxBin; iEta++) {
167  dummyTableCountAll[iEta][phiBin]++;
168  if (status) dummyTableCountDead[iEta][phiBin]++;
169  }
170 
171  }
172  } // end straw Identifier loop
173 
174  for (unsigned int i = 0; i < writeCdo->getEtaBins().size(); ++i) { // fill the table
175  for (unsigned int j = 0; j < writeCdo->getPhiBins().size(); ++j) {
176  float deadFraction = 1. * dummyTableCountDead[i][j];
177  if (dummyTableCountAll[i][j]>0) deadFraction /= (1. * dummyTableCountAll[i][j]);
178  writeCdo->setActiveFraction(i, j, 1. - deadFraction);
179  ATH_MSG_DEBUG( "dummyTableCountDead: " << i << ", " << j << ", count " << dummyTableCountAll[i][j] << " dead " << deadFraction);
180  }
181  }
182 
183  float deadStrawFraction = (1.*countDead) / (1.*countAll);
184  ATH_MSG_INFO( " Initialize TRTCond::ActiveFraction table finished, count all TRT straws: " << countAll
185  << ", count dead straws: " << countDead << " (" << deadStrawFraction
186  << "%), straws skipped due to invalid phi, eta range: " << countPhiSkipped << " " << countEtaSkipped );
187 
188  if (countInvalidEtaValues) ATH_MSG_WARNING("TRT_ActiveFraction: found invalid eta range, check: " << countInvalidEtaValues);
189 
190 
191 
192  //__________ Assign range of writeCdo to that of the ReadHandle___________
193  EventIDRange rangeW;
194 
196  const StrawStatusContainer* strawContainer{*strawReadHandle};
197  if(strawContainer==nullptr) {
198  ATH_MSG_ERROR("Null pointer to the straw status container");
199  return StatusCode::FAILURE;
200  }
201 
202  // Get range
203  if(!strawReadHandle.range(rangeW)) {
204  ATH_MSG_ERROR("Failed to retrieve validity range for " << strawReadHandle.key());
205  return StatusCode::FAILURE;
206  }
207 
208 
209  // Record CDO
210  if(writeHandle.record(rangeW,std::move(writeCdo)).isFailure()) {
211  ATH_MSG_ERROR("Could not record ActiveFraction " << writeHandle.key()
212  << " with EventRange " << rangeW
213  << " into Conditions Store");
214  return StatusCode::FAILURE;
215  }
216 
217 
218  return StatusCode::SUCCESS;
219 }
220 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
beamspotman.r
def r
Definition: beamspotman.py:674
TRT_ID::layer_id
Identifier layer_id(int barrel_ec, int phi_module, int layer_or_wheel, int straw_layer) const
For an individual straw layer.
Definition: TRT_ID.h:494
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TRT_ID::straw_max
int straw_max(const Identifier &id) const
Definition: TRT_ID.cxx:803
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TRTCond::StrawStatusMultChanContainer
Definition: StrawStatusMultChanContainer.h:19
TRTActiveCondAlg::~TRTActiveCondAlg
virtual ~TRTActiveCondAlg() override
TRT_ID::straw_layer_end
const_id_iterator straw_layer_end() const
Definition: TRT_ID.h:955
TRT_ID::straw_layer_begin
const_id_iterator straw_layer_begin() const
Iterators over full set of straw_layer ids. Straw_layer ids are sorted.
Definition: TRT_ID.h:947
skel.it
it
Definition: skel.GENtoEVGEN.py:407
M_PI
#define M_PI
Definition: ActiveFraction.h:11
SG::ReadCondHandle::isValid
bool isValid()
Definition: ReadCondHandle.h:210
bin
Definition: BinsDiffFromStripMedian.h:43
xAOD::etaMax
etaMax
Definition: HIEventShape_v2.cxx:46
TRTActiveCondAlg::m_strawReadKey
SG::ReadCondHandleKey< StrawStatusContainer > m_strawReadKey
Definition: TRTActiveCondAlg.h:33
drawFromPickle.atan
atan
Definition: drawFromPickle.py:36
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
TRT::Hit::side
@ side
Definition: HitInfo.h:83
TRTActiveCondAlg::initialize
virtual StatusCode initialize() override
Definition: TRTActiveCondAlg.cxx:19
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
GeoPrimitives.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
z
#define z
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TRTActiveCondAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: TRTActiveCondAlg.cxx:39
TRTActiveCondAlg::m_trtDetEleContKey
SG::ReadCondHandleKey< InDetDD::TRT_DetElementContainer > m_trtDetEleContKey
Definition: TRTActiveCondAlg.h:34
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
drawFromPickle.tan
tan
Definition: drawFromPickle.py:36
TRT_ID::barrel_ec
int barrel_ec(const Identifier &id) const
Values of different levels (failure returns 0)
Definition: TRT_ID.h:860
TRTActiveCondAlg::TRTActiveCondAlg
TRTActiveCondAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TRTActiveCondAlg.cxx:13
TRT_ID::straw_layer
int straw_layer(const Identifier &id) const
Definition: TRT_ID.h:887
TRT_ID::layer_or_wheel
int layer_or_wheel(const Identifier &id) const
Definition: TRT_ID.h:878
TRT_BaseElement.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
xAOD::phiBin
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setPhiMap phiBin
Definition: L2StandAloneMuon_v2.cxx:145
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
TRTActiveCondAlg.h
TRTActiveCondAlg::m_trtId
const TRT_ID * m_trtId
Definition: TRTActiveCondAlg.h:39
LArCellBinning.etaMin
etaMin
Definition: LArCellBinning.py:84
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
InDetDD::TRT_DetElementCollection::getDetectorElement
const TRT_BaseElement * getDetectorElement(const IdentifierHash &hash) const
Definition: TRT_DetElementCollection.cxx:9
InDetDD::TRT_BaseElement::center
virtual const Amg::Vector3D & center() const override final
Element Surface: center of a straw layer.
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
InDetDD::TRT_DetElementContainer::getElements
const TRT_DetElementCollection * getElements() const
Definition: TRT_DetElementContainer.cxx:29
TRTActiveCondAlg::m_strawWriteKey
SG::WriteCondHandleKey< TRTCond::ActiveFraction > m_strawWriteKey
Definition: TRTActiveCondAlg.h:36
merge.status
status
Definition: merge.py:16
InDetDD::TRT_DetElementCollection
Class to hold collection of TRT detector elements.
Definition: TRT_DetElementCollection.h:26
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
pow
constexpr int pow(int base, int exp) noexcept
Definition: ap_fixedTest.cxx:15
xAOD::iEta
setScale setgFexType iEta
Definition: gFexJetRoI_v1.cxx:77
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
TRT_ID::straw_layer_hash
IdentifierHash straw_layer_hash(Identifier straw_layer_id) const
straw_layer hash from id - optimized
Definition: TRT_ID.h:744
TRT_ID::straw_id
Identifier straw_id(int barrel_ec, int phi_module, int layer_or_wheel, int straw_layer, int straw) const
Three ways of getting id for a single straw:
Definition: TRT_ID.h:575
TRTActiveCondAlg::m_strawStatus
ToolHandle< ITRT_StrawStatusSummaryTool > m_strawStatus
Definition: TRTActiveCondAlg.h:38
Identifier
Definition: IdentifierFieldParser.cxx:14