StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IO_AsciiParticles.cc
1 //--------------------------------------------------------------------------
2 
4 // Mikhail.Kirsanov@Cern.CH, 2006
5 // event input/output in ascii format for eye and machine reading
6 //
7 // for arguments mostly similar to IO_Ascii. Special value of
8 // argument filename in constructor: if it is "cout" the output is to std::cout
10 
11 #include "HepMC/IO_AsciiParticles.h"
12 #include "HepMC/GenEvent.h"
13 #include "HepMC/Version.h"
14 
15 namespace HepMC {
16 
17  IO_AsciiParticles::IO_AsciiParticles( const char* filename, std::ios::openmode mode )
18  : m_precision(2),
19  m_mode(mode), m_finished_first_event_io(0)
20  {
21  if(std::string(filename) == std::string("cout")) {
22  m_outstream = &(std::cout);
23  m_file = 0;
24  } else {
25  m_file = new std::fstream(filename, mode);
26  m_outstream = m_file;
27  if ( (m_mode&std::ios::out && m_mode&std::ios::in) ||
28  (m_mode&std::ios::app && m_mode&std::ios::in) ) {
29  std::cerr << "IO_AsciiParticles::IO_AsciiParticles Error, open of file requested "
30  << "of input AND output type. Not allowed. Closing file."
31  << std::endl;
32  m_file->close();
33  delete m_file;
34  return;
35  }
36  }
37  // precision 16 (# digits following decimal point) is the minimum that
38  // will capture the full information stored in a double
39  // with precision <= 2 the width of output will be < 80 characters
40  m_outstream->precision(m_precision);
41  // we use decimal to store integers, because it is smaller than hex!
42  m_outstream->setf(std::ios::dec,std::ios::basefield);
43  m_outstream->setf(std::ios::scientific,std::ios::floatfield);
44  }
45 
46  IO_AsciiParticles::~IO_AsciiParticles() {
47  if(m_file) {
48  m_file->close();
49  delete m_file;
50  }
51  }
52 
53  void IO_AsciiParticles::print( std::ostream& ostr ) const {
54  ostr << "IO_AsciiParticles: formated ascii file IO for eye and machine reading.\n"
55  << "\tFile openmode: " << m_mode
56  << " file state: " << m_outstream->rdstate()
57  << " bad:" << (m_outstream->rdstate()&std::ios::badbit)
58  << " eof:" << (m_outstream->rdstate()&std::ios::eofbit)
59  << " fail:" << (m_outstream->rdstate()&std::ios::failbit)
60  << " good:" << (m_outstream->rdstate()&std::ios::goodbit) << std::endl;
61  }
62 
64  // Writes evt to m_outstream. It does NOT delete the event after writing.
65  //
66  // check the state of m_outstream is good, and that it is in output mode
67  if ( !evt || !m_outstream ) return;
68  if ( !(m_mode&std::ios::out) ) {
69  std::cerr << "HepMC::IO_AsciiParticles::write_event "
70  << " attempt to write to input file." << std::endl;
71  return;
72  }
73  //
74  // write event listing key before first event only.
75  if ( !m_finished_first_event_io ) {
76  m_finished_first_event_io = 1;
77  *m_outstream << "0 Run HepMC::IO_AsciiParticles eye-readable events output"
78  << std::endl;
79  *m_outstream << "# HepMC::Version " << versionName() << std::endl;
80  *m_outstream <<
81  " # stat pdg moth1 px py pz energy mass eta"
82  << std::endl;
83  }
84  //
85  // output the event data
86  std::vector<long int> random_states = evt->random_states();
87  *m_outstream << evt->event_number() << " Event" << std::endl;
88 #if 0
89  *m_outstream << " " << evt->event_scale();
90  output( evt->alphaQCD() );
91  output( evt->alphaQED() );
92  output( evt->signal_process_id() );
93  output( ( evt->signal_process_vertex() ?
94  evt->signal_process_vertex()->barcode() : 0 ) );
95  output( evt->vertices_size() ); // total number of vertices.
96  output( (int)random_states.size() );
97  for ( std::vector<long int>::iterator rs = random_states.begin();
98  rs != random_states.end(); ++rs ) {
99  output( *rs );
100  }
101  output( (int)evt->weights().size() );
102  for ( WeightContainer::const_iterator w = evt->weights().begin();
103  w != evt->weights().end(); ++w ) {
104  output( *w );
105  }
106  output('\n');
107 #endif
108  //
109  int nparticles=0, imoth=0, ip=0, istati;
110  double xmassi, etai;
111  *m_outstream << evt->particles_size() << " particles" << std::endl;
112  GenVertex* orig;
114  part != evt->particles_end(); ++part ) {
115  //if( (*part)->status() != 1 ) continue;
116  nparticles++;
117  ip++;
118  istati = (*part)->status();
119  if( (*part)->end_vertex() && istati == 1) {
120  std::cout << "final particle with end vertex!" << std::endl;
121  istati = -100;
122  }
123  imoth=0;
124  orig = (*part)->production_vertex();
125  if(orig) {
126  imoth = 0;
127  bool ifound=false;
129  evt->particles_begin();
130  part1 != part; part1++ ) {
131  imoth++;
132  if( (*part1)->end_vertex() == orig ) { ifound = true; break; }
133  }
134  if(!ifound) imoth = 0;
135  }
136 
137  m_outstream->width(4);
138  *m_outstream << ip << " ";
139 
140  m_outstream->width(3);
141  *m_outstream << istati << " ";
142 
143  m_outstream->width(5);
144  *m_outstream << (*part)->pdg_id() << " ";
145 
146  m_outstream->width(3);
147  *m_outstream << imoth << " ";
148 
149  if((*part)->momentum().px() >= 0.) *m_outstream << " ";
150  *m_outstream << (*part)->momentum().px() << " ";
151  if((*part)->momentum().py() >= 0.) *m_outstream << " ";
152  *m_outstream << (*part)->momentum().py() << " ";
153  if((*part)->momentum().pz() >= 0.) *m_outstream << " ";
154  *m_outstream << (*part)->momentum().pz() << " "
155  << (*part)->momentum().e() << " ";
156 
157  xmassi = (*part)->generatedMass();
158  if(fabs(xmassi) < 0.0001) xmassi =0.;
159  m_outstream->setf(std::ios::fixed);
160  m_outstream->precision(3);
161  m_outstream->width(8);
162  *m_outstream << xmassi << " ";
163  m_outstream->setf(std::ios::scientific,std::ios::floatfield);
164  m_outstream->precision(m_precision);
165 
166  m_outstream->setf(std::ios::fixed);
167  m_outstream->precision(3);
168  m_outstream->width(6);
169  etai = (*part)->momentum().eta();
170  if(etai > 999.)etai = 999.;
171  if(etai < -999.)etai = -999.;
172  *m_outstream << etai << std::endl;
173  m_outstream->setf(std::ios::scientific,std::ios::floatfield);
174  m_outstream->precision(m_precision);
175 
176  }
177  }
178 
180  //
181  //
182  // test that evt pointer is not null
183  if ( !evt ) {
184  std::cerr
185  << "IO_AsciiParticles::fill_next_event error - passed null event."
186  << std::endl;
187  return false;
188  }
189  // check the state of m_outstream is good, and that it is in input mode
190  if ( !m_file )
191  std::cerr << "HepMC::IO_AsciiParticles::fill_next_event "
192  << " no file for input" << std::endl;
193  if ( !(m_mode&std::ios::in) ) {
194  std::cerr << "HepMC::IO_AsciiParticles::fill_next_event "
195  << " attempt to read from output file" << std::endl;
196  return false;
197  }
198  std::cerr << "IO_AsciiParticles input is not yet implemented" << std::endl;
199  return false;
200  }
201 
202  void IO_AsciiParticles::write_comment( const std::string comment ) {
203  // check the state of *m_outstream is good, and that it is in output mode
204  if ( !m_outstream ) return;
205  if ( !(m_mode&std::ios::out) ) {
206  std::cerr << "HepMC::IO_AsciiParticles::write_particle_data_table "
207  << " attempt to write to input file." << std::endl;
208  return;
209  }
210  // write end of event listing key if events have already been written
212  // insert the comment key before the comment
213  *m_outstream << "\n" << "HepMC::IO_AsciiParticles-COMMENT\n";
214  *m_outstream << comment << std::endl;
215  }
216 
218  return false;
219  }
220 
221 } // HepMC
222 
IO_AsciiParticles(const char *filename="IO_AsciiParticles.dat", std::ios::openmode mode=std::ios::out)
constructor requiring a file name and std::ios mode
int barcode() const
unique identifier
Definition: GenVertex.h:416
bool fill_next_event(GenEvent *evt)
get the next event
const particle iterator
Definition: GenEvent.h:464
int particles_size() const
how many particle barcodes exist?
Definition: GenEvent.h:830
bool write_end_listing()
write end tag
particle_const_iterator particles_begin() const
begin particle iteration
Definition: GenEvent.h:507
GenVertex contains information about decay vertices.
Definition: GenVertex.h:52
std::vector< double >::const_iterator const_iterator
const iterator for the weight container
double alphaQED() const
Definition: GenEvent.h:692
The GenEvent class is the core of HepMC.
Definition: GenEvent.h:155
GenVertex * signal_process_vertex() const
pointer to the vertex containing the signal process
Definition: GenEvent.h:694
void write_event(const GenEvent *evt)
write this event
iterator begin()
begining of the weight container
void write_comment(const std::string comment)
std::string versionName()
return HepMC version
Definition: Version.h:22
WeightContainer & weights()
direct access to WeightContainer
Definition: GenEvent.h:699
int vertices_size() const
how many vertex barcodes exist?
Definition: GenEvent.h:836
particle_const_iterator particles_end() const
end particle iteration
Definition: GenEvent.h:511
iterator end()
end of the weight container
int event_number() const
event number
Definition: GenEvent.h:682
int signal_process_id() const
unique signal process id
Definition: GenEvent.h:679
void print(std::ostream &ostr=std::cout) const
write to ostr
const std::vector< long > & random_states() const
vector of integers containing information about the random state
Definition: GenEvent.h:727
double event_scale() const
energy scale, see hep-ph/0109068
Definition: GenEvent.h:688
size_type size() const
size of weight container
double alphaQCD() const
QCD coupling, see hep-ph/0109068.
Definition: GenEvent.h:690