| 1 |
// -*- C++ -*- |
| 2 |
// |
| 3 |
// This file is part of HepMC |
| 4 |
// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details) |
| 5 |
// |
| 6 |
/** |
| 7 |
* @file WriterRootTree.cc |
| 8 |
* @brief Implementation of \b class WriterRootTree |
| 9 |
* |
| 10 |
*/ |
| 11 |
#include "HepMC3/WriterRootTree.h" |
| 12 |
#include <cstdio> // sprintf |
| 13 |
// ROOT header files |
| 14 |
#include "TFile.h" |
| 15 |
#include "TTree.h" |
| 16 |
|
| 17 |
namespace HepMC3 |
| 18 |
{ |
| 19 |
|
| 20 |
WriterRootTree::WriterRootTree(const std::string &filename, shared_ptr<GenRunInfo> run): |
| 21 |
m_tree(0), |
| 22 |
m_events_count(0), |
| 23 |
m_tree_name("hepmc3_tree"), |
| 24 |
m_branch_name("hepmc3_event") |
| 25 |
{ |
| 26 |
m_file = TFile::Open(filename.c_str(),"RECREATE"); |
| 27 |
if (!init(run)) return; |
| 28 |
} |
| 29 |
|
| 30 |
WriterRootTree::WriterRootTree(const std::string &filename,const std::string &treename,const std::string &branchname, shared_ptr<GenRunInfo> run): |
| 31 |
m_tree(0), |
| 32 |
m_events_count(0), |
| 33 |
m_tree_name(treename.c_str()), |
| 34 |
m_branch_name(branchname.c_str()) |
| 35 |
{ |
| 36 |
m_file = TFile::Open(filename.c_str(),"RECREATE"); |
| 37 |
if (!init(run)) return; |
| 38 |
} |
| 39 |
|
| 40 |
bool WriterRootTree::init(shared_ptr<GenRunInfo> run ) |
| 41 |
{ |
| 42 |
if ( !m_file->IsOpen() ) |
| 43 |
{ |
| 44 |
ERROR( "WriterRootTree: problem opening file: " <<m_file->GetName() ) |
| 45 |
return false; |
| 46 |
} |
| 47 |
m_event_data= new GenEventData(); |
| 48 |
m_run_info_data= new GenRunInfoData(); |
| 49 |
set_run_info(run); |
| 50 |
if ( run_info() ) run_info()->write_data(*m_run_info_data); |
| 51 |
m_tree= new TTree(m_tree_name.c_str(),"hepmc3_tree"); |
| 52 |
m_tree->Branch(m_branch_name.c_str(), m_event_data); |
| 53 |
m_tree->Branch("GenRunInfo", m_run_info_data); |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
void WriterRootTree::write_event(const GenEvent &evt) |
| 58 |
{ |
| 59 |
if ( !m_file->IsOpen() ) return; |
| 60 |
bool refill=false; |
| 61 |
if ( evt.run_info()&&(!run_info() || (run_info() != evt.run_info()))) { set_run_info(evt.run_info()); refill=true;} |
| 62 |
if (refill) |
| 63 |
{ |
| 64 |
m_run_info_data->weight_names.clear(); |
| 65 |
m_run_info_data->tool_name.clear(); |
| 66 |
m_run_info_data->tool_version.clear(); |
| 67 |
m_run_info_data->tool_description.clear(); |
| 68 |
m_run_info_data->attribute_name.clear(); |
| 69 |
m_run_info_data->attribute_string.clear(); |
| 70 |
run_info()->write_data(*m_run_info_data); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
m_event_data->particles.clear(); |
| 76 |
m_event_data->vertices.clear(); |
| 77 |
m_event_data->links1.clear(); |
| 78 |
m_event_data->links2.clear(); |
| 79 |
m_event_data->attribute_id.clear(); |
| 80 |
m_event_data->attribute_name.clear(); |
| 81 |
m_event_data->attribute_string.clear(); |
| 82 |
|
| 83 |
evt.write_data(*m_event_data); |
| 84 |
m_tree->Fill(); |
| 85 |
++m_events_count; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
void WriterRootTree::write_run_info() {} |
| 90 |
|
| 91 |
void WriterRootTree::close() |
| 92 |
{ |
| 93 |
|
| 94 |
m_file->WriteTObject(m_tree); |
| 95 |
m_file->Close(); |
| 96 |
delete m_event_data; |
| 97 |
delete m_run_info_data; |
| 98 |
} |
| 99 |
|
| 100 |
bool WriterRootTree::failed() |
| 101 |
{ |
| 102 |
if ( !m_file->IsOpen() ) return true; |
| 103 |
|
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
} // namespace HepMC3 |