StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IO_GenEvent.cc
1 //--------------------------------------------------------------------------
2 
4 // garren@fnal.gov, July 2006
5 // event input/output in ascii format for machine reading
6 // IO_GenEvent format contains HeavyIon and PdfInfo classes
8 
9 #include "HepMC/IO_GenEvent.h"
10 #include "HepMC/IO_Exception.h"
11 #include "HepMC/GenEvent.h"
12 #include "HepMC/StreamHelpers.h"
13 
14 namespace HepMC {
15 
16  IO_GenEvent::IO_GenEvent( const std::string& filename, std::ios::openmode mode )
17  : m_mode(mode),
18  m_file(filename.c_str(), mode),
19  m_ostr(0),
20  m_istr(0),
21  m_iostr(0),
22  m_have_file(false),
23  m_error_type(IO_Exception::OK),
24  m_error_message()
25  {
26  if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
27  (m_mode&std::ios::app && m_mode&std::ios::in) ) {
28  m_error_type = IO_Exception::InputAndOutput;
29  m_error_message ="IO_GenEvent::IO_GenEvent Error, open of file requested of input AND output type. Not allowed. Closing file.";
30  std::cerr << m_error_message << std::endl;
31  m_file.close();
32  return;
33  }
34  // now we set the streams
35  m_iostr = &m_file;
36  if ( m_mode&std::ios::in ) {
37  m_istr = &m_file;
38  m_ostr = NULL;
39  detail::establish_input_stream_info(m_file);
40  }
41  if ( m_mode&std::ios::out ) {
42  m_ostr = &m_file;
43  m_istr = NULL;
44  detail::establish_output_stream_info(m_file);
45  }
46  m_have_file = true;
47  }
48 
49 
50  IO_GenEvent::IO_GenEvent( std::istream & istr )
51  : m_ostr(0),
52  m_istr(&istr),
53  m_iostr(&istr),
54  m_have_file(false),
55  m_error_type(IO_Exception::OK),
56  m_error_message()
57  {
58  detail::establish_input_stream_info( istr );
59  }
60 
61  IO_GenEvent::IO_GenEvent( std::ostream & ostr )
62  : m_ostr(&ostr),
63  m_istr(0),
64  m_iostr(&ostr),
65  m_have_file(false),
66  m_error_type(IO_Exception::OK),
67  m_error_message()
68  {
69  detail::establish_output_stream_info( ostr );
70  }
71 
72  IO_GenEvent::~IO_GenEvent() {
73  if ( m_ostr != NULL ) {
74  write_HepMC_IO_block_end(*m_ostr);
75  }
76  if(m_have_file) m_file.close();
77  }
78 
79  void IO_GenEvent::use_input_units( Units::MomentumUnit mom,
80  Units::LengthUnit len ) {
81  if( m_istr != NULL ) {
82  set_input_units( *m_istr, mom, len );
83  }
84  }
85 
86  void IO_GenEvent::print( std::ostream& ostr ) const {
87  ostr << "IO_GenEvent: unformated ascii file IO for machine reading.\n";
88  if(m_have_file) ostr << "\tFile openmode: " << m_mode ;
89  ostr << " stream state: " << m_ostr->rdstate()
90  << " bad:" << (m_ostr->rdstate()&std::ios::badbit)
91  << " eof:" << (m_ostr->rdstate()&std::ios::eofbit)
92  << " fail:" << (m_ostr->rdstate()&std::ios::failbit)
93  << " good:" << (m_ostr->rdstate()&std::ios::goodbit) << std::endl;
94  }
95 
96  void IO_GenEvent::precision( int size ) {
97  if( size > 16 ) {
98  std::cerr << "IO_GenEvent::precision Error, "
99  << "precision is greater than 16. "
100  << "Not allowed. Using default precision of 16."
101  << std::endl;
102  size = 16;
103  }
104  if(m_ostr) {
105  m_ostr->precision(size);
106  }
107  }
108 
110  //
111  // reset error type
112  m_error_type = IO_Exception::OK;
113  //
114  // test that evt pointer is not null
115  if ( !evt ) {
116  m_error_type = IO_Exception::NullEvent;
117  m_error_message = "IO_GenEvent::fill_next_event error - passed null event.";
118  std::cerr << m_error_message << std::endl;
119  return false;
120  }
121  // make sure the stream is good, and that it is in input mode
122  if ( !(*m_istr) ) return false;
123  if ( !m_istr ) {
124  m_error_type = IO_Exception::WrongFileType;
125  m_error_message = "HepMC::IO_GenEvent::fill_next_event attempt to read from output file.";
126  std::cerr << m_error_message << std::endl;
127  return false;
128  }
129  // use streaming input
130  try {
131  *m_istr >> *evt;
132  }
133  catch (IO_Exception& e) {
134  m_error_type = IO_Exception::InvalidData;
135  m_error_message = e.what();
136  evt->clear();
137  return false;
138  }
139  if( evt->is_valid() ) return true;
140  return false;
141  }
142 
143  void IO_GenEvent::write_event( const GenEvent* evt ) {
145  //
146  // make sure the state is good, and that it is in output mode
147  if ( !evt ) return;
148  if ( m_ostr == NULL ) {
149  m_error_type = IO_Exception::WrongFileType;
150  m_error_message = "HepMC::IO_GenEvent::write_event attempt to write to input file.";
151  std::cerr << m_error_message << std::endl;
152  return;
153  }
154  //
155  // write event listing key before first event only.
157  // explicit cast is necessary
158  GenEvent e = *evt;
159  *m_ostr << e ;
160  }
161 
162  void IO_GenEvent::write_comment( const std::string comment ) {
163  // make sure the stream is good, and that it is in output mode
164  if ( !(*m_ostr) ) return;
165  if ( m_ostr == NULL ) {
166  m_error_type = IO_Exception::WrongFileType;
167  m_error_message = "HepMC::IO_GenEvent::write_event attempt to write to input file.";
168  std::cerr << m_error_message << std::endl;
169  return;
170  }
171  // write end of event listing key if events have already been written
172  write_HepMC_IO_block_end(*m_ostr);
173  // insert the comment key before the comment
174  *m_ostr << "\n" << "HepMC::IO_GenEvent-COMMENT\n";
175  *m_ostr << comment << std::endl;
176  }
177 
178 } // HepMC
IO exception handling.
Definition: IO_Exception.h:28
void write_event(const GenEvent *evt)
write this event
Definition: IO_GenEvent.cc:143
IO_GenEvent(const std::string &filename="IO_GenEvent.dat", std::ios::openmode mode=std::ios::out)
constructor requiring a file name and std::ios mode
Definition: IO_GenEvent.cc:16
bool is_valid() const
Definition: GenEvent.cc:677
std::istream & set_input_units(std::istream &, Units::MomentumUnit, Units::LengthUnit)
set the units for this input stream
The GenEvent class is the core of HepMC.
Definition: GenEvent.h:155
void print(std::ostream &ostr=std::cout) const
write to ostr
Definition: IO_GenEvent.cc:86
void precision(int)
Definition: IO_GenEvent.cc:96
bool fill_next_event(GenEvent *evt)
get the next event
Definition: IO_GenEvent.cc:109
void clear()
empties the entire event
Definition: GenEvent.cc:365
std::ostream & write_HepMC_IO_block_begin(std::ostream &)
Explicitly write the begin block lines that IO_GenEvent uses.
void write_comment(const std::string comment)
Definition: IO_GenEvent.cc:162
void use_input_units(Units::MomentumUnit, Units::LengthUnit)
Definition: IO_GenEvent.cc:79
std::ostream & write_HepMC_IO_block_end(std::ostream &)
Explicitly write the end block line that IO_GenEvent uses.