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