File indexing completed on 2025-12-17 09:19:45
0001 #include "Fun4AllRolloverFileOutStream.h"
0002
0003 #include "Fun4AllEventOutputManager.h"
0004
0005 #include <Event/Event.h>
0006 #include <Event/oBuffer.h> // for oBuffer
0007 #include <Event/ophBuffer.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 <iostream>
0017
0018 Fun4AllRolloverFileOutStream::Fun4AllRolloverFileOutStream(const std::string &frule,
0019 const unsigned int nEvents,
0020 const unsigned int sizeInMB,
0021 const int offset,
0022 const int increment,
0023 const std::string &name)
0024 : Fun4AllFileOutStream(frule, name)
0025 , m_MaxFileFize(sizeInMB)
0026 , m_MaxNEvents(nEvents)
0027 , m_CurrentSequence(offset)
0028 , m_Increment(increment)
0029 {
0030 m_MaxFileFize = m_MaxFileFize * 1024 * 1024;
0031 if (m_MaxFileFize == 0 || m_MaxFileFize > MaxSize())
0032 {
0033 if (m_MaxFileFize > MaxSize())
0034 {
0035 uint64_t maxmb = MaxSize() / (1024ULL * 1024ULL);
0036 std::cout << "setting maximum size to current max (in MB): " << maxmb << std::endl;
0037 }
0038 m_MaxFileFize = MaxSize();
0039 }
0040 if (m_Increment <= 0)
0041 {
0042 m_Increment = 1;
0043 }
0044 }
0045
0046 int Fun4AllRolloverFileOutStream::WriteEventOut(Event *evt)
0047 {
0048 if (!GetoBuffer())
0049 {
0050 int irun = evt->getRunNumber();
0051 unsigned filenamesize = FileRule().size() + 15;
0052
0053 char *outfilename = new char[filenamesize];
0054 iSeq(m_CurrentSequence);
0055 int snprintfbytes = snprintf(outfilename, filenamesize, FileRule().c_str(), irun, iSeq());
0056 if (static_cast<unsigned>(snprintfbytes) > filenamesize)
0057 {
0058 std::cout << PHWHERE << " " << Name() << ": filename exceeds length " << filenamesize
0059 << ", tried " << snprintfbytes
0060 << ". probably it is the filerule" << FileRule()
0061 << " which uses other than %010d-%04d for runnumber/segment" << std::endl;
0062 exit(1);
0063 }
0064 m_CurrentSequence += m_Increment;
0065
0066 OutFileDescriptor(open(outfilename, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
0067 S_IRWXU | S_IROTH | S_IRGRP));
0068 if (OutFileDescriptor() == -1)
0069 {
0070 std::cout << "could not open " << outfilename << " quitting" << std::endl;
0071 exit(1);
0072 }
0073 if (Verbosity() > 0)
0074 {
0075 std::cout << "Fun4AllRolloverFileOutStream: opening new file " << outfilename << std::endl;
0076 }
0077 MyManager()->SetOutfileName(outfilename);
0078 SetoBuffer(new ophBuffer(OutFileDescriptor(), xb(), LENGTH, irun, iSeq()));
0079 delete[] outfilename;
0080 }
0081
0082 int status = GetoBuffer()->addEvent(evt);
0083 if (status)
0084 {
0085 std::cout << Name() << ": ERROR WRITING OUT FILTERED EVENT "
0086 << evt->getEvtSequence() << " FOR RUN "
0087 << evt->getRunNumber() << " Status: " << status << std::endl;
0088 }
0089 SetNEvents(GetNEvents() + 1);
0090 BytesWritten(GetoBuffer()->getBytesWritten());
0091
0092 if (m_MaxNEvents > 0 && GetNEvents() >= m_MaxNEvents)
0093 {
0094 open_new_file();
0095 }
0096 if (BytesWritten() >= m_MaxFileFize)
0097 {
0098 open_new_file();
0099 }
0100 return 0;
0101 }
0102 void Fun4AllRolloverFileOutStream::identify(std::ostream &os) const
0103 {
0104 os << "Fun4AllRolloverFileOutStream writing to " << FileRule()
0105 << " current sequence " << m_CurrentSequence << std::endl;
0106 return;
0107 }
0108
0109 void Fun4AllRolloverFileOutStream::open_new_file()
0110 {
0111 DeleteoBuffer();
0112 BytesWritten(0);
0113 SetNEvents(0);
0114 close(OutFileDescriptor());
0115 OutFileDescriptor(-1);
0116 MyManager()->RunAfterClosing();
0117 }