-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathEventReadingAlgorithm.cc
242 lines (194 loc) · 7.82 KB
/
EventReadingAlgorithm.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* @file PandoraSDK/src/Persistency/EventReadingAlgorithm.cc
*
* @brief Implementation of the event reading algorithm class.
*
* $Log: $
*/
#include "Pandora/AlgorithmHeaders.h"
#include "Persistency/BinaryFileReader.h"
#include "Persistency/XmlFileReader.h"
#include "larpandoracontent/LArObjects/LArCaloHit.h"
#include "larpandoracontent/LArObjects/LArMCParticle.h"
#include "larpandoracontent/LArPersistency/EventReadingAlgorithm.h"
#include <algorithm>
using namespace pandora;
namespace lar_content
{
EventReadingAlgorithm::EventReadingAlgorithm() :
m_skipToEvent(0),
m_useLArCaloHits(true),
m_larCaloHitVersion(1),
m_useLArMCParticles(true),
m_larMCParticleVersion(2),
m_pEventFileReader(nullptr)
{
}
//------------------------------------------------------------------------------------------------------------------------------------------
EventReadingAlgorithm::~EventReadingAlgorithm()
{
delete m_pEventFileReader;
}
//------------------------------------------------------------------------------------------------------------------------------------------
StatusCode EventReadingAlgorithm::Initialize()
{
if (!m_geometryFileName.empty())
{
const FileType geometryFileType(this->GetFileType(m_geometryFileName));
if (BINARY == geometryFileType)
{
BinaryFileReader fileReader(this->GetPandora(), m_geometryFileName);
PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, fileReader.ReadGeometry());
}
else if (XML == geometryFileType)
{
XmlFileReader fileReader(this->GetPandora(), m_geometryFileName);
PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, fileReader.ReadGeometry());
}
else
{
return STATUS_CODE_FAILURE;
}
}
if (!m_eventFileName.empty())
{
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReplaceEventFileReader(m_eventFileName));
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, m_pEventFileReader->GoToEvent(m_skipToEvent));
}
return STATUS_CODE_SUCCESS;
}
//------------------------------------------------------------------------------------------------------------------------------------------
StatusCode EventReadingAlgorithm::Run()
{
if ((nullptr != m_pEventFileReader) && !m_eventFileName.empty())
{
try
{
m_pEventFileReader->ReadEvent();
}
catch (const StatusCodeException &)
{
this->MoveToNextEventFile();
}
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::RepeatEventPreparation(*this));
}
return STATUS_CODE_SUCCESS;
}
//------------------------------------------------------------------------------------------------------------------------------------------
void EventReadingAlgorithm::MoveToNextEventFile()
{
if (m_eventFileNameVector.empty())
throw StopProcessingException("All event files processed");
m_eventFileName = m_eventFileNameVector.back();
m_eventFileNameVector.pop_back();
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReplaceEventFileReader(m_eventFileName));
try
{
m_pEventFileReader->ReadEvent();
}
catch (const StatusCodeException &)
{
this->MoveToNextEventFile();
}
}
//------------------------------------------------------------------------------------------------------------------------------------------
StatusCode EventReadingAlgorithm::ReplaceEventFileReader(const std::string &fileName)
{
delete m_pEventFileReader;
m_pEventFileReader = nullptr;
std::cout << "EventReadingAlgorithm: Processing event file: " << fileName << std::endl;
const FileType eventFileType(this->GetFileType(fileName));
if (BINARY == eventFileType)
{
m_pEventFileReader = new BinaryFileReader(this->GetPandora(), fileName);
}
else if (XML == eventFileType)
{
m_pEventFileReader = new XmlFileReader(this->GetPandora(), fileName);
}
else
{
return STATUS_CODE_FAILURE;
}
if (m_useLArCaloHits)
m_pEventFileReader->SetFactory(new LArCaloHitFactory(m_larCaloHitVersion));
if (m_useLArMCParticles)
m_pEventFileReader->SetFactory(new LArMCParticleFactory(m_larMCParticleVersion));
return STATUS_CODE_SUCCESS;
}
//------------------------------------------------------------------------------------------------------------------------------------------
FileType EventReadingAlgorithm::GetFileType(const std::string &fileName) const
{
std::string fileExtension(fileName.substr(fileName.find_last_of(".")));
std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
if (std::string(".xml") == fileExtension)
{
return XML;
}
else if (std::string(".pndr") == fileExtension)
{
return BINARY;
}
else
{
std::cout << "EventReadingAlgorithm: Unknown file type specified " << fileName << std::endl;
throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
}
}
//------------------------------------------------------------------------------------------------------------------------------------------
StatusCode EventReadingAlgorithm::ReadSettings(const TiXmlHandle xmlHandle)
{
ExternalEventReadingParameters *pExternalParameters(nullptr);
if (this->ExternalParametersPresent())
{
pExternalParameters = dynamic_cast<ExternalEventReadingParameters *>(this->GetExternalParameters());
if (!pExternalParameters)
return STATUS_CODE_FAILURE;
}
if (pExternalParameters && !pExternalParameters->m_geometryFileName.empty())
{
m_geometryFileName = pExternalParameters->m_geometryFileName;
}
else
{
PANDORA_RETURN_RESULT_IF_AND_IF(
STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "GeometryFileName", m_geometryFileName));
}
if (pExternalParameters && !pExternalParameters->m_eventFileNameList.empty())
{
XmlHelper::TokenizeString(pExternalParameters->m_eventFileNameList, m_eventFileNameVector, ":");
}
else
{
PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
XmlHelper::ReadVectorOfValues(xmlHandle, "EventFileNameList", m_eventFileNameVector));
}
if (!m_eventFileNameVector.empty())
{
std::reverse(m_eventFileNameVector.begin(), m_eventFileNameVector.end());
m_eventFileName = m_eventFileNameVector.back();
m_eventFileNameVector.pop_back();
}
if (pExternalParameters && pExternalParameters->m_skipToEvent.IsInitialized())
{
m_skipToEvent = pExternalParameters->m_skipToEvent.Get();
}
else
{
PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "SkipToEvent", m_skipToEvent));
}
if (m_geometryFileName.empty() && m_eventFileName.empty())
{
std::cout << "EventReadingAlgorithm - nothing to do; neither geometry nor event file specified." << std::endl;
return STATUS_CODE_NOT_INITIALIZED;
}
PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "UseLArCaloHits", m_useLArCaloHits));
PANDORA_RETURN_RESULT_IF_AND_IF(
STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "LArCaloHitVersion", m_larCaloHitVersion));
PANDORA_RETURN_RESULT_IF_AND_IF(
STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "LArMCParticleVersion", m_larMCParticleVersion));
PANDORA_RETURN_RESULT_IF_AND_IF(
STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "UseLArMCParticles", m_useLArMCParticles));
return STATUS_CODE_SUCCESS;
}
} // namespace lar_content