HepMC3 event record library
WriterRoot.cc
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6/**
7 * @file WriterRoot.cc
8 * @brief Implementation of \b class WriterRoot
9 *
10 */
11#include <array>
12#include <cstdio> // sprintf
13#include "HepMC3/WriterRoot.h"
14#include "HepMC3/Version.h"
15// ROOT header files
16#include "TFile.h"
17#include "TTree.h"
18
19namespace HepMC3 {
20HEPMC3_DECLARE_WRITER_FILE(WriterRoot)
21
22WriterRoot::WriterRoot(const std::string &filename, std::shared_ptr<GenRunInfo> run):
23 m_events_count(0) {
24 set_run_info(run);
25
26 m_file = TFile::Open(filename.c_str(), "RECREATE");
27 if ( !m_file->IsOpen() ) {
28 HEPMC3_ERROR("WriterRoot: problem opening file: " << filename)
29 return;
30 }
31
32 if ( run_info() ) write_run_info();
33}
34
36 if ( !m_file->IsOpen() ) return;
37
38 if ( !run_info() ) {
41 } else {
42 if ( evt.run_info() && run_info() != evt.run_info() ) {
43 HEPMC3_WARNING("WriterRoot::write_event: GenEvents contain "
44 "different GenRunInfo objects from - only the "
45 "first such object will be serialized.")
46 }
47 }
48
49 GenEventData data;
50 evt.write_data(data);
51
52 std::array<char,16> buf;
53 snprintf(buf.data(), buf.size(), "%15i", ++m_events_count);
54
55 int nbytes = m_file->WriteObject(&data, buf.data());
56
57 if ( nbytes == 0 ) {
58 HEPMC3_ERROR("WriterRoot: error writing event")
59 m_file->Close();
60 }
61}
62
64 if ( !m_file->IsOpen() || !run_info() ) return;
65
66 GenRunInfoData data;
67 run_info()->write_data(data);
68
69 int nbytes = m_file->WriteObject(&data, "GenRunInfoData");
70
71 if ( nbytes == 0 ) {
72 HEPMC3_ERROR("WriterRoot: error writing GenRunInfo")
73 m_file->Close();
74 }
75}
76
78 m_file->Close();
79}
80
82 return !m_file->IsOpen();
83}
84
85} // namespace HepMC3
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition Errors.h:27
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition Errors.h:24
Definition of class WriterRoot.
Stores event-related information.
Definition GenEvent.h:41
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition GenEvent.cc:631
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition GenEvent.h:137
Stores run-related information.
Definition GenRunInfo.h:33
GenEvent I/O serialization for root files.
Definition WriterRoot.h:37
bool failed() override
Get stream error state flag.
Definition WriterRoot.cc:81
int m_events_count
Events count. Needed to generate unique object name.
Definition WriterRoot.h:71
void close() override
Close file stream.
Definition WriterRoot.cc:77
TFile * m_file
File handler.
Definition WriterRoot.h:70
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterRoot.cc:35
void write_run_info()
Write the GenRunInfo object to file.
Definition WriterRoot.cc:63
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Writer.h:42
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Writer.h:45
HepMC3 main namespace.
Stores serializable event information.
Stores serializable run information.