otsdaq  v1_01_03
 All Classes Namespaces Functions
EventDataSaver.cc
1 #include "otsdaq-core/EventBuilder/EventDataSaver.h"
2 #include "otsdaq-core/MessageFacility/MessageFacility.h"
3 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
4 #include "otsdaq-core/EventBuilder/Event.h"
5 
6 #include <iostream>
7 #include <cassert>
8 
9 #include <TFile.h>
10 #include <TTree.h>
11 #include <TBufferFile.h>
12 
13 using namespace ots;
14 
15 //========================================================================================================================
16 EventDataSaver::EventDataSaver(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, ConsumerPriority priority)
17 : WorkLoop (processorUID)
18 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, HighConsumerPriority)
19 , outFile_ (0)
20 , anEvent_ (new Event())
21 , outTree_ (0)
22 {}
23 
24 //========================================================================================================================
25 EventDataSaver::~EventDataSaver(void)
26 {
27  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Destructor" << std::endl;
28  closeFile();
29  delete anEvent_;
30 }
31 
32 //========================================================================================================================
33 void EventDataSaver::openFile(std::string fileName)
34 {
35  //anEvent_ = new Event();//FIXME maybe, hopefully is deleted when the file is closed
36  outFile_ = TFile::Open((fileName + "_Event.root").c_str(), "NEW");
37  if(!outFile_->IsOpen())
38  {
39  std::cout << __COUT_HDR_FL__ << "Can't open file " << fileName + "_Event.root" << std::endl;
40  assert(0);
41  }
42  outFile_->cd();
43  outTree_ = new TTree("EventTree","Event Tree");//deleted when the file closes
44  outTree_-> SetAutoSave(10000);//Saves every 1 Mbyte
45  outTree_->Branch("EventBranch", anEvent_->GetName(), &anEvent_, 16000, 0);
46  writeHeader();
47 }
48 
49 //========================================================================================================================
50 void EventDataSaver::closeFile(void)
51 {
52  std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Saving file!" << std::endl;
53  if(outFile_ != 0 && outFile_->IsOpen())
54  {
55  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl;
56  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " Saving event" << std::endl;
57  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl;
58  outFile_->Write();
59  outFile_->Close();
60  outFile_ = 0;
61  outTree_ = 0;
62  }
63 }
64 
65 //========================================================================================================================
66 void EventDataSaver::save(std::string& data)
67 {
68  //TBufferFile eventBuffer(TBuffer::kRead, data.length(), &(data.at(0)));
69  //anEvent_->Streamer(eventBuffer);
70 
71  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl;
72  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " Filling event" << std::endl;
73  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl;
74  if(outTree_ != 0) outTree_->Fill();
75 }
76 
77 //========================================================================================================================
78 void EventDataSaver::writeHeader(void)
79 {
80  // unsigned char fileFormatVersion = 1;
81  // unsigned char dataFormatVersion = 1;
82  // outFile_.write( (char*)&fileFormatVersion, sizeof(char));
83  // outFile_.write( (char*)&dataFormatVersion, sizeof(char));
84 }
85 
86 
87 //========================================================================================================================
88 bool EventDataSaver::workLoopThread(toolbox::task::WorkLoop* workLoop)
89 {
90  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " running!" << std::endl;
91  std::string buffer;
92  //unsigned long block;
93  if(read<ots::Event, std::map<std::string,std::string>>(*anEvent_) < 0)
94  usleep(100000);
95  else
96  {
97  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl;
98  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " BCO Number: " << anEvent_->getBCONumber() << std::endl;
99  //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl;
100  save(buffer);
101  }
102  return true;
103 }
104