ATLAS Offline Software
FPGATrackSimOutputHeaderTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
8 
9 FPGATrackSimOutputHeaderTool::FPGATrackSimOutputHeaderTool(std::string const & algname, std::string const & name, IInterface const * ifc) :
10  base_class(algname, name, ifc) {}
11 
12 
13 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
15 {
16  // close old file (I don't think we delete the pointer. Does ROOT handle that?)
17  if (m_infile && m_infile->IsOpen()) m_infile->Close();
18 
19  // open new file
20  ATH_MSG_DEBUG ("Opening file " << path.c_str() << " in " << m_rwoption.value() << " mode.");
21  m_infile = TFile::Open(path.c_str(), m_rwoption.value().c_str());
22 
23  if (!m_infile) {
24  ATH_MSG_FATAL("Could not open input file: " << path);
25  return StatusCode::FAILURE;
26  }
27 
28  if (m_rwoption.value() == std::string("READ")) {
29  // get the tree, work on backwards compatability, so try two of them
30  m_EventTree = (TTree*) m_infile->Get("FPGATrackSimLogicalEventTree");
31  if (!m_EventTree || m_EventTree->GetEntries() == -1) {
32  ATH_MSG_FATAL ("Input file: " << m_inpath.value() << " has no entries");
33  return StatusCode::FAILURE;
34  }
35 
36  ATH_MSG_INFO ("Input file: " << path << " has " << m_EventTree->GetEntries() << " event entries.");
37 
38  // branches
39  if (!m_EventTree->GetListOfBranches()->FindObject(m_branchNameIn_1st.c_str())) {
40  ATH_MSG_FATAL ("Branch: " << m_branchNameIn_1st << " not found!");
41  return StatusCode::FAILURE;
42  }
43  TBranch *branchIn_1st = m_EventTree->GetBranch(m_branchNameIn_1st.c_str());
44  branchIn_1st->SetAddress(&m_eventInputHeader_1st);
45  m_EventTree->SetBranchStatus(m_branchNameIn_1st.c_str(), 1);
46 
47  if (m_runSecondStage) {
48  if (!m_EventTree->GetListOfBranches()->FindObject(m_branchNameIn_2nd.c_str())) {
49  ATH_MSG_FATAL ("Branch: " << m_branchNameIn_2nd << " not found!");
50  return StatusCode::FAILURE;
51  }
52  TBranch *branchIn_2nd = m_EventTree->GetBranch(m_branchNameIn_2nd.c_str());
53  branchIn_2nd->SetAddress(&m_eventInputHeader_2nd);
54  m_EventTree->SetBranchStatus(m_branchNameIn_2nd.c_str(), 1);
55  }
56 
57  if (!m_EventTree->GetListOfBranches()->FindObject(m_branchNameOut.c_str())) {
58  ATH_MSG_FATAL ("Branch: " << m_branchNameOut << " not found!");
59  return StatusCode::FAILURE;
60  }
61  TBranch *branchOut = m_EventTree->GetBranch(m_branchNameOut.c_str());
62  branchOut->SetAddress(&m_eventOutputHeader);
63  m_EventTree->SetBranchStatus(m_branchNameOut.c_str(),1);
64  }
65  m_infile->cd();
66  m_event = 0;
67  return StatusCode::SUCCESS;
68 }
69 
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
73 {
74  m_eventInputHeader_1st = new FPGATrackSimLogicalEventInputHeader();
75  m_eventOutputHeader = new FPGATrackSimLogicalEventOutputHeader();
76  if (m_runSecondStage) m_eventInputHeader_2nd = new FPGATrackSimLogicalEventInputHeader();
77 
78  // set defaults
79  m_branchNameIn_1st = "FPGATrackSimLogicalEventInputHeader_1st";
80  m_branchNameIn_2nd = "FPGATrackSimLogicalEventInputHeader_2nd";
81  m_branchNameOut = "FPGATrackSimLogicalEventOutputHeader";
82 
83  if( m_rwoption.value()!=std::string("HEADER"))
84  {
85  if (m_inpath.value().empty())
86  {
87  ATH_MSG_ERROR("Empty input file list");
88  return StatusCode::FAILURE;
89  }
90  ATH_CHECK(openFile(m_inpath.value().front()));
91  }
92 
93  if (m_rwoption.value() == std::string("READ")) {
94  ATH_MSG_DEBUG ("Initialized in READ MODE");
95  }
96  else if (m_rwoption.value()==std::string("RECREATE") || m_rwoption.value()==std::string("HEADER")) {
97  ATH_MSG_INFO ("Creating empty branches in output file");
98  ATH_CHECK(openFile(m_inpath.value().front()));
99  m_EventTree = new TTree("FPGATrackSimLogicalEventTree","data");
100 
101  m_EventTree->Branch(m_branchNameIn_1st.c_str(),
102  "FPGATrackSimLogicalEventInputHeader", // class name
103  &m_eventInputHeader_1st);
104  if (m_runSecondStage) {
105  m_EventTree->Branch(m_branchNameIn_2nd.c_str(),
106  "FPGATrackSimLogicalEventInputHeader", // class name
107  &m_eventInputHeader_2nd);
108  }
109  m_EventTree->Branch(m_branchNameOut.c_str(),
110  "FPGATrackSimLogicalEventOutputHeader", // class name
111  &m_eventOutputHeader);
112  }
113  else {
114  ATH_MSG_ERROR ("RWstatus = " << m_rwoption.value() << " is not allowed!");
115  return StatusCode::FAILURE;
116  }
117 
118  m_event = 0; // in file
119  m_totevent = 0; // total counter
120  return StatusCode::SUCCESS;
121 }
122 
123 
124 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
126 {
127  ATH_MSG_INFO ("finalize: closing files");
128 
129  if (m_rwoption.value() == std::string("RECREATE")) {
130  ATH_MSG_INFO ("Contains " << m_EventTree->GetEntries() << " entries, over " << m_event << " events run");
131  // close the output files, but check that it exists (for athenaMP)
132  m_infile->Write();
133  }
134 
135  if (m_rwoption.value() != std::string("HEADER")) {
136  m_infile->Close();
137  }
138  //deleting pointers
139  delete m_eventInputHeader_1st ;
140  delete m_eventOutputHeader;
141 
142  return StatusCode::SUCCESS;
143 }
144 
145 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
147 {
148 
149  if (m_rwoption.value() == std::string("READ")) {
150  ATH_MSG_WARNING ("Asked to write file in READ mode");
151  return StatusCode::SUCCESS;
152  }
153 
154  ATH_MSG_DEBUG ("Writing data in TTree");
155  if (INheader_1st)
156  *m_eventInputHeader_1st = *INheader_1st; //copy object
157  else
158  ATH_MSG_INFO ("m_eventInputHeader_1st is empty");
159 
160  if (m_runSecondStage) {
161  if (INheader_2nd)
162  *m_eventInputHeader_2nd = *INheader_2nd;
163  else
164  ATH_MSG_INFO("m_eventInputHeader_2nd is empty");
165  }
166 
167  if (OUTheader)
168  *m_eventOutputHeader = *OUTheader;
169  else
170  ATH_MSG_INFO ("m_eventOutputHeader is empty");
171 
172  m_EventTree->Fill();
173 
174  if (m_eventInputHeader_1st) {
175  ATH_MSG_DEBUG ("Wrote Event " << m_event << " in first stage header event " << m_eventInputHeader_1st->event());
176  } else {
177  ATH_MSG_ERROR("m_eventInputHeader_1st is null in FPGATrackSimOutputHeaderTool::writeData");
178  return StatusCode::FAILURE;
179  }
180  if (m_runSecondStage && m_eventInputHeader_2nd) {
181  ATH_MSG_DEBUG ("Wrote Event " << m_event << " in second stage header event " << m_eventInputHeader_2nd->event());
182  }
183  if (m_eventOutputHeader) {
184  ATH_MSG_DEBUG ("n.roads_1st = " << m_eventOutputHeader->nFPGATrackSimRoads_1st());
185  ATH_MSG_DEBUG ("n.roads_2nd = " << m_eventOutputHeader->nFPGATrackSimRoads_2nd());
186  ATH_MSG_DEBUG ("n.tracks_1st = " << m_eventOutputHeader->nFPGATrackSimTracks_1st());
187  ATH_MSG_DEBUG ("n.tracks_2nd = " << m_eventOutputHeader->nFPGATrackSimTracks_2nd());
188  } else {
189  ATH_MSG_ERROR("m_eventOutputHeader is null in FPGATrackSimOutputHeaderTool::writeData");
190  return StatusCode::FAILURE;
191  }
192 
193  m_event++;
194 
195  m_eventInputHeader_1st->reset();
196  if (m_runSecondStage and m_eventInputHeader_2nd) m_eventInputHeader_2nd->reset();
197  m_eventOutputHeader->reset();
198 
199  return StatusCode::SUCCESS;
200 }
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
204 {
205  if (m_rwoption.value() != std::string("READ")) {
206  ATH_MSG_WARNING ("Asked to read file that is not in READ mode");
207  return StatusCode::SUCCESS;
208  }
209 
210  m_eventInputHeader_1st->reset();
211  m_eventOutputHeader->reset();
212  if (m_runSecondStage) m_eventInputHeader_2nd->reset();
213 
214  ATH_MSG_DEBUG ("Asked Event " << m_event << " in this file; current total is " << m_totevent);
215  last = false;
216  if (m_event >= m_EventTree->GetEntries()) {
217  if (++m_file < m_inpath.value().size()) {
218  ATH_CHECK(openFile(m_inpath.value().at(m_file)));
219  }
220  else {
221  last = true;
222  return StatusCode::SUCCESS;
223  }
224  }
225 
226 
227  Int_t statIn_1st = m_EventTree->GetBranch(m_branchNameIn_1st.c_str())->GetEntry(m_event);
228  Int_t statOut = m_EventTree->GetBranch(m_branchNameOut.c_str())->GetEntry(m_event);
229 
230  if (statIn_1st <= 0) ATH_MSG_WARNING("Error in reading from branch " << m_branchNameIn_1st);
231  if (statOut <= 0) ATH_MSG_WARNING("Error in reading from branch " << m_branchNameOut);
232 
233  if (m_runSecondStage) {
234  Int_t statIn_2nd = m_EventTree->GetBranch(m_branchNameIn_2nd.c_str())->GetEntry(m_event);
235  if (statIn_2nd <= 0) ATH_MSG_WARNING("Error in reading from branch " << m_branchNameIn_2nd);
236  }
237 
238 
239  ATH_MSG_DEBUG ("Asked in first stage header event: " << m_eventInputHeader_1st->event());
240  if (m_runSecondStage) ATH_MSG_DEBUG ("Asked in second stage header event: " << m_eventInputHeader_2nd->event());
241 
242  ATH_MSG_DEBUG ("n.roads_1st = " << m_eventOutputHeader->nFPGATrackSimRoads_1st());
243  ATH_MSG_DEBUG ("n.tracks_1st = " << m_eventOutputHeader->nFPGATrackSimTracks_1st());
244 
245  if (m_runSecondStage) {
246  ATH_MSG_DEBUG ("n.roads_2nd = " << m_eventOutputHeader->nFPGATrackSimRoads_2nd());
247  ATH_MSG_DEBUG ("n.tracks_2nd = " << m_eventOutputHeader->nFPGATrackSimTracks_2nd());
248  }
249 
250  *INheader_1st = *m_eventInputHeader_1st; //copy object to the external pointer
251  *OUTheader = *m_eventOutputHeader;
252  if (m_runSecondStage) *INheader_2nd = *m_eventInputHeader_2nd;
253 
254  m_event++;
255  m_totevent++;
256 
257 
258  return StatusCode::SUCCESS;
259 }
260 
FPGATrackSimOutputHeaderTool::m_branchNameIn_1st
std::string m_branchNameIn_1st
Definition: FPGATrackSimOutputHeaderTool.h:49
getMenu.algname
algname
Definition: getMenu.py:53
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
FPGATrackSimLogicalEventInputHeader
Definition: FPGATrackSimLogicalEventInputHeader.h:21
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:126
FPGATrackSimOutputHeaderTool::openFile
StatusCode openFile(std::string const &path)
Definition: FPGATrackSimOutputHeaderTool.cxx:14
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
FPGATrackSimOutputHeaderTool::m_runSecondStage
BooleanProperty m_runSecondStage
Definition: FPGATrackSimOutputHeaderTool.h:42
FPGATrackSimOutputHeaderTool::m_branchNameOut
std::string m_branchNameOut
Definition: FPGATrackSimOutputHeaderTool.h:51
FPGATrackSimOutputHeaderTool::writeData
virtual StatusCode writeData(FPGATrackSimLogicalEventInputHeader *INheader_1st, FPGATrackSimLogicalEventInputHeader *INheader_2nd, FPGATrackSimLogicalEventOutputHeader *OUTheader) override
Definition: FPGATrackSimOutputHeaderTool.cxx:146
FPGATrackSimOutputHeaderTool::finalize
virtual StatusCode finalize() override
Definition: FPGATrackSimOutputHeaderTool.cxx:125
FPGATrackSimOutputHeaderTool::m_file
std::atomic< unsigned > m_file
Definition: FPGATrackSimOutputHeaderTool.h:47
FPGATrackSimOutputHeaderTool::m_inpath
StringArrayProperty m_inpath
Definition: FPGATrackSimOutputHeaderTool.h:40
FPGATrackSimOutputHeaderTool::m_rwoption
StringProperty m_rwoption
Definition: FPGATrackSimOutputHeaderTool.h:41
FPGATrackSimOutputHeaderTool::m_branchNameIn_2nd
std::string m_branchNameIn_2nd
Definition: FPGATrackSimOutputHeaderTool.h:50
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
FPGATrackSimLogicalEventOutputHeader.h
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
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
FPGATrackSimOutputHeaderTool::m_event
std::atomic< unsigned > m_event
Definition: FPGATrackSimOutputHeaderTool.h:45
FPGATrackSimOutputHeaderTool.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
FPGATrackSimOutputHeaderTool::readData
virtual StatusCode readData(FPGATrackSimLogicalEventInputHeader *INheader_1st, FPGATrackSimLogicalEventInputHeader *INheader_2nd, FPGATrackSimLogicalEventOutputHeader *OUTheader, bool &last) override
Definition: FPGATrackSimOutputHeaderTool.cxx:203
FPGATrackSimOutputHeaderTool::m_totevent
std::atomic< unsigned > m_totevent
Definition: FPGATrackSimOutputHeaderTool.h:46
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
FPGATrackSimOutputHeaderTool::FPGATrackSimOutputHeaderTool
FPGATrackSimOutputHeaderTool(std::string const &, std::string const &, IInterface const *)
Definition: FPGATrackSimOutputHeaderTool.cxx:9
FPGATrackSimLogicalEventOutputHeader
Definition: FPGATrackSimLogicalEventOutputHeader.h:12
FPGATrackSimLogicalEventInputHeader.h
FPGATrackSimOutputHeaderTool::initialize
virtual StatusCode initialize() override
Definition: FPGATrackSimOutputHeaderTool.cxx:72