Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllFileOutStream.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllFileOutStream.cc
1 #include "Fun4AllFileOutStream.h"
2 
4 
5 #include <Event/Event.h>
6 #include <Event/oBuffer.h> // for oBuffer
7 #include <Event/olzoBuffer.h>
8 
9 #include <phool/phool.h>
10 
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <unistd.h> // for close
14 #include <cstdio> // for snprintf
15 #include <cstdlib> // for exit
16 #include <cstring>
17 #include <iostream>
18 
20  : Fun4AllEventOutStream(name)
21  , m_FileRule(frule)
22 {
23  memset(m_xb, 0, sizeof(m_xb));
24 }
25 
27 {
28  delete m_ob;
29  if (m_OutFileDesc >= 0)
30  {
31  close(m_OutFileDesc);
32  }
33  return;
34 }
35 
37 {
38  if (!m_ob)
39  {
41  int irun = evt->getRunNumber();
42  unsigned filenamesize = m_FileRule.size() + 15; // %010d-%04d is 14 + /0 = 15
43 
44  char *outfilename = new char[filenamesize];
45  m_iSeq = se->SegmentNumber();
46  int snprintfbytes = snprintf(outfilename, filenamesize, m_FileRule.c_str(), irun, m_iSeq);
47  if (static_cast<unsigned>(snprintfbytes) > filenamesize)
48  {
49  std::cout << PHWHERE << " " << Name() << ": filename exceeds length " << filenamesize
50  << ", tried " << snprintfbytes
51  << ". probably it is the filerule" << m_OutFileDesc
52  << " which uses other than %010d-%04d for runnumber/segment" << std::endl;
53  exit(1);
54  }
55  m_OutFileDesc = open(outfilename, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
56  S_IRWXU | S_IROTH | S_IRGRP);
57  if (m_OutFileDesc == -1) // failure to open
58  {
59  std::cout << "could not open " << outfilename << " quitting" << std::endl;
60  exit(1);
61  }
62  std::cout << "opening new file " << outfilename << std::endl;
64  delete[] outfilename;
65  }
66 
67  int status = m_ob->addEvent(evt);
68  if (status)
69  {
70  std::cout << Name() << ": ERROR WRITING OUT FILTERED EVENT "
71  << evt->getEvtSequence() << " FOR RUN "
72  << evt->getRunNumber() << " Status: " << status << std::endl;
73  }
74  // m_BytesWritten += 4*evt->getEvtLength(); // evtlength is in 32bit words
77  {
78  DeleteoBuffer();
79  m_iSeq++;
80  m_BytesWritten = 0;
81  close(m_OutFileDesc);
82  m_OutFileDesc = -1;
83  }
84  return 0;
85 }
86 
88 {
89  DeleteoBuffer();
90  return 0;
91 }
92 
93 void Fun4AllFileOutStream::identify(std::ostream &os) const
94 {
95  os << "Fun4AllFileOutStream writing to " << m_OutFileDesc << std::endl;
96  return;
97 }
98 
100 {
101  delete m_ob;
102  m_ob = nullptr;
103 }