ATLAS Offline Software
TRTActiveCondAlg.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 <cmath>
6 
7 
8 
9 #include "TRTActiveCondAlg.h"
12 
14  , ISvcLocator* pSvcLocator )
15  : ::AthReentrantAlgorithm(name,pSvcLocator),
16  m_strawStatus("TRT_StrawStatusSummaryTool",this),
17  m_trtId(nullptr)
18 { declareProperty("TRTStrawStatusSummaryTool",m_strawStatus); }
20 
22 {
23  // Straw status
24  ATH_CHECK ( m_strawStatus.retrieve() );
25 
26  // Read key
27  ATH_CHECK( m_strawReadKey.initialize() );
28 
29  // Register write handle
31 
32  // Initialize readCondHandle key
34 
35  // TRT ID helper
36  ATH_CHECK(detStore()->retrieve(m_trtId,"TRT_ID"));
37 
38  return StatusCode::SUCCESS;
39 }
40 
41 StatusCode TRTActiveCondAlg::execute(const EventContext& ctx) const
42 {
43  ATH_MSG_DEBUG("execute " << name());
44 
45  // ____________ Construct Write Cond Handle and check its validity ____________
46 
48 
49  // Do we have a valid Write Cond Handle for current time?
50  if(writeHandle.isValid()) {
51  ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
52  << ". In theory this should not be called, but may happen"
53  << " if multiple concurrent events are being processed out of order.");
54 
55  return StatusCode::SUCCESS;
56  }
57 
58 
59 
60  // ____________ Construct new Write Cond Object ____________
61  std::unique_ptr<TRTCond::ActiveFraction> writeCdo{std::make_unique<TRTCond::ActiveFraction>()};
62 
63 
64  // ____________ Compute number of alive straws for Write Cond object ____________
65 
66 
67 
68  ATH_MSG_INFO(" Initialize TRTCond::ActiveFraction table with number of phi, eta bins: "
69  << writeCdo->getPhiBins().size() << ", " << writeCdo->getEtaBins().size() );
70 
71  for (unsigned int i=0; i<writeCdo->getEtaBins().size(); i++) {
72  float etaMin = writeCdo->getEtaBins()[i].first;
73  float etaMax = writeCdo->getEtaBins()[i].second;
74  int bin = writeCdo->findEtaBin( (etaMin+etaMax)/2. );
75  ATH_MSG_DEBUG("TRTCond::ActiveFraction: findEtaBin( " << etaMin << ", " << etaMax << " ] = " << bin );
76 
77  for (unsigned int j=0; j<writeCdo->getPhiBins().size(); j++) {
78  float phiMin = writeCdo->getPhiBins()[j].first;
79  float phiMax = writeCdo->getPhiBins()[j].second;
80  int bin = writeCdo->findPhiBin( (phiMin+phiMax)/2. );
81  ATH_MSG_DEBUG("TRTCond::ActiveFraction: findPhiBin( " << phiMin << ", " << phiMax << " ] = " << bin );
82  }
83  }
84 
85  std::vector<int> dummyPhiVec( writeCdo->getPhiBins().size(), 0 );
86  std::vector<std::vector<int> > dummyTableCountAll( writeCdo->getEtaBins().size(), dummyPhiVec );
87  std::vector<std::vector<int> > dummyTableCountDead( writeCdo->getEtaBins().size(), dummyPhiVec );
88 
90  const InDetDD::TRT_DetElementCollection* elements(trtDetEleHandle->getElements());
91  if (not trtDetEleHandle.isValid() or elements==nullptr) {
92  ATH_MSG_FATAL(m_trtDetEleContKey.fullKey() << " is not available.");
93  return StatusCode::FAILURE;
94  }
95 
96  float rMinEndcap = 617.;
97  float rMaxEndcap = 1106.;
98  int countAll(0), countDead(0), countPhiSkipped(0), countEtaSkipped(0), countInvalidEtaValues(0);
99  for (std::vector<Identifier>::const_iterator it = m_trtId->straw_layer_begin(); it != m_trtId->straw_layer_end(); ++it ) {
100  int nStrawsInLayer = m_trtId->straw_max(*it);
101  for (int i=0; i<=nStrawsInLayer; i++) {
102 
103  Identifier id = m_trtId->straw_id(*it, i);
104  // Make sure it is a straw_layer id
105  Identifier strawLayerId = m_trtId->layer_id(id);
106  //Get hash Id
107  IdentifierHash hashId = m_trtId->straw_layer_hash(strawLayerId);
108 
109  bool status = m_strawStatus->get_status(id, ctx);
110  countAll++; if (status) countDead++;
111 
112  const Amg::Vector3D &strawPosition = elements->getDetectorElement(hashId)->center(id);
113  double phi = std::atan2( strawPosition.y(), strawPosition.x() );
114  int phiBin = writeCdo->findPhiBin( phi );
115  if (phiBin<0) {
116  ATH_MSG_DEBUG("TRTCond::ActiveFraction phiBin<0: " << phi << " " << phiBin);
117  countPhiSkipped++;
118  continue;
119  }
120 
121  // calculate etaMin, etaMax
122  int side = m_trtId->barrel_ec(id);
123  float z = std::abs( strawPosition.z() );
124  float thetaMin(0.), thetaMax(0.);
125  if (abs(side)==1) { // barrel
126  float zRange = 360.; // straw length / 2
127  if ( m_trtId->layer_or_wheel(id) == 0 && m_trtId->straw_layer(id) < 9 ) zRange = 160.; // short straws
128  float r = std::sqrt( std::pow(strawPosition.x(), 2) + std::pow(strawPosition.y(), 2) );
129  thetaMin = std::atan( r / (z+zRange) );
130  thetaMax = ((z-zRange)>0.) ? std::atan( r / (z-zRange) ) : 1.57; // M_PI_2 - epsilon
131  } else { // endcap
132  thetaMin = std::atan( rMinEndcap / z );
133  thetaMax = std::atan( rMaxEndcap / z );
134  }
135  if (side<0) { // theta -> M_PI - theta
136  float thetaDummy = thetaMin;
137  thetaMin = M_PI - thetaMax;
138  thetaMax = M_PI - thetaDummy;
139  }
140 
141  float thetaCheck[] = {thetaMax, thetaMin};
142  float etaCheck[] = {0., 0.};
143  for (int ti=0; ti<2; ti++) {
144  if (thetaCheck[ti]<=0.||thetaCheck[ti]>=M_PI) ATH_MSG_DEBUG("TRTCond::ActiveFraction: theta " << ti << " " << thetaCheck[ti]);
145  float tanTheta = std::tan(thetaCheck[ti]/2.);
146  if (tanTheta<=0.) {
147  ATH_MSG_DEBUG("TRTCond::ActiveFraction: theta tan " << ti << " " << tanTheta );
148  countInvalidEtaValues++;
149  continue;
150  }
151  etaCheck[ti] = -std::log( tanTheta );
152  }
153  float etaMin = etaCheck[0];
154  float etaMax = etaCheck[1];
155  int etaMinBin = writeCdo->findEtaBin( etaMin );
156  int etaMaxBin = writeCdo->findEtaBin( etaMax );
157  if (etaMin>=etaMax) ATH_MSG_WARNING("TRTCond::ActiveFraction: etaMaxBin<etaMinBin " << etaMin << " " << etaMax << " " << thetaMin << " " << thetaMax);
158  if (etaMinBin<0 && etaMaxBin<0) {
159  ATH_MSG_WARNING("TRTCond::ActiveFraction: etaMaxBin<etaMinBin " << thetaMin << " " << thetaMax
160  << " " << etaMin << " " << etaMax << " " << etaMinBin << " " << etaMaxBin << ", side: " << side);
161  countEtaSkipped++;
162  continue;
163  }
164  if (etaMinBin<0) etaMinBin = 0;
165  if (etaMaxBin<0) etaMaxBin = writeCdo->getEtaBins().size() - 1;
166  if (etaMaxBin<etaMinBin) ATH_MSG_WARNING( "TRTCond::ActiveFraction: etaMaxBin<etaMinBin " << etaMinBin << " " << etaMaxBin << ", side: " << side);
167 
168  for (int iEta = etaMinBin; iEta <= etaMaxBin; iEta++) {
169  dummyTableCountAll[iEta][phiBin]++;
170  if (status) dummyTableCountDead[iEta][phiBin]++;
171  }
172 
173  }
174  } // end straw Identifier loop
175 
176  for (unsigned int i = 0; i < writeCdo->getEtaBins().size(); ++i) { // fill the table
177  for (unsigned int j = 0; j < writeCdo->getPhiBins().size(); ++j) {
178  float deadFraction = 1. * dummyTableCountDead[i][j];
179  if (dummyTableCountAll[i][j]>0) deadFraction /= (1. * dummyTableCountAll[i][j]);
180  writeCdo->setActiveFraction(i, j, 1. - deadFraction);
181  ATH_MSG_DEBUG( "dummyTableCountDead: " << i << ", " << j << ", count " << dummyTableCountAll[i][j] << " dead " << deadFraction);
182  }
183  }
184 
185  float deadStrawFraction = (1.*countDead) / (1.*countAll);
186  ATH_MSG_INFO( " Initialize TRTCond::ActiveFraction table finished, count all TRT straws: " << countAll
187  << ", count dead straws: " << countDead << " (" << deadStrawFraction
188  << "%), straws skipped due to invalid phi, eta range: " << countPhiSkipped << " " << countEtaSkipped );
189 
190  if (countInvalidEtaValues) ATH_MSG_WARNING("TRT_ActiveFraction: found invalid eta range, check: " << countInvalidEtaValues);
191 
192 
193 
194  //__________ Assign range of writeCdo to that of the ReadHandle___________
195  EventIDRange rangeW;
196 
198  const StrawStatusContainer* strawContainer{*strawReadHandle};
199  if(strawContainer==nullptr) {
200  ATH_MSG_ERROR("Null pointer to the straw status container");
201  return StatusCode::FAILURE;
202  }
203 
204  // Get range
205  if(!strawReadHandle.range(rangeW)) {
206  ATH_MSG_ERROR("Failed to retrieve validity range for " << strawReadHandle.key());
207  return StatusCode::FAILURE;
208  }
209 
210 
211  // Record CDO
212  if(writeHandle.record(rangeW,std::move(writeCdo)).isFailure()) {
213  ATH_MSG_ERROR("Could not record ActiveFraction " << writeHandle.key()
214  << " with EventRange " << rangeW
215  << " into Conditions Store");
216  return StatusCode::FAILURE;
217  }
218 
219 
220  return StatusCode::SUCCESS;
221 }
222 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
beamspotman.r
def r
Definition: beamspotman.py:676
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:500
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
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
skel.it
it
Definition: skel.GENtoEVGEN.py:396
M_PI
#define M_PI
Definition: ActiveFraction.h:11
SG::ReadCondHandle::isValid
bool isValid()
Definition: ReadCondHandle.h:206
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
TRT_ID::straw_layer_end
const_id_iterator straw_layer_end(void) const
Definition: TRT_ID.h:961
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:83
TRT::Hit::side
@ side
Definition: HitInfo.h:83
TRTActiveCondAlg::initialize
virtual StatusCode initialize() override
Definition: TRTActiveCondAlg.cxx:21
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:41
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:866
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:893
TRT_ID::layer_or_wheel
int layer_or_wheel(const Identifier &id) const
Definition: TRT_ID.h:884
TRT_BaseElement.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
xAOD::phiBin
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setPhiMap phiBin
Definition: L2StandAloneMuon_v2.cxx:144
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:11
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:17
TRT_ID::straw_layer_begin
const_id_iterator straw_layer_begin(void) const
Iterators over full set of straw_layer ids. Straw_layer ids are sorted.
Definition: TRT_ID.h:953
InDetDD::TRT_DetElementCollection
Class to hold collection of TRT detector elements.
Definition: TRT_DetElementCollection.h:28
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:750
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:581
TRTActiveCondAlg::m_strawStatus
ToolHandle< ITRT_StrawStatusSummaryTool > m_strawStatus
Definition: TRTActiveCondAlg.h:38
Identifier
Definition: IdentifierFieldParser.cxx:14