LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
EventReadingAlgorithm.cc
Go to the documentation of this file.
1 
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_useLArMCParticles(true),
30  m_pEventFileReader(nullptr)
31 {
32 }
33 
34 //------------------------------------------------------------------------------------------------------------------------------------------
35 
37 {
38  delete m_pEventFileReader;
39 }
40 
41 //------------------------------------------------------------------------------------------------------------------------------------------
42 
44 {
45  if (!m_geometryFileName.empty())
46  {
47  const FileType geometryFileType(this->GetFileType(m_geometryFileName));
48 
49  if (BINARY == geometryFileType)
50  {
51  BinaryFileReader fileReader(this->GetPandora(), m_geometryFileName);
52  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, fileReader.ReadGeometry());
53  }
54  else if (XML == geometryFileType)
55  {
56  XmlFileReader fileReader(this->GetPandora(), m_geometryFileName);
57  PANDORA_RETURN_RESULT_IF(pandora::STATUS_CODE_SUCCESS, !=, fileReader.ReadGeometry());
58  }
59  else
60  {
61  return STATUS_CODE_FAILURE;
62  }
63  }
64 
65  if (!m_eventFileName.empty())
66  {
67  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReplaceEventFileReader(m_eventFileName));
68  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, m_pEventFileReader->GoToEvent(m_skipToEvent));
69  }
70 
71  return STATUS_CODE_SUCCESS;
72 }
73 
74 //------------------------------------------------------------------------------------------------------------------------------------------
75 
77 {
78  if ((nullptr != m_pEventFileReader) && !m_eventFileName.empty())
79  {
80  try
81  {
82  m_pEventFileReader->ReadEvent();
83  }
84  catch (const StatusCodeException &)
85  {
86  this->MoveToNextEventFile();
87  }
88 
89  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::RepeatEventPreparation(*this));
90  }
91 
92  return STATUS_CODE_SUCCESS;
93 }
94 
95 //------------------------------------------------------------------------------------------------------------------------------------------
96 
98 {
99  if (m_eventFileNameVector.empty())
100  throw StopProcessingException("All event files processed");
101 
103  m_eventFileNameVector.pop_back();
104  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReplaceEventFileReader(m_eventFileName));
105 
106  try
107  {
108  m_pEventFileReader->ReadEvent();
109  }
110  catch (const StatusCodeException &)
111  {
112  this->MoveToNextEventFile();
113  }
114 }
115 
116 //------------------------------------------------------------------------------------------------------------------------------------------
117 
118 StatusCode EventReadingAlgorithm::ReplaceEventFileReader(const std::string &fileName)
119 {
120  delete m_pEventFileReader;
121  m_pEventFileReader = nullptr;
122 
123  std::cout << "EventReadingAlgorithm: Processing event file: " << fileName << std::endl;
124  const FileType eventFileType(this->GetFileType(fileName));
125 
126  if (BINARY == eventFileType)
127  {
128  m_pEventFileReader = new BinaryFileReader(this->GetPandora(), fileName);
129  }
130  else if (XML == eventFileType)
131  {
132  m_pEventFileReader = new XmlFileReader(this->GetPandora(), fileName);
133  }
134  else
135  {
136  return STATUS_CODE_FAILURE;
137  }
138 
139  if (m_useLArCaloHits)
140  m_pEventFileReader->SetFactory(new LArCaloHitFactory);
141 
143  m_pEventFileReader->SetFactory(new LArMCParticleFactory);
144 
145  return STATUS_CODE_SUCCESS;
146 }
147 
148 //------------------------------------------------------------------------------------------------------------------------------------------
149 
150 FileType EventReadingAlgorithm::GetFileType(const std::string &fileName) const
151 {
152  std::string fileExtension(fileName.substr(fileName.find_last_of(".")));
153  std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
154 
155  if (std::string(".xml") == fileExtension)
156  {
157  return XML;
158  }
159  else if (std::string(".pndr") == fileExtension)
160  {
161  return BINARY;
162  }
163  else
164  {
165  std::cout << "EventReadingAlgorithm: Unknown file type specified " << fileName << std::endl;
166  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
167  }
168 }
169 
170 //------------------------------------------------------------------------------------------------------------------------------------------
171 
172 StatusCode EventReadingAlgorithm::ReadSettings(const TiXmlHandle xmlHandle)
173 {
174  ExternalEventReadingParameters *pExternalParameters(nullptr);
175 
176  if (this->ExternalParametersPresent())
177  {
178  pExternalParameters = dynamic_cast<ExternalEventReadingParameters*>(this->GetExternalParameters());
179 
180  if (!pExternalParameters)
181  return STATUS_CODE_FAILURE;
182  }
183 
184  if (pExternalParameters && !pExternalParameters->m_geometryFileName.empty())
185  {
186  m_geometryFileName = pExternalParameters->m_geometryFileName;
187  }
188  else
189  {
190  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "GeometryFileName", m_geometryFileName));
191  }
192 
193  if (pExternalParameters && !pExternalParameters->m_eventFileNameList.empty())
194  {
195  XmlHelper::TokenizeString(pExternalParameters->m_eventFileNameList, m_eventFileNameVector, ":");
196  }
197  else
198  {
199  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(xmlHandle, "EventFileNameList", m_eventFileNameVector));
200  }
201 
202  if (!m_eventFileNameVector.empty())
203  {
204  std::reverse(m_eventFileNameVector.begin(), m_eventFileNameVector.end());
206  m_eventFileNameVector.pop_back();
207  }
208 
209  if (pExternalParameters && pExternalParameters->m_skipToEvent.IsInitialized())
210  {
211  m_skipToEvent = pExternalParameters->m_skipToEvent.Get();
212  }
213  else
214  {
215  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "SkipToEvent", m_skipToEvent));
216  }
217 
218  if (m_geometryFileName.empty() && m_eventFileName.empty())
219  {
220  std::cout << "EventReadingAlgorithm - nothing to do; neither geometry nor event file specified." << std::endl;
221  return STATUS_CODE_NOT_INITIALIZED;
222  }
223 
224  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
225  "UseLArCaloHits", m_useLArCaloHits));
226 
227  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
228  "UseLArMCParticles", m_useLArMCParticles));
229 
230  return STATUS_CODE_SUCCESS;
231 }
232 
233 } // 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.
Header file for the lar calo hit class.
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.
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.
Definition: LArMCParticle.h:64
pandora::FileReader * m_pEventFileReader
Address of the event file reader.
Header file for the event reading algorithm class.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
LArCaloHitFactory responsible for object creation.
Definition: LArCaloHit.h:64
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.