generated from hyperk/hk-LibToolPackTemplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
7 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# HKGFileWriterBase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# HKGFileWriterRootWCSim |