Skip to content

Commit

Permalink
Add a file writer base class & start setting up an implementation to …
Browse files Browse the repository at this point in the history
…write WCSim files. It compiles here, but not in hk-ToolApp - need to chat with people about how to setup ABCs
  • Loading branch information
tdealtry committed May 3, 2024
1 parent d0992ae commit 8064986
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
76 changes: 76 additions & 0 deletions HKGFileWriterBase/HKGFileWriterBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "HKGFileWriterBase.h"

#include <sstream>

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;
}
79 changes: 79 additions & 0 deletions HKGFileWriterBase/HKGFileWriterBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef HKGFileWriterBase_H
#define HKGFileWriterBase_H

#include <iostream>
#include <string>

#include <DataModel.h>
#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
1 change: 1 addition & 0 deletions HKGFileWriterBase/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# HKGFileWriterBase
51 changes: 51 additions & 0 deletions HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
74 changes: 74 additions & 0 deletions HKGFileWriterRootWCSim/HKGFileWriterRootWCSim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef HKGFileWriterRootWCSim_H
#define HKGFileWriterRootWCSim_H

#include <iostream>
#include <string>

#include "TTree.h"
#include "TFile.h"

#include "../HKGFileWriterBase/HKGFileWriterBase.h"

#include <DataModel.h>
#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
1 change: 1 addition & 0 deletions HKGFileWriterRootWCSim/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# HKGFileWriterRootWCSim

0 comments on commit 8064986

Please sign in to comment.