1 | ////////////////////////////////////////////////////////////////////////// |
---|
2 | // example_PythiaStreamIO.cc |
---|
3 | // |
---|
4 | // garren@fnal.gov, May 2009 |
---|
5 | // |
---|
6 | ////////////////////////////////////////////////////////////////////////// |
---|
7 | /// example of generating events with Pythia using HepMC/PythiaWrapper.h |
---|
8 | /// Events are read into the HepMC event record from the FORTRAN HEPEVT |
---|
9 | /// common block using the IO_HEPEVT strategy |
---|
10 | /// |
---|
11 | /// To Compile: go to the HepMC example directory and type: |
---|
12 | /// make example_PythiaStreamIO.exe |
---|
13 | /// |
---|
14 | /// This example uses streaming I/O |
---|
15 | /// writePythiaStreamIO() sets the cross section in GenRun |
---|
16 | /// readPythiaStreamIO() reads the file written by writePythiaStreamIO() |
---|
17 | /// |
---|
18 | ////////////////////////////////////////////////////////////////////////// |
---|
19 | |
---|
20 | |
---|
21 | #include <fstream> |
---|
22 | #include <iostream> |
---|
23 | #include "HepMC/PythiaWrapper.h" |
---|
24 | #include "HepMC/IO_HEPEVT.h" |
---|
25 | #include "HepMC/GenEvent.h" |
---|
26 | #include "PythiaHelper.h" |
---|
27 | |
---|
28 | void writePythiaStreamIO(); |
---|
29 | void readPythiaStreamIO(); |
---|
30 | |
---|
31 | int main() { |
---|
32 | |
---|
33 | writePythiaStreamIO(); |
---|
34 | readPythiaStreamIO(); |
---|
35 | |
---|
36 | return 0; |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | void writePythiaStreamIO() { |
---|
41 | // example to generate events and write output |
---|
42 | std::cout << std::endl; |
---|
43 | std::cout << "Begin pythia_out()" << std::endl; |
---|
44 | //........................................HEPEVT |
---|
45 | // Pythia 6.1 uses HEPEVT with 4000 entries and 8-byte floating point |
---|
46 | // numbers. We need to explicitly pass this information to the |
---|
47 | // HEPEVT_Wrapper. |
---|
48 | // |
---|
49 | HepMC::HEPEVT_Wrapper::set_max_number_entries(4000); |
---|
50 | HepMC::HEPEVT_Wrapper::set_sizeof_real(8); |
---|
51 | // |
---|
52 | //........................................PYTHIA INITIALIZATIONS |
---|
53 | initPythia(); |
---|
54 | |
---|
55 | //........................................HepMC INITIALIZATIONS |
---|
56 | // |
---|
57 | // Instantiate an IO strategy for reading from HEPEVT. |
---|
58 | HepMC::IO_HEPEVT hepevtio; |
---|
59 | // |
---|
60 | { // begin scope of ascii_io |
---|
61 | // declare an output stream |
---|
62 | const char outfile[] = "example_PythiaStreamIO_write.dat"; |
---|
63 | std::ofstream ascii_io( outfile ); |
---|
64 | if( !ascii_io ) { |
---|
65 | std::cerr << "cannot open " << outfile << std::endl; |
---|
66 | exit(-1); |
---|
67 | } |
---|
68 | // use the default IO_GenEvent precision |
---|
69 | ascii_io.precision(16); |
---|
70 | // write the line that defines the beginning of a GenEvent block |
---|
71 | HepMC::write_HepMC_IO_block_begin( ascii_io ); |
---|
72 | // |
---|
73 | //........................................EVENT LOOP |
---|
74 | for ( int i = 1; i <= 100; i++ ) { |
---|
75 | if ( i%50==1 ) std::cout << "Processing Event Number " |
---|
76 | << i << std::endl; |
---|
77 | call_pyevnt(); // generate one event with Pythia |
---|
78 | // pythia pyhepc routine converts common PYJETS in common HEPEVT |
---|
79 | call_pyhepc( 1 ); |
---|
80 | HepMC::GenEvent* evt = hepevtio.read_next_event(); |
---|
81 | // define the units (Pythia uses GeV and mm) |
---|
82 | evt->use_units(HepMC::Units::GEV, HepMC::Units::MM); |
---|
83 | // add some information to the event |
---|
84 | evt->set_event_number(i); |
---|
85 | evt->set_signal_process_id(20); |
---|
86 | // set number of multi parton interactions |
---|
87 | evt->set_mpi( pypars.msti[31-1] ); |
---|
88 | // set cross section information |
---|
89 | evt->set_cross_section( HepMC::getPythiaCrossSection() ); |
---|
90 | // write the event out to the ascii files |
---|
91 | ascii_io << (*evt);; |
---|
92 | // we also need to delete the created event from memory |
---|
93 | delete evt; |
---|
94 | } |
---|
95 | // write the line that defines the end of a GenEvent block |
---|
96 | HepMC::write_HepMC_IO_block_end( ascii_io ); |
---|
97 | //........................................TERMINATION |
---|
98 | // write out some information from Pythia to the screen |
---|
99 | call_pystat( 1 ); |
---|
100 | } // end scope of ascii_io |
---|
101 | } |
---|
102 | |
---|
103 | void readPythiaStreamIO() { |
---|
104 | // example to read events written by writePythiaStreamIO |
---|
105 | // and write them back out |
---|
106 | std::cout << std::endl; |
---|
107 | // input units are GeV and mm |
---|
108 | const char infile[] = "example_PythiaStreamIO_write.dat"; |
---|
109 | std::ifstream is( infile ); |
---|
110 | if( !is ) { |
---|
111 | std::cerr << "cannot open " << infile << std::endl; |
---|
112 | exit(-1); |
---|
113 | } |
---|
114 | // |
---|
115 | { // begin scope of ascii_io |
---|
116 | // declare an output stream |
---|
117 | const char outfile[] = "example_PythiaStreamIO_read.dat"; |
---|
118 | std::ofstream ascii_io( outfile ); |
---|
119 | if( !ascii_io ) { |
---|
120 | std::cerr << "cannot open " << outfile << std::endl; |
---|
121 | exit(-1); |
---|
122 | } |
---|
123 | ascii_io.precision(16); |
---|
124 | HepMC::write_HepMC_IO_block_begin( ascii_io ); |
---|
125 | // |
---|
126 | //........................................EVENT LOOP |
---|
127 | HepMC::GenEvent evt; |
---|
128 | int i = 0; |
---|
129 | while ( is ) { |
---|
130 | evt.read( is ); |
---|
131 | // make sure we have a valid event |
---|
132 | if( evt.is_valid() ) { |
---|
133 | ++i; |
---|
134 | if ( i%50==1 ) std::cout << "Processing Event Number " |
---|
135 | << i << std::endl; |
---|
136 | if ( i%25==2 ) { |
---|
137 | // write the cross section if it exists |
---|
138 | if( evt.cross_section() ) { |
---|
139 | std::cout << "cross section at event " << i << " is " |
---|
140 | << evt.cross_section()->cross_section() |
---|
141 | << std::endl; |
---|
142 | } |
---|
143 | } |
---|
144 | // write the event out to the ascii files |
---|
145 | evt.write( ascii_io ); |
---|
146 | } |
---|
147 | } |
---|
148 | //........................................TERMINATION |
---|
149 | HepMC::write_HepMC_IO_block_end( ascii_io ); |
---|
150 | } // end scope of ascii_io |
---|
151 | } |
---|