00001 #include "otsdaq-core/DataManager/RawDataSaverConsumerBase.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003
00004
00005 #include <iostream>
00006 #include <cassert>
00007 #include <unistd.h>
00008
00009 #include <sstream>
00010
00011 using namespace ots;
00012
00013
00014
00015 RawDataSaverConsumerBase::RawDataSaverConsumerBase(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
00016 : WorkLoop (processorUID)
00017 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, HighConsumerPriority)
00018 , Configurable (theXDAQContextConfigTree, configurationPath)
00019 , filePath_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("FilePath").getValue<std::string>())
00020 , fileRadix_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("RadixFileName").getValue<std::string>())
00021 , maxFileSize_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("MaxFileSize").getValue<long>()*1000000)
00022 , currentSubRunNumber_(0)
00023
00024 {
00025 }
00026
00027
00028 RawDataSaverConsumerBase::~RawDataSaverConsumerBase(void)
00029 {}
00030
00031
00032 void RawDataSaverConsumerBase::startProcessingData(std::string runNumber)
00033 {
00034 openFile(runNumber);
00035 DataConsumer::startProcessingData(runNumber);
00036 }
00037
00038
00039 void RawDataSaverConsumerBase::stopProcessingData(void)
00040 {
00041 DataConsumer::stopProcessingData();
00042 closeFile();
00043 }
00044
00045
00046 void RawDataSaverConsumerBase::openFile(std::string runNumber)
00047 {
00048 currentRunNumber_ = runNumber;
00049
00050 std::stringstream fileName;
00051 fileName << filePath_ << "/" << fileRadix_ << "_Run" << runNumber;
00052
00053 if(maxFileSize_ > 0)
00054 fileName << "_" << currentSubRunNumber_;
00055 fileName << "_Raw.dat";
00056 std::cout << __COUT_HDR_FL__ << "Saving file: " << fileName.str() << std::endl;
00057 outFile_.open(fileName.str().c_str(), std::ios::out | std::ios::binary);
00058 if(!outFile_.is_open())
00059 {
00060 __SS__ << "Can't open file " << fileName.str() << std::endl;
00061 __MOUT_ERR__ << "\n" << ss.str();
00062 throw std::runtime_error(ss.str());
00063 }
00064
00065 writeHeader();
00066 }
00067
00068
00069 void RawDataSaverConsumerBase::closeFile(void)
00070 {
00071 if(outFile_.is_open())
00072 {
00073 writeFooter();
00074 outFile_.close();
00075 }
00076 }
00077
00078
00079 void RawDataSaverConsumerBase::save(const std::string& data)
00080 {
00081 if(maxFileSize_ > 0)
00082 {
00083 long length = outFile_.tellp();
00084 if(length >= maxFileSize_)
00085 {
00086 closeFile();
00087 ++currentSubRunNumber_;
00088 openFile(currentRunNumber_);
00089 }
00090 }
00091
00092 writePacketHeader(data);
00093 outFile_.write( (char*)&data[0], data.length());
00094 writePacketFooter(data);
00095 }
00096
00097
00098 bool RawDataSaverConsumerBase::workLoopThread(toolbox::task::WorkLoop* workLoop)
00099 {
00100
00101 fastRead();
00102 return WorkLoop::continueWorkLoop_;
00103 }
00104
00105
00106 void RawDataSaverConsumerBase::fastRead(void)
00107 {
00108
00109 if(DataConsumer::read(dataP_, headerP_) < 0)
00110 {
00111 usleep(100);
00112 return;
00113 }
00114
00115
00116 save(*dataP_);
00117 DataConsumer::setReadSubBuffer<std::string, std::map<std::string, std::string>>();
00118 }
00119
00120
00121 void RawDataSaverConsumerBase::slowRead(void)
00122 {
00123
00124
00125 if(DataConsumer::read(data_, header_) < 0)
00126 {
00127 usleep(1000);
00128 return;
00129 }
00130 save(data_);
00131 }