ATLAS Offline Software
Loading...
Searching...
No Matches
FPGATrackSimMatrixReductionAlgo.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2
9
13
14#include "TFile.h"
15#include "TTree.h"
16
17#include <sstream>
18#include <cassert>
19
20
22// Initialize
24
25FPGATrackSimMatrixReductionAlgo::FPGATrackSimMatrixReductionAlgo(const std::string& name, ISvcLocator* pSvcLocator) :
26 AthAlgorithm(name, pSvcLocator)
27{
28}
29
31{
33 ATH_CHECK(m_tHistSvc.retrieve());
34
35 m_pmap_1st = m_FPGATrackSimMapping->PlaneMap_1st(0);
36 m_pmap_2nd = m_FPGATrackSimMapping->PlaneMap_2nd(0);
37
38 // Setup the boundaries for the merge
39 if (m_allregion)
40 {
43 }
44 else
45 {
48 }
50
51 ATH_MSG_INFO("Reading " << m_filePath);
52 TFile *file = TFile::Open(m_filePath.value().c_str(), "");
54
55 // Main execution function
57
58 return StatusCode::SUCCESS;
59}
60
61
63{
64 // Is there a better way to do this? Idk how the interface to tHistSvc works
68
69 // Read old tree
70 TTree *old_tree = (TTree*)file->Get("slice");
71 old_tree->SetBranchAddress("c_max", &max.qOverPt);
72 old_tree->SetBranchAddress("c_min", &min.qOverPt);
73 old_tree->SetBranchAddress("c_slices", &nBins.qOverPt);
74
75 old_tree->SetBranchAddress("phi_max", &max.phi);
76 old_tree->SetBranchAddress("phi_min", &min.phi);
77 old_tree->SetBranchAddress("phi_slices", &nBins.phi);
78
79 old_tree->SetBranchAddress("d0_max", &max.d0);
80 old_tree->SetBranchAddress("d0_min", &min.d0);
81 old_tree->SetBranchAddress("d0_slices", &nBins.d0);
82
83 old_tree->SetBranchAddress("z0_max", &max.z0);
84 old_tree->SetBranchAddress("z0_min", &min.z0);
85 old_tree->SetBranchAddress("z0_slices", &nBins.z0);
86
87 old_tree->SetBranchAddress("eta_max", &max.eta);
88 old_tree->SetBranchAddress("eta_min", &min.eta);
89 old_tree->SetBranchAddress("eta_slices", &nBins.eta);
90
91 old_tree->GetEntry(0);
92
93 // Write new tree
94 TTree *new_tree = new TTree("slice", "Slice boundaries");
95 ATH_CHECK(m_tHistSvc->regTree("/TRIGFPGATrackSimMATRIX1STSTAGEOUT/slice", new_tree));
96
97 new_tree->Branch("c_max", &max.qOverPt);
98 new_tree->Branch("c_min", &min.qOverPt);
99 new_tree->Branch("c_slices", &nBins.qOverPt);
100
101 new_tree->Branch("phi_max", &max.phi);
102 new_tree->Branch("phi_min", &min.phi);
103 new_tree->Branch("phi_slices", &nBins.phi);
104
105 new_tree->Branch("d0_max", &max.d0);
106 new_tree->Branch("d0_min", &min.d0);
107 new_tree->Branch("d0_slices", &nBins.d0);
108
109 new_tree->Branch("z0_max", &max.z0);
110 new_tree->Branch("z0_min", &min.z0);
111 new_tree->Branch("z0_slices", &nBins.z0);
112
113 new_tree->Branch("eta_max", &max.eta);
114 new_tree->Branch("eta_min", &min.eta);
115 new_tree->Branch("eta_slices", &nBins.eta);
116
117 new_tree->Fill();
118
119 return StatusCode::SUCCESS;
120}
121
122
124
125
126/*
127 * Matches first stage layers to second stage layers.
128 *
129 * @return { layer_map, coord_map }
130 * - layer_map[1st stage layer] = 2nd stage layer
131 * - coord_map[1st stage coord] = 2nd stage coord (index)
132 *
133 * TODO this should be handled by the pmap?
134 */
135
136std::pair<std::vector<size_t>, std::vector<size_t>> FPGATrackSimMatrixReductionAlgo::matchStages()
137{
138 size_t iCoord_1st = 0;
139
140 std::vector<size_t> layers_1st_to_2nd(m_pmap_1st->getNLogiLayers());
141 std::vector<size_t> coords_1st_to_2nd(m_pmap_1st->getNCoords());
142
143 for (unsigned layer_1st = 0; layer_1st < m_pmap_1st->getNLogiLayers(); layer_1st++)
144 {
145 LayerInfo const & info_1st = m_pmap_1st->getLayerInfo(layer_1st, 0);
146 // all sections in stage 1 should also be in stage 2, so can just use 0 to retrieve info
147 LayerSection const & ls_2nd = m_pmap_2nd->getLayerSection(info_1st);
148
149 int inversion(0);
150 layers_1st_to_2nd[layer_1st] = ls_2nd.layer + inversion;
151 for (size_t iDim = 0; iDim < m_pmap_1st->getDim(layer_1st); iDim++)
152 {
153 coords_1st_to_2nd[iCoord_1st] = m_pmap_2nd->getCoordOffset(ls_2nd.layer) + iDim + inversion;
154 iCoord_1st++;
155 }
156 }
157
158 assert(iCoord_1st == m_pmap_1st->getNCoords());
159 return { layers_1st_to_2nd, coords_1st_to_2nd };
160}
161
162
163/*
164 * Reduces a second stage sector and corresponding matrix to first stage.
165 *
166 * This simply copies the sector matrices from the input file,
167 * but only keeping 1st stage layers/coordinates. In python pseudocode,
168 * layer_filter = [ is1stStage(layer_2nd) for layer_2nd in range(# 2nd stage layers) ]
169 * modules_1st = modules_2nd[layer_filter]
170 * and similarly for the coordinate-length members.
171 *
172 * @param modules_2nd - Second stage sector definition (read from file)
173 * @param acc_2nd - Second stage accumulator (read from file)
174 * @param layers_1st_to_2nd - Layer mapping from s1 to s2
175 * @param coords_1st_to_2nd - Coordinate (index) mapping from s1 to s2
176 *
177 * @return { modules_1st, acc_1st}
178 * - modules_1st - First stage sector definition
179 * - acc_1st - First stage matrix accumulator
180 */
182 std::vector<module_t> const & modules_2nd, FPGATrackSimMatrixAccumulator const & acc_2nd,
183 std::vector<size_t> const & layers_1st_to_2nd, std::vector<size_t> const & coords_1st_to_2nd)
184{
185 std::vector<module_t> modules_1st(m_pmap_1st->getNLogiLayers()); // First stage sector definition (to be filled)
186 FPGATrackSimMatrixAccumulator acc_1st(m_pmap_1st->getNLogiLayers(), m_pmap_1st->getNCoords()); // First stage accumulator (to be filled)
187
188 acc_1st.pars = acc_2nd.pars;
189 acc_1st.track_bins = acc_2nd.track_bins;
190
191 // For each first stage layer, copy relavant info from matching second stage layer
192 for (size_t layer_1st = 0; layer_1st < m_pmap_1st->getNLogiLayers(); layer_1st++)
193 {
194 size_t layer_2nd = layers_1st_to_2nd[layer_1st];
195 modules_1st[layer_1st] = modules_2nd[layer_2nd];
196 acc_1st.FTK_modules[layer_1st] = acc_2nd.FTK_modules[layer_2nd];
197 }
198
199 // Copy coordinates from matching second stage coordinate
200 for (size_t iCoord_1st = 0; iCoord_1st < m_pmap_1st->getNCoords(); iCoord_1st++)
201 {
202 size_t iCoord_2nd = coords_1st_to_2nd[iCoord_1st];
203
204 acc_1st.hit_coords[iCoord_1st] = acc_2nd.hit_coords[iCoord_2nd];
205 acc_1st.hit_x_QoP [iCoord_1st] = acc_2nd.hit_x_QoP [iCoord_2nd];
206 acc_1st.hit_x_d0 [iCoord_1st] = acc_2nd.hit_x_d0 [iCoord_2nd];
207 acc_1st.hit_x_z0 [iCoord_1st] = acc_2nd.hit_x_z0 [iCoord_2nd];
208 acc_1st.hit_x_eta [iCoord_1st] = acc_2nd.hit_x_eta [iCoord_2nd];
209 acc_1st.hit_x_phi [iCoord_1st] = acc_2nd.hit_x_phi [iCoord_2nd];
210
211 // Covariance calculation. This requires a second loop through layers/coordinates,
212 // so postfix those variables with _j, as in (i,j). Only need triangular matrix
213 // so start loop at iCoord_1st.
214 for (size_t iCoord_1st_j = iCoord_1st; iCoord_1st_j < m_pmap_1st->getNCoords(); iCoord_1st_j++)
215 {
216 size_t iCoord_2nd_j = coords_1st_to_2nd[iCoord_1st_j];
217 acc_1st.covariance[iCoord_1st * m_pmap_1st->getNCoords() + iCoord_1st_j] =
218 acc_2nd.covariance[iCoord_2nd * m_pmap_2nd->getNCoords() + iCoord_2nd_j];
219 }
220 }
221
222 return { modules_1st, acc_1st };
223}
224
225
226/*
227 * Runs the main algorithm, accumulating the 1st stage matrices from file into m_sector_cum.
228 */
230{
231 auto matched_layers_coords = matchStages();
232 // Read 2nd stage info from file
233 TTree *tree_2nd = (TTree*)file->Get(Form("am0"));
234 FPGATrackSimMatrixReader reader(tree_2nd, m_pmap_2nd->getNLogiLayers(), m_pmap_2nd->getNCoords());
235
236 // Create 1st stage matrix and modules, and accumulate into m_sector_cum
237 while (reader.nextEntry())
238 {
239 auto sector_acc = reduce(reader.getModules(), reader.getAccumulator(), matched_layers_coords.first, matched_layers_coords.second);
240 ::accumulate(m_sector_cum[0], sector_acc.first, sector_acc.second);
241 }
242}
243
244
246// Execute
248
249
251{
252 // Do nothing; this class does not process events. The main algorithm is
253 // called in initialize() and finalize().
254 return StatusCode::SUCCESS;
255}
256
258// Finalize
260
262{
263 {
264 // Create the tree
265 std::stringstream name;
266 std::stringstream title;
267 name << "am0";
268 title << "Ambank 0" << " parameters";
269 TTree* tree = new TTree(name.str().c_str(), title.str().c_str());
270 ATH_CHECK(m_tHistSvc->regTree(Form("/TRIGFPGATrackSimMATRIX1STSTAGEOUT/%s",tree->GetName()), tree));
271
272 // Fill the tree
273 ::fillTree(m_sector_cum[0], tree, m_pmap_1st->getNLogiLayers(), m_pmap_1st->getNCoords());
274 }
275
276 ATH_CHECK(m_tHistSvc->finalize());
277 return StatusCode::SUCCESS;
278}
279
280
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
void fillTree(AccumulateMap &map, TTree *tree, int nLayers, int nCoords)
Writes the contents of an AccumulateMap into the supplied tree (one entry per sector).
Classes to read/write matrix files event by event.
Algorithm to reduce matrix files from 2nd stage to 1st stage.
Maps physical layers to logical layers.
#define min(a, b)
Definition cfImp.cxx:40
#define max(a, b)
Definition cfImp.cxx:41
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
std::pair< std::vector< module_t >, FPGATrackSimMatrixAccumulator > reduce(std::vector< module_t > const &modules_2nd, FPGATrackSimMatrixAccumulator const &acc_2nd, std::vector< size_t > const &layers_1st_to_2nd, std::vector< size_t > const &coords_1st_to_2nd)
ServiceHandle< IFPGATrackSimMappingSvc > m_FPGATrackSimMapping
FPGATrackSimMatrixReductionAlgo(const std::string &name, ISvcLocator *pSvcLocator)
std::pair< std::vector< size_t >, std::vector< size_t > > matchStages()
std::vector< FPGATrackSimTrackParsI > track_bins
TChain * tree
TFile * file