00001 #include "otsdaq-core/EventBuilder/EventDataSaver.h" 00002 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00003 #include "otsdaq-core/Macros/CoutMacros.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 //anEvent_ = new Event();//FIXME maybe, hopefully is deleted when the file is closed 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");//deleted when the file closes 00044 outTree_-> SetAutoSave(10000);//Saves every 1 Mbyte 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 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl; 00056 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " Saving event" << std::endl; 00057 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl; 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 //TBufferFile eventBuffer(TBuffer::kRead, data.length(), &(data.at(0))); 00069 //anEvent_->Streamer(eventBuffer); 00070 00071 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl; 00072 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " Filling event" << std::endl; 00073 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl; 00074 if(outTree_ != 0) outTree_->Fill(); 00075 } 00076 00077 //======================================================================================================================== 00078 void EventDataSaver::writeHeader(void) 00079 { 00080 // unsigned char fileFormatVersion = 1; 00081 // unsigned char dataFormatVersion = 1; 00082 // outFile_.write( (char*)&fileFormatVersion, sizeof(char)); 00083 // outFile_.write( (char*)&dataFormatVersion, sizeof(char)); 00084 } 00085 00086 00087 //======================================================================================================================== 00088 bool EventDataSaver::workLoopThread(toolbox::task::WorkLoop* workLoop) 00089 { 00090 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " running!" << std::endl; 00091 std::string buffer; 00092 //unsigned long block; 00093 if(read<ots::Event, std::map<std::string,std::string>>(*anEvent_) < 0) 00094 usleep(100000); 00095 else 00096 { 00097 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl; 00098 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << processorUID_ << " BCO Number: " << anEvent_->getBCONumber() << std::endl; 00099 //std::cout << __COUT_HDR_FL__ << __PRETTY_FUNCTION__ << "*************************************" << std::endl; 00100 save(buffer); 00101 } 00102 return true; 00103 } 00104