File indexing completed on 2025-08-06 08:17:19
0001 #include "Fun4AllFileOutStream.h"
0002
0003 #include <fun4all/Fun4AllServer.h>
0004
0005 #include <Event/Event.h>
0006 #include <Event/oBuffer.h> // for oBuffer
0007 #include <Event/olzoBuffer.h>
0008
0009 #include <phool/phool.h>
0010
0011 #include <fcntl.h>
0012 #include <sys/stat.h>
0013 #include <unistd.h> // for close
0014 #include <cstdio> // for snprintf
0015 #include <cstdlib> // for exit
0016 #include <cstring>
0017 #include <iostream>
0018
0019 Fun4AllFileOutStream::Fun4AllFileOutStream(const std::string &frule, const std::string &name)
0020 : Fun4AllEventOutStream(name)
0021 , m_FileRule(frule)
0022 {
0023 memset(m_xb, 0, sizeof(m_xb));
0024 }
0025
0026 Fun4AllFileOutStream::~Fun4AllFileOutStream()
0027 {
0028 delete m_ob;
0029 if (m_OutFileDesc >= 0)
0030 {
0031 close(m_OutFileDesc);
0032 }
0033 return;
0034 }
0035
0036 int Fun4AllFileOutStream::WriteEventOut(Event *evt)
0037 {
0038 if (!m_ob)
0039 {
0040 Fun4AllServer *se = Fun4AllServer::instance();
0041 int irun = evt->getRunNumber();
0042 unsigned filenamesize = m_FileRule.size() + 15;
0043
0044 char *outfilename = new char[filenamesize];
0045 m_iSeq = se->SegmentNumber();
0046
0047 int snprintfbytes = snprintf(outfilename, filenamesize, m_FileRule.c_str(), irun, m_iSeq);
0048 if (static_cast<unsigned>(snprintfbytes) > filenamesize)
0049 {
0050 std::cout << PHWHERE << " " << Name() << ": filename exceeds length " << filenamesize
0051 << ", tried " << snprintfbytes
0052 << ". probably it is the filerule" << m_OutFileDesc
0053 << " which uses other than %010d-%04d for runnumber/segment" << std::endl;
0054 exit(1);
0055 }
0056
0057 m_OutFileDesc = open(outfilename, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
0058 S_IRWXU | S_IROTH | S_IRGRP);
0059 if (m_OutFileDesc == -1)
0060 {
0061 std::cout << "could not open " << outfilename << " quitting" << std::endl;
0062 exit(1);
0063 }
0064 std::cout << "opening new file " << outfilename << std::endl;
0065 m_ob = new olzoBuffer(m_OutFileDesc, m_xb, LENGTH, irun, m_iSeq);
0066 delete[] outfilename;
0067 }
0068
0069 int status = m_ob->addEvent(evt);
0070 if (status)
0071 {
0072 std::cout << Name() << ": ERROR WRITING OUT FILTERED EVENT "
0073 << evt->getEvtSequence() << " FOR RUN "
0074 << evt->getRunNumber() << " Status: " << status << std::endl;
0075 }
0076
0077 m_BytesWritten = m_ob->getBytesWritten();
0078 if (m_BytesWritten >= m_MaxSize)
0079 {
0080 DeleteoBuffer();
0081 m_iSeq++;
0082 m_BytesWritten = 0;
0083 close(m_OutFileDesc);
0084 m_OutFileDesc = -1;
0085 }
0086 return 0;
0087 }
0088
0089 int Fun4AllFileOutStream::CloseOutStream()
0090 {
0091 DeleteoBuffer();
0092 return 0;
0093 }
0094
0095 void Fun4AllFileOutStream::identify(std::ostream &os) const
0096 {
0097 os << "Fun4AllFileOutStream writing to " << m_OutFileDesc << std::endl;
0098 return;
0099 }
0100
0101 void Fun4AllFileOutStream::DeleteoBuffer()
0102 {
0103 delete m_ob;
0104 m_ob = nullptr;
0105 }