File indexing completed on 2024-11-16 09:02:34
0001 #ifndef TREEHANDLER_HH
0002 #define TREEHANDLER_HH
0003
0004 #include <iostream>
0005 #include <vector>
0006 #include <string>
0007
0008 #include "TFile.h"
0009 #include "TTree.h"
0010 #include "external/ExRootAnalysis/ExRootTreeReader.h"
0011
0012 using namespace std;
0013
0014 class TreeHandler {
0015 static TreeHandler *instance;
0016
0017 TFile* _file = nullptr;
0018 TTree* _tree = nullptr;
0019
0020
0021 TreeHandler(std::string filename, std::string treename) {
0022 _filename = filename;
0023 _treename = treename;
0024 }
0025
0026 public:
0027
0028 static TreeHandler *getInstance(std::string filename="", std::string treename="") {
0029 if (!instance)
0030 instance = new TreeHandler(filename, treename);
0031 return instance;
0032 }
0033
0034 TFile* getFile() {
0035 return this -> _file;
0036 }
0037
0038 TTree* getTree() {
0039 return this -> _tree;
0040 }
0041
0042 void initialize() {
0043 _file = new TFile(_filename.c_str(), "RECREATE");
0044 _file->cd();
0045 _tree = new TTree(_treename.c_str(), "");
0046 }
0047
0048 void execute() {
0049 if (_tree == nullptr)
0050 return;
0051 _tree->Fill();
0052 }
0053
0054 void finalize() {
0055 if (_tree == nullptr)
0056 return;
0057 if (_file == nullptr)
0058 return;
0059 _file->cd();
0060 _tree->Write();
0061 _file->Close();
0062 }
0063
0064 private:
0065 std::string _filename;
0066 std::string _treename;
0067
0068 };
0069
0070 #endif