From 8064986a17d26b7d9c7ec76469f3f855b9b5b77e Mon Sep 17 00:00:00 2001 From: Tom Dealtry Date: Fri, 3 May 2024 17:23:17 +0100 Subject: [PATCH] Add a file writer base class & start setting up an implementation to write WCSim files. It compiles here, but not in hk-ToolApp - need to chat with people about how to setup ABCs --- CMakeLists.txt | 2 + HKGFileWriterBase/HKGFileWriterBase.cpp | 76 ++++++++++++++++++ HKGFileWriterBase/HKGFileWriterBase.h | 79 +++++++++++++++++++ HKGFileWriterBase/README.md | 1 + .../HKGFileWriterRootWCSim.cpp | 51 ++++++++++++ .../HKGFileWriterRootWCSim.h | 74 +++++++++++++++++ HKGFileWriterRootWCSim/README.md | 1 + 7 files changed, 284 insertions(+) create mode 100644 HKGFileWriterBase/HKGFileWriterBase.cpp create mode 100644 HKGFileWriterBase/HKGFileWriterBase.h create mode 100644 HKGFileWriterBase/README.md create mode 100644 HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.cpp create mode 100644 HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.h create mode 100644 HKGFileWriterRootWCSim/README.md diff --git a/CMakeLists.txt b/CMakeLists.txt index d52674d..b259327 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ hk_add_tool(HKG4SteppingAction) hk_add_tool(HKG4PrimaryGeneratorAction) hk_add_tool(HKG4EventAction) hk_add_tool(HKG4RunAction) +hk_add_tool(HKGFileWriterBase) +hk_add_tool(HKGFileWriterRootWCSim) option(WCSIM_Check_Geometry_Overlaps "Toggle WCSim to save photon scattering and reflection history" diff --git a/HKGFileWriterBase/HKGFileWriterBase.cpp b/HKGFileWriterBase/HKGFileWriterBase.cpp new file mode 100644 index 0000000..2ab5e1f --- /dev/null +++ b/HKGFileWriterBase/HKGFileWriterBase.cpp @@ -0,0 +1,76 @@ +#include "HKGFileWriterBase.h" + +#include + +using namespace HK::Ghost::IO; + +HKGFileWriterBase::HKGFileWriterBase() : Tool() {} + +bool HKGFileWriterBase::Initialise(std::string configfile, DataModel& data) { + + if(configfile != "") + m_variables.Initialise(configfile); + // m_variables.Print(); + + m_data = &data; + m_log = m_data->Log; + + if(!m_variables.Get("verbose", m_verbose)) + m_verbose = 1; + + if(!GetOutputFilename()) { + *m_log << ML(0) << "GetOutputFilename() did not succeed. Stopping toolchain" << std::endl; + return false; + } + if(!SetupFile()) { + *m_log << ML(0) << "SetupFile() did not succeed. Stopping toolchain" << std::endl; + return false; + } + if(!FillEventIndependent()) { + *m_log << ML(0) << "FillEventIndependent() did not succeed. Stopping toolchain" << std::endl; + return false; + } + + return true; +} + +bool HKGFileWriterBase::GetOutputFilename() { + // Override the automatic filename construction + if(!m_variables.Get("override_filename", m_filename)) { + + //Setup the filename automatically + std::stringstream ss; + ss << "ghost_" + << "RUN" << "_" + << "SUBRUN" << "_" + << "PHYSICS" + << ".root"; + m_filename = ss.str(); + + *m_log << ML(1) << "TODO build auto filename from things stored in the DataModel" << std::endl; + } + *m_log << ML(2) << "Using output filename: " << m_filename << std::endl; + return true; +} + +bool HKGFileWriterBase::Execute() { + + if(!FillThisEvent()) { + *m_log << ML(0) << "FillThisEvent() did not succeed. Stopping toolchain" << std::endl; + return false; + } + return true; +} + +bool HKGFileWriterBase::Finalise() { + + if(!WriteFile()) { + *m_log << ML(0) << "WriteFile() did not succeed. Stopping toolchain" << std::endl; + return false; + } + if(!Cleanup()) { + *m_log << ML(0) << "Cleanup() did not succeed. Stopping toolchain" << std::endl; + return false; + } + return true; +} diff --git a/HKGFileWriterBase/HKGFileWriterBase.h b/HKGFileWriterBase/HKGFileWriterBase.h new file mode 100644 index 0000000..8aa9799 --- /dev/null +++ b/HKGFileWriterBase/HKGFileWriterBase.h @@ -0,0 +1,79 @@ +#ifndef HKGFileWriterBase_H +#define HKGFileWriterBase_H + +#include +#include + +#include +#include "Tool.h" + +/** + * \class HKGFileWriterBase + * + * This is a blank template for a Tool used by the script to generate a new custom tool. Please fill out the + * description and author information. + */ + +namespace HK { + namespace Ghost { + namespace IO { + class HKGFileWriterBase : public Tool { + + public: + + HKGFileWriterBase(); ///< Simple constructor + bool Initialise(std::string configfile, + DataModel& data); ///< Initialise Function for setting up Tool resources. @param + ///< configfile The path and name of the dynamic configuration file + ///< to read in. @param data A reference to the transient data + ///< class used to pass information between Tools. + bool Execute(); ///< Execute function used to perform Tool purpose. + bool Finalise(); ///< Finalise funciton used to clean up resources. + + private: + /** + * Get the output filename. + * This can be automatically generated based on your GHOST toolchain configuration (run number, physics simulated, etc.). + * Or can be overridden using the override_filename option in your Writer tool config. + * @return True if success. + */ + virtual bool GetOutputFilename(); + /** + * Open the output file(s) and setup the output tree(s)/histogram(s)/... + * Called in Initalise(). + * @return True if success. + */ + virtual bool SetupFile() = 0; + /** + * Fill the output tree(s)/histogram(s) that are event-independent (e.g. geometry). + * Called in Initalise(). + * @return True if success. + */ + virtual bool FillEventIndependent() = 0; + /** + * Fill the output tree(s)/histogram(s)/... + * Called in Execute(). + * @return True if success. + */ + virtual bool FillThisEvent() = 0; + /** + * Write the output tree(s)/histogram(s)/... to file. + * Called in Finalise(). + * @return True if success. + */ + virtual bool WriteFile() = 0; + /** + * Cleanup the memory used by this tool. + * Called in Finalise(). + * @return True if success. + */ + virtual bool Cleanup() = 0; + protected: + /// Output filename + std::string m_filename; + }; + } // namespace IO + } // namespace Ghost +} // namespace HK + +#endif diff --git a/HKGFileWriterBase/README.md b/HKGFileWriterBase/README.md new file mode 100644 index 0000000..024a3e0 --- /dev/null +++ b/HKGFileWriterBase/README.md @@ -0,0 +1 @@ +# HKGFileWriterBase diff --git a/HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.cpp b/HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.cpp new file mode 100644 index 0000000..54281eb --- /dev/null +++ b/HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.cpp @@ -0,0 +1,51 @@ +#include "HKGFileWriterRootWCSim.h" + +using namespace HK::Ghost::IO; + +HKGFileWriterRootWCSim::HKGFileWriterRootWCSim() : HKGFileWriterBase() { + m_p_file = nullptr; + m_p_tree_event = nullptr; + m_p_tree_geom = nullptr; +} + +bool HKGFileWriterRootWCSim::SetupFile() { + //Create the file + m_p_file = new TFile(m_filename.c_str(), "RECREATE"); + if(m_p_file == nullptr) + return false; + + //Create & setup the event tree + m_p_tree_event = new TTree("wcsimT", "WCSim Event tree"); + + + //Create & setup the geometry tree + m_p_tree_geom = new TTree("wcsimGeoT", "WCSim geometry tree"); + + + return true; +} + +bool HKGFileWriterRootWCSim::FillEventIndependent() { + if(m_p_tree_geom->Fill() < 0) + return false; + return true; +} + +bool HKGFileWriterRootWCSim::FillThisEvent() { + if(m_p_tree_event->Fill() < 0) + return false; + return true; +} + +bool HKGFileWriterRootWCSim::WriteFile() { + m_p_file->cd(); + m_p_file->Write(); + return true; +} + +bool HKGFileWriterRootWCSim::Cleanup() { + delete m_p_tree_geom; + delete m_p_tree_event; + delete m_p_file; + return true; +} diff --git a/HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.h b/HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.h new file mode 100644 index 0000000..a1b3647 --- /dev/null +++ b/HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.h @@ -0,0 +1,74 @@ +#ifndef HKGFileWriterRootWCSim_H +#define HKGFileWriterRootWCSim_H + +#include +#include + +#include "TTree.h" +#include "TFile.h" + +#include "../HKGFileWriterBase/HKGFileWriterBase.h" + +#include +#include "Tool.h" + +/** + * \class HKGFileWriterRootWCSim + * + * This is a blank template for a Tool used by the script to generate a new custom tool. Please fill out the + * description and author information. + */ + +namespace HK { + namespace Ghost { + namespace IO { + class HKGFileWriterRootWCSim : public HKGFileWriterBase { + + public: + + HKGFileWriterRootWCSim(); ///< Simple constructor + + private: + /** + * Open the output file(s) and setup the output tree(s)/histogram(s)/... + * Called in Initalise(). + * @return True if success. + */ + bool SetupFile() override; + /** + * Fill the output tree(s)/histogram(s) that are event-independent (e.g. geometry). + * Called in Initalise(). + * @return True if success. + */ + bool FillEventIndependent() override; + /** + * Fill the output tree(s)/histogram(s)/... + * Called in Execute(). + * @return True if success. + */ + bool FillThisEvent() override; + /** + * Write the output tree(s)/histogram(s)/... to file. + * Called in Finalise(). + * @return True if success. + */ + bool WriteFile() override; + /** + * Cleanup the memory used by this tool. + * Called in Finalise(). + * @return True if success. + */ + bool Cleanup() override; + }; + + /// Pointer to output file + TFile * m_p_file; + /// Pointer to event tree + TTree * m_p_tree_event; + /// Pointer to geometry tree + TTree * m_p_tree_geom; + } // namespace IO + } // namespace Ghost +} // namespace HK + +#endif diff --git a/HKGFileWriterRootWCSim/README.md b/HKGFileWriterRootWCSim/README.md new file mode 100644 index 0000000..8c6548b --- /dev/null +++ b/HKGFileWriterRootWCSim/README.md @@ -0,0 +1 @@ +# HKGFileWriterRootWCSim