Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllRolloverFileOutStream.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllRolloverFileOutStream.cc
2 
4 
5 #include <Event/Event.h>
6 #include <Event/oBuffer.h> // for oBuffer
7 #include <Event/ophBuffer.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 <iostream>
17 
19  const unsigned int nEvents,
20  const unsigned int sizeInMB,
21  const int offset,
22  const int increment,
23  const std::string &name)
24  : Fun4AllFileOutStream(frule, name)
25 
26 {
27  m_Offset = offset;
30  m_MaxFileFize = sizeInMB;
31  m_MaxFileFize = m_MaxFileFize * 1024 * 1024;
32  if (m_MaxFileFize == 0 || m_MaxFileFize > MaxSize())
33  {
34  if (m_MaxFileFize > MaxSize())
35  {
36  uint64_t maxmb = MaxSize() / (1024ULL * 1024ULL);
37  std::cout << "setting maximum size to current max (in MB): " << maxmb << std::endl;
38  }
40  }
41  m_Increment = increment;
42  if (m_Increment <= 0)
43  {
44  m_Increment = 1; // safety belt against overwriting files
45  }
46 }
47 
49 {
50  if (!GetoBuffer())
51  {
52  int irun = evt->getRunNumber();
53  unsigned filenamesize = FileRule().size() + 15;
54 
55  char *outfilename = new char[filenamesize];
57  int snprintfbytes = snprintf(outfilename, filenamesize, FileRule().c_str(), irun, iSeq());
58  if (static_cast<unsigned>(snprintfbytes) > filenamesize)
59  {
60  std::cout << PHWHERE << " " << Name() << ": filename exceeds length " << filenamesize
61  << ", tried " << snprintfbytes
62  << ". probably it is the filerule" << FileRule()
63  << " which uses other than %010d-%04d for runnumber/segment" << std::endl;
64  exit(1);
65  }
67  OutFileDescriptor(open(outfilename, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
68  S_IRWXU | S_IROTH | S_IRGRP));
69  if (OutFileDescriptor() == -1) // failure to open
70  {
71  std::cout << "could not open " << outfilename << " quitting" << std::endl;
72  exit(1);
73  }
74  if (Verbosity() > 0)
75  {
76  std::cout << "Fun4AllRolloverFileOutStream: opening new file " << outfilename << std::endl;
77  }
78  MyManager()->SetOutfileName(outfilename);
79  SetoBuffer(new ophBuffer(OutFileDescriptor(), xb(), LENGTH, irun, iSeq()));
80  delete[] outfilename;
81  }
82 
83  int status = GetoBuffer()->addEvent(evt);
84  if (status)
85  {
86  std::cout << Name() << ": ERROR WRITING OUT FILTERED EVENT "
87  << evt->getEvtSequence() << " FOR RUN "
88  << evt->getRunNumber() << " Status: " << status << std::endl;
89  }
90  SetNEvents(GetNEvents() + 1);
91  BytesWritten(GetoBuffer()->getBytesWritten());
92 
93  if (m_MaxNEvents > 0 && GetNEvents() >= m_MaxNEvents)
94  {
95  open_new_file();
96  }
97  if (BytesWritten() >= m_MaxFileFize)
98  {
99  open_new_file();
100  }
101  return 0;
102 }
104 {
105  os << "Fun4AllRolloverFileOutStream writing to " << FileRule()
106  << " current sequence " << m_CurrentSequence << std::endl;
107  return;
108 }
109 
111 {
112  DeleteoBuffer();
113  BytesWritten(0);
114  SetNEvents(0);
115  close(OutFileDescriptor());
116  OutFileDescriptor(-1);
117  MyManager()->SetClosingScriptArgs(MyManager()->OutFileName());
119 }