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 WriterRoot.cc
|
---|
8 | * @brief Implementation of \b class WriterRoot
|
---|
9 | *
|
---|
10 | */
|
---|
11 | #include <cstdio> // sprintf
|
---|
12 | #include "HepMC3/WriterRoot.h"
|
---|
13 | #include "HepMC3/Version.h"
|
---|
14 | // ROOT header files
|
---|
15 | #include "TFile.h"
|
---|
16 | #include "TTree.h"
|
---|
17 |
|
---|
18 | namespace HepMC3 {
|
---|
19 | HEPMC3_DECLARE_WRITER_FILE(WriterRoot)
|
---|
20 |
|
---|
21 | WriterRoot::WriterRoot(const std::string &filename, std::shared_ptr<GenRunInfo> run):
|
---|
22 | m_events_count(0) {
|
---|
23 | set_run_info(run);
|
---|
24 |
|
---|
25 | m_file = TFile::Open(filename.c_str(), "RECREATE");
|
---|
26 | if ( !m_file->IsOpen() ) {
|
---|
27 | HEPMC3_ERROR("WriterRoot: problem opening file: " << filename)
|
---|
28 | return;
|
---|
29 | }
|
---|
30 |
|
---|
31 | if ( run_info() ) write_run_info();
|
---|
32 | }
|
---|
33 |
|
---|
34 | void WriterRoot::write_event(const GenEvent &evt) {
|
---|
35 | if ( !m_file->IsOpen() ) return;
|
---|
36 |
|
---|
37 | if ( !run_info() ) {
|
---|
38 | set_run_info(evt.run_info());
|
---|
39 | write_run_info();
|
---|
40 | } else {
|
---|
41 | if ( evt.run_info() && run_info() != evt.run_info() )
|
---|
42 | HEPMC3_WARNING("WriterRoot::write_event: GenEvents contain "
|
---|
43 | "different GenRunInfo objects from - only the "
|
---|
44 | "first such object will be serialized.")
|
---|
45 | }
|
---|
46 |
|
---|
47 | GenEventData data;
|
---|
48 | evt.write_data(data);
|
---|
49 |
|
---|
50 | char buf[16] = "";
|
---|
51 | sprintf(buf, "%15i", ++m_events_count);
|
---|
52 |
|
---|
53 | int nbytes = m_file->WriteObject(&data, buf);
|
---|
54 |
|
---|
55 | if ( nbytes == 0 ) {
|
---|
56 | HEPMC3_ERROR("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("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 | if ( !m_file->IsOpen() ) return true;
|
---|
81 |
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 |
|
---|
85 | } // namespace HepMC3
|
---|