Skip to content

Commit

Permalink
Added a new output module to write ROOT histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
forthommel committed Jul 25, 2019
1 parent 0f79fb7 commit e3b10e3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CepGen/CMakeLists.txt
Expand Up @@ -99,7 +99,7 @@ endif()
if(ROOT_FOUND)
message(STATUS "ROOT found in ${ROOT_LIBRARY_DIR}")
list(APPEND addons_libraries ${ROOT_LIBRARIES})
list(APPEND io_sources "IO/ROOTTreeHandler.cpp")
list(APPEND io_sources "IO/ROOTHandler.cpp" "IO/ROOTTreeHandler.cpp")
include_directories(${ROOT_INCLUDE_DIRS})
link_directories(${ROOT_LIBRARY_DIR})
if(DELPHES)
Expand Down
82 changes: 82 additions & 0 deletions CepGen/IO/ROOTHandler.cpp
@@ -0,0 +1,82 @@
#include "CepGen/IO/ExportHandler.h"

#include "CepGen/Core/Exception.h"
#include "CepGen/Core/ParametersList.h"
#include "CepGen/Core/utils.h"

#include "CepGen/Event/Event.h"
#include "CepGen/Event/EventBrowser.h"
#include "CepGen/Parameters.h"

#include "CepGen/Version.h"

#include "TFile.h"
#include "TH1.h"

namespace cepgen
{
namespace io
{
/**
* \brief Handler for the generic ROOT file output
* \author Laurent Forthomme <laurent.forthomme@cern.ch>
* \date Jul 2019
*/
class ROOTHandler : public GenericExportHandler
{
public:
explicit ROOTHandler( const ParametersList& );
~ROOTHandler();

void initialise( const Parameters& ) override {}
void setCrossSection( double xsec, double ) override { xsec_ = xsec; }
void operator<<( const Event& ) override;

private:
std::unique_ptr<TFile> file_;
std::vector<std::pair<std::string,TH1*> > hists_;
const ParametersList variables_;

double xsec_;
const utils::EventBrowser browser_;
};

ROOTHandler::ROOTHandler( const ParametersList& params ) :
GenericExportHandler( "text" ),
file_ ( TFile::Open( params.get<std::string>( "filename", "output.root" ).c_str(), "recreate" ) ),
variables_( params.get<ParametersList>( "variables" ) ),
xsec_( 1. )
{
//--- extract list of variables to be plotted in histograms
for ( const auto& var : variables_.keys() ) {
const auto& hvar = variables_.get<ParametersList>( var );
const int nbins = hvar.get<int>( "nbins", 10 );
const double min = hvar.get<double>( "low", 0. ), max = hvar.get<double>( "high", 1. );
const auto title = Form( "%s;%s;d#sigma/d(%s) (pb/bin)", var.c_str(), var.c_str(), var.c_str() );
hists_.emplace_back( std::make_pair( var, new TH1D( var.c_str(), title.c_str(), nbins, min, max ) ) );
CG_INFO( "ROOTHandler" )
<< "Booking a histogram with " << nbins << " bin" << utils::s( nbins )
<< " between " << min << " and " << max << " for \"" << var << "\".";
}
}

ROOTHandler::~ROOTHandler()
{
//--- finalisation of the output file
for ( const auto& hist : hists_ )
hist.second->Write( hist.first.c_str() );
// ROOT and its sumptuous memory management disallows the "delete" here
file_->Close();
}

void
ROOTHandler::operator<<( const Event& ev )
{
//--- increment the corresponding histograms
for ( const auto& h_var : hists_ )
h_var.second->Fill( browser_.get( ev, h_var.first ), xsec_ );
}
}
}

REGISTER_IO_MODULE( root, ROOTHandler )

0 comments on commit e3b10e3

Please sign in to comment.