Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:20:25

0001 /**
0002  *  @file   PandoraSDK/include/Persistency/BinaryFileReader.h
0003  *
0004  *  @brief  Header file for the binary file reader class.
0005  *
0006  *  $Log: $
0007  */
0008 #ifndef PANDORA_BINARY_FILE_READER_H
0009 #define PANDORA_BINARY_FILE_READER_H 1
0010 
0011 #include "Pandora/Pandora.h"
0012 
0013 #include "Objects/CartesianVector.h"
0014 #include "Objects/TrackState.h"
0015 
0016 #include "Persistency/FileReader.h"
0017 
0018 #include <fstream>
0019 
0020 namespace pandora
0021 {
0022 
0023 /**
0024  *  @brief  BinaryFileReader class
0025  */
0026 class BinaryFileReader : public FileReader
0027 {
0028 public:
0029     /**
0030      *  @brief  Constructor
0031      *
0032      *  @param  pandora the pandora instance to be used alongside the file reader
0033      *  @param  fileName the name of the file containing the pandora objects
0034      */
0035     BinaryFileReader(const pandora::Pandora &pandora, const std::string &fileName);
0036 
0037     /**
0038      *  @brief  Destructor
0039      */
0040     ~BinaryFileReader();
0041 
0042     /**
0043      *  @brief  Read a variable from the file
0044      */
0045     template <typename T>
0046     StatusCode ReadVariable(T &t);
0047 
0048 private:
0049     StatusCode ReadHeader();
0050     StatusCode GoToNextContainer();
0051     ContainerId GetNextContainerId();
0052     StatusCode GoToGeometry(const unsigned int geometryNumber);
0053     StatusCode GoToEvent(const unsigned int eventNumber);
0054     StatusCode ReadNextGlobalHeaderComponent();
0055     StatusCode ReadNextGeometryComponent();
0056     StatusCode ReadNextEventComponent();
0057 
0058     /**
0059      *  @brief  Read file version information from the current position in the file
0060      *
0061      *  @param  checkComponentId whether to check the component id before deserializing
0062      */
0063     StatusCode ReadVersion(bool checkComponentId = true);
0064 
0065     /**
0066      *  @brief  Read a sub detector from the current position in the file
0067      *
0068      *  @param  checkComponentId whether to check the component id before deserializing
0069      */
0070     StatusCode ReadSubDetector(bool checkComponentId = true);
0071 
0072     /**
0073      *  @brief  Read a lar tpc from the current position in the file
0074      *
0075      *  @param  checkComponentId whether to check the component id before deserializing
0076      */
0077     StatusCode ReadLArTPC(bool checkComponentId = true);
0078 
0079     /**
0080      *  @brief  Read a line gap from the current position in the file
0081      *
0082      *  @param  checkComponentId whether to check the component id before deserializing
0083      */
0084     StatusCode ReadLineGap(bool checkComponentId = true);
0085 
0086     /**
0087      *  @brief  Read a box gap from the current position in the file
0088      *
0089      *  @param  checkComponentId whether to check the component id before deserializing
0090      */
0091     StatusCode ReadBoxGap(bool checkComponentId = true);
0092 
0093     /**
0094      *  @brief  Read a concentric gap from the current position in the file
0095      *
0096      *  @param  checkComponentId whether to check the component id before deserializing
0097      */
0098     StatusCode ReadConcentricGap(bool checkComponentId = true);
0099 
0100     /**
0101      *  @brief  Read a calo hit from the current position in the file, recreating the stored object
0102      *
0103      *  @param  checkComponentId whether to check the component id before deserializing
0104      */
0105     StatusCode ReadCaloHit(bool checkComponentId = true);
0106 
0107     /**
0108      *  @brief  Read a track from the current position in the file, recreating the stored object
0109      *
0110      *  @param  checkComponentId whether to check the component id before deserializing
0111      */
0112     StatusCode ReadTrack(bool checkComponentId = true);
0113 
0114     /**
0115      *  @brief  Read a mc particle from the current position in the file, recreating the stored object
0116      *
0117      *  @param  checkComponentId whether to check the component id before deserializing
0118      */
0119     StatusCode ReadMCParticle(bool checkComponentId = true);
0120 
0121     /**
0122      *  @brief  Read a relationship from the current position in the file, recreating the stored relationship
0123      *
0124      *  @param  checkComponentId whether to check the component id before deserializing
0125      */
0126     StatusCode ReadRelationship(bool checkComponentId = true);
0127 
0128     /**
0129      *  @brief  Read event-level information from the current position in the file, adding it to the pandora instance
0130      *
0131      *  @param  checkComponentId whether to check the component id before deserializing
0132      */
0133     StatusCode ReadEventInformation(bool checkComponentId = true);
0134 
0135     std::ifstream::pos_type m_containerPosition; ///< Position of start of the current event/geometry container object in file
0136     std::ifstream::pos_type m_containerSize;     ///< Size of the current event/geometry container object in the file
0137     std::ifstream m_fileStream;                  ///< The stream class to read from the file
0138 };
0139 
0140 //------------------------------------------------------------------------------------------------------------------------------------------
0141 
0142 template <typename T>
0143 inline StatusCode BinaryFileReader::ReadVariable(T &t)
0144 {
0145     char *const pMemBlock = new char[sizeof(T)];
0146     m_fileStream.read(pMemBlock, sizeof(T));
0147 
0148     t = *(reinterpret_cast<T *>(pMemBlock));
0149     delete[] pMemBlock;
0150 
0151     if (!m_fileStream.good())
0152         return STATUS_CODE_FAILURE;
0153 
0154     return STATUS_CODE_SUCCESS;
0155 }
0156 
0157 template <>
0158 inline StatusCode BinaryFileReader::ReadVariable(std::string &t)
0159 {
0160     unsigned int stringSize;
0161     const StatusCode statusCode(this->ReadVariable(stringSize));
0162 
0163     if (STATUS_CODE_SUCCESS != statusCode)
0164         return statusCode;
0165 
0166     char *const pMemBlock = new char[stringSize];
0167     m_fileStream.read(pMemBlock, stringSize);
0168 
0169     t = std::string(pMemBlock, stringSize);
0170     delete[] pMemBlock;
0171 
0172     if (!m_fileStream.good())
0173         return STATUS_CODE_FAILURE;
0174 
0175     return STATUS_CODE_SUCCESS;
0176 }
0177 
0178 template <>
0179 inline StatusCode BinaryFileReader::ReadVariable(CartesianVector &t)
0180 {
0181     float x, y, z;
0182     PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReadVariable(x));
0183     PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReadVariable(y));
0184     PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReadVariable(z));
0185     t = CartesianVector(x, y, z);
0186 
0187     return STATUS_CODE_SUCCESS;
0188 }
0189 
0190 template <>
0191 inline StatusCode BinaryFileReader::ReadVariable(TrackState &t)
0192 {
0193     CartesianVector position(0.f, 0.f, 0.f), momentum(0.f, 0.f, 0.f);
0194     PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReadVariable(position));
0195     PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->ReadVariable(momentum));
0196     t = TrackState(position, momentum);
0197 
0198     return STATUS_CODE_SUCCESS;
0199 }
0200 
0201 } // namespace pandora
0202 
0203 #endif // #ifndef PANDORA_BINARY_FILE_READER_H