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