ATLAS Offline Software
Loading...
Searching...
No Matches
FillAlignTRTHits.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/********************************************************************
6
7NAME:
8PACKAGE:
9
10AUTHORS:
11CREATED:
12
13PURPOSE: Tool
14
15
16********************************************************************/
17
18// INCLUDES:
21#include "FillAlignTRTHits.h"
23#include "TrkTrack/Track.h"
34
36
37
38FillAlignTRTHits::FillAlignTRTHits(const std::string& type, const std::string& name, const IInterface* parent) :
39 AthAlgTool(type, name, parent),
40 m_DetID(nullptr), m_TRTID(nullptr),
41 m_maxDistance(2.8),
49 m_f(nullptr), m_ntuple(nullptr)
50{
51 declareInterface<IFillAlignTrkInfo>(this);
52 declareProperty("maxDistance",m_maxDistance) ;
53 declareProperty("maxTimeResidual",m_maxTimeResidual) ;
54 declareProperty("minTimebinsOverThreshold",m_minTimebinsOverThreshold) ;
55 declareProperty("maxTrackChisquarePerDof",m_maxTrackChisquarePerDof) ;
56 declareProperty("DoMCCosmicTimeShift",m_DoMCCosmicTimeShift);
57}
58
60 ATH_CHECK ( detStore()->retrieve(m_DetID, "AtlasID") );
61 ATH_CHECK ( detStore()->retrieve(m_TRTID, "TRT_ID") );
62
63 ATH_CHECK ( m_trtcaldbTool.retrieve() );
64 ATH_CHECK ( m_neighbourSvc.retrieve() );
65 ATH_CHECK ( m_driftFunctionTool.retrieve() );
66 ATH_CHECK ( m_updator.retrieve() );
67 ATH_CHECK ( m_TRTStrawSummaryTool.retrieve() );
68
69 m_f = new TFile(m_ntupleName.value().c_str(),"RECREATE");
70 m_ntuple = new TNtuple("ntuple","TRT calibration ntuple","run:evt:lbn:nvx:trk:det:lay:mod:stl:stw:brd:chp:sid:locx:locy:locz:x:y:z:r:dr:t:rtrack:drrtrack:rtrackunbias:drrtrackunbias:ttrack:ttrackunbias:t0:ephase:phi:theta:pt:qoverp:d0:ToT:HT:ToTCorrection:HTCorrection:isArgonStraw");
71 return StatusCode::SUCCESS;
72}
73
74
76 m_f->Write();
77 m_f->Close();
78 std::cout << "CALIBSTAT TRKS: " << m_numOfProcessedTracks << std::endl;
79 std::cout << "CALIBSTAT HTOT: " << m_numOfHitsTotal << std::endl;
80 std::cout << "CALIBSTAT HACC: " << m_numOfHitsAccepted << std::endl;
81 return StatusCode::SUCCESS;
82}
83
84
85bool FillAlignTRTHits::fill(const Trk::Track* aTrack, TRT::TrackInfo* output, const xAOD::EventInfo& eventInfo,
86 const xAOD::VertexContainer& vertices) {
87
89 float rtrackunbias = 0;
90 float drrtrackunbias = 0;
91 float drrtrack = 0;
92 float ttrackunbias = 0;
93 (*output)[TRT::Track::numberOfPixelHits] = 0;
94 (*output)[TRT::Track::numberOfSCTHits] = 0;
95 (*output)[TRT::Track::numberOfTRTHits] = 0;
96 // loop over the TrackStateonSurfaces
97
98 const Trk::TrackParameters *unbiasedTrkParameters(nullptr);
99 const Trk::TrackStateOnSurface* HitOnTrackToRemove(nullptr);
100
101 double timecor = 0.;
102 const Trk::Track* pTrack = aTrack ;
103 const Trk::Perigee* mesp = pTrack->perigeeParameters();
104 float lbn = -1;
105 float nvrt_rec = -1;
106 double phi = 10;
107 double theta = 100;
108 double pt = 0;
109 double qoverp = 0;
110 double d0 = 0;
111
112 if(mesp){
113 phi = mesp->parameters()[Trk::phi0];
114 theta = mesp->parameters()[Trk::theta];
115 if(fabs(theta)==0) theta=1e-24;
116 float ptinv = std::abs(mesp->parameters()[Trk::qOverP]) / sin(theta);
117 qoverp = mesp->parameters()[Trk::qOverP];
118 if (ptinv != 0) {
119 pt = 1. / ptinv;
120 } else {
121 pt = 1e24;
122 }
123 d0 = mesp->parameters()[Trk::d0];
124 }
125
126 timecor = m_DoMCCosmicTimeShift ;
127
128 lbn = (float)eventInfo.lumiBlock();
129 //Number of Prim vertex:
130 nvrt_rec = 0;
131 int countVertices(0);
132 for (const xAOD::Vertex* vx : vertices) {
133 if (vx->vertexType() == xAOD::VxType::PriVtx) {
134 if ( vx-> nTrackParticles() >= 3) countVertices++;
135 }
136 }
137 nvrt_rec = countVertices;
138
139 auto tsos = aTrack->trackStateOnSurfaces()->begin();
140 auto tsosEnd = aTrack->trackStateOnSurfaces()->end();
141
142 const Trk::MeasurementBase* mesb = nullptr;
143 const Trk::RIO_OnTrack* rotp = nullptr;
144 const InDet::TRT_DriftCircle* dcp = nullptr;
145 const Trk::TrackParameters* tparp = nullptr;
146 const Trk::TrackParameters* mparp = nullptr;
147 const InDet::TRT_DriftCircleOnTrack* trtcirc = nullptr;
148 const TRTCond::RtRelation* rtrelation = nullptr;
149
150
151 for (; tsos != tsosEnd; ++tsos) {
152 mesb = (*tsos)->measurementOnTrack();
153 rotp = dynamic_cast<const Trk::RIO_OnTrack*>(mesb);
154 if(rotp != nullptr) {
155 Identifier ident = rotp->identify();
156 if (m_DetID->is_sct(ident)) {
157 (*output)[TRT::Track::numberOfSCTHits]++;
158 } else if (m_DetID->is_trt(ident)) {
159 (*output)[TRT::Track::numberOfTRTHits]++;
161 trtcirc = dynamic_cast<const InDet::TRT_DriftCircleOnTrack*>(rotp);
162 if (trtcirc != nullptr) {
163 dcp = trtcirc->prepRawData();
164 tparp = ((*tsos)->trackParameters());
165 mparp = (tparp);
166
167 if (tparp == nullptr) {
168 if (msgLvl(MSG::DEBUG)) msg() << "strange: trk parameters not available" << endmsg;
169 }
170 if (dcp == nullptr) {
171 msg(MSG::ERROR) << "strange: prepRawData not available" << endmsg;
172 }
173
174 if (mparp && dcp) {
175 TRT::HitInfo* newhit = new TRT::HitInfo();
176 output->push_back(newhit); // do we make sure this one is deleted properly
177
178 (*newhit)[TRT::Hit::ident] = ident.get_identifier32().get_compact();
179 (*newhit)[TRT::Hit::detector] = m_TRTID->barrel_ec(ident);
180 (*newhit)[TRT::Hit::layer] = m_TRTID->layer_or_wheel(ident);
181 (*newhit)[TRT::Hit::phiModule] = m_TRTID->phi_module(ident);
182 (*newhit)[TRT::Hit::strawLayer] = m_TRTID->straw_layer(ident);
183 (*newhit)[TRT::Hit::straw] = m_TRTID->straw(ident);
184 (*newhit)[TRT::Hit::side] = static_cast<int>(trtcirc->side());
185
186 // Local wire specific
188 float errsq=Amg::error(rotp->localCovariance(),Trk::driftRadius);
189 if(errsq<0) errsq=1.0;
190 (*newhit)[TRT::Hit::errorSignedDriftRadius] = sqrt(errsq) ;
191 bool isvalid = false;
192
193
194 (*newhit)[TRT::Hit::driftTime] = dcp->driftTime(isvalid) - timecor;
195 (*newhit)[TRT::Hit::driftTimeStatus] = isvalid ;
196 // this is the integer drift time, but multiplied by the scale.
197 // to calculate the residual in the drift time, I need to have the t0 as well.
198 // I'd also like to store the drift velocity
199 if (!isvalid) (*newhit)[TRT::Hit::driftTime] = -1.0;
200
201 (*newhit)[TRT::Hit::t0] = m_trtcaldbTool->getT0(ident) ;
202
204 //CORRECT FOR TUBEHITS!!!:
205 rtrelation = m_trtcaldbTool->getRtRelation(ident) ;
206 // added High Level Threshold information
207 (*newhit)[TRT::Hit::HTLevel] = dcp->highLevel();
208 // Extract the correction in the db for the ToT:
209 float tot = (*newhit)[TRT::Hit::TimeoverThreshold];
210 float ToTCorrection = m_driftFunctionTool->driftTimeToTCorrection(tot, ident);
211
212 // Extract the correction for HT:
213 float HTCorrection = 0;
214 if ((*newhit)[TRT::Hit::HTLevel]){
215 HTCorrection = m_driftFunctionTool->driftTimeHTCorrection(ident);
216 }
217
218 (*newhit)[TRT::Hit::positionOnWire] = tparp->parameters()[Trk::locZ];
219
220 (*newhit)[TRT::Hit::trackDriftRadius] = tparp->parameters()[Trk::driftRadius];
221 errsq=Amg::error(*(mparp->covariance()),Trk::locZ);
222 if(errsq<0) errsq=1.0;
223 (*newhit)[TRT::Hit::errorPositionOnWire] = sqrt(errsq);
224 errsq=Amg::error(*(mparp->covariance()),Trk::driftRadius);
225 if(errsq<0) errsq=1.0;
226 (*newhit)[TRT::Hit::errorTrackDriftRadius] = sqrt(errsq);
227 // calculate the 'trktime' and the 'trkdriftvelocity'
228 if( rtrelation ) {
229 (*newhit)[TRT::Hit::trackDriftTime] = rtrelation->drifttime(std::abs( (*newhit)[TRT::Hit::trackDriftRadius] )) ;
230 (*newhit)[TRT::Hit::driftVelocity] = rtrelation->drdt( (*newhit)[TRT::Hit::trackDriftTime] ) ;
231 }
232
233 (*newhit)[TRT::Hit::trackT0]= timecor ;
234
235 int chip = 0;
236 int board = -1;
237 m_neighbourSvc->getChip(ident,chip);
238 if(abs(m_TRTID->barrel_ec(ident))<2){
239 board = m_neighbourSvc->chipToBoardBarrel(chip, m_TRTID->layer_or_wheel(ident));
240 } else if (chip<12) {
241 board = 0;
242 } else {
243 chip = chip-20;
244 board = 1;
245 }
246
247 // Prepare for Xe-Ar mixed conditions:
248 int isArgonStraw = 0;
249 if (!m_TRTStrawSummaryTool.empty()) {
250 if (m_TRTStrawSummaryTool->getStatusHT(ident, Gaudi::Hive::currentContext()) != TRTCond::StrawStatus::Good) {
251 isArgonStraw = 1;
252 }
253 }
254
255
256 float h_trkDistance = (*newhit)[TRT::Hit::trackDriftRadius];
257
258 float h_driftTime = (*newhit)[TRT::Hit::driftTime] - (*newhit)[TRT::Hit::t0];
259 float h_trkDriftTime = (*newhit)[TRT::Hit::trackDriftTime];
260 float h_timeResidual = h_driftTime - h_trkDriftTime;
261
262 float h_trkVariance = (*newhit)[TRT::Hit::errorTrackDriftRadius] * (*newhit)[TRT::Hit::errorTrackDriftRadius];
263
264 bool h_hasValidDriftTime = (*newhit)[TRT::Hit::driftTimeStatus] ;
265
266 float h_timeOverThreshold = (*newhit)[TRT::Hit::TimeoverThreshold] ;
267
268 float h_residual = (*newhit)[TRT::Hit::signedDriftRadius] - (*newhit)[TRT::Hit::trackDriftRadius] ;
269 float h_residualVariance = h_trkVariance + ((*newhit)[TRT::Hit::errorSignedDriftRadius] * (*newhit)[TRT::Hit::errorSignedDriftRadius]);
270 float d = h_residualVariance;
271 if(d==0) d=1.0e-24;
272 float h_chiSquare = h_residual*h_residual/d ;
273 int dof = (*output)[TRT::Track::degreesOfFreedom]-1;
274 if(dof<1) dof=1;
275 bool hitsel=false;
276 if( std::abs( h_trkDistance ) < m_maxDistance &&
277 std::abs( h_timeResidual ) < m_maxTimeResidual &&
278 h_trkVariance > 0 &&
279 h_hasValidDriftTime &&
280 h_timeOverThreshold/3.125 >= m_minTimebinsOverThreshold &&
281 ((*output)[TRT::Track::chiSquare] - h_chiSquare) / (float)dof < m_maxTrackChisquarePerDof ){
282 hitsel = true;
283 }
284
286 rtrackunbias = 0;
287 drrtrackunbias = 0;
288 errsq = Amg::error(*(mparp->covariance()),Trk::driftRadius);
289 drrtrack = sqrt(errsq);
290 ttrackunbias = 0;
291
292 if (m_updator){
293 tparp = ((*tsos)->trackParameters());
294 HitOnTrackToRemove = *tsos;
295
296 if(HitOnTrackToRemove){
297 unbiasedTrkParameters = m_updator->removeFromState(*(HitOnTrackToRemove->trackParameters()),
298 HitOnTrackToRemove->measurementOnTrack()->localParameters(),
299 HitOnTrackToRemove->measurementOnTrack()->localCovariance()).release();
300 ATH_MSG_DEBUG ("TrackParameters 1: " << *(HitOnTrackToRemove->trackParameters()));
301 }
302 else if (msgLvl(MSG::DEBUG)) {
303 msg() << "TrackParameters 1: nullptr" << endmsg;
304 }
305
306 if(unbiasedTrkParameters){
307 const Trk::TrackParameters *unmparp = (unbiasedTrkParameters);
308 rtrackunbias = unbiasedTrkParameters->parameters()[Trk::driftRadius];
309 errsq=Amg::error(*(unmparp->covariance()),Trk::driftRadius);
310 if(errsq<0) errsq=1.;
311 drrtrackunbias = sqrt(errsq);
312
313 if( rtrelation ) ttrackunbias = rtrelation->drifttime(std::abs( rtrackunbias ));
314 ATH_MSG_DEBUG("Unbiased TrackParameters 2: " << *unbiasedTrkParameters );
315 ATH_MSG_DEBUG("Radius : " << (*newhit)[TRT::Hit::trackDriftRadius] );
316 ATH_MSG_DEBUG("Radius 2: " << rtrackunbias );
317 }
318
319 }
321 float const ntvar[40]={
322 (float)(*output)[TRT::Track::run],
323 (float)(*output)[TRT::Track::event],
324 lbn,
325 nvrt_rec,
326 (float)(*output)[TRT::Track::trackNumber],
327
328 (float)(*newhit)[TRT::Hit::detector],
329 (float)(*newhit)[TRT::Hit::layer],
330 (float)(*newhit)[TRT::Hit::phiModule],
331 (float)(*newhit)[TRT::Hit::strawLayer],
332 (float)(*newhit)[TRT::Hit::straw],
333 (float)board,
334 (float)chip,
335 (float)m_TRTID->straw_id((*newhit)[TRT::Hit::detector],
336 (*newhit)[TRT::Hit::phiModule],
337 (*newhit)[TRT::Hit::layer],
338 (*newhit)[TRT::Hit::strawLayer],
339 (*newhit)[TRT::Hit::straw]).get_identifier32().get_compact() ,
340 (float)tparp->position().x(),
341 (float)tparp->position().y(),
342 (float)tparp->position().z(),
343 (float)(rotp->detectorElement()->center(ident)).x(),
344 (float)(rotp->detectorElement()->center(ident)).y(),
345 (float)(rotp->detectorElement()->center(ident)).z(),
346
349 (*newhit)[TRT::Hit::driftTime],
351 drrtrack,
352 rtrackunbias,
353 drrtrackunbias,
354 (*newhit)[TRT::Hit::trackDriftTime],
355 ttrackunbias,
356 m_trtcaldbTool->getT0(ident),
357 (float)timecor,
358 (float)phi ,
359 (float)theta,
360 (float)pt ,
361 (float)qoverp ,
362 (float)d0 ,
364 (*newhit)[TRT::Hit::HTLevel],
365 ToTCorrection,
366 HTCorrection,
367 (float)isArgonStraw
368 };
369
370 if (hitsel) {
372 m_ntuple->Fill(ntvar);
373 }
374
375 }
376 } else {
377 msg(MSG::ERROR) << "TRT drift RIO cast failed - no hit stored" << endmsg;
378 }
379
380
381 } // identified TRT hit
382 else if (m_DetID->is_pixel(ident)) (*output)[TRT::Track::numberOfPixelHits]++;
383 } // non-zero ROTpointer
384 } // end loop on Surfaces
385 if (msgLvl(MSG::VERBOSE)) msg() << "Track has " << (*output)[TRT::Track::numberOfTRTHits] << " TRT hits --> of which "
386 << output->size() << " hits had FULL info available" << endmsg;
387
388
389
390 delete unbiasedTrkParameters;
391
392
393 return true;
394}
Scalar phi() const
phi method
Scalar theta() const
theta method
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x)
This class provides an interface to generate or decode an identifier for the upper levels of the dete...
abstract base class for rt-relations
This is an Identifier helper class for the TRT subdetector.
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
const ServiceHandle< StoreGateSvc > & detStore() const
bool msgLvl(const MSG::Level lvl) const
MsgStream & msg() const
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
FillAlignTRTHits(const std::string &type, const std::string &name, const IInterface *parent)
virtual StatusCode finalize()
virtual bool fill(const Trk::Track *aTrack, TRT::TrackInfo *output, const xAOD::EventInfo &eventInfo, const xAOD::VertexContainer &vertices)
const TRT_ID * m_TRTID
unsigned int m_numOfHitsTotal
Gaudi::Property< std::string > m_ntupleName
unsigned int m_numOfProcessedTracks
ToolHandle< Trk::IUpdator > m_updator
virtual StatusCode initialize()
ServiceHandle< ITRT_StrawNeighbourSvc > m_neighbourSvc
const AtlasDetectorID * m_DetID
unsigned int m_numOfHitsAccepted
ToolHandle< ITRT_CalDbTool > m_trtcaldbTool
ToolHandle< ITRT_StrawStatusSummaryTool > m_TRTStrawSummaryTool
ToolHandle< ITRT_DriftFunctionTool > m_driftFunctionTool
Represents 'corrected' measurements from the TRT (for example, corrected for wire sag).
Trk::DriftCircleSide side() const
returns the side on which the drift radius is.
virtual const TRT_DriftCircle * prepRawData() const override final
returns the PrepRawData - is a TRT_DriftCircle in this scope
bool highLevel() const
returns true if the high level threshold was passed
double timeOverThreshold() const
returns Time over threshold in ns
double driftTime(bool &valid) const
returns the raw driftTime, the passed boolean indicates if the drift time is valid or not.
Base class for rt-relations in the TRT.
Definition RtRelation.h:27
virtual float drdt(float driftime) const =0
driftvelocity for given drifttime
virtual float drifttime(float radius) const =0
drifttime for given radius
This class is the pure abstract base class for all fittable tracking measurements.
const LocalParameters & localParameters() const
Interface method to get the LocalParameters.
const Amg::MatrixX & localCovariance() const
Interface method to get the localError.
const Amg::Vector3D & position() const
Access method for the position.
Class to handle RIO On Tracks ROT) for InDet and Muons, it inherits from the common MeasurementBase.
Definition RIO_OnTrack.h:70
virtual const TrkDetElementBase * detectorElement() const =0
returns the detector element, assoicated with the PRD of this class
Identifier identify() const
return the identifier -extends MeasurementBase
represents the track state (measurement, material, fit parameters and quality) at a surface.
const MeasurementBase * measurementOnTrack() const
returns MeasurementBase const overload
const TrackParameters * trackParameters() const
return ptr to trackparameters const overload
const Trk::TrackStates * trackStateOnSurfaces() const
return a pointer to a const DataVector of const TrackStateOnSurfaces.
const Perigee * perigeeParameters() const
return Perigee.
virtual const Amg::Vector3D & center() const =0
Return the center of the element.
uint32_t lumiBlock() const
The current event's luminosity block number.
double error(const Amg::MatrixX &mat, int index)
return diagonal error of the matrix caller should ensure the matrix is symmetric and the index is in ...
@ layer
Definition HitInfo.h:79
@ strawLayer
Definition HitInfo.h:81
@ phiModule
Definition HitInfo.h:80
@ ident
Definition HitInfo.h:77
@ driftTimeStatus
Definition HitInfo.h:84
@ straw
Definition HitInfo.h:82
@ detector
Definition HitInfo.h:78
@ TimeoverThreshold
Definition HitInfo.h:44
@ driftVelocity
Definition HitInfo.h:50
@ trackT0
Definition HitInfo.h:52
@ driftTime
Definition HitInfo.h:43
@ signedDriftRadius
Definition HitInfo.h:40
@ errorTrackDriftRadius
Definition HitInfo.h:49
@ errorPositionOnWire
Definition HitInfo.h:47
@ errorSignedDriftRadius
Definition HitInfo.h:41
@ trackDriftRadius
Definition HitInfo.h:48
@ HTLevel
Definition HitInfo.h:45
@ positionOnWire
Definition HitInfo.h:46
@ trackDriftTime
Definition HitInfo.h:51
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
@ driftRadius
trt, straws
Definition ParamDefs.h:53
@ phi0
Definition ParamDefs.h:65
@ theta
Definition ParamDefs.h:66
@ qOverP
perigee
Definition ParamDefs.h:67
@ locZ
local cylindrical
Definition ParamDefs.h:42
@ d0
Definition ParamDefs.h:63
ParametersBase< TrackParametersDim, Charged > TrackParameters
@ PriVtx
Primary vertex.
EventInfo_v1 EventInfo
Definition of the latest event info version.
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.