ATLAS Offline Software
FPGATrackSimMatrixMergeAlgo.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
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 
30 using namespace std;
31 
33 // Constructor / Initialize
35 
36 FPGATrackSimMatrixMergeAlgo::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) {
80  m_region_start = 0;
82  }
83  else {
85  m_region_end = m_region + 1;
86  }
87 
88  m_sector_cum.resize(m_nRegions);
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 }
FPGATrackSimMatrixMergeAlgo::FPGATrackSimMatrixMergeAlgo
FPGATrackSimMatrixMergeAlgo(const std::string &name, ISvcLocator *pSvcLocator)
Definition: FPGATrackSimMatrixMergeAlgo.cxx:36
FPGATrackSimMatrixMergeAlgo::m_nDim2
int m_nDim2
Definition: FPGATrackSimMatrixMergeAlgo.h:73
FPGATrackSimMatrixMergeAlgo::m_sector_cum
std::vector< AccumulateMap > m_sector_cum
Definition: FPGATrackSimMatrixMergeAlgo.h:50
max
#define max(a, b)
Definition: cfImp.cxx:41
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
FPGATrackSimMatrixMergeAlgo::readFiles
void readFiles()
Definition: FPGATrackSimMatrixMergeAlgo.cxx:184
FPGATrackSimTrackPars
Definition: FPGATrackSimTrackPars.h:22
FPGATrackSimMatrixMergeAlgo::m_nRegions
Gaudi::Property< int > m_nRegions
Definition: FPGATrackSimMatrixMergeAlgo.h:63
FPGATrackSimMatrixMergeAlgo::m_h_nSector
TH1I * m_h_nSector
Definition: FPGATrackSimMatrixMergeAlgo.h:79
FPGATrackSimMatrixMergeAlgo::m_nFiles
int m_nFiles
Definition: FPGATrackSimMatrixMergeAlgo.h:70
tree
TChain * tree
Definition: tile_monitor.h:30
FPGATrackSimMatrixMergeAlgo::m_tHistSvc
ServiceHandle< ITHistSvc > m_tHistSvc
Definition: FPGATrackSimMatrixMergeAlgo.h:55
FPGATrackSimMatrixMergeAlgo::finalize
StatusCode finalize() override
Definition: FPGATrackSimMatrixMergeAlgo.cxx:229
FPGATrackSimMatrixMergeAlgo::m_h_phi
TH1F * m_h_phi
Definition: FPGATrackSimMatrixMergeAlgo.h:83
FPGATrackSimMatrixMergeAlgo::m_nDim
int m_nDim
Definition: FPGATrackSimMatrixMergeAlgo.h:72
FPGATrackSimMatrixMergeAlgo::initialize
StatusCode initialize() override
Definition: FPGATrackSimMatrixMergeAlgo.cxx:42
FPGATrackSimMatrixMergeAlgo::readTree
void readTree(TTree *matrix_tree, int region)
FPGATrackSimMatrixMergeAlgo::m_h_coto
TH1F * m_h_coto
Definition: FPGATrackSimMatrixMergeAlgo.h:84
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
FPGATrackSimMatrixMergeAlgo::m_region_start
int m_region_start
Definition: FPGATrackSimMatrixMergeAlgo.h:67
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
FPGATrackSimMatrixMergeAlgo::m_region_end
int m_region_end
Definition: FPGATrackSimMatrixMergeAlgo.h:68
FPGATrackSimMatrixMergeAlgo::m_allregion
Gaudi::Property< bool > m_allregion
Definition: FPGATrackSimMatrixMergeAlgo.h:61
FPGATrackSimMatrixMergeAlgo::m_h_nHit
TH1I * m_h_nHit
Definition: FPGATrackSimMatrixMergeAlgo.h:80
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
covarianceTool.title
title
Definition: covarianceTool.py:542
generateReferenceFile.files
files
Definition: generateReferenceFile.py:12
file
TFile * file
Definition: tile_monitor.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
FPGATrackSimMatrixMergeAlgo::m_h_z
TH1F * m_h_z
Definition: FPGATrackSimMatrixMergeAlgo.h:85
AthAlgorithm
Definition: AthAlgorithm.h:47
min
#define min(a, b)
Definition: cfImp.cxx:40
dumpTgcDigiJitter.nBins
list nBins
Definition: dumpTgcDigiJitter.py:29
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
FPGATrackSimMatrixMergeAlgo::m_nLayers
int m_nLayers
Definition: FPGATrackSimMatrixMergeAlgo.h:71
FPGATrackSimMatrixMergeAlgo.h
Algorithm to merge matrix files, to be used for sector and constant generation.
FPGATrackSimMatrixMergeAlgo::execute
StatusCode execute() override
Definition: FPGATrackSimMatrixMergeAlgo.cxx:218
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
FPGATrackSimMatrixMergeAlgo::bookHistograms
StatusCode bookHistograms()
Definition: FPGATrackSimMatrixMergeAlgo.cxx:96
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
FPGATrackSimMatrixMergeAlgo::copySliceTree
StatusCode copySliceTree(TFile *file)
Definition: FPGATrackSimMatrixMergeAlgo.cxx:123
FPGATrackSimMatrixMergeAlgo::m_h_c
TH1F * m_h_c
Definition: FPGATrackSimMatrixMergeAlgo.h:81
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
FPGATrackSimMatrixMergeAlgo::m_fpath
Gaudi::Property< std::vector< std::string > > m_fpath
Definition: FPGATrackSimMatrixMergeAlgo.h:60
FPGATrackSimTrackParsI
Definition: FPGATrackSimTrackPars.h:56
FPGATrackSimMatrixMergeAlgo::m_h_d
TH1F * m_h_d
Definition: FPGATrackSimMatrixMergeAlgo.h:82
FPGATrackSimMatrixMergeAlgo::m_Monitor
Gaudi::Property< bool > m_Monitor
Definition: FPGATrackSimMatrixMergeAlgo.h:64
FPGATrackSimMatrixMergeAlgo::m_region
Gaudi::Property< int > m_region
Definition: FPGATrackSimMatrixMergeAlgo.h:62