Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllHepMCOutputManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllHepMCOutputManager.cc
2 
3 #include "PHHepMCGenEvent.h"
4 #include "PHHepMCGenEventMap.h"
5 
6 #include <fun4all/Fun4AllOutputManager.h> // for Fun4AllOutp...
8 
9 #include <phool/getClass.h>
10 #include <phool/phool.h> // for PHWHERE
11 
12 #include <HepMC/IO_GenEvent.h>
13 
14 #include <TPRegexp.h>
15 #include <TString.h>
16 
17 #include <boost/iostreams/filter/bzip2.hpp>
18 #include <boost/iostreams/filter/gzip.hpp>
19 #include <boost/iostreams/filtering_streambuf.hpp>
20 
21 #include <cassert>
22 #include <cstdlib> // for exit
23 #include <fstream>
24 #include <iostream>
25 #include <ostream>
26 #include <string>
27 #include <utility> // for swap
28 
29 namespace HepMC { class GenEvent; }
30 
31 using namespace std;
32 
33 static boost::iostreams::filtering_streambuf<boost::iostreams::output> zoutbuffer;
34 
36  const string &filename)
37  : Fun4AllOutputManager(myname)
38  , outfilename(filename)
39  , comment_written(0)
40  , filestream(nullptr)
41  , zipstream(nullptr)
42  , _embedding_id(0)
43 {
44  TString tstr(filename);
45  TPRegexp bzip_ext(".bz2$");
46  TPRegexp gzip_ext(".gz$");
47 
48  if (tstr.Contains(bzip_ext))
49  {
50  // use boost iosteam library to compress to bz2 file on the fly
51  filestream = new ofstream(filename.c_str(), std::ios::out | std::ios::binary);
52  zoutbuffer.push(boost::iostreams::bzip2_compressor(9));
53  zoutbuffer.push(*filestream);
54  zipstream = new ostream(&zoutbuffer);
55  ascii_out = new HepMC::IO_GenEvent(*zipstream);
56  }
57  else if (tstr.Contains(gzip_ext))
58  {
59  // use boost iosream to compress to gzip file on the fly
60  filestream = new ofstream(filename.c_str(), std::ios::out | std::ios::binary);
61  zoutbuffer.push(boost::iostreams::gzip_compressor(9));
62  zoutbuffer.push(*filestream);
63  zipstream = new ostream(&zoutbuffer);
64  ascii_out = new HepMC::IO_GenEvent(*zipstream);
65  }
66  else
67  {
68  // produces normal ascii hepmc file
69  ascii_out = new HepMC::IO_GenEvent(filename, std::ios::out);
70  }
71 
72  if (ascii_out->rdstate())
73  {
74  cout << "error opening " << outfilename << " exiting " << endl;
75  exit(1);
76  }
77  return;
78 }
79 
81 {
82  if (ascii_out)
83  {
84  if (!comment_written)
85  {
86  if (comment.size())
87  {
88  ascii_out->write_comment(comment);
89  }
90  }
91  ascii_out->clear();
92  }
93 
94  delete ascii_out;
95 
96  if (zoutbuffer.size() > 0) zoutbuffer.reset();
97 
98  if (zipstream) delete zipstream;
99  if (filestream) delete filestream;
100 
101  return;
102 }
103 
104 void Fun4AllHepMCOutputManager::Print(const string &what) const
105 {
106  cout << Name() << " writes " << outfilename << endl;
107  if (comment.size())
108  {
109  cout << "comment : " << comment << endl;
110  }
111  // base class print method
113 
114  return;
115 }
116 
118 {
119  if (!comment_written)
120  {
121  if (comment.size())
122  {
123  ascii_out->write_comment(comment);
124  }
125  comment_written = 1;
126  }
127 
128  PHHepMCGenEventMap *geneventmap = findNode::getClass<PHHepMCGenEventMap>(topNode, "PHHepMCGenEventMap");
129 
130  if (!geneventmap)
131  {
132  cout << "Fun4AllHepMCOutputManager::Write - Fatal Error - missing source node PHHepMCGenEventMap" << endl;
134  }
135  assert(geneventmap);
136 
137  PHHepMCGenEvent *genevt = geneventmap->get(_embedding_id);
138  if (!genevt)
139  {
140  cout << "Fun4AllHepMCOutputManager::Write - Warning - missing sub-event with embedding ID" << _embedding_id << " on node PHHepMCGenEventMap" << endl;
142  }
143  assert(genevt);
144 
145  HepMC::GenEvent *evt = genevt->getEvent();
146  if (!evt)
147  {
148  cout << PHWHERE << "0 HepMC Pointer" << endl;
150  }
151  assert(evt);
152 
153  IncrementEvents(1);
154  ascii_out->write_event(evt);
156 }
157 
159 {
160  comment = text;
161  comment_written = 0;
162  return 0;
163 }