Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 //-----------------------------------------------------------------------------
0002 //
0003 //  The PHOOL's Software
0004 //  Copyright (C) PHENIX collaboration, 1999
0005 //
0006 //  Implementation of class PHRawOManager
0007 //
0008 //  Author: Matthias Messer
0009 //-----------------------------------------------------------------------------
0010 #include "PHRawOManager.h"
0011 
0012 #include "PHRawDataNode.h"
0013 
0014 #include <phool/PHCompositeNode.h>
0015 #include <phool/phool.h>
0016 
0017 #include <Event/EventTypes.h>
0018 #include <Event/oBuffer.h>
0019 #include <Event/ospBuffer.h>
0020 #include <Event/phenixTypes.h>
0021 
0022 #include <fcntl.h>
0023 #include <sys/stat.h>
0024 #include <unistd.h>  // for close
0025 #include <iostream>
0026 
0027 PHRawOManager::PHRawOManager(const std::string& newFile, const int run, const int bufl, const int evtl, const int complvl)
0028 {
0029   if (!setFile(newFile, run, bufl, evtl, complvl))
0030   {
0031     filename = "file open failed";
0032     filedesc = -1;
0033     memBuffer = nullptr;
0034     fileBuffer = nullptr;
0035     compressionLevel = 0;
0036   }
0037 }
0038 
0039 PHRawOManager::~PHRawOManager()
0040 {
0041   closeFile();
0042 }
0043 
0044 void PHRawOManager::closeFile()
0045 {
0046   delete fileBuffer;
0047   delete memBuffer;
0048   fileBuffer = nullptr;
0049   memBuffer = nullptr;
0050   if (filedesc >= 0)
0051   {
0052     close(filedesc);
0053     filedesc = -1;
0054   }
0055 }
0056 
0057 bool PHRawOManager::setFile(const std::string& setFile, const int setRun, const int setBufl, const int setEvtl, const int complvl)
0058 {
0059   filename = setFile;
0060   runNumber = setRun;
0061   bufferSize = setBufl;
0062   compressionLevel = complvl;
0063 
0064   if (setEvtl == -1)
0065   {
0066     eventLength = bufferSize / 4;
0067   }
0068   else
0069   {
0070     eventLength = setEvtl;
0071   }
0072 
0073   if (filedesc >= 0)
0074   {
0075     closeFile();  // close the file if it is open (originally unprotected close)
0076   }
0077 
0078   // open file
0079   // NOLINTNEXTLINE(hicpp-vararg)
0080   filedesc = open(filename.c_str(),
0081                   O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE,
0082                   S_IRWXU | S_IROTH | S_IRGRP);
0083 
0084   if (filedesc < 0)
0085   {
0086     std::cout << PHWHERE << " could not open file "
0087           << filename << std::endl;;
0088     return false;
0089   }
0090   memBuffer = new PHDWORD[bufferSize];
0091   fileBuffer = new ospBuffer(filedesc, memBuffer, bufferSize, runNumber);
0092 
0093   return true;
0094 }
0095 
0096 bool PHRawOManager::write(PHCompositeNode* topNode)
0097 {
0098   //
0099   // The write function of the PHCompositeNode topNode will
0100   // recursively call the write functions of its subnodes.
0101   // The PHRawDataNodes among them call the write function found
0102   // below.
0103   //
0104   if (filedesc >= 0 && fileBuffer)
0105   {
0106     fileBuffer->nextEvent(eventLength, DATAEVENT);
0107     topNode->write(this);
0108     eventNumber++;
0109     return true;
0110   }
0111   return false;
0112 }
0113 
0114 bool PHRawOManager::write(PHRawDataNode* node)
0115 {
0116   if (filedesc >= 0 && fileBuffer)
0117   {
0118     int bytesAddedToBuffer = fileBuffer->addUnstructPacketData(node->getData(), node->getLength(), node->getID(),
0119                                                                node->getWordLength(), node->getHitFormat());
0120     if (bytesAddedToBuffer <= 0)
0121     {
0122       std::cout << PHWHERE << " Zero bytes added to buffer" << std::endl;
0123       return false;
0124     }
0125     return true;
0126   }
0127   return false;
0128 }
0129 
0130 void PHRawOManager::print() const
0131 {
0132   std::cout << "File attached : " << filename << std::endl;
0133   std::cout << "Buffer size   : " << bufferSize << std::endl;
0134   std::cout << "Event Length  : " << eventLength << std::endl;
0135   std::cout << "Run number    : " << runNumber << std::endl;
0136   std::cout << "Events written: " << eventNumber << std::endl;
0137 }