ATLAS Offline Software
FPGATrackSimMergeOutputsAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
9 
10 #include "TH2F.h"
11 #include "TTree.h"
12 #include "TFile.h"
13 
14 FPGATrackSimMergeOutputsAlg::FPGATrackSimMergeOutputsAlg (const std::string& name, ISvcLocator* pSvcLocator) :
15  AthAlgorithm(name, pSvcLocator) {
16 }
17 
18 
20 {
21 
22  ATH_MSG_INFO ( "FPGATrackSimMergeOutputsAlg::initialize()");
23 
26  ATH_CHECK(m_overlapRemovalTool.retrieve());
27 
28  unsigned nfiles = m_inpaths.size();
29 
30  m_files.resize(nfiles);
31  m_trees.resize(nfiles);
32 
33  m_eventOutputHeaders.resize(nfiles);
34 
35  bool regionsFound[N]; // did we find this region in ANY file yet?
36  for (unsigned iregion = 0; iregion < N; iregion++) {
37  regionsFound[iregion] = false;
38  }
39 
40  bool foundDP=false;
41 
42  for (unsigned ifile = 0; ifile < nfiles; ifile++) { // loop over paths to files
43  m_files[ifile] = new TFile(m_inpaths[ifile].c_str(),"READ");
44  if (!m_files[ifile]->IsOpen() || m_files[ifile]->IsZombie()) {
45  ATH_MSG_ERROR("Could not open" << m_inpaths[ifile]);
46  return StatusCode::FAILURE;
47  }
48 
49  if (!foundDP) { // only needed for one file
50  m_dataprep_tree = (TTree*)(m_files[ifile]->Get("FPGATrackSimDataPrepTree"));
51  if (m_dataprep_tree) {
52  if (m_dataprep_tree->GetEntries() > 0) {
53  foundDP = true;
55 
56  TBranch *dpb = m_dataprep_tree->GetBranch("LogicalEventInputHeader_PostCluster");
57  if (!dpb) {
58  ATH_MSG_ERROR("Could not get LogicalEventInputHeader_PostCluster file " << m_inpaths[ifile]);
59  return StatusCode::FAILURE;
60  }
61  dpb->SetAddress(&m_dataprep);
62  m_dataprep_tree->SetBranchStatus("LogicalEventInputHeader_Pre*",0);
63  }
64  }
65  }
66 
67 
68  m_trees[ifile].resize(N);
69  m_eventOutputHeaders[ifile].resize(N);
70 
71  for (unsigned iregion = 0; iregion < N; iregion++) {
72  m_trees[ifile][iregion] = (TTree*)(m_files[ifile]->Get(Form("FPGATrackSimLogicalEventTree_reg%d",iregion)));
73  if (!m_trees[ifile][iregion]) {
74  continue;
75  }
76  else if (regionsFound[iregion]) {
77  ATH_MSG_ERROR("Found two files with region number " << iregion << " and I do not know which one to use!");
78  return StatusCode::FAILURE;
79  }
80  else {
81  regionsFound[iregion] = true;
82  }
83 
85  TBranch *b = m_trees[ifile][iregion]->GetBranch("LogicalEventOutputHeader");
86  if (!b) {
87  ATH_MSG_ERROR("Could not get LogicalEventOutputHeader in file " << m_inpaths[ifile]);
88  return StatusCode::FAILURE;
89  }
90  b->SetAddress(&m_eventOutputHeaders[ifile][iregion]);
91 
92  m_trees[ifile][iregion]->SetBranchStatus("LogicalEventSecond*",0);
93  m_trees[ifile][iregion]->SetBranchStatus("LogicalEventFirstP*",0);
94  m_trees[ifile][iregion]->SetBranchStatus("LogicalEventSpace*",0);
95  m_trees[ifile][iregion]->SetBranchStatus("LogicalEventStrip*",0);
96 
97  }
98  }
99 
100  if (!foundDP) {
101  ATH_MSG_ERROR("Did not find the DP tree");
102  return StatusCode::FAILURE;
103  }
104 
105  return StatusCode::SUCCESS;
106 }
107 
109  ATH_MSG_DEBUG ("Running on event ");
110 
111  const EventContext& ctx = getContext();
112  SG::WriteHandle<FPGATrackSimHitCollection> FPGAHits_Handle (m_FPGAHitKey, ctx); // all the clusters across the regions
113 
114  SG::WriteHandle<FPGATrackSimTrackCollection> FPGATracks_Handle (m_FPGATrackKey, ctx); // all the tracks across the regions
115  // Do this now so we can deference it
116  ATH_CHECK(FPGATracks_Handle.record (std::make_unique<FPGATrackSimTrackCollection>()));
117  FPGATrackSimTrackCollection *FPGATracks = FPGATracks_Handle.ptr();
118 
119  // get the hits
120  ATH_CHECK(FPGAHits_Handle.record (std::make_unique<FPGATrackSimHitCollection>()));
121  m_dataprep_tree->GetEntry(m_evtloop);
122  for (const auto & tower : m_dataprep->towers()) {
123  const std::vector<FPGATrackSimHit> hits = tower.hits();
124  FPGAHits_Handle->insert(FPGAHits_Handle->end(), make_move_iterator(hits.begin()), make_move_iterator(hits.end()));
125  }
126 
127  for (unsigned ivec = 0; ivec < m_eventOutputHeaders.size(); ivec++) {
128  for (unsigned iregion = 0; iregion < N; iregion++) {
129  if (!m_trees[ivec][iregion]) continue;
130  if (m_evtloop >= m_trees[ivec][iregion]->GetEntries()) {
131  ATH_MSG_ERROR("Tried reading in more events than the tree had available!");
132  return StatusCode::FAILURE;
133  }
134  m_trees[ivec][iregion]->GetEntry(m_evtloop);
135  // Time to load up these tracks! Only bother using ones that already passed OLR
136  std::vector<FPGATrackSimTrack> const tracks = m_eventOutputHeaders[ivec][iregion]->getFPGATrackSimTracks_1st();
137  m_alltracks += tracks.size();
138  for (const auto &track : tracks) {
139  if (track.passedOR()) FPGATracks->push_back(track);
140  }
141  }
142  }
143 
144  // Now run overlap removal on all the tracks that already passed overlap removal between other regions
145  ATH_CHECK(m_overlapRemovalTool->runOverlapRemoval(*FPGATracks));
146  for (const auto &track : *FPGATracks) {
147  if (track.passedOR()) m_tracksPassOR++;
148  }
149  // Increase evtloop
150  m_evtloop++;
151  return StatusCode::SUCCESS;
152 }
153 
155  ATH_MSG_INFO("Processed " << m_evtloop << " events.");
156  ATH_MSG_INFO("Average number of tracks per event = " << (m_alltracks/m_evtloop));
157  ATH_MSG_INFO("Average number of tracks per event passing global OLR = " << (m_tracksPassOR/m_evtloop));
158  return StatusCode::SUCCESS;
159 }
FPGATrackSimMergeOutputsAlg.h
FPGATrackSimMergeOutputsAlg::m_FPGAHitKey
SG::WriteHandleKey< FPGATrackSimHitCollection > m_FPGAHitKey
Definition: FPGATrackSimMergeOutputsAlg.h:50
FPGATrackSimMergeOutputsAlg::finalize
virtual StatusCode finalize() override
Definition: FPGATrackSimMergeOutputsAlg.cxx:154
FPGATrackSimMergeOutputsAlg::m_tracksPassOR
unsigned long m_tracksPassOR
Definition: FPGATrackSimMergeOutputsAlg.h:55
FPGATrackSimMergeOutputsAlg::m_files
std::vector< TFile * > m_files
Definition: FPGATrackSimMergeOutputsAlg.h:41
FPGATrackSimLogicalEventInputHeader
Definition: FPGATrackSimLogicalEventInputHeader.h:21
TRTCalib_Extractor.hits
hits
Definition: TRTCalib_Extractor.py:35
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
FPGATrackSimMergeOutputsAlg::execute
virtual StatusCode execute() override
Definition: FPGATrackSimMergeOutputsAlg.cxx:108
GetEntries
TGraphErrors * GetEntries(TH2F *histo)
Definition: TRTCalib_makeplots.cxx:4025
FPGATrackSimMergeOutputsAlg::m_overlapRemovalTool
ToolHandle< FPGATrackSimOverlapRemovalTool > m_overlapRemovalTool
Definition: FPGATrackSimMergeOutputsAlg.h:51
FPGATrackSimMergeOutputsAlg::initialize
virtual StatusCode initialize() override
Definition: FPGATrackSimMergeOutputsAlg.cxx:19
FPGATrackSimMergeOutputsAlg::FPGATrackSimMergeOutputsAlg
FPGATrackSimMergeOutputsAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: FPGATrackSimMergeOutputsAlg.cxx:14
Get
T * Get(TFile &f, const std::string &n, const std::string &dir="", const chainmap_t *chainmap=0, std::vector< std::string > *saved=0)
get a histogram given a path, and an optional initial directory if histogram is not found,...
Definition: comparitor.cxx:181
FPGATrackSimMergeOutputsAlg::m_evtloop
double m_evtloop
Definition: FPGATrackSimMergeOutputsAlg.h:53
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
FPGATrackSimMergeOutputsAlg::m_trees
std::vector< std::vector< TTree * > > m_trees
Definition: FPGATrackSimMergeOutputsAlg.h:42
FPGATrackSimLogicalEventOutputHeader.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
FPGATrackSimMergeOutputsAlg::N
static const unsigned N
Definition: FPGATrackSimMergeOutputsAlg.h:37
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
AthAlgorithm
Definition: AthAlgorithm.h:47
FPGATrackSimMergeOutputsAlg::m_dataprep
FPGATrackSimLogicalEventInputHeader * m_dataprep
Definition: FPGATrackSimMergeOutputsAlg.h:45
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
FPGATrackSimMergeOutputsAlg::m_eventOutputHeaders
std::vector< std::vector< FPGATrackSimLogicalEventOutputHeader * > > m_eventOutputHeaders
Definition: FPGATrackSimMergeOutputsAlg.h:43
FPGATrackSimOverlapRemovalTool.h
Overlap removal tool for FPGATrackSimTrack.
FPGATrackSimTrackCollection
std::vector< FPGATrackSimTrack > FPGATrackSimTrackCollection
Definition: FPGATrackSimTrackCollection.h:13
FPGATrackSimMergeOutputsAlg::m_alltracks
unsigned long m_alltracks
Definition: FPGATrackSimMergeOutputsAlg.h:54
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
FPGATrackSimMergeOutputsAlg::m_FPGATrackKey
SG::WriteHandleKey< FPGATrackSimTrackCollection > m_FPGATrackKey
Definition: FPGATrackSimMergeOutputsAlg.h:49
FPGATrackSimMergeOutputsAlg::m_dataprep_tree
TTree * m_dataprep_tree
Definition: FPGATrackSimMergeOutputsAlg.h:46
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
FPGATrackSimMergeOutputsAlg::m_inpaths
Gaudi::Property< std::vector< std::string > > m_inpaths
Definition: FPGATrackSimMergeOutputsAlg.h:39
FPGATrackSimLogicalEventOutputHeader
Definition: FPGATrackSimLogicalEventOutputHeader.h:12
FPGATrackSimLogicalEventInputHeader::towers
const std::vector< FPGATrackSimTowerInputHeader > & towers() const
Definition: FPGATrackSimLogicalEventInputHeader.h:36
LArCellNtuple.ifile
string ifile
Definition: LArCellNtuple.py:133
xAOD::track
@ track
Definition: TrackingPrimitives.h:513
FPGATrackSimLogicalEventInputHeader.h
CxxUtils::ivec
vec_fb< typename boost::int_t< sizeof(T) *8 >::exact, N > ivec
Definition: vec_fb.h:53
FPGATrackSimTrack.h