ATLAS Offline Software
Loading...
Searching...
No Matches
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
28 ATH_CHECK( m_strawWriteKey.initialize() );
29
30 // Initialize readCondHandle key
31 ATH_CHECK(m_trtDetEleContKey.initialize());
32
33 // TRT ID helper
34 ATH_CHECK(detStore()->retrieve(m_trtId,"TRT_ID"));
35
36 return StatusCode::SUCCESS;
37}
38
39StatusCode 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
#define M_PI
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
#define z
const ServiceHandle< StoreGateSvc > & detStore() const
An algorithm that can be simultaneously executed in multiple threads.
This is a "hash" representation of an Identifier.
virtual const Amg::Vector3D & center() const override final
Element Surface: center of a straw layer.
Class to hold collection of TRT detector elements.
const TRT_BaseElement * getDetectorElement(const IdentifierHash &hash) const
bool range(EventIDRange &r)
const std::string & key() const
const std::string & key() const
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
const DataObjID & fullKey() const
TRTActiveCondAlg(const std::string &name, ISvcLocator *pSvcLocator)
const TRT_ID * m_trtId
virtual StatusCode initialize() override
TRTCond::StrawStatusMultChanContainer StrawStatusContainer
ToolHandle< ITRT_StrawStatusSummaryTool > m_strawStatus
virtual StatusCode execute(const EventContext &ctx) const override
virtual ~TRTActiveCondAlg() override
SG::ReadCondHandleKey< InDetDD::TRT_DetElementContainer > m_trtDetEleContKey
SG::WriteCondHandleKey< TRTCond::ActiveFraction > m_strawWriteKey
SG::ReadCondHandleKey< StrawStatusContainer > m_strawReadKey
int r
Definition globals.cxx:22
Eigen::Matrix< double, 3, 1 > Vector3D