ATLAS Offline Software
Loading...
Searching...
No Matches
FPGATrackSimMatrixMergeAlgo.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2
11
12
14#include "GaudiKernel/MsgStream.h"
15#include "GaudiKernel/ITHistSvc.h"
16
17#include "TTree.h"
18#include "TFile.h"
19#include "TROOT.h"
20
21#include <sstream>
22#include <iostream>
23#include <fstream>
24#include <cmath>
25#include <string>
26#include <stdio.h>
27#include <math.h>
28#include <stdlib.h>
29
30using namespace std;
31
33// Constructor / Initialize
35
36FPGATrackSimMatrixMergeAlgo::FPGATrackSimMatrixMergeAlgo(const std::string& name, ISvcLocator* pSvcLocator) :
37 AthAlgorithm(name, pSvcLocator)
38{
39}
40
41
43{
44 ATH_MSG_DEBUG("initialize()");
45
46 ATH_CHECK(m_tHistSvc.retrieve());
48
49 m_nFiles = m_fpath.size();
50 ATH_MSG_INFO("Merging " << m_nFiles << " files");
51
52 // Use the first valid file to extract some general information used to intialize
53 TFile *file = nullptr;
54 std::vector<std::string> files = m_fpath.value();
55 for (int iFile = 0; iFile < m_nFiles; iFile++) {
56 file = TFile::Open(files[iFile].c_str());
57 if (!file)
58 ATH_MSG_WARNING("Could not open " << files[iFile]);
59 else {
60 if (file->GetNkeys() != 0) break;
61 else file->Close();
62 }
63 }
64 if (!file) return StatusCode::FAILURE;
65
66 // Get the number of layers and total dimension from a sample region
67 TTree *matrix_tree = (TTree*)file->Get(Form("am%d",m_region.value()));
68 matrix_tree->SetBranchAddress("nplanes", &m_nLayers);
69 matrix_tree->SetBranchAddress("ndim", &m_nDim);
70 matrix_tree->SetBranchAddress("ndim2", &m_nDim2);
71 matrix_tree->GetEntry(0);
72
73 // Copy the slice tree into the merged file
75
76 file->Close();
77
78 // Setup the boundaries for the merge
79 if (m_allregion) {
82 }
83 else {
86 }
87
89
90
91 readFiles();
92 return StatusCode::SUCCESS;
93}
94
95
97{
98 m_h_nSector = new TH1I("h_nSector", "number of sectors in merged file", 100, 0, 10000);
99 ATH_CHECK(m_tHistSvc->regHist("/TRIGFPGATrackSimMATRIXOUT/h_nSector",m_h_nSector));
100
101 m_h_nHit = new TH1I("h_nHit", "number of hits in sector", 100, 0, 10000);
102 ATH_CHECK(m_tHistSvc->regHist("/TRIGFPGATrackSimMATRIXOUT/h_nHit",m_h_nHit));
103
104 m_h_c = new TH1F("h_c", "Truth curvature in sector",100,-1e-8,1e-8);
105 ATH_CHECK(m_tHistSvc->regHist("/TRIGFPGATrackSimMATRIXOUT/h_c",m_h_c));
106
107 m_h_d = new TH1F("h_d", "Truth d0 in sector",100,-2.5,2.5);
108 ATH_CHECK(m_tHistSvc->regHist("/TRIGFPGATrackSimMATRIXOUT/h_d",m_h_d));
109
110 m_h_phi = new TH1F("h_phi", "Truth phi in sector",100,0,6.3);
111 ATH_CHECK(m_tHistSvc->regHist("/TRIGFPGATrackSimMATRIXOUT/h_phi",m_h_phi));
112
113 m_h_coto = new TH1F("h_coto", "Truth coto in sector",100,-4,4);
114 ATH_CHECK(m_tHistSvc->regHist("/TRIGFPGATrackSimMATRIXOUT/h_coto",m_h_coto));
115
116 m_h_z = new TH1F("h_z", "Truth z in sector",100,-200,200);
117 ATH_CHECK(m_tHistSvc->regHist("/TRIGFPGATrackSimMATRIXOUT/h_z",m_h_z));
118
119 return StatusCode::SUCCESS;
120}
121
122
124{
128
129 // Read old tree
130 TTree *old_tree = (TTree*)file->Get("slice");
131 old_tree->SetBranchAddress("c_max", &max.qOverPt);
132 old_tree->SetBranchAddress("c_min", &min.qOverPt);
133 old_tree->SetBranchAddress("c_slices", &nBins.qOverPt);
134
135 old_tree->SetBranchAddress("phi_max", &max.phi);
136 old_tree->SetBranchAddress("phi_min", &min.phi);
137 old_tree->SetBranchAddress("phi_slices", &nBins.phi);
138
139 old_tree->SetBranchAddress("d0_max", &max.d0);
140 old_tree->SetBranchAddress("d0_min", &min.d0);
141 old_tree->SetBranchAddress("d0_slices", &nBins.d0);
142
143 old_tree->SetBranchAddress("z0_max", &max.z0);
144 old_tree->SetBranchAddress("z0_min", &min.z0);
145 old_tree->SetBranchAddress("z0_slices", &nBins.z0);
146
147 old_tree->SetBranchAddress("eta_max", &max.eta);
148 old_tree->SetBranchAddress("eta_min", &min.eta);
149 old_tree->SetBranchAddress("eta_slices", &nBins.eta);
150
151 old_tree->GetEntry(0);
152
153 // Write new tree
154 TTree *new_tree = new TTree("slice", "Slice boundaries");
155 ATH_CHECK(m_tHistSvc->regTree("/TRIGFPGATrackSimMATRIXOUT/slice", new_tree));
156
157 new_tree->Branch("c_max", &max.qOverPt);
158 new_tree->Branch("c_min", &min.qOverPt);
159 new_tree->Branch("c_slices", &nBins.qOverPt);
160
161 new_tree->Branch("phi_max", &max.phi);
162 new_tree->Branch("phi_min", &min.phi);
163 new_tree->Branch("phi_slices", &nBins.phi);
164
165 new_tree->Branch("d0_max", &max.d0);
166 new_tree->Branch("d0_min", &min.d0);
167 new_tree->Branch("d0_slices", &nBins.d0);
168
169 new_tree->Branch("z0_max", &max.z0);
170 new_tree->Branch("z0_min", &min.z0);
171 new_tree->Branch("z0_slices", &nBins.z0);
172
173 new_tree->Branch("eta_max", &max.eta);
174 new_tree->Branch("eta_min", &min.eta);
175 new_tree->Branch("eta_slices", &nBins.eta);
176
177 new_tree->Fill();
178
179 return StatusCode::SUCCESS;
180}
181
182
183// Read each file and accumulate everything into m_sector_cum
185{
186 for (int iFile = 0; iFile < m_nFiles; iFile++)
187 {
188 // Open the file
189 const string &fpath = m_fpath[iFile];
190 ATH_MSG_INFO("Reading " << fpath);
191 TFile *file = TFile::Open(fpath.c_str());
192
193 // Check if the file is valid
194 if (!file || file->GetNkeys() == 0)
195 {
196 ATH_MSG_WARNING("Invalid file: " << fpath);
197 continue;
198 }
199
200 // Read each tree
201 for (int region = m_region_start; region < m_region_end; region++)
202 {
203 TTree *tree = (TTree*)file->Get(Form("am%d", region));
205 delete tree;
206 }
207
208 file->Close();
209 delete file;
210 }
211}
212
214// Execute
216
217
219{
220 // Do nothing; this class does not process events. The main algorithm is
221 // called in initialize() and finalize().
222 return StatusCode::SUCCESS;
223}
224
226// Finalize
228
230{
231 for (int region = m_region_start; region < m_region_end; region++)
232 {
233 // Create the tree
234 std::stringstream name;
235 std::stringstream title;
236 name << "am" << region;
237 title << "Ambank " << region << " parameters";
238 TTree* tree = new TTree(name.str().c_str(), title.str().c_str());
239 ATH_CHECK(m_tHistSvc->regTree(Form("/TRIGFPGATrackSimMATRIXOUT/%s",tree->GetName()), tree));
240
241 ATH_MSG_INFO("Sectors found in region " << region << ": " << m_sector_cum[region].size());
242
243
244 // Fill the tree
246
247 // Monitoring
248 if (m_Monitor)
249 {
250 ATH_MSG_INFO("Sectors found in region " << region << ": " << m_sector_cum[region].size());
251 m_h_nSector->Fill(m_sector_cum[region].size());
252 for (auto & sector_info : m_sector_cum[region])
253 {
254 double coverage = sector_info.second.track_bins.size();
255 m_h_nHit->Fill(coverage);
256 m_h_c->Fill(sector_info.second.pars.qOverPt / coverage);
257 m_h_d->Fill(sector_info.second.pars.d0 / coverage);
258 m_h_phi->Fill(sector_info.second.pars.phi / coverage);
259 m_h_coto->Fill(sector_info.second.pars.eta / coverage);
260 m_h_z->Fill(sector_info.second.pars.z0 / coverage);
261 }
262 }
263 }
264
265 ATH_CHECK(m_tHistSvc->finalize());
266 return StatusCode::SUCCESS;
267}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
void fillTree(AccumulateMap &map, TTree *tree, int nLayers, int nCoords)
Writes the contents of an AccumulateMap into the supplied tree (one entry per sector).
Algorithm to merge matrix files, to be used for sector and constant generation.
#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::vector< AccumulateMap > m_sector_cum
void readTree(TTree *matrix_tree, int region)
Gaudi::Property< std::vector< std::string > > m_fpath
FPGATrackSimMatrixMergeAlgo(const std::string &name, ISvcLocator *pSvcLocator)
ServiceHandle< ITHistSvc > m_tHistSvc
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:50
STL namespace.
TChain * tree
TFile * file