Fork me on GitHub

source: svn/trunk/Utilities/HepMC/interface/IO_GenEvent.h@ 349

Last change on this file since 349 was 349, checked in by severine ovyn, 15 years ago

first test

File size: 7.4 KB
Line 
1//--------------------------------------------------------------------------
2#ifndef HEPMC_IO_GENEVENT_H
3#define HEPMC_IO_GENEVENT_H
4
5//////////////////////////////////////////////////////////////////////////
6// garren@fnal.gov, July 2007
7// with input from Gavin Salam, salam@lpthe.jussieu.fr
8//
9// event input/output in ascii format for machine reading
10// This class persists all information found in a GenEvent
11//////////////////////////////////////////////////////////////////////////
12
13#include <fstream>
14#include <string>
15#include <map>
16#include <vector>
17#include "IO_BaseClass.h"
18#include "TempParticleMap.h"
19#include "CommonIO.h"
20#include "Units.h"
21
22namespace HepMC {
23
24 class GenEvent;
25 class GenVertex;
26 class GenParticle;
27 class ParticleData;
28 class HeavyIon;
29 class PdfInfo;
30
31 //! IO_GenEvent also deals with HeavyIon and PdfInfo
32
33 ///
34 /// \class IO_GenEvent
35 /// event input/output in ascii format for machine reading
36 /// extended format contains HeavyIon and PdfInfo classes
37 ///
38 /// Strategy for reading or writing events using iostreams
39 /// When instantiating with a file name, the mode of file to be created
40 /// must be specified. Options are:
41 /// std::ios::in open file for input
42 /// std::ios::out open file for output
43 /// std::ios::trunc erase old file when opening (i.e. ios::out|ios::trunc
44 /// removes oldfile, and creates a new one for output )
45 /// std::ios::app append output to end of file
46 /// for the purposes of this class, simultaneous input and output mode
47 /// ( std::ios::in | std::ios::out ) is not allowed.
48 ///
49 /// Event listings are preceded by the key:
50 /// "HepMC::IO_GenEvent-START_EVENT_LISTING\n"
51 /// and terminated by the key:
52 /// "HepMC::IO_GenEvent-END_EVENT_LISTING\n"
53 /// GenParticle Data tables are preceded by the key:
54 /// "HepMC::IO_GenEvent-START_PARTICLE_DATA\n"
55 /// and terminated by the key:
56 /// "HepMC::IO_GenEvent-END_PARTICLE_DATA\n"
57 /// Comments are allowed. They need not be preceded by anything, though if
58 /// a comment is written using write_comment( const string ) then it will be
59 /// preceded by "HepMC::IO_GenEvent-COMMENT\n"
60 /// Each event, vertex, particle, particle data, heavy ion, or pdf info line
61 /// is preceded by "E ","V ","P ","D ","H ","F " respectively.
62 /// Comments may appear anywhere in the file -- so long as they do not contain
63 /// any of the start/stop keys.
64 ///
65 class IO_GenEvent : public IO_BaseClass {
66 public:
67 /// constructor requiring a file name and std::ios mode
68 IO_GenEvent( const std::string& filename="IO_GenEvent.dat",
69 std::ios::openmode mode=std::ios::out );
70 /// constructor requiring an input stream
71 IO_GenEvent( std::istream & );
72 /// constructor requiring an output stream
73 IO_GenEvent( std::ostream & );
74 virtual ~IO_GenEvent();
75
76 /// write this event
77 void write_event( const GenEvent* evt );
78 /// get the next event
79 bool fill_next_event( GenEvent* evt );
80 void write_particle_data_table(const ParticleDataTable*);
81 bool fill_particle_data_table( ParticleDataTable* );
82 /// insert a comment directly into the output file --- normally you
83 /// only want to do this at the beginning or end of the file. All
84 /// comments are preceded with "HepMC::IO_GenEvent-COMMENT\n"
85 void write_comment( const std::string comment );
86
87 int rdstate() const; //!< check the state of the IO stream
88 void clear(); //!< clear the IO stream
89
90 /// write to ostr
91 void print( std::ostream& ostr = std::cout ) const;
92
93 /// needed when reading a file without units if those units are
94 /// different than the declared default units
95 /// (e.g., the default units are MeV, but the file was written with GeV)
96 /// This method is not necessary if the units are written in the file
97 void use_input_units( Units::MomentumUnit, Units::LengthUnit );
98
99 protected: // for internal use only
100 /// write vertex information
101 void write_vertex( GenVertex* );
102 /// write beam particle information
103 void write_beam_particles( std::pair<HepMC::GenParticle *,HepMC::GenParticle *> );
104 /// write heavy ion information
105 void write_heavy_ion( HeavyIon const * );
106 /// write PDF information
107 void write_pdf_info( PdfInfo const * );
108 /// write units
109 void write_unit_info( const GenEvent* evt );
110 /// write particle information
111 void write_particle( GenParticle* p );
112 /// write particle data information
113 void write_particle_data( const ParticleData* d );
114 /// read vertex information
115 GenVertex* read_vertex( TempParticleMap& particle_to_end_vertex );
116 /// read GenParticle information
117 GenParticle* read_particle( TempParticleMap& particle_to_end_vertex );
118 /// read particle data table information
119 ParticleData* read_particle_data( ParticleDataTable* );
120 /// read heavy ion information
121 HeavyIon* read_heavy_ion( );
122 /// read units
123 void read_unit_info( GenEvent* evt );
124 /// write end tag
125 bool write_end_listing();
126
127 void output( const double& ); //!< write double
128 void output( const float& ); //!< write float
129 void output( const int& ); //!< write int
130 void output( const long& ); //!< write long
131 void output( const char& ); //!< write a single character
132 private: // use of copy constructor is not allowed
133 IO_GenEvent( const IO_GenEvent& ) : IO_BaseClass() {}
134 private: // data members
135 std::ios::openmode m_mode;
136 std::fstream m_file;
137 std::ostream * m_ostr;
138 std::istream * m_istr;
139 std::ios * m_iostr;
140 bool m_finished_first_event_io;
141 bool m_have_file;
142 CommonIO m_common_io;
143
144 };
145
146 //////////////
147 // Inlines //
148 //////////////
149
150 inline void IO_GenEvent::output( const double& d ) {
151 if( m_ostr ) {
152 if ( d == 0. ) {
153 *m_ostr << ' ' << (int)0;
154 } else {
155 *m_ostr << ' ' << d;
156 }
157 }
158 }
159 inline void IO_GenEvent::output( const float& d ) {
160 if( m_ostr ) {
161 if ( d == 0. ) {
162 *m_ostr << ' ' << (int)0;
163 } else {
164 *m_ostr << ' ' << d;
165 }
166 }
167 }
168 inline void IO_GenEvent::output( const int& i ) {
169 if( m_ostr ) {
170 if ( i == 0. ) {
171 *m_ostr << ' ' << (int)0;
172 } else {
173 *m_ostr << ' ' << i;
174 }
175 }
176 }
177 inline void IO_GenEvent::output( const long& i ) {
178 if( m_ostr ) {
179 if ( i == 0. ) {
180 *m_ostr << ' ' << (int)0;
181 } else {
182 *m_ostr << ' ' << i;
183 }
184 }
185 }
186 inline void IO_GenEvent::output( const char& c ) {
187 if( m_ostr ) {
188 if ( c ) {
189 *m_ostr << c;
190 } else {
191 *m_ostr << ' ' ;
192 }
193 }
194 }
195 inline int IO_GenEvent::rdstate() const {
196 int state;
197 if( m_istr ) {
198 state = (int)m_istr->rdstate();
199 } else {
200 state = (int)m_ostr->rdstate();
201 }
202 return state;
203 }
204 inline void IO_GenEvent::clear() {
205 if( m_istr ) {
206 m_istr->clear();
207 } else {
208 m_ostr->clear();
209 }
210 }
211 // these are required by IO_BaseClass, but not used here
212 inline void IO_GenEvent::write_particle_data_table(const ParticleDataTable*) {;}
213 inline bool IO_GenEvent::fill_particle_data_table( ParticleDataTable* )
214 { return false;}
215
216} // HepMC
217
218#endif // HEPMC_IO_GENEVENT_H
219//--------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.