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, std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
00017 : WorkLoop (processorUID)
00018 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, HighConsumerPriority)
00019 , Configurable (theXDAQContextConfigTree, configurationPath)
00020 , filePath_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("FilePath").getValue<std::string>())
00021 , fileRadix_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("RadixFileName").getValue<std::string>())
00022 , maxFileSize_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("MaxFileSize").getValue<long>()*1000000)
00023 , currentSubRunNumber_(0)
00024
00025 {
00026
00027 FILE *fp = fopen( "/home/otsdaq/tsave.txt","w");
00028 if(fp)fclose(fp);
00029 }
00030
00031
00032 RawDataSaverConsumerBase::~RawDataSaverConsumerBase(void)
00033 {}
00034
00035
00036 void RawDataSaverConsumerBase::startProcessingData(std::string runNumber)
00037 {
00038 openFile(runNumber);
00039 DataConsumer::startProcessingData(runNumber);
00040 }
00041
00042
00043 void RawDataSaverConsumerBase::stopProcessingData(void)
00044 {
00045 DataConsumer::stopProcessingData();
00046 closeFile();
00047 }
00048
00049
00050 void RawDataSaverConsumerBase::openFile(std::string runNumber)
00051 {
00052 currentRunNumber_ = runNumber;
00053
00054 std::stringstream fileName;
00055 fileName << filePath_ << "/" << fileRadix_ << "_Run" << runNumber;
00056
00057 if(maxFileSize_ > 0)
00058 fileName << "_" << currentSubRunNumber_;
00059 fileName << "_Raw.dat";
00060 std::cout << __COUT_HDR_FL__ << "Saving file: " << fileName.str() << std::endl;
00061 outFile_.open(fileName.str().c_str(), std::ios::out | std::ios::binary);
00062 if(!outFile_.is_open())
00063 {
00064 __SS__ << "Can't open file " << fileName.str() << std::endl;
00065 __COUT_ERR__ << "\n" << ss.str();
00066 throw std::runtime_error(ss.str());
00067 }
00068
00069 writeHeader();
00070 }
00071
00072
00073 void RawDataSaverConsumerBase::closeFile(void)
00074 {
00075 if(outFile_.is_open())
00076 {
00077 writeFooter();
00078 outFile_.close();
00079 }
00080 }
00081
00082
00083 void RawDataSaverConsumerBase::save(const std::string& data)
00084 {
00085
00086 std::ofstream output;
00087
00088
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 if(maxFileSize_ > 0)
00133 {
00134 long length = outFile_.tellp();
00135 if(length >= maxFileSize_)
00136 {
00137 closeFile();
00138 ++currentSubRunNumber_;
00139 openFile(currentRunNumber_);
00140 }
00141 }
00142
00143 writePacketHeader(data);
00144 outFile_.write( (char*)&data[0], data.length());
00145 writePacketFooter(data);
00146 }
00147
00148
00149 bool RawDataSaverConsumerBase::workLoopThread(toolbox::task::WorkLoop* workLoop)
00150 {
00151
00152 fastRead();
00153 return WorkLoop::continueWorkLoop_;
00154 }
00155
00156
00157 void RawDataSaverConsumerBase::fastRead(void)
00158 {
00159
00160 if(DataConsumer::read(dataP_, headerP_) < 0)
00161 {
00162 usleep(100);
00163 return;
00164 }
00165
00166
00167 save(*dataP_);
00168 DataConsumer::setReadSubBuffer<std::string, std::map<std::string, std::string>>();
00169 }
00170
00171
00172 void RawDataSaverConsumerBase::slowRead(void)
00173 {
00174
00175
00176 if(DataConsumer::read(data_, header_) < 0)
00177 {
00178 usleep(1000);
00179 return;
00180 }
00181 save(data_);
00182 }