Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHRawOManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHRawOManager.cc
1 //-----------------------------------------------------------------------------
2 //
3 // The PHOOL's Software
4 // Copyright (C) PHENIX collaboration, 1999
5 //
6 // Implementation of class PHRawOManager
7 //
8 // Author: Matthias Messer
9 //-----------------------------------------------------------------------------
10 #include "PHRawOManager.h"
11 
12 #include "PHRawDataNode.h"
13 
14 #include <phool/PHCompositeNode.h>
15 #include <phool/phool.h>
16 
17 #include <Event/EventTypes.h>
18 #include <Event/oBuffer.h>
19 #include <Event/ospBuffer.h>
20 #include <Event/phenixTypes.h>
21 
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <unistd.h> // for close
25 #include <iostream>
26 
27 using namespace std;
28 
29 PHRawOManager::PHRawOManager(const string& newFile, const int run, const int bufl, const int evtl, const int complvl)
30  : filedesc(-1)
31  , runNumber(0)
32  , bufferSize(0)
33  , eventLength(0)
34 {
35  if (!setFile(newFile, run, bufl, evtl, complvl))
36  {
37  filename = "file open failed";
38  filedesc = -1;
39  memBuffer = nullptr;
40  fileBuffer = nullptr;
41  compressionLevel = 0;
42  }
43 }
44 
46  : filedesc(-1)
47  , memBuffer(nullptr)
48  , fileBuffer(nullptr)
49  , runNumber(0)
50  , bufferSize(0)
51  , eventLength(0)
52  , compressionLevel(0)
53 {
54 }
55 
57 {
58  closeFile();
59 }
60 
62 {
63  if (fileBuffer)
64  {
65  delete fileBuffer;
66  }
67  if (memBuffer)
68  {
69  delete memBuffer;
70  }
71  fileBuffer = nullptr;
72  memBuffer = nullptr;
73  if (filedesc >= 0)
74  {
75  close(filedesc);
76  filedesc = -1;
77  }
78 }
79 
80 bool PHRawOManager::setFile(const string& setFile, const int setRun, const int setBufl, const int setEvtl, const int complvl)
81 {
82  filename = setFile;
83  runNumber = setRun;
84  bufferSize = setBufl;
85  compressionLevel = complvl;
86 
87  if (setEvtl == -1)
88  {
89  eventLength = bufferSize / 4;
90  }
91  else
92  {
93  eventLength = setEvtl;
94  }
95 
96  if (filedesc >= 0)
97  {
98  closeFile(); // close the file if it is open (originally unprotected close)
99  }
100 
101  // open file
102  filedesc = open(filename.c_str(),
103  O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
104  S_IRWXU | S_IROTH | S_IRGRP);
105 
106  if (filedesc < 0)
107  {
108  PHMessage("PHRawOManager::setFile", PHError, "could not open file");
109  return false;
110  }
113 
114  return true;
115 }
116 
118 {
119  //
120  // The write function of the PHCompositeNode topNode will
121  // recursively call the write functions of its subnodes.
122  // The PHRawDataNodes among them call the write function found
123  // below.
124  //
125  if (filedesc >= 0 && fileBuffer)
126  {
128  topNode->write(this);
129  eventNumber++;
130  return true;
131  }
132  return false;
133 }
134 
136 {
137  if (filedesc >= 0 && fileBuffer)
138  {
139  int bytesAddedToBuffer = fileBuffer->addUnstructPacketData(node->getData(), node->getLength(), node->getID(),
140  node->getWordLength(), node->getHitFormat());
141  if (bytesAddedToBuffer <= 0)
142  {
143  PHMessage("PHRawOManager::write", PHError, "Zero bytes added to buffer");
144  return false;
145  }
146  return true;
147  }
148  return false;
149 }
150 
152 {
153  cout << "File attached : " << filename << endl;
154  cout << "Buffer size : " << bufferSize << endl;
155  cout << "Event Length : " << eventLength << endl;
156  cout << "Run number : " << runNumber << endl;
157  cout << "Events written: " << eventNumber << endl;
158 }