Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:16:13

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