otsdaq  v2_00_00
RawDataSaverConsumerBase.cc
1 #include "otsdaq-core/DataManager/RawDataSaverConsumerBase.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 //#include "otsdaq-core/Macros/CoutHeaderMacros.h"
4 
5 #include <iostream>
6 #include <cassert>
7 #include <unistd.h>
8 //#include <string.h> //memcpy
9 #include <sstream>
10 #include <fstream>
11 
12 using namespace ots;
13 
14 
15 //========================================================================================================================
16 RawDataSaverConsumerBase::RawDataSaverConsumerBase(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
17 : WorkLoop (processorUID)
18 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, HighConsumerPriority)
19 , Configurable (theXDAQContextConfigTree, configurationPath)
20 , filePath_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("FilePath").getValue<std::string>())
21 , fileRadix_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("RadixFileName").getValue<std::string>())
22 , maxFileSize_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("MaxFileSize").getValue<long>()*1000000)//Instead of 2^6=1048576
23 , currentSubRunNumber_(0)
24 
25 {
26 
27  //FILE *fp = fopen( "/home/otsdaq/tsave.txt","w");
28  //if(fp)fclose(fp);
29 }
30 
31 //========================================================================================================================
32 RawDataSaverConsumerBase::~RawDataSaverConsumerBase(void)
33 {}
34 
35 //========================================================================================================================
36 void RawDataSaverConsumerBase::startProcessingData(std::string runNumber)
37 {
38  openFile(runNumber);
39  DataConsumer::startProcessingData(runNumber);
40 }
41 
42 //========================================================================================================================
43 void RawDataSaverConsumerBase::stopProcessingData(void)
44 {
45  DataConsumer::stopProcessingData();
46  closeFile();
47 }
48 
49 //========================================================================================================================
50 void RawDataSaverConsumerBase::openFile(std::string runNumber)
51 {
52  currentRunNumber_ = runNumber;
53 // std::string fileName = "Run" + runNumber + "_" + processorUID_ + "_Raw.dat";
54  std::stringstream fileName;
55  fileName << filePath_ << "/" << fileRadix_ << "_Run" << runNumber;
56  //if split file is there then subrunnumber must be set!
57  if(maxFileSize_ > 0)
58  fileName << "_" << currentSubRunNumber_;
59  fileName << "_Raw.dat";
60  std::cout << __COUT_HDR_FL__ << "Saving file: " << fileName.str() << std::endl;
61  outFile_.open(fileName.str().c_str(), std::ios::out | std::ios::binary);
62  if(!outFile_.is_open())
63  {
64  __SS__ << "Can't open file " << fileName.str() << std::endl;
65  __COUT_ERR__ << "\n" << ss.str();
66  throw std::runtime_error(ss.str());
67  }
68 
69  writeHeader(); //write start of file header
70 }
71 
72 //========================================================================================================================
73 void RawDataSaverConsumerBase::closeFile(void)
74 {
75  if(outFile_.is_open())
76  {
77  writeFooter(); //write end of file footer
78  outFile_.close();
79  }
80 }
81 
82 //========================================================================================================================
83 void RawDataSaverConsumerBase::save(const std::string& data)
84 {
85 
86  std::ofstream output;
87 
88 
89 // std::string outputPath = "/home/otsdaq/tsave.txt";
90 // if(0)
91 // {
92 // output.open(outputPath, std::ios::out | std::ios::app | std::ios::binary);
93 // output << data;
94 // }
95 // else
96 // {
97 // output.open(outputPath, std::ios::out | std::ios::app);
98 // output << data;
99 //
100 // char str[5];
101 // for(unsigned int j=0;j<data.length();++j)
102 // {
103 // sprintf(str,"%2.2x",((unsigned int)data[j]) & ((unsigned int)(0x0FF)));
104 //
105 // if(j%64 == 0) std::cout << "SAVE " << j << "\t: 0x\t";
106 // std::cout << str;
107 // if(j%8 == 7) std::cout << " ";
108 // if(j%64 == 63) std::cout << std::endl;
109 // }
110 // std::cout << std::endl;
111 // std::cout << std::endl;
112 // }
113 //
114 // if(1)
115 // {
116 // char str[5];
117 // for(unsigned int j=0;j<data.length();++j)
118 // {
119 // sprintf(str,"%2.2x",((unsigned int)data[j]) & ((unsigned int)(0x0FF)));
120 //
121 // if(j%64 == 0) std::cout << "SAVE " << j << "\t: 0x\t";
122 // std::cout << str;
123 // if(j%8 == 7) std::cout << " ";
124 // if(j%64 == 63) std::cout << std::endl;
125 // }
126 // std::cout << std::endl;
127 // std::cout << std::endl;
128 // }
129 
130 
131 
132  if(maxFileSize_ > 0)
133  {
134  long length = outFile_.tellp();
135  if(length >= maxFileSize_)
136  {
137  closeFile();
138  ++currentSubRunNumber_;
139  openFile(currentRunNumber_);
140  }
141  }
142 
143  writePacketHeader(data); //write start of packet header
144  outFile_.write( (char*)&data[0], data.length());
145  writePacketFooter(data); //write start of packet footer
146 }
147 
148 //========================================================================================================================
149 bool RawDataSaverConsumerBase::workLoopThread(toolbox::task::WorkLoop* workLoop)
150 {
151  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << DataProcessor::processorUID_ << " running, because workloop: " << WorkLoop::continueWorkLoop_ << std::endl;
152  fastRead();
153  return WorkLoop::continueWorkLoop_;
154 }
155 
156 //========================================================================================================================
157 void RawDataSaverConsumerBase::fastRead(void)
158 {
159  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " running!" << std::endl;
160  if(DataConsumer::read(dataP_, headerP_) < 0)
161  {
162  usleep(100);
163  return;
164  }
165  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " Buffer: " << buffer << std::endl;
166  //std::cout << __COUT_HDR_FL__ << dataP_->length() << std::endl;
167  save(*dataP_);
168  DataConsumer::setReadSubBuffer<std::string, std::map<std::string, std::string>>();
169 }
170 
171 //========================================================================================================================
172 void RawDataSaverConsumerBase::slowRead(void)
173 {
174  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " running!" << std::endl;
175  //This is making a copy!!!
176  if(DataConsumer::read(data_, header_) < 0)
177  {
178  usleep(1000);
179  return;
180  }
181  save(data_);
182 }