ECCE @ EIC 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 
19 using namespace std;
20 
21 Fun4AllFileOutStream::Fun4AllFileOutStream(const string &frule, const string &name)
22  : Fun4AllEventOutStream(name)
23  , m_FileRule(frule)
24  , m_ob(nullptr)
25  , m_iSeq(0)
26  , m_OutFileDesc(-1)
27  , m_BytesWritten(0)
28  , m_MaxSize(10000000000LL)
29 {
30  memset(m_xb, 0, sizeof(m_xb));
31 }
32 
34 {
35  delete m_ob;
36  if (m_OutFileDesc >= 0)
37  {
39  }
40  return;
41 }
42 
44 {
45  if (!m_ob)
46  {
48  int irun = evt->getRunNumber();
49  unsigned filenamesize = m_FileRule.size() + 15; // %010d-%04d is 14 + /0 = 15
50 
51  char *outfilename = new char[filenamesize];
52  m_iSeq = se->SegmentNumber();
53  int snprintfbytes = snprintf(outfilename, filenamesize, m_FileRule.c_str(), irun, m_iSeq);
54  if (static_cast<unsigned>(snprintfbytes) > filenamesize)
55  {
56  cout << PHWHERE << " " << Name() << ": filename exceeds length " << filenamesize
57  << ", tried " << snprintfbytes
58  << ". probably it is the filerule" << m_OutFileDesc
59  << " which uses other than %010d-%04d for runnumber/segment" << endl;
60  exit(1);
61  }
62  m_OutFileDesc = open(outfilename, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
63  S_IRWXU | S_IROTH | S_IRGRP);
64  if (m_OutFileDesc == -1) // failure to open
65  {
66  cout << "could not open " << outfilename << " quitting" << endl;
67  exit(1);
68  }
69  cout << "opening new file " << outfilename << endl;
71  delete[] outfilename;
72  }
73 
74  int status = m_ob->addEvent(evt);
75  if (status)
76  {
77  cout << Name() << ": ERROR WRITING OUT FILTERED EVENT "
78  << evt->getEvtSequence() << " FOR RUN "
79  << evt->getRunNumber() << " Status: " << status << endl;
80  }
81  // m_BytesWritten += 4*evt->getEvtLength(); // evtlength is in 32bit words
84  {
85  DeleteoBuffer();
86  m_iSeq++;
87  m_BytesWritten = 0;
89  m_OutFileDesc = -1;
90  }
91  return 0;
92 }
93 
95 {
96  DeleteoBuffer();
97  return 0;
98 }
99 
100 void Fun4AllFileOutStream::identify(ostream &os) const
101 {
102  os << "Fun4AllFileOutStream writing to " << m_OutFileDesc << endl;
103  return;
104 }
105 
107 {
108  delete m_ob;
109  m_ob = nullptr;
110 }