EventReadingAlgorithm.cc
Go to the documentation of this file.
1 /**
2  * @file PandoraSDK/src/Persistency/EventReadingAlgorithm.cc
3  *
4  * @brief Implementation of the event reading algorithm class.
5  *
6  * $Log: $
7  */
8 
9 #include "Pandora/AlgorithmHeaders.h"
10 
11 #include "Persistency/BinaryFileReader.h"
12 #include "Persistency/XmlFileReader.h"
13 
16 
18 
19 #include <algorithm>
20 
21 using namespace pandora;
22 
23 namespace lar_content
24 {
25 
26 EventReadingAlgorithm::EventReadingAlgorithm() :
27  m_skipToEvent(0),
28  m_useLArCaloHits(true),
29  m_larCaloHitVersion(1),
30  m_useLArMCParticles(true),
31  m_larMCParticleVersion(2),
32  m_pEventFileReader(nullptr)
33 {
34 }
35 
36 //------------------------------------------------------------------------------------------------------------------------------------------
37 
39 {
40  delete m_pEventFileReader;
41 }
42 
43 //------------------------------------------------------------------------------------------------------------------------------------------
44 
46 {
47  if (!m_geometryFileName.empty())
48  {
49  const FileType geometryFileType(this->GetFileType(m_geometryFileName));
50 
51  if (BINARY == geometryFileType)
52  {
53  BinaryFileReader fileReader(this->GetPandora(), m_geometryFileName);
54  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, fileReader.ReadGeometry());
55  }
56  else if (XML == geometryFileType)
57  {
58  XmlFileReader fileReader(this->GetPandora(), m_geometryFileName);
59  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, fileReader.ReadGeometry());
60  }
61  else
62  {
63  return STATUS_CODE_FAILURE;
64  }
65  }
66 
67  if (!m_eventFileName.empty())
68  {
69  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReplaceEventFileReader(m_eventFileName));
70  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, m_pEventFileReader->GoToEvent(m_skipToEvent));
71  }
72 
73  return STATUS_CODE_SUCCESS;
74 }
75 
76 //------------------------------------------------------------------------------------------------------------------------------------------
77 
79 {
80  if ((nullptr != m_pEventFileReader) && !m_eventFileName.empty())
81  {
82  try
83  {
84  m_pEventFileReader->ReadEvent();
85  }
86  catch (const StatusCodeException &)
87  {
88  this->MoveToNextEventFile();
89  }
90 
91  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::RepeatEventPreparation(*this));
92  }
93 
94  return STATUS_CODE_SUCCESS;
95 }
96 
97 //------------------------------------------------------------------------------------------------------------------------------------------
98 
100 {
101  if (m_eventFileNameVector.empty())
102  throw StopProcessingException("All event files processed");
103 
105  m_eventFileNameVector.pop_back();
106  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReplaceEventFileReader(m_eventFileName));
107 
108  try
109  {
110  m_pEventFileReader->ReadEvent();
111  }
112  catch (const StatusCodeException &)
113  {
114  this->MoveToNextEventFile();
115  }
116 }
117 
118 //------------------------------------------------------------------------------------------------------------------------------------------
119 
121 {
122  delete m_pEventFileReader;
123  m_pEventFileReader = nullptr;
124 
125  std::cout << "EventReadingAlgorithm: Processing event file: " << fileName << std::endl;
126  const FileType eventFileType(this->GetFileType(fileName));
127 
128  if (BINARY == eventFileType)
129  {
130  m_pEventFileReader = new BinaryFileReader(this->GetPandora(), fileName);
131  }
132  else if (XML == eventFileType)
133  {
134  m_pEventFileReader = new XmlFileReader(this->GetPandora(), fileName);
135  }
136  else
137  {
138  return STATUS_CODE_FAILURE;
139  }
140 
141  if (m_useLArCaloHits)
143 
146 
147  return STATUS_CODE_SUCCESS;
148 }
149 
150 //------------------------------------------------------------------------------------------------------------------------------------------
151 
153 {
154  std::string fileExtension(fileName.substr(fileName.find_last_of(".")));
155  std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
156 
157  if (std::string(".xml") == fileExtension)
158  {
159  return XML;
160  }
161  else if (std::string(".pndr") == fileExtension)
162  {
163  return BINARY;
164  }
165  else
166  {
167  std::cout << "EventReadingAlgorithm: Unknown file type specified " << fileName << std::endl;
168  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
169  }
170 }
171 
172 //------------------------------------------------------------------------------------------------------------------------------------------
173 
174 StatusCode EventReadingAlgorithm::ReadSettings(const TiXmlHandle xmlHandle)
175 {
176  ExternalEventReadingParameters *pExternalParameters(nullptr);
177 
178  if (this->ExternalParametersPresent())
179  {
180  pExternalParameters = dynamic_cast<ExternalEventReadingParameters *>(this->GetExternalParameters());
181 
182  if (!pExternalParameters)
183  return STATUS_CODE_FAILURE;
184  }
185 
186  if (pExternalParameters && !pExternalParameters->m_geometryFileName.empty())
187  {
188  m_geometryFileName = pExternalParameters->m_geometryFileName;
189  }
190  else
191  {
192  PANDORA_RETURN_RESULT_IF_AND_IF(
193  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "GeometryFileName", m_geometryFileName));
194  }
195 
196  if (pExternalParameters && !pExternalParameters->m_eventFileNameList.empty())
197  {
198  XmlHelper::TokenizeString(pExternalParameters->m_eventFileNameList, m_eventFileNameVector, ":");
199  }
200  else
201  {
202  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
203  XmlHelper::ReadVectorOfValues(xmlHandle, "EventFileNameList", m_eventFileNameVector));
204  }
205 
206  if (!m_eventFileNameVector.empty())
207  {
210  m_eventFileNameVector.pop_back();
211  }
212 
213  if (pExternalParameters && pExternalParameters->m_skipToEvent.IsInitialized())
214  {
215  m_skipToEvent = pExternalParameters->m_skipToEvent.Get();
216  }
217  else
218  {
219  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "SkipToEvent", m_skipToEvent));
220  }
221 
222  if (m_geometryFileName.empty() && m_eventFileName.empty())
223  {
224  std::cout << "EventReadingAlgorithm - nothing to do; neither geometry nor event file specified." << std::endl;
225  return STATUS_CODE_NOT_INITIALIZED;
226  }
227 
228  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "UseLArCaloHits", m_useLArCaloHits));
229 
230  PANDORA_RETURN_RESULT_IF_AND_IF(
231  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "LArCaloHitVersion", m_larCaloHitVersion));
232 
233  PANDORA_RETURN_RESULT_IF_AND_IF(
234  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "LArMCParticleVersion", m_larMCParticleVersion));
235 
236  PANDORA_RETURN_RESULT_IF_AND_IF(
237  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "UseLArMCParticles", m_useLArMCParticles));
238 
239  return STATUS_CODE_SUCCESS;
240 }
241 
242 } // namespace lar_content
std::string m_geometryFileName
Name of the file containing geometry information.
bool m_useLArMCParticles
Whether to read lar mc particles, or standard pandora mc particles.
std::string string
Definition: nybbler.cc:12
Header file for the lar calo hit class.
unsigned int m_larCaloHitVersion
LArCaloHit version for LArCaloHitFactory.
pandora::StatusCode ReplaceEventFileReader(const std::string &fileName)
Replace the current event file reader with a new reader for the specified file.
pandora::StringVector m_eventFileNameVector
Vector of file names to be processed.
unsigned int m_skipToEvent
Index of first event to consider in first input file.
pandora::InputUInt m_skipToEvent
Index of first event to consider in input file.
std::string m_geometryFileName
Name of the file containing geometry information.
fileName
Definition: dumpTree.py:9
bool m_useLArCaloHits
Whether to read lar calo hits, or standard pandora calo hits.
pandora::FileType GetFileType(const std::string &fileName) const
Analyze a provided file name to extract the file type/extension.
Header file for the lar mc particle class.
std::string m_eventFileNameList
Colon-separated list of file names to be processed.
LArMCParticleFactory responsible for object creation.
pandora::FileReader * m_pEventFileReader
Address of the event file reader.
Header file for the event reading algorithm class.
static unsigned int reverse(QString &chars, unsigned char *level, unsigned int a, unsigned int b)
Definition: qstring.cpp:11649
unsigned int m_larMCParticleVersion
LArMCParticle version for LArMCParticleFactory.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
LArCaloHitFactory responsible for object creation.
Definition: LArCaloHit.h:110
std::string m_eventFileName
Name of the current file containing event information.
void MoveToNextEventFile()
Proceed to process next event file named in the input list.
QTextStream & endl(QTextStream &s)