EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TreeHandler.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TreeHandler.h
1 #ifndef TREEHANDLER_HH
2 #define TREEHANDLER_HH
3 
4 #include <iostream>
5 #include <vector>
6 #include <string>
7 
8 #include "TFile.h"
9 #include "TTree.h"
10 #include "external/ExRootAnalysis/ExRootTreeReader.h"
11 
12 using namespace std;
13 
14 class TreeHandler {
16 
17  TFile* _file = nullptr;
18  TTree* _tree = nullptr;
19 
20  // Private constructor so that no objects can be created.
21  TreeHandler(std::string filename, std::string treename) {
22  _filename = filename;
23  _treename = treename;
24  }
25 
26  public:
27 
28  static TreeHandler *getInstance(std::string filename="", std::string treename="") {
29  if (!instance)
30  instance = new TreeHandler(filename, treename);
31  return instance;
32  }
33 
34  TFile* getFile() {
35  return this -> _file;
36  }
37 
38  TTree* getTree() {
39  return this -> _tree;
40  }
41 
42  void initialize() {
43  _file = new TFile(_filename.c_str(), "RECREATE");
44  _file->cd();
45  _tree = new TTree(_treename.c_str(), "");
46  }
47 
48  void execute() {
49  if (_tree == nullptr)
50  return;
51  _tree->Fill();
52  }
53 
54  void finalize() {
55  if (_tree == nullptr)
56  return;
57  if (_file == nullptr)
58  return;
59  _file->cd();
60  _tree->Write();
61  _file->Close();
62  }
63 
64  private:
65  std::string _filename;
66  std::string _treename;
67 
68 };
69 
70 #endif