ATLAS Offline Software
T2TrackBeamSpotTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020, 2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <string>
6 #include <sstream>
7 #include <cmath>
8 
9 // This algorithm
10 #include "T2TrackBeamSpotTool.h"
11 
12 // Specific to this algorithm
13 #include "T2Track.h"
15 #include "T2TrackBSLLPoly.h"
16 
17 // Generic Trigger tools
19 
20 //Conversion units
21 #include "GaudiKernel/SystemOfUnits.h"
22 
23 using Gaudi::Units::GeV;
24 using Gaudi::Units::mm;
25 
26 
27 using namespace PESA;
28 
29 namespace {
30 
31 
32 // BCID is in the range 0 to NUM_BCID-1 (inclusive)
33 unsigned const NUM_BCID = 3564;
34 
35 /*
36 * This struct wraps a special monitored array which holds elements of
37 * matrices used by LS fitter. Array elements need to be mapped to
38 * individual histogram bins (have to use "kVec" option) and summed
39 * across whole farm.
40 */
41 struct MonitoredLSMatrices {
42 
43  // update with a single track
44  void update(T2TrackBeamSpotTool::TrackData const& seed, double beamSize);
45 
46  void publish(ToolHandle<GenericMonitoringTool> const& monTool);
47 
48  std::vector<double> beamLSMatrices; // 16 elements total
49  std::vector<double> beamLSMatricesBCID; // 8 * NUM_BCID elements
50 
51 };
52 
53 // update with a single track
54 void MonitoredLSMatrices::update(T2TrackBeamSpotTool::TrackData const& seed, double beamSize) {
55 
56  unsigned const sizeBCID = 2*(2+1)/2 + 2 + 2 + 1;
57 
58  if (beamLSMatrices.empty()) {
59  // reserve space for elements of all matrices:
60  // - 4x4 symmetrical matrix
61  // - 4-element vector
62  // - 1 sum of d0 squares
63  // - 1 track count
64  beamLSMatrices.resize(4*(4+1)/2 + 4 + 1 + 2 + 1);
65  // per-BCID needs space for:
66  // - 2x2 symmetrical matrix
67  // - 2-element vector
68  beamLSMatricesBCID.resize(sizeBCID * NUM_BCID);
69  }
70 
71  double const sin_phi0 = std::sin(seed.phi0);
72  double const cos_phi0 = std::cos(seed.phi0);
73  double const param[] = { -sin_phi0, cos_phi0, -seed.z0*sin_phi0, seed.z0*cos_phi0};
74  double d0_var = seed.d0_var + beamSize*beamSize;
75  int idx = 0;
76  for (int i = 0; i < 4; ++ i) {
77  for (int j = 0; j <= i; ++ j) {
78  beamLSMatrices[idx++] += param[i]*param[j] / d0_var;
79  }
80  }
81  for (int i = 0; i < 4; ++ i) {
82  beamLSMatrices[idx++] += seed.d0*param[i] / d0_var;
83  }
84  beamLSMatrices[idx++] += seed.d0*seed.d0 / d0_var;
85  beamLSMatrices[idx++] += seed.z0;
86  beamLSMatrices[idx++] += seed.z0*seed.z0;
87  beamLSMatrices[idx++] += 1;
88 
89  idx = sizeBCID * seed.bcid;
90  beamLSMatricesBCID[idx++] += param[0]*param[0] / d0_var;
91  beamLSMatricesBCID[idx++] += param[0]*param[1] / d0_var;
92  beamLSMatricesBCID[idx++] += param[1]*param[1] / d0_var;
93  beamLSMatricesBCID[idx++] += seed.d0*param[0] / d0_var;
94  beamLSMatricesBCID[idx++] += seed.d0*param[1] / d0_var;
95  beamLSMatricesBCID[idx++] += seed.z0;
96  beamLSMatricesBCID[idx++] += seed.z0*seed.z0;
97  beamLSMatricesBCID[idx++] += 1;
98 }
99 
100 void MonitoredLSMatrices::publish(ToolHandle<GenericMonitoringTool> const& monTool)
101 {
102  if (not beamLSMatrices.empty()) {
103  auto matrices = Monitored::Collection("BeamLSMatrices", beamLSMatrices);
104  auto matricesBCID = Monitored::Collection("BeamLSMatricesBCID", beamLSMatricesBCID);
105  auto mon = Monitored::Group(monTool, matrices, matricesBCID);
106  }
107 }
108 
109 } // namespace
110 
111 
112 
113 T2TrackBeamSpotTool::T2TrackBeamSpotTool( const std::string& type, const std::string& name, const IInterface* parent )
115 {
116  declareProperty("doLeastSquares", m_doLeastSquares = true);
117  declareProperty("doLogLikelihood", m_doLogLikelihood = true);
118  declareProperty("beamSizeLS", m_beamSizeLS = 0.01*mm);
119 }
120 
122 {
123  ATH_MSG_INFO("Initialising T2TrackBeamSpotTool tool");
124 
125  // Retrieve tools
126  ATH_CHECK(m_trackFilterTool.retrieve());
127  if (!m_monTool.empty()) ATH_CHECK(m_monTool.retrieve());
128 
129  return StatusCode::SUCCESS;
130 }
131 
132 void T2TrackBeamSpotTool::updateBS(const TrackCollection& tracks, EventIDBase const& eventID) const
133 {
134  //Select tracks
135  auto selectedTracks = m_trackFilterTool->filter(tracks);
136 
137  std::vector<TrackData> bsTracks;
138  bool has_bs = m_trackFilterTool->updateBS(selectedTracks, eventID.lumi_block(),
139  eventID.bunch_crossing_id(), &bsTracks);
140 
141  // when beamspot is known we can call actual update methods
142  if (has_bs) {
143  if (not bsTracks.empty()) {
144  updateBS(std::move(bsTracks));
145  } else {
146  updateBS(m_trackFilterTool->filterBS(selectedTracks), eventID.bunch_crossing_id());
147  }
148  }
149 }
150 
151 void T2TrackBeamSpotTool::updateBS(std::vector<const Trk::Track*>&& tracks, unsigned bcid) const
152 {
153  auto timer = Monitored::Timer("TIME_updateBS");
154  if (m_doLeastSquares) {
155  ATH_MSG_DEBUG("Fill LS matrices with " << tracks.size() << " Trk::Track tracks");
156  MonitoredLSMatrices lsMatrices;
157  for (auto& track: tracks) {
158  TrackData tdata(*track->perigeeParameters(), 0, bcid);
159  lsMatrices.update(tdata, m_beamSizeLS);
160  }
161  lsMatrices.publish(m_monTool);
162  }
163  if (m_doLogLikelihood) {
164  ATH_MSG_DEBUG("Fill LL coefficients with " << tracks.size() << " Trk::Track tracks");
165  std::vector<double> poly_coeff;
167  for (auto& track: tracks) {
168  auto const& par = track->perigeeParameters()->parameters();
169  auto const& cov = track->perigeeParameters()->covariance();
170  llpoly.update(par[Trk::z0], par[Trk::d0], par[Trk::phi0], (*cov)(Trk::d0, Trk::d0), poly_coeff);
171  }
172  if (not poly_coeff.empty()) {
173  auto coeff = Monitored::Collection("TrackLLPolyCoeff", poly_coeff);
174  auto mon = Monitored::Group(m_monTool, coeff);
175  }
176  }
178 }
179 
180 void T2TrackBeamSpotTool::updateBS(std::vector<TrackData>&& tracks) const
181 {
182  auto timer = Monitored::Timer("TIME_updateBS");
183  if (m_doLeastSquares) {
184  ATH_MSG_DEBUG("Fill LS matrices with " << tracks.size() << " TrackData tracks");
185  MonitoredLSMatrices lsMatrices;
186  for (auto& track: tracks) {
187  lsMatrices.update(track, m_beamSizeLS);
188  }
189  lsMatrices.publish(m_monTool);
190  }
191  if (m_doLogLikelihood) {
192  ATH_MSG_DEBUG("Fill LL coefficients with " << tracks.size() << " TrackData tracks");
193  std::vector<double> poly_coeff;
195  for (auto& track: tracks) {
196  llpoly.update(track.z0, track.d0, track.phi0, track.d0_var, poly_coeff);
197  }
198  auto coeff = Monitored::Collection("TrackLLPolyCoeff", poly_coeff);
199  auto mon = Monitored::Group(m_monTool, coeff);
200  }
202 }
PESA::T2TrackBeamSpotTool::initialize
StatusCode initialize() final
Definition: T2TrackBeamSpotTool.cxx:121
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
TrackParameters.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
PESA::T2TrackBeamSpotTool::m_doLeastSquares
bool m_doLeastSquares
Definition: T2TrackBeamSpotTool.h:63
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
PESA::T2TrackBSLLPoly
Class that knows details of LogLikelihood approximation with a polynomial.
Definition: T2TrackBSLLPoly.h:58
PESA::T2TrackBeamSpotTool::m_beamSizeLS
double m_beamSizeLS
Definition: T2TrackBeamSpotTool.h:65
plotBeamSpotVxVal.cov
cov
Definition: plotBeamSpotVxVal.py:201
Trk::z0
@ z0
Definition: ParamDefs.h:70
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
T2TrackBSLLPoly.h
PESA::T2TrackBSLLPoly::update
void update(double z0, double d0, double phi0, double d0_var, std::vector< double > &coeff)
Update polynomial coefficients with track data.
Definition: T2TrackBSLLPoly.cxx:112
PESA::T2TrackBeamSpotTool::updateBS
void updateBS(const TrackCollection &tracks, EventIDBase const &eventID) const
Update beam spot data with new track information.
Definition: T2TrackBeamSpotTool.cxx:132
python.utils.AtlRunQueryTimer.timer
def timer(name, disabled=False)
Definition: AtlRunQueryTimer.py:86
Monitored::Collection
ValuesCollection< T > Collection(std::string name, const T &collection)
Declare a monitored (double-convertible) collection.
Definition: MonitoredCollection.h:38
PESA
Local tools.
Definition: T2BeamSpot.cxx:13
Generate_dsid_ranseed.seed
seed
Definition: Generate_dsid_ranseed.py:10
lumiFormat.i
int i
Definition: lumiFormat.py:92
TrackCollection
DataVector< Trk::Track > TrackCollection
This typedef represents a collection of Trk::Track objects.
Definition: TrackCollection.h:19
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
T2Track.h
PESA::T2BSTrackFilterTool::TrackData
Class which holds track parameters.
Definition: T2BSTrackFilterTool.h:38
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
Trk::d0
@ d0
Definition: ParamDefs.h:69
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
xAOD::bcid
setEventNumber setTimeStamp bcid
Definition: EventInfo_v1.cxx:133
python.SystemOfUnits.mm
int mm
Definition: SystemOfUnits.py:83
PESA::T2TrackBeamSpotTool::T2TrackBeamSpotTool
T2TrackBeamSpotTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: T2TrackBeamSpotTool.cxx:113
dqt_zlumi_pandas.update
update
Definition: dqt_zlumi_pandas.py:42
T2TrackBeamSpotTool.h
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
plotBeamSpotMon.mon
mon
Definition: plotBeamSpotMon.py:67
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
PESA::T2TrackBeamSpotTool::m_doLogLikelihood
bool m_doLogLikelihood
Definition: T2TrackBeamSpotTool.h:64
TrigRoiDescriptor.h
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
AthAlgTool
Definition: AthAlgTool.h:26
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
Monitored::Timer
A monitored timer.
Definition: MonitoredTimer.h:32
PESA::T2TrackBeamSpotTool::m_trackFilterTool
ToolHandle< T2BSTrackFilterTool > m_trackFilterTool
Definition: T2TrackBeamSpotTool.h:67
PESA::T2TrackBeamSpotTool::m_monTool
ToolHandle< GenericMonitoringTool > m_monTool
Definition: T2TrackBeamSpotTool.h:68
Trk::phi0
@ phi0
Definition: ParamDefs.h:71