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 #include <fstream>
00011
00012 using namespace ots;
00013
00014
00015
00016 RawDataSaverConsumerBase::RawDataSaverConsumerBase(std::string supervisorApplicationUID, std::string bufferUID,
00017 std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree,
00018 const std::string& configurationPath)
00019 : WorkLoop (processorUID)
00020 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, HighConsumerPriority)
00021 , Configurable (theXDAQContextConfigTree, configurationPath)
00022 , filePath_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("FilePath").getValue<std::string>())
00023 , fileRadix_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("RadixFileName").getValue<std::string>())
00024 , maxFileSize_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("MaxFileSize").getValue<long>()*1000000)
00025 , currentSubRunNumber_(0)
00026
00027 {
00028
00029
00030
00031 }
00032
00033
00034 RawDataSaverConsumerBase::~RawDataSaverConsumerBase(void)
00035 {}
00036
00037
00038 void RawDataSaverConsumerBase::startProcessingData(std::string runNumber)
00039 {
00040 openFile(runNumber);
00041 DataConsumer::startProcessingData(runNumber);
00042 }
00043
00044
00045 void RawDataSaverConsumerBase::stopProcessingData(void)
00046 {
00047 DataConsumer::stopProcessingData();
00048 closeFile();
00049 }
00050
00051
00052 void RawDataSaverConsumerBase::openFile(std::string runNumber)
00053 {
00054 currentRunNumber_ = runNumber;
00055
00056 std::stringstream fileName;
00057 fileName << filePath_ << "/" << fileRadix_ << "_Run" << runNumber;
00058
00059 if(maxFileSize_ > 0)
00060 fileName << "_" << currentSubRunNumber_;
00061 fileName << "_Raw.dat";
00062 std::cout << __COUT_HDR_FL__ << "Saving file: " << fileName.str() << std::endl;
00063 outFile_.open(fileName.str().c_str(), std::ios::out | std::ios::binary);
00064 if(!outFile_.is_open())
00065 {
00066 __SS__ << "Can't open file " << fileName.str() << std::endl;
00067 __COUT_ERR__ << "\n" << ss.str();
00068 throw std::runtime_error(ss.str());
00069 }
00070
00071 writeHeader();
00072 }
00073
00074
00075 void RawDataSaverConsumerBase::closeFile(void)
00076 {
00077 if(outFile_.is_open())
00078 {
00079 writeFooter();
00080 outFile_.close();
00081 }
00082 }
00083
00084
00085 void RawDataSaverConsumerBase::save(const std::string& data)
00086 {
00087
00088 std::ofstream output;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 if(maxFileSize_ > 0)
00135 {
00136 long length = outFile_.tellp();
00137 if(length >= maxFileSize_)
00138 {
00139 closeFile();
00140 ++currentSubRunNumber_;
00141 openFile(currentRunNumber_);
00142 }
00143 }
00144
00145 writePacketHeader(data);
00146 outFile_.write( (char*)&data[0], data.length());
00147 writePacketFooter(data);
00148 }
00149
00150
00151 bool RawDataSaverConsumerBase::workLoopThread(toolbox::task::WorkLoop* workLoop)
00152 {
00153
00154
00155 fastRead();
00156 return WorkLoop::continueWorkLoop_;
00157 }
00158
00159
00160 void RawDataSaverConsumerBase::fastRead(void)
00161 {
00162
00163 if(DataConsumer::read(dataP_, headerP_) < 0)
00164 {
00165 usleep(100);
00166 return;
00167 }
00168
00169
00170 save(*dataP_);
00171 DataConsumer::setReadSubBuffer<std::string, std::map<std::string, std::string>>();
00172 }
00173
00174
00175 void RawDataSaverConsumerBase::slowRead(void)
00176 {
00177
00178
00179 if(DataConsumer::read(data_, header_) < 0)
00180 {
00181 usleep(1000);
00182 return;
00183 }
00184 save(data_);
00185 }