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-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 "HepMC3/WriterRoot.h"
12 #include <cstdio> // sprintf
13 // ROOT header files
14 #include "TFile.h"
15 #include "TTree.h"
16 
17 namespace HepMC3 {
18 
19 WriterRoot::WriterRoot(const std::string &filename, shared_ptr<GenRunInfo> run):
20  m_events_count(0) {
21  set_run_info(run);
22 
23  m_file = TFile::Open(filename.c_str(),"RECREATE");
24  if ( !m_file->IsOpen() ) {
25  ERROR( "WriterRoot: problem opening file: " << filename )
26  return;
27  }
28 
29  if ( run_info() ) write_run_info();
30 }
31 
33  if ( !m_file->IsOpen() ) return;
34 
35  if ( !run_info() ) {
36  set_run_info(evt.run_info());
38  } else {
39  if ( evt.run_info() && run_info() != evt.run_info() )
40  WARNING( "WriterAscii::write_event: GenEvents contain "
41  "different GenRunInfo objects from - only the "
42  "first such object will be serialized." )
43  }
44 
45  GenEventData data;
46  evt.write_data(data);
47 
48  char buf[16] = "";
49  sprintf(buf,"%15i",++m_events_count);
50 
51  int nbytes = m_file->WriteObject(&data, buf);
52 
53  if( nbytes == 0 ) {
54  ERROR( "WriterRoot: error writing event")
55  m_file->Close();
56  }
57 }
58 
60  if ( !m_file->IsOpen() || !run_info() ) return;
61 
62  GenRunInfoData data;
63  run_info()->write_data(data);
64 
65  int nbytes = m_file->WriteObject(&data,"GenRunInfoData");
66 
67  if( nbytes == 0 ) {
68  ERROR( "WriterRoot: error writing GenRunInfo")
69  m_file->Close();
70  }
71 }
72 
74  m_file->Close();
75 }
76 
78  if ( !m_file->IsOpen() ) return true;
79 
80  return false;
81 }
82 
83 } // namespace HepMC3
ERROR
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:23
HepMC3::Writer::run_info
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Writer.h:44
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:42
HepMC3::WriterRoot::m_events_count
int m_events_count
Events count. Needed to generate unique object name.
Definition: WriterRoot.h:70
HepMC3::WriterRoot::failed
bool failed()
Get stream error state flag.
Definition: WriterRoot.cc:77
HepMC3
HepMC3 main namespace.
Definition: ReaderGZ.h:28
HepMC3::GenEventData
Stores serializable event information.
Definition: GenEventData.h:26
HepMC3::Writer::set_run_info
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Writer.h:39
HepMC3::WriterRoot::write_event
void write_event(const GenEvent &evt)
Write event to file.
Definition: WriterRoot.cc:32
HepMC3::GenEvent::write_data
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition: GenEvent.cc:647
WriterRoot.h
Definition of class WriterRoot.
HepMC3::WriterRoot::write_run_info
void write_run_info()
Write the GenRunInfo object to file.
Definition: WriterRoot.cc:59
HepMC3::WriterRoot::close
void close()
Close file stream.
Definition: WriterRoot.cc:73
HepMC3::GenEvent::run_info
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:125
HepMC3::GenRunInfoData
Stores serializable run information.
Definition: GenRunInfoData.h:23
HepMC3::WriterRoot::m_file
TFile * m_file
File handler.
Definition: WriterRoot.h:69
HepMC3::WriterRoot::WriterRoot
WriterRoot(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Default constructor.
Definition: WriterRoot.cc:19
WARNING
#define WARNING(MESSAGE)
Macro for printing warning messages.
Definition: Errors.h:26