ATLAS Offline Software
TRT_PrepDataToxAOD.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // TRT_PrepDataToxAOD.cxx
7 // Implementation file for class TRT_PrepDataToxAOD
9 
10 #include "TRT_PrepDataToxAOD.h"
11 
13 //#include "EventPrimitives/EventPrimitivesHelpers.h"
14 
18 
19 
20 #include "Identifier/Identifier.h"
21 #include "InDetIdentifier/TRT_ID.h"
22 
23 
24 #include "TrkSurfaces/Surface.h"
25 
29 
30 #include "StoreGate/ReadHandle.h"
31 #include "StoreGate/WriteHandle.h"
32 
33 #define AUXDATA(OBJ, TYP, NAME) \
34  static const SG::AuxElement::Accessor<TYP> acc_##NAME (#NAME); acc_##NAME(*(OBJ))
35 
37 //
38 // Constructor with parameters:
39 //
41 TRT_PrepDataToxAOD::TRT_PrepDataToxAOD(const std::string &name, ISvcLocator *pSvcLocator) :
42  AthAlgorithm(name,pSvcLocator),
43  m_driftFunctionTool("TRT_DriftFunctionTool", this),
44  m_trtcaldbTool("TRT_CalDbTool", this),
45  m_neighbourSvc("TRT_StrawNeighbourSvc", name),
46  m_TRTStrawSummaryTool("TRT_StrawStatusSummaryTool",this),
47  m_TRTHelper(nullptr),
48  m_trtman(nullptr),
49  m_firstEventWarnings(true)
50 {
51 
52  // --- Services and Tools
53  declareProperty("TRTDriftFunctionTool", m_driftFunctionTool);
54  declareProperty("TRTCalDbTool", m_trtcaldbTool);
55  declareProperty("NeighbourSvc", m_neighbourSvc);
56  declareProperty("TRTStrawSummaryTool", m_TRTStrawSummaryTool);
57 
58 }
59 
61 //
62 // Initialize method:
63 //
66 {
67  ATH_MSG_DEBUG("Initialize");
68 
69  //Initialize VarHandleKeys
73  ATH_CHECK(m_xAodContainer.initialize());
75 
76  // --- Retrieve services and tools
77  CHECK ( detStore()->retrieve(m_TRTHelper, "TRT_ID") );
78 
79  CHECK ( detStore()->retrieve(m_trtman, "TRT") );
80 
81  CHECK ( m_neighbourSvc.retrieve() );
82 
83  CHECK ( m_trtcaldbTool.retrieve() );
84 
85  CHECK ( m_TRTStrawSummaryTool.retrieve() );
86 
87  CHECK( m_driftFunctionTool.retrieve() );
88 
89  return StatusCode::SUCCESS;
90 }
91 
93 //
94 // Execute method:
95 //
98 {
99  //This is needed for the algorithm. If not there, it fails
100 
102  if (not h_trtPrds.isValid()) {
103  ATH_MSG_ERROR("Cannot retrieve TRT PrepDataContainer " << m_driftcirclecontainer.key());
104  return StatusCode::FAILURE;
105  }
106 
107  //This is optional for the algorithm. If not there, just print a one-time warning
108  // On ESD
109 
110  const PRD_MultiTruthCollection* prdmtColl = nullptr; // to be used in the loop later
111  if (m_useTruthInfo && (!m_multiTruth.key().empty()) ) {
113  if (not h_prdmtColl.isValid()){
114  if (m_firstEventWarnings) {
115  ATH_MSG_WARNING("PRD MultiTruth collection not available (" << m_multiTruth.key() << "). Skipping this info although requested.");}
116  } else {
117  prdmtColl = h_prdmtColl.cptr();
118  }
119  }
120 
121  //This is optional for the algorithm. If not there, just print a one-time warning
122  // On RDO
123 
124  const InDetSimDataCollection* sdoCollection = nullptr; // to be used in the loop later
125  if (m_writeSDOs && m_useTruthInfo && (!m_SDOcontainer.key().empty()) ) {
127  if (not h_sdoCollection.isValid()) {
129  ATH_MSG_WARNING("SDO Collection not available (" << m_SDOcontainer.key() << "). Skipping this info although requested.");}
130  } else{
131  sdoCollection = h_sdoCollection.cptr();
132  }
133  }
134 
135 
136  // Create the xAOD container and its auxiliary store:
138  ATH_CHECK(xaod.record(std::make_unique<xAOD::TrackMeasurementValidationContainer>(),std::make_unique<xAOD::TrackMeasurementValidationAuxContainer>() ) );
139 
141  ATH_CHECK(offsets.record(std::make_unique<std::vector<unsigned int>>(m_TRTHelper->straw_layer_hash_max() , 0) ));
142 
143  InDet::TRT_DriftCircleContainer::const_iterator it = h_trtPrds->begin();
144  InDet::TRT_DriftCircleContainer::const_iterator it_end = h_trtPrds->end();
145  unsigned int counter(0);
146  for( ; it!=it_end; ++it ) {
147 
148  //Fill Offset container
149  if( m_TRTHelper->straw_layer_hash_max() <= (*it)->identifyHash() )
150  ATH_MSG_ERROR("My assumption about the maximum size of the hash was wrong");
151  (*offsets)[ (*it)->identifyHash() ] = counter;
152 
153  // skip empty collections
154  if( (*it)->empty() ) continue;
155 
156  // loop over collection and convert to xAOD
157  for( const auto prd : **it ){
158 
159  ++counter;
160 
161  // create and add xAOD object
163  xaod->push_back(xprd);
164 
165  Identifier surfaceID = prd->identify();
166 
167  xprd->setIdentifier(surfaceID.get_compact());
168 
169 
170  // Save ID info:
171  AUXDATA(xprd, int, bec) = m_TRTHelper->barrel_ec( surfaceID ) ;
172  AUXDATA(xprd, int, layer) = m_TRTHelper->layer_or_wheel(surfaceID ) ;
173  AUXDATA(xprd, int, phi_module) = m_TRTHelper->phi_module( surfaceID ) ;
174  AUXDATA(xprd, int, strawlayer) = m_TRTHelper->straw_layer( surfaceID ) ;
175  AUXDATA(xprd, int, strawnumber) = m_TRTHelper->straw( surfaceID ) ;
176  const InDetDD::TRT_BaseElement* element = m_trtman->getElement(surfaceID);
177  AUXDATA(xprd, float, strawphi) = element->center(surfaceID).phi();
178 
179  int chip=0;
180  int board=-1;
181  m_neighbourSvc->getChip(surfaceID,chip);
182  if(abs(m_TRTHelper->barrel_ec(surfaceID))<2)
183  board=m_neighbourSvc->chipToBoardBarrel(chip,m_TRTHelper->layer_or_wheel(surfaceID));
184  else if (chip<12)
185  board=0;
186  else {
187  chip=chip-20;
188  board=1;
189  }
190  AUXDATA(xprd, int, TRTboard) = board ;
191  AUXDATA(xprd, int, TRTchip) = chip ;
192 
193 
194  //Set Local Position
195  const Amg::Vector2D& locpos = prd->localPosition();
196  float locY(0.);
197  float locX = locpos.x();
198  if ( !(std::isinf(locpos.y()) || std::isnan(locpos.y())) ){
199  if (locpos.y()>=1e-07)
200  locY=locpos.y();
201  } else {
202  locY = -9999.;
203  }
204 
205  // Set local error matrix
206  xprd->setLocalPosition(locX,locY);
207 
208  const Amg::MatrixX& localCov = prd->localCovariance();
209  if(localCov.size() == 1){
210  xprd->setLocalPositionError( sqrt(localCov(0,0)), 0., 0. );
211  } else if(localCov.size() == 2){
212  xprd->setLocalPositionError( sqrt(localCov(0,0)), sqrt(localCov(1,1)), sqrt(localCov(0,1)) );
213  } else {
214  xprd->setLocalPositionError(0.,0.,0.);
215  }
216 
217 
218  //Set Global position
219  const Amg::Vector3D gpos = prd->detectorElement()->surface(surfaceID).localToGlobal(prd->localPosition());
220  xprd->setGlobalPosition(gpos.x(),gpos.y(),gpos.z());
221 
222 
223  //TRT hit bit word
224  unsigned int word = prd->getWord();
225 
226 
227  double tot = prd->timeOverThreshold();
228  bool isvalid=false;
229  AUXDATA(xprd, float, drifttime) = prd->driftTime(isvalid) ;
230  AUXDATA(xprd, int, status) = isvalid;
231  AUXDATA(xprd, float, tot) = tot ;
232  AUXDATA(xprd, char, isHT) = prd->highLevel() ;
233  AUXDATA(xprd, float, T0) = m_trtcaldbTool->getT0(surfaceID) ;
234 
235 
236  // Save time info:
237  AUXDATA(xprd, float, leadingEdge) = prd->rawDriftTime();
238 
239  //Drift Time corrections
240  AUXDATA(xprd, float, driftTimeToTCorrection) = m_driftFunctionTool->driftTimeToTCorrection(tot,surfaceID);
241  if(prd->highLevel()) {
242  AUXDATA(xprd, float, driftTimeHTCorrection) = m_driftFunctionTool->driftTimeHTCorrection(surfaceID);
243  }
244  else {
245  AUXDATA(xprd, float, driftTimeHTCorrection) = 0;
246  }
247 
248 
249  //HT Bit patterens
250  unsigned int theWord = word & 0x04020100; //HT bits mask
251  char highThreshold = 0;
252  //this is the agreed-upon binning for HT bit patterns...
253  if (theWord == 0x04000000) //pattern 1
254  highThreshold = 1;
255  else if (theWord == 0x00020000) //pattern 2
256  highThreshold = 2;
257  else if (theWord == 0x00000100) //pattern 3
258  highThreshold = 3;
259  else if (theWord == 0x04020000) //pattern 4
260  highThreshold = 4;
261  else if (theWord == 0x00020100) //pattern 5
262  highThreshold = 5;
263  else if (theWord == 0x04000100) //pattern 6
264  highThreshold = 6;
265  else if (theWord == 0x04020100) //pattern 7
266  highThreshold = 7;
267 
268  AUXDATA(xprd, char, highThreshold) = highThreshold;
269 
270  //Full bit patternword from the TRT
271  AUXDATA(xprd, unsigned int, bitPattern) = word;
272 
273  char gas_type = kUnset;
274  if (!m_TRTStrawSummaryTool.empty()) {
275  int stat = m_TRTStrawSummaryTool->getStatusHT(surfaceID, Gaudi::Hive::currentContext());
276 
277  if ( stat==1 || stat==4 ) { gas_type = kArgon; }
278  else if ( stat==5 ) { gas_type = kKrypton; }
279  else if ( stat==2 || stat==3 ) { gas_type = kXenon; }
280  else if ( stat==6 ) gas_type = kEmAr;
281  else if ( stat==7 ) gas_type = kEmKr;
282  }
283  AUXDATA(xprd, char, gasType) = gas_type;
284 
285  // Use the MultiTruth Collection to get a list of all true particle contributing to the DC
286  if (m_useTruthInfo){
287  if(prdmtColl){
288  std::vector<int> barcodes;
289  auto range = prdmtColl->equal_range(surfaceID);
290  for (auto i = range.first; i != range.second; ++i) {
291  barcodes.push_back( i->second.barcode() );
292  }
293  AUXDATA(xprd, std::vector<int> , truth_barcode) = barcodes;
294  }
295  }
296  if (m_writeSDOs) {
297  if(sdoCollection){
298  // find hit
299  auto pos = sdoCollection->find(surfaceID);
300  int sdo_word = -1000000;
301  if( pos != sdoCollection->end() ) {
302  sdo_word = pos->second.word();
303  }
304  AUXDATA(xprd, int, sdo_word) = sdo_word;
305  }
306  }
307 
308  }
309  }
310  ATH_MSG_DEBUG( " recorded TRT_PrepData obejcts: size " << xaod->size() );
311 
312 
313 
314  // Code to test that something was added to SG:
315  StatusCode sc = StatusCode::SUCCESS;
316 
317  // --- end of event. Disable one-time warnings
318  m_firstEventWarnings = false;
319 
320  return sc;
321 }
322 
324 //
325 // Finalize method:
326 //
329 {
330  return StatusCode::SUCCESS;
331 }
TRT_PrepDataToxAOD::TRT_PrepDataToxAOD
TRT_PrepDataToxAOD(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TRT_PrepDataToxAOD.cxx:41
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
Amg::MatrixX
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
Definition: EventPrimitives.h:27
Trk::locX
@ locX
Definition: ParamDefs.h:37
Trk::locY
@ locY
local cartesian
Definition: ParamDefs.h:38
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
Surface.h
TRT_PrepDataToxAOD::m_driftcirclecontainer
SG::ReadHandleKey< InDet::TRT_DriftCircleContainer > m_driftcirclecontainer
Definition: TRT_PrepDataToxAOD.h:59
ParticleGun_SamplingFraction.bec
int bec
Definition: ParticleGun_SamplingFraction.py:89
make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: SkimmingToolEXOT5.cxx:23
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
TrackMeasurementValidationAuxContainer.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
xAOD::TrackMeasurementValidation
TrackMeasurementValidation_v1 TrackMeasurementValidation
Reference the current persistent version:
Definition: TrackMeasurementValidation.h:13
TRT_PrepDataToxAOD::m_firstEventWarnings
bool m_firstEventWarnings
Definition: TRT_PrepDataToxAOD.h:75
PRD_MultiTruthCollection
A PRD is mapped onto all contributing particles.
Definition: PRD_MultiTruthCollection.h:24
TRT_ID::straw_layer_hash_max
size_type straw_layer_hash_max(void) const
Definition: TRT_ID.h:920
TRT_PrepDataToxAOD::m_trtman
const InDetDD::TRT_DetectorManager * m_trtman
Definition: TRT_PrepDataToxAOD.h:72
AUXDATA
#define AUXDATA(OBJ, TYP, NAME)
Definition: TRT_PrepDataToxAOD.cxx:33
skel.it
it
Definition: skel.GENtoEVGEN.py:396
TRT_ID.h
This is an Identifier helper class for the TRT subdetector. This class is a factory for creating comp...
Identifier::get_compact
value_type get_compact() const
Get the compact id.
InDetSimDataCollection
Definition: InDetSimDataCollection.h:25
TRT_PrepDataToxAOD::m_TRTHelper
const TRT_ID * m_TRTHelper
Definition: TRT_PrepDataToxAOD.h:71
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
TRT_PrepDataToxAOD::m_multiTruth
SG::ReadHandleKey< PRD_MultiTruthCollection > m_multiTruth
Definition: TRT_PrepDataToxAOD.h:60
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
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
TRT_PrepDataToxAOD::m_SDOcontainer
SG::ReadHandleKey< InDetSimDataCollection > m_SDOcontainer
Definition: TRT_PrepDataToxAOD.h:61
TRT_PrepDataToxAOD::m_neighbourSvc
ServiceHandle< ITRT_StrawNeighbourSvc > m_neighbourSvc
Definition: TRT_PrepDataToxAOD.h:69
xAOD::TrackMeasurementValidation_v1
Class describing a TrackMeasurementValidation.
Definition: TrackMeasurementValidation_v1.h:27
WriteHandle.h
Handle class for recording to StoreGate.
TRT_PrepDataToxAOD::kXenon
@ kXenon
Definition: TRT_PrepDataToxAOD.h:43
TRT_ID::straw
int straw(const Identifier &id) const
Definition: TRT_ID.h:902
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
xAOD::TrackMeasurementValidation_v1::setLocalPosition
void setLocalPosition(float localX, float localY)
Sets the local position.
python.Dumpers.barcodes
def barcodes(beg, end, sz)
Definition: Dumpers.py:2800
lumiFormat.i
int i
Definition: lumiFormat.py:85
TRT_PrepDataToxAOD::initialize
virtual StatusCode initialize()
Definition: TRT_PrepDataToxAOD.cxx:65
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TRT_PrepDataToxAOD::m_TRTStrawSummaryTool
ToolHandle< ITRT_StrawStatusSummaryTool > m_TRTStrawSummaryTool
Definition: TRT_PrepDataToxAOD.h:70
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
TRT_PrepDataToxAOD::kEmKr
@ kEmKr
Definition: TRT_PrepDataToxAOD.h:43
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
TRT_PrepDataToxAOD::finalize
virtual StatusCode finalize()
Definition: TRT_PrepDataToxAOD.cxx:328
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
TRT_ID::barrel_ec
int barrel_ec(const Identifier &id) const
Values of different levels (failure returns 0)
Definition: TRT_ID.h:866
TRT_ID::straw_layer
int straw_layer(const Identifier &id) const
Definition: TRT_ID.h:893
xAOD::TrackMeasurementValidation_v1::setIdentifier
void setIdentifier(uint64_t identifier)
Sets the identifier.
TRT_PrepDataToxAOD::execute
virtual StatusCode execute()
Definition: TRT_PrepDataToxAOD.cxx:97
TRT_ID::layer_or_wheel
int layer_or_wheel(const Identifier &id) const
Definition: TRT_ID.h:884
beamspotman.stat
stat
Definition: beamspotman.py:266
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
TRT_PrepDataToxAOD::m_useTruthInfo
Gaudi::Property< bool > m_useTruthInfo
Definition: TRT_PrepDataToxAOD.h:55
TRT_PrepDataToxAOD::m_xAodOffset
SG::WriteHandleKey< std::vector< unsigned int > > m_xAodOffset
Definition: TRT_PrepDataToxAOD.h:63
TRT_DriftCircleContainer.h
xAOD::TrackMeasurementValidation_v1::setGlobalPosition
void setGlobalPosition(float globalX, float globalY, float globalZ)
Sets the global position.
Definition: TrackMeasurementValidation_v1.cxx:50
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
InDetDD::TRT_DetectorManager::getElement
const TRT_BaseElement * getElement(Identifier id) const
Access Elements Generically---------------------------------------------—.
Definition: TRT_DetectorManager.cxx:148
xAOD::TrackMeasurementValidation_v1::setLocalPositionError
void setLocalPositionError(float localXError, float localYError, float localXYCorrelation)
Sets the local position error.
Definition: TrackMeasurementValidation_v1.cxx:37
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
TRT_ID::phi_module
int phi_module(const Identifier &id) const
Definition: TRT_ID.h:875
TRT_PrepDataToxAOD::kEmAr
@ kEmAr
Definition: TRT_PrepDataToxAOD.h:43
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
TRT_PrepDataToxAOD::m_trtcaldbTool
ToolHandle< ITRT_CalDbTool > m_trtcaldbTool
Definition: TRT_PrepDataToxAOD.h:68
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
TRTUncompressedHitCollection.h
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
InDetSimDataCollection.h
InDetDD::TRT_BaseElement::center
virtual const Amg::Vector3D & center() const override final
Element Surface: center of a straw layer.
TrackMeasurementValidation.h
TRT_PrepDataToxAOD::m_xAodContainer
SG::WriteHandleKey< xAOD::TrackMeasurementValidationContainer > m_xAodContainer
Definition: TRT_PrepDataToxAOD.h:62
PRD_MultiTruthCollection.h
TRT_PrepDataToxAOD::kArgon
@ kArgon
Definition: TRT_PrepDataToxAOD.h:43
TrackMeasurementValidationContainer.h
merge.status
status
Definition: merge.py:17
ReadHandle.h
Handle class for reading from StoreGate.
TRT_PrepDataToxAOD::m_driftFunctionTool
ToolHandle< ITRT_DriftFunctionTool > m_driftFunctionTool
DriftFunctionTool.
Definition: TRT_PrepDataToxAOD.h:67
TRT_PrepDataToxAOD.h
test_pyathena.counter
counter
Definition: test_pyathena.py:15
TRT_PrepDataToxAOD::m_writeSDOs
Gaudi::Property< bool > m_writeSDOs
Definition: TRT_PrepDataToxAOD.h:56
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TRT_PrepDataToxAOD::kUnset
@ kUnset
Definition: TRT_PrepDataToxAOD.h:43
InDetDD::TRT_BaseElement
Definition: TRT_BaseElement.h:57
Identifier
Definition: IdentifierFieldParser.cxx:14
TRT_PrepDataToxAOD::kKrypton
@ kKrypton
Definition: TRT_PrepDataToxAOD.h:43