Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
FPGATrackSimMatrixReductionAlgo.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
11 #include "FPGATrackSimMatrixIO.h"
13 
14 #include "TFile.h"
15 #include "TTree.h"
16 
17 #include <sstream>
18 #include <cassert>
19 
20 
22 // Initialize
24 
25 FPGATrackSimMatrixReductionAlgo::FPGATrackSimMatrixReductionAlgo(const std::string& name, ISvcLocator* pSvcLocator) :
26  AthAlgorithm(name, pSvcLocator)
27 {
28 }
29 
31 {
32  ATH_CHECK(m_FPGATrackSimMapping.retrieve());
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  {
41  m_region_start = 0;
43  }
44  else
45  {
47  m_region_end = m_region + 1;
48  }
49  m_sector_cum.resize(m_nRegions);
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 
136 std::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"));
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
274  }
275 
276  ATH_CHECK(m_tHistSvc->finalize());
277  return StatusCode::SUCCESS;
278 }
279 
280 
FPGATrackSimMatrixAccumulator::hit_x_QoP
std::vector< double > hit_x_QoP
Definition: FPGATrackSimMatrixAccumulator.h:77
FPGATrackSimMatrixReductionAlgo::m_pmap_2nd
FPGATrackSimPlaneMap const * m_pmap_2nd
Definition: FPGATrackSimMatrixReductionAlgo.h:54
FPGATrackSimMatrixAccumulator::hit_x_phi
std::vector< double > hit_x_phi
Definition: FPGATrackSimMatrixAccumulator.h:83
FPGATrackSimPlaneMap::getNLogiLayers
uint32_t getNLogiLayers() const
Definition: FPGATrackSimPlaneMap.h:75
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
FPGATrackSimPlaneMap.h
Maps physical layers to logical layers.
FPGATrackSimTrackPars
Definition: FPGATrackSimTrackPars.h:22
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
FPGATrackSimMatrixReductionAlgo::m_tHistSvc
ServiceHandle< ITHistSvc > m_tHistSvc
Definition: FPGATrackSimMatrixReductionAlgo.h:52
tree
TChain * tree
Definition: tile_monitor.h:30
FPGATrackSimMatrixReductionAlgo::extract_1stStage
void extract_1stStage(TFile *file)
Definition: FPGATrackSimMatrixReductionAlgo.cxx:229
FPGATrackSimPlaneMap::getNCoords
uint32_t getNCoords() const
Definition: FPGATrackSimPlaneMap.h:76
FPGATrackSimMatrixAccumulator::hit_x_d0
std::vector< double > hit_x_d0
Definition: FPGATrackSimMatrixAccumulator.h:79
FPGATrackSimMatrixReductionAlgo::m_nRegions
Gaudi::Property< int > m_nRegions
Definition: FPGATrackSimMatrixReductionAlgo.h:61
FPGATrackSimMatrixAccumulator::track_bins
std::vector< FPGATrackSimTrackParsI > track_bins
Definition: FPGATrackSimMatrixAccumulator.h:91
FPGATrackSimPlaneMap::getLayerSection
const LayerSection & getLayerSection(SiliconTech siTech, DetectorZone zone, uint32_t physLayer) const
Definition: FPGATrackSimPlaneMap.h:117
FPGATrackSimMatrixReductionAlgo::reduce
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)
Definition: FPGATrackSimMatrixReductionAlgo.cxx:181
FPGATrackSimPlaneMap::getDim
uint32_t getDim(size_t logiLayer) const
Definition: FPGATrackSimPlaneMap.h:78
FPGATrackSimMatrixReductionAlgo::m_filePath
Gaudi::Property< std::string > m_filePath
Definition: FPGATrackSimMatrixReductionAlgo.h:59
FPGATrackSimPlaneMap::getCoordOffset
uint32_t getCoordOffset(size_t logiLayer) const
Definition: FPGATrackSimPlaneMap.h:92
FPGATrackSimMatrixReductionAlgo::finalize
StatusCode finalize() override
Definition: FPGATrackSimMatrixReductionAlgo.cxx:261
fillTree
void fillTree(AccumulateMap &map, TTree *tree, int nLayers, int nCoords)
Writes the contents of an AccumulateMap into the supplied tree (one entry per sector).
Definition: FPGATrackSimMatrixIO.cxx:226
FPGATrackSimMatrixAccumulator::hit_x_eta
std::vector< double > hit_x_eta
Definition: FPGATrackSimMatrixAccumulator.h:81
FPGATrackSimMatrixReductionAlgo::matchStages
std::pair< std::vector< size_t >, std::vector< size_t > > matchStages()
Definition: FPGATrackSimMatrixReductionAlgo.cxx:136
FPGATrackSimMatrixIO.h
Classes to read/write matrix files event by event.
FPGATrackSimMatrixAccumulator::covariance
std::vector< double > covariance
Definition: FPGATrackSimMatrixAccumulator.h:84
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
FPGATrackSimMatrixAccumulator
Definition: FPGATrackSimMatrixAccumulator.h:36
covarianceTool.title
title
Definition: covarianceTool.py:542
file
TFile * file
Definition: tile_monitor.h:29
FPGATrackSimMatrixReductionAlgo::FPGATrackSimMatrixReductionAlgo
FPGATrackSimMatrixReductionAlgo(const std::string &name, ISvcLocator *pSvcLocator)
Definition: FPGATrackSimMatrixReductionAlgo.cxx:25
FPGATrackSimPlaneMap::getLayerInfo
const LayerInfo & getLayerInfo(uint32_t layer, uint32_t section) const
Definition: FPGATrackSimPlaneMap.h:104
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
FPGATrackSimMatrixReductionAlgo::m_region
Gaudi::Property< int > m_region
Definition: FPGATrackSimMatrixReductionAlgo.h:60
FPGATrackSimMatrixReader
Definition: FPGATrackSimMatrixIO.h:41
FPGATrackSimMatrixReductionAlgo::m_FPGATrackSimMapping
ServiceHandle< IFPGATrackSimMappingSvc > m_FPGATrackSimMapping
Definition: FPGATrackSimMatrixReductionAlgo.h:51
LayerSection
Definition: FPGATrackSimPlaneMap.h:40
FPGATrackSimMatrixReductionAlgo::m_allregion
Gaudi::Property< bool > m_allregion
Definition: FPGATrackSimMatrixReductionAlgo.h:58
FPGATrackSimMatrixReductionAlgo.h
Algorithm to reduce matrix files from 2nd stage to 1st stage.
AthAlgorithm
Definition: AthAlgorithm.h:47
FPGATrackSimMatrixAccumulator::hit_x_z0
std::vector< double > hit_x_z0
Definition: FPGATrackSimMatrixAccumulator.h:80
FPGATrackSimMatrixAccumulator::FTK_modules
std::vector< module_t > FTK_modules
Definition: FPGATrackSimMatrixAccumulator.h:56
dumpTgcDigiJitter.nBins
list nBins
Definition: dumpTgcDigiJitter.py:29
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
FPGATrackSimMatrixReductionAlgo::execute
StatusCode execute() override
Definition: FPGATrackSimMatrixReductionAlgo.cxx:250
FPGATrackSimMatrixReductionAlgo::m_region_start
int m_region_start
Definition: FPGATrackSimMatrixReductionAlgo.h:63
LayerInfo
Definition: FPGATrackSimPlaneMap.h:52
FPGATrackSimMatrixAccumulator::pars
FPGATrackSimTrackPars pars
Definition: FPGATrackSimMatrixAccumulator.h:70
LayerSection::layer
int layer
Definition: FPGATrackSimPlaneMap.h:41
FPGATrackSimMatrixReductionAlgo::m_pmap_1st
FPGATrackSimPlaneMap const * m_pmap_1st
Definition: FPGATrackSimMatrixReductionAlgo.h:53
FPGATrackSimMatrixReductionAlgo::m_sector_cum
std::vector< AccumulateMap > m_sector_cum
Definition: FPGATrackSimMatrixReductionAlgo.h:69
collisions.reader
reader
read the goodrunslist xml file(s)
Definition: collisions.py:22
FPGATrackSimTrackParsI
Definition: FPGATrackSimTrackPars.h:56
FPGATrackSimMatrixAccumulator::hit_coords
std::vector< float > hit_coords
Definition: FPGATrackSimMatrixAccumulator.h:75
FPGATrackSimMatrixReductionAlgo::initialize
StatusCode initialize() override
Definition: FPGATrackSimMatrixReductionAlgo.cxx:30
FPGATrackSimMatrixReductionAlgo::m_region_end
int m_region_end
Definition: FPGATrackSimMatrixReductionAlgo.h:64
FPGATrackSimMatrixReductionAlgo::copySliceTree
StatusCode copySliceTree(TFile *file)
Definition: FPGATrackSimMatrixReductionAlgo.cxx:62