ATLAS Offline Software
Loading...
Searching...
No Matches
FPGATrackSimInputHeaderTool.cxx
Go to the documentation of this file.
1/*
2 0;95;0c Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3*/
4
7
8
9FPGATrackSimInputHeaderTool::FPGATrackSimInputHeaderTool(const std::string& algname, const std::string &name, const IInterface *ifc) :
10 base_class(algname,name,ifc)
11{}
12
13StatusCode FPGATrackSimInputHeaderTool::openFile(std::string const & path) const
14{
15 // close old file
16 if (m_infile && m_infile->IsOpen())
17 {
18 m_infile->Close();
19 // I don't think we delete the pointer cause root handles that?
20 }
21
22 // open new file
23 ATH_MSG_DEBUG ( "Opening file " << path << " in " << m_rwoption.value() <<" mode" );
24 m_infile = TFile::Open(path.c_str(), m_rwoption.value().c_str());
25
26 if (m_infile == nullptr)
27 {
28 ATH_MSG_FATAL("Could not open input file: " << path);
29 return StatusCode::FAILURE;
30 }
31 if (m_rwoption.value()==std::string("READ") )
32 {
33 //get the tree, try the old name and also the new name for backwards compatability
34 m_EventTree = (TTree*) m_infile->Get("FPGATrackSimEventTree");
35
36 if (m_EventTree == nullptr || m_EventTree->GetEntries() == -1 ){
37 ATH_MSG_FATAL ("Input file: " << path << " has no entries");
38 return StatusCode::FAILURE;
39 }
40 ATH_MSG_INFO ( "Input file: " << path << " has "<< m_EventTree->GetEntries() <<" event entries" );
41
42 if(!m_EventTree->GetListOfBranches()->FindObject(m_branchName.c_str())){
43 ATH_MSG_FATAL ("Branch: " << m_branchName << " not found!");
44 return StatusCode::FAILURE;
45 }
46 ATH_MSG_INFO ( "Getting branch and set EventHeader" );
47 TBranch *branch = m_EventTree->GetBranch(m_branchName.c_str());
48 branch->SetAddress(&m_eventHeader);
49 }
50 m_event=0;
51 return StatusCode::SUCCESS;
52}
53
54
55// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
57
58 m_eventHeader = new FPGATrackSimEventInputHeader();
59 // set default name
60 m_branchName="FPGATrackSimEventInputHeader";
61
62 // open input file
63 if( m_rwoption.value()!=std::string("HEADER"))
64 {
65 if (m_inpath.value().empty())
66 {
67 ATH_MSG_ERROR("Empty input file list");
68 return StatusCode::FAILURE;
69 }
70 ATH_CHECK(openFile(m_inpath.value().front()));
71 }
72 if (m_rwoption.value()==std::string("READ") ) {
73 ATH_MSG_DEBUG ("Initialized in READ MODE");
74 }
75 else if (m_rwoption.value()==std::string("RECREATE") || m_rwoption.value()==std::string("HEADER")) {
76 ATH_MSG_INFO ("Creating empty brnaches in output file");
77 m_EventTree = new TTree("FPGATrackSimEventTree","data");
78 m_EventTree->Branch(m_branchName.c_str(),
79 m_branchName.c_str(),// class name
80 &m_eventHeader);
81 }
82 else {
83 ATH_MSG_ERROR("RWstatus =" <<m_rwoption.value()<<" not allowed!");
84 return StatusCode::FAILURE;
85 }
86 m_event=0; // in file
87 m_totevent=0; // total counter
88 return StatusCode::SUCCESS;
89}
90
91
93 ATH_MSG_INFO ( "finalize: closing files");
94 if (m_rwoption.value()==std::string("RECREATE")){
95 m_EventTree->Print();
96 // close the output files, but check that it exists (for athenaMP)
97 m_infile ->Write();
98 }
99 if (m_rwoption.value()!=std::string("HEADER"))
100 m_infile ->Close();
101
102 delete m_eventHeader;
103 return StatusCode::SUCCESS;
104}
105
106
108 if (m_rwoption.value()==std::string("READ") ){
109 ATH_MSG_WARNING ("Asked to write file in READ mode");
110 return StatusCode::SUCCESS;
111 }
112
113 if (header != nullptr){
114 *m_eventHeader= *header;//copy object
115 m_EventTree->Fill();
116 ATH_MSG_DEBUG ("Wrote Event "<<m_event <<" in header event "<<m_eventHeader->event() );
117 m_event++;
118 }
119
120 // Do we need this? TODO
121 //m_eventHeader->Clear(); //clean the pointer
122
123 return StatusCode::SUCCESS;
124}
125
127{
128 if (m_rwoption.value()!=std::string("READ") ){
129 ATH_MSG_WARNING ("Asked to read file that is not in READ mode");
130 return StatusCode::SUCCESS;
131 }
132
133 last=false;
134
135 ATH_MSG_DEBUG ("Asked Event "<<m_event <<" in this file; current total is "<<m_totevent);
136 if (m_event >= static_cast<unsigned>(m_EventTree->GetEntries()))
137 {
138 std::lock_guard<std::mutex> lock(m_fileMutex);
139 if (m_event >= static_cast<unsigned>(m_EventTree->GetEntries()))
140 {
141 unsigned current_file = m_file++;
142 if (current_file < m_inpath.value().size())
143 ATH_CHECK(openFile(m_inpath.value().at(current_file)));
144 else {
145 last=true;
146 return StatusCode::SUCCESS;
147 }
148 }
149 }
150
151 // Protect ROOT I/O with mutex
152 {
153 std::lock_guard<std::mutex> lock(m_fileMutex);
154 m_EventTree->GetEntry(m_event);
155 }
156
157 // increase counters
158 m_event++;
159 m_totevent++;
160
161 ATH_MSG_DEBUG("Reading event "<<m_eventHeader->event() );
162 *header= *m_eventHeader; //copy object to the external pointer
163
164 return StatusCode::SUCCESS;
165
166}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
This class reads FPGATrackSim input data from a ROOT file (wrapper file) Designed to be not thread-sa...
virtual StatusCode finalize() override
FPGATrackSimInputHeaderTool(const std::string &, const std::string &, const IInterface *)
virtual StatusCode initialize() override
virtual StatusCode readData(FPGATrackSimEventInputHeader *header, bool &last) const override
StatusCode openFile(std::string const &path) const
virtual StatusCode writeData(FPGATrackSimEventInputHeader *header) const override