$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-core/DataManager/RawDataSaverConsumerBase.h" 00002 #include "otsdaq-core/Macros/BinaryStringMacros.h" 00003 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00004 00005 #include <unistd.h> 00006 #include <cassert> 00007 #include <iostream> 00008 //#include <string.h> //memcpy 00009 #include <fstream> 00010 #include <sstream> 00011 00012 using namespace ots; 00013 00014 //======================================================================================================================== 00015 RawDataSaverConsumerBase::RawDataSaverConsumerBase( 00016 std::string supervisorApplicationUID, 00017 std::string bufferUID, 00018 std::string processorUID, 00019 const ConfigurationTree& theXDAQContextConfigTree, 00020 const std::string& configurationPath) 00021 : WorkLoop(processorUID) 00022 , DataConsumer( 00023 supervisorApplicationUID, bufferUID, processorUID, HighConsumerPriority) 00024 , Configurable(theXDAQContextConfigTree, configurationPath) 00025 , filePath_(theXDAQContextConfigTree.getNode(configurationPath) 00026 .getNode("FilePath") 00027 .getValue<std::string>()) 00028 , fileRadix_(theXDAQContextConfigTree.getNode(configurationPath) 00029 .getNode("RadixFileName") 00030 .getValue<std::string>()) 00031 , maxFileSize_(theXDAQContextConfigTree.getNode(configurationPath) 00032 .getNode("MaxFileSize") 00033 .getValue<long>() * 00034 1000000) // Instead of 2^6=1048576 00035 , currentSubRunNumber_(0) 00036 00037 { 00038 // FILE *fp = fopen( "/home/otsdaq/tsave.txt","w"); 00039 // if(fp)fclose(fp); 00040 } 00041 00042 //======================================================================================================================== 00043 RawDataSaverConsumerBase::~RawDataSaverConsumerBase(void) {} 00044 00045 //======================================================================================================================== 00046 void RawDataSaverConsumerBase::startProcessingData(std::string runNumber) 00047 { 00048 if(runNumber != "") // If there is no number it means it was paused 00049 { 00050 currentSubRunNumber_ = 0; 00051 openFile(runNumber); 00052 } 00053 DataConsumer::startProcessingData(runNumber); 00054 } 00055 00056 //======================================================================================================================== 00057 void RawDataSaverConsumerBase::stopProcessingData(void) 00058 { 00059 DataConsumer::stopProcessingData(); 00060 closeFile(); 00061 } 00062 00063 //======================================================================================================================== 00064 void RawDataSaverConsumerBase::openFile(std::string runNumber) 00065 { 00066 currentRunNumber_ = runNumber; 00067 // std::string fileName = "Run" + runNumber + "_" + processorUID_ + "_Raw.dat"; 00068 std::stringstream fileName; 00069 fileName << filePath_ << "/" << fileRadix_ << "_Run" << runNumber; 00070 // if split file is there then subrunnumber must be set! 00071 if(maxFileSize_ > 0) 00072 fileName << "_" << currentSubRunNumber_; 00073 fileName << "_Raw.dat"; 00074 __CFG_COUT__ << "Saving file: " << fileName.str() << std::endl; 00075 outFile_.open(fileName.str().c_str(), std::ios::out | std::ios::binary); 00076 if(!outFile_.is_open()) 00077 { 00078 __CFG_SS__ << "Can't open file " << fileName.str() << std::endl; 00079 __CFG_SS_THROW__; 00080 } 00081 00082 writeHeader(); // write start of file header 00083 } 00084 00085 //======================================================================================================================== 00086 void RawDataSaverConsumerBase::closeFile(void) 00087 { 00088 if(outFile_.is_open()) 00089 { 00090 writeFooter(); // write end of file footer 00091 outFile_.close(); 00092 } 00093 } 00094 00095 //======================================================================================================================== 00096 void RawDataSaverConsumerBase::save(const std::string& data) 00097 { 00098 std::ofstream output; 00099 00100 // std::string outputPath = "/home/otsdaq/tsave.txt"; 00101 // if(0) 00102 // { 00103 // output.open(outputPath, std::ios::out | std::ios::app | std::ios::binary); 00104 // output << data; 00105 // } 00106 // else 00107 // { 00108 // output.open(outputPath, std::ios::out | std::ios::app); 00109 // output << data; 00110 // 00111 // char str[5]; 00112 // for(unsigned int j=0;j<data.length();++j) 00113 // { 00114 // sprintf(str,"%2.2x",((unsigned int)data[j]) & ((unsigned int)(0x0FF))); 00115 // 00116 // if(j%64 == 0) std::cout << "SAVE " << j << "\t: 0x\t"; 00117 // std::cout << str; 00118 // if(j%8 == 7) std::cout << " "; 00119 // if(j%64 == 63) std::cout << std::endl; 00120 // } 00121 // std::cout << std::endl; 00122 // std::cout << std::endl; 00123 // } 00124 // 00125 // if(1) 00126 // { 00127 // char str[5]; 00128 // for(unsigned int j=0;j<data.length();++j) 00129 // { 00130 // sprintf(str,"%2.2x",((unsigned int)data[j]) & ((unsigned int)(0x0FF))); 00131 // 00132 // if(j%64 == 0) std::cout << "SAVE " << j << "\t: 0x\t"; 00133 // std::cout << str; 00134 // if(j%8 == 7) std::cout << " "; 00135 // if(j%64 == 63) std::cout << std::endl; 00136 // } 00137 // std::cout << std::endl; 00138 // std::cout << std::endl; 00139 // } 00140 00141 if(maxFileSize_ > 0) 00142 { 00143 long length = outFile_.tellp(); 00144 if(length >= maxFileSize_ / 1000) 00145 { 00146 closeFile(); 00147 ++currentSubRunNumber_; 00148 openFile(currentRunNumber_); 00149 } 00150 } 00151 00152 writePacketHeader(data); // write start of packet header 00153 outFile_.write((char*)&data[0], data.length()); 00154 writePacketFooter(data); // write start of packet footer 00155 } 00156 00157 //======================================================================================================================== 00158 bool RawDataSaverConsumerBase::workLoopThread(toolbox::task::WorkLoop* workLoop) 00159 { 00160 //__CFG_COUT__ << DataProcessor::processorUID_ << " running, because workloop: " 00161 //<< WorkLoop::continueWorkLoop_ << std::endl; 00162 fastRead(); 00163 return WorkLoop::continueWorkLoop_; 00164 } 00165 00166 //======================================================================================================================== 00167 void RawDataSaverConsumerBase::fastRead(void) 00168 { 00169 //__CFG_COUT__ << processorUID_ << " running!" << std::endl; 00170 if(DataConsumer::read(dataP_, headerP_) < 0) 00171 { 00172 usleep(100); 00173 return; 00174 } 00175 __CFG_COUTV__(dataP_->length()); 00176 std::string& buffer = *dataP_; 00177 00178 //__CFG_COUT__ << "Reading from buffer length " << buffer.length() << " bytes!" << 00179 //__E__; 00180 //__CFG_COUT__ << "Buffer Data: " << 00181 // BinaryStringMacros::binaryTo8ByteHexString(buffer) << __E__; 00182 00183 save(*dataP_); 00184 DataConsumer::setReadSubBuffer<std::string, std::map<std::string, std::string> >(); 00185 } 00186 00187 //======================================================================================================================== 00188 void RawDataSaverConsumerBase::slowRead(void) 00189 { 00190 //__CFG_COUT__ << processorUID_ << " running!" << std::endl; 00191 // This is making a copy!!! 00192 if(DataConsumer::read(data_, header_) < 0) 00193 { 00194 usleep(1000); 00195 return; 00196 } 00197 save(data_); 00198 }