00001 #include "otsdaq-core/EventBuilder/EventDataSaver.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00004 #include "otsdaq-core/EventBuilder/Event.h"
00005
00006 #include <iostream>
00007 #include <cassert>
00008
00009 #include <TFile.h>
00010 #include <TTree.h>
00011 #include <TBufferFile.h>
00012
00013 using namespace ots;
00014
00015
00016 EventDataSaver::EventDataSaver(std::string supervisorApplicationUID, std::string bufferUID, std::string processorUID, ConsumerPriority priority)
00017 : WorkLoop (processorUID)
00018 , DataConsumer (supervisorApplicationUID, bufferUID, processorUID, HighConsumerPriority)
00019 , outFile_ (0)
00020 , anEvent_ (new Event())
00021 , outTree_ (0)
00022 {}
00023
00024
00025 EventDataSaver::~EventDataSaver(void)
00026 {
00027 std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Destructor" << std::endl;
00028 closeFile();
00029 delete anEvent_;
00030 }
00031
00032
00033 void EventDataSaver::openFile(std::string fileName)
00034 {
00035
00036 outFile_ = TFile::Open((fileName + "_Event.root").c_str(), "NEW");
00037 if(!outFile_->IsOpen())
00038 {
00039 std::cout << __COUT_HDR_FL__ << "Can't open file " << fileName + "_Event.root" << std::endl;
00040 assert(0);
00041 }
00042 outFile_->cd();
00043 outTree_ = new TTree("EventTree","Event Tree");
00044 outTree_-> SetAutoSave(10000);
00045 outTree_->Branch("EventBranch", anEvent_->GetName(), &anEvent_, 16000, 0);
00046 writeHeader();
00047 }
00048
00049
00050 void EventDataSaver::closeFile(void)
00051 {
00052 std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "Saving file!" << std::endl;
00053 if(outFile_ != 0 && outFile_->IsOpen())
00054 {
00055
00056
00057
00058 outFile_->Write();
00059 outFile_->Close();
00060 outFile_ = 0;
00061 outTree_ = 0;
00062 }
00063 }
00064
00065
00066 void EventDataSaver::save(std::string& data)
00067 {
00068
00069
00070
00071
00072
00073
00074 if(outTree_ != 0) outTree_->Fill();
00075 }
00076
00077
00078 void EventDataSaver::writeHeader(void)
00079 {
00080
00081
00082
00083
00084 }
00085
00086
00087
00088 bool EventDataSaver::workLoopThread(toolbox::task::WorkLoop* workLoop)
00089 {
00090
00091 std::string buffer;
00092
00093 if(read<ots::Event, std::map<std::string,std::string>>(*anEvent_) < 0)
00094 usleep(100000);
00095 else
00096 {
00097
00098
00099
00100 save(buffer);
00101 }
00102 return true;
00103 }
00104