otsdaq  v1_01_03
 All Classes Namespaces Functions
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 
11 using namespace ots;
12 
13 
14 //========================================================================================================================
15 RawDataSaverConsumerBase::RawDataSaverConsumerBase(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, const ConfigurationTree& theXDAQContextConfigTree, const std::string& configurationPath)
16 : WorkLoop (processorUID)
17 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, HighConsumerPriority)
18 , Configurable (theXDAQContextConfigTree, configurationPath)
19 , filePath_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("FilePath").getValue<std::string>())
20 , fileRadix_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("RadixFileName").getValue<std::string>())
21 , maxFileSize_ (theXDAQContextConfigTree.getNode(configurationPath).getNode("MaxFileSize").getValue<long>()*1000000)//Instead of 2^6=1048576
22 , currentSubRunNumber_(0)
23 
24 {
25 }
26 
27 //========================================================================================================================
28 RawDataSaverConsumerBase::~RawDataSaverConsumerBase(void)
29 {}
30 
31 //========================================================================================================================
32 void RawDataSaverConsumerBase::startProcessingData(std::string runNumber)
33 {
34  openFile(runNumber);
35  DataConsumer::startProcessingData(runNumber);
36 }
37 
38 //========================================================================================================================
39 void RawDataSaverConsumerBase::stopProcessingData(void)
40 {
41  DataConsumer::stopProcessingData();
42  closeFile();
43 }
44 
45 //========================================================================================================================
46 void RawDataSaverConsumerBase::openFile(std::string runNumber)
47 {
48  currentRunNumber_ = runNumber;
49 // std::string fileName = "Run" + runNumber + "_" + processorUID_ + "_Raw.dat";
50  std::stringstream fileName;
51  fileName << filePath_ << "/" << fileRadix_ << "_Run" << runNumber;
52  //if split file is there then subrunnumber must be set!
53  if(maxFileSize_ > 0)
54  fileName << "_" << currentSubRunNumber_;
55  fileName << "_Raw.dat";
56  std::cout << __COUT_HDR_FL__ << "Saving file: " << fileName.str() << std::endl;
57  outFile_.open(fileName.str().c_str(), std::ios::out | std::ios::binary);
58  if(!outFile_.is_open())
59  {
60  __SS__ << "Can't open file " << fileName.str() << std::endl;
61  __MOUT_ERR__ << "\n" << ss.str();
62  throw std::runtime_error(ss.str());
63  }
64 
65  writeHeader(); //write start of file header
66 }
67 
68 //========================================================================================================================
69 void RawDataSaverConsumerBase::closeFile(void)
70 {
71  if(outFile_.is_open())
72  {
73  writeFooter(); //write end of file footer
74  outFile_.close();
75  }
76 }
77 
78 //========================================================================================================================
79 void RawDataSaverConsumerBase::save(const std::string& data)
80 {
81  if(maxFileSize_ > 0)
82  {
83  long length = outFile_.tellp();
84  if(length >= maxFileSize_)
85  {
86  closeFile();
87  ++currentSubRunNumber_;
88  openFile(currentRunNumber_);
89  }
90  }
91 
92  writePacketHeader(data); //write start of packet header
93  outFile_.write( (char*)&data[0], data.length());
94  writePacketFooter(data); //write start of packet footer
95 }
96 
97 //========================================================================================================================
98 bool RawDataSaverConsumerBase::workLoopThread(toolbox::task::WorkLoop* workLoop)
99 {
100  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << DataProcessor::processorUID_ << " running, because workloop: " << WorkLoop::continueWorkLoop_ << std::endl;
101  fastRead();
102  return WorkLoop::continueWorkLoop_;
103 }
104 
105 //========================================================================================================================
106 void RawDataSaverConsumerBase::fastRead(void)
107 {
108  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " running!" << std::endl;
109  if(DataConsumer::read(dataP_, headerP_) < 0)
110  {
111  usleep(100);
112  return;
113  }
114  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " Buffer: " << buffer << std::endl;
115  //std::cout << __COUT_HDR_FL__ << dataP_->length() << std::endl;
116  save(*dataP_);
117  DataConsumer::setReadSubBuffer<std::string, std::map<std::string, std::string>>();
118 }
119 
120 //========================================================================================================================
121 void RawDataSaverConsumerBase::slowRead(void)
122 {
123  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " running!" << std::endl;
124  //This is making a copy!!!
125  if(DataConsumer::read(data_, header_) < 0)
126  {
127  usleep(1000);
128  return;
129  }
130  save(data_);
131 }