HepMC3 event record library: WriterRoot.cc Source File

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_LEVEL(100,"WriterRoot: problem opening file: " << filename)
29 return;
30 }
31
32 if ( run_info() ) write_run_info();
33}
34
35 void WriterRoot::write_event(const GenEvent &evt) {
36 if ( !m_file->IsOpen() ) return;
37
38 if ( !run_info() ) {
39 set_run_info(evt.run_info());
40 write_run_info();
41 } else {
42 if ( evt.run_info() && run_info() != evt.run_info() ) {
43 HEPMC3_WARNING_LEVEL(100,"WriterRoot::write_event: GenEvents contain different GenRunInfo objects from - only the first such object will be serialized.")
44 }
45 }
46
47 GenEventData data;
48 evt.write_data(data);
49
50 std::array<char,16> buf{};
51 snprintf(buf.data(), buf.size(), "%15i", ++m_events_count);
52
53 int nbytes = m_file->WriteObject(&data, buf.data());
54
55 if ( nbytes == 0 ) {
56 HEPMC3_ERROR_LEVEL(100,"WriterRoot: error writing event")
57 m_file->Close();
58 }
59}
60
61 void WriterRoot::write_run_info() {
62 if ( !m_file->IsOpen() || !run_info() ) return;
63
64 GenRunInfoData data;
65 run_info()->write_data(data);
66
67 int nbytes = m_file->WriteObject(&data, "GenRunInfoData");
68
69 if ( nbytes == 0 ) {
70 HEPMC3_ERROR_LEVEL(100,"WriterRoot: error writing GenRunInfo")
71 m_file->Close();
72 }
73}
74
75 void WriterRoot::close() {
76 m_file->Close();
77}
78
79 bool WriterRoot::failed() {
80 return !m_file->IsOpen();
81}
82
83} // namespace HepMC3
HEPMC3_WARNING_LEVEL
#define HEPMC3_WARNING_LEVEL(LEVEL, MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
Definition Errors.h:34
HEPMC3_ERROR_LEVEL
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Definition Errors.h:27
Version.h
Declaration of the Verrion functions and some macros.
WriterRoot.h
Definition of class WriterRoot.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::GenEvent::write_data
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition GenEvent.cc:625
HepMC3::GenEvent::run_info
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition GenEvent.h:144
HepMC3::GenRunInfo
Stores run-related information.
Definition GenRunInfo.h:33
HepMC3::WriterRoot
GenEvent I/O serialization for root files.
Definition WriterRoot.h:37
HepMC3::WriterRoot::failed
bool failed() override
Get stream error state flag.
Definition WriterRoot.cc:79
HepMC3::WriterRoot::WriterRoot
WriterRoot(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Default constructor.
Definition WriterRoot.cc:22
HepMC3::WriterRoot::m_events_count
int m_events_count
Events count. Needed to generate unique object name.
Definition WriterRoot.h:71
HepMC3::WriterRoot::close
void close() override
Close file stream.
Definition WriterRoot.cc:75
HepMC3::WriterRoot::m_file
TFile * m_file
File handler.
Definition WriterRoot.h:70
HepMC3::WriterRoot::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterRoot.cc:35
HepMC3::WriterRoot::write_run_info
void write_run_info()
Write the GenRunInfo object to file.
Definition WriterRoot.cc:61
HepMC3::Writer::set_run_info
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Writer.h:42
HepMC3::Writer::run_info
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Writer.h:45
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::GenEventData
Stores serializable event information.
Definition GenEventData.h:26
HepMC3::GenRunInfoData
Stores serializable run information.
Definition GenRunInfoData.h:23