ATLAS Offline Software
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();
36  m_pmap_2nd = m_FPGATrackSimMapping->PlaneMap_2nd();
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 std::pair<std::vector<size_t>, std::vector<size_t>> FPGATrackSimMatrixReductionAlgo::matchStages()
136 {
137  size_t iCoord_1st = 0;
138 
139  std::vector<size_t> layers_1st_to_2nd(m_pmap_1st->getNLogiLayers());
140  std::vector<size_t> coords_1st_to_2nd(m_pmap_1st->getNCoords());
141 
142  for (unsigned layer_1st = 0; layer_1st < m_pmap_1st->getNLogiLayers(); layer_1st++)
143  {
144  LayerInfo const & info_1st = m_pmap_1st->getLayerInfo(layer_1st, 0);
145  // all sections in stage 1 should also be in stage 2, so can just use 0 to retrieve info
146  LayerSection const & ls_2nd = m_pmap_2nd->getLayerSection(info_1st);
147 
148  int inversion(0);
149  layers_1st_to_2nd[layer_1st] = ls_2nd.layer + inversion;
150  for (size_t iDim = 0; iDim < m_pmap_1st->getDim(layer_1st); iDim++)
151  {
152  coords_1st_to_2nd[iCoord_1st] = m_pmap_2nd->getCoordOffset(ls_2nd.layer) + iDim + inversion;
153  iCoord_1st++;
154  }
155  }
156 
157  assert(iCoord_1st == m_pmap_1st->getNCoords());
158  return { layers_1st_to_2nd, coords_1st_to_2nd };
159 }
160 
161 
162 /*
163  * Reduces a second stage sector and corresponding matrix to first stage.
164  *
165  * This simply copies the sector matrices from the input file,
166  * but only keeping 1st stage layers/coordinates. In python pseudocode,
167  * layer_filter = [ is1stStage(layer_2nd) for layer_2nd in range(# 2nd stage layers) ]
168  * modules_1st = modules_2nd[layer_filter]
169  * and similarly for the coordinate-length members.
170  *
171  * @param modules_2nd - Second stage sector definition (read from file)
172  * @param acc_2nd - Second stage accumulator (read from file)
173  * @param layers_1st_to_2nd - Layer mapping from s1 to s2
174  * @param coords_1st_to_2nd - Coordinate (index) mapping from s1 to s2
175  *
176  * @return { modules_1st, acc_1st}
177  * - modules_1st - First stage sector definition
178  * - acc_1st - First stage matrix accumulator
179  */
181  std::vector<module_t> const & modules_2nd, FPGATrackSimMatrixAccumulator const & acc_2nd,
182  std::vector<size_t> const & layers_1st_to_2nd, std::vector<size_t> const & coords_1st_to_2nd)
183 {
184  std::vector<module_t> modules_1st(m_pmap_1st->getNLogiLayers()); // First stage sector definition (to be filled)
185  FPGATrackSimMatrixAccumulator acc_1st(m_pmap_1st->getNLogiLayers(), m_pmap_1st->getNCoords()); // First stage accumulator (to be filled)
186 
187  acc_1st.pars = acc_2nd.pars;
188  acc_1st.track_bins = acc_2nd.track_bins;
189 
190  // For each first stage layer, copy relavant info from matching second stage layer
191  for (size_t layer_1st = 0; layer_1st < m_pmap_1st->getNLogiLayers(); layer_1st++)
192  {
193  size_t layer_2nd = layers_1st_to_2nd[layer_1st];
194  modules_1st[layer_1st] = modules_2nd[layer_2nd];
195  acc_1st.FTK_modules[layer_1st] = acc_2nd.FTK_modules[layer_2nd];
196  }
197 
198  // Copy coordinates from matching second stage coordinate
199  for (size_t iCoord_1st = 0; iCoord_1st < m_pmap_1st->getNCoords(); iCoord_1st++)
200  {
201  size_t iCoord_2nd = coords_1st_to_2nd[iCoord_1st];
202 
203  acc_1st.hit_coords[iCoord_1st] = acc_2nd.hit_coords[iCoord_2nd];
204  acc_1st.hit_x_QoP [iCoord_1st] = acc_2nd.hit_x_QoP [iCoord_2nd];
205  acc_1st.hit_x_d0 [iCoord_1st] = acc_2nd.hit_x_d0 [iCoord_2nd];
206  acc_1st.hit_x_z0 [iCoord_1st] = acc_2nd.hit_x_z0 [iCoord_2nd];
207  acc_1st.hit_x_eta [iCoord_1st] = acc_2nd.hit_x_eta [iCoord_2nd];
208  acc_1st.hit_x_phi [iCoord_1st] = acc_2nd.hit_x_phi [iCoord_2nd];
209 
210  // Covariance calculation. This requires a second loop through layers/coordinates,
211  // so postfix those variables with _j, as in (i,j). Only need triangular matrix
212  // so start loop at iCoord_1st.
213  for (size_t iCoord_1st_j = iCoord_1st; iCoord_1st_j < m_pmap_1st->getNCoords(); iCoord_1st_j++)
214  {
215  size_t iCoord_2nd_j = coords_1st_to_2nd[iCoord_1st_j];
216  acc_1st.covariance[iCoord_1st * m_pmap_1st->getNCoords() + iCoord_1st_j] =
217  acc_2nd.covariance[iCoord_2nd * m_pmap_2nd->getNCoords() + iCoord_2nd_j];
218  }
219  }
220 
221  return { modules_1st, acc_1st };
222 }
223 
224 
225 /*
226  * Runs the main algorithm, accumulating the 1st stage matrices from file into m_sector_cum.
227  */
229 {
230  auto matched_layers_coords = matchStages();
231  // Read 2nd stage info from file
232  TTree *tree_2nd = (TTree*)file->Get(Form("am0"));
234 
235  // Create 1st stage matrix and modules, and accumulate into m_sector_cum
236  while (reader.nextEntry())
237  {
238  auto sector_acc = reduce(reader.getModules(), reader.getAccumulator(), matched_layers_coords.first, matched_layers_coords.second);
239  ::accumulate(m_sector_cum[0], sector_acc.first, sector_acc.second);
240  }
241 }
242 
243 
245 // Execute
247 
248 
250 {
251  // Do nothing; this class does not process events. The main algorithm is
252  // called in initialize() and finalize().
253  return StatusCode::SUCCESS;
254 }
255 
257 // Finalize
259 
261 {
262  {
263  // Create the tree
264  std::stringstream name;
265  std::stringstream title;
266  name << "am0";
267  title << "Ambank 0" << " parameters";
268  TTree* tree = new TTree(name.str().c_str(), title.str().c_str());
269  ATH_CHECK(m_tHistSvc->regTree(Form("/TRIGFPGATrackSimMATRIX1STSTAGEOUT/%s",tree->GetName()), tree));
270 
271  // Fill the tree
273  }
274 
275  ATH_CHECK(m_tHistSvc->finalize());
276  return StatusCode::SUCCESS;
277 }
278 
279 
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:78
max
#define max(a, b)
Definition: cfImp.cxx:41
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
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:228
FPGATrackSimPlaneMap::getNCoords
uint32_t getNCoords() const
Definition: FPGATrackSimPlaneMap.h:79
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:120
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:180
FPGATrackSimPlaneMap::getDim
uint32_t getDim(size_t logiLayer) const
Definition: FPGATrackSimPlaneMap.h:81
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:95
FPGATrackSimMatrixReductionAlgo::finalize
StatusCode finalize() override
Definition: FPGATrackSimMatrixReductionAlgo.cxx:260
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:225
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:135
FPGATrackSimMatrixIO.h
Classes to read/write matrix files event by event.
FPGATrackSimMatrixAccumulator::hit_coords
std::vector< double > hit_coords
Definition: FPGATrackSimMatrixAccumulator.h:75
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:107
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
min
#define min(a, b)
Definition: cfImp.cxx:40
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:195
FPGATrackSimMatrixReductionAlgo::execute
StatusCode execute() override
Definition: FPGATrackSimMatrixReductionAlgo.cxx:249
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
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