forked from h2gglobe/h2gglobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeContainer.cc
176 lines (150 loc) · 5.52 KB
/
TreeContainer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "TreeContainer.h"
#include <utility>
#include <iostream>
#define TCDEBUG 0
TreeContainer::TreeContainer() {}
TreeContainer::TreeContainer(int setTo, std::string setName, std::string setDirNam) {
setTreeVal(setTo);
setTreeNam(setName);
setDirName(setDirNam);
}
TreeContainer::~TreeContainer()
{
}
void TreeContainer::setTreeVal(int setTo) {
treeVal = setTo;
}
void TreeContainer::setDirName(std::string setName) {
dirName = setName;
};
void TreeContainer::setTreeNam(std::string setName) {
treeNam = setName;
tr_ = new TTree(setName.c_str(),setName.c_str()); tr_->SetDirectory(0);
if (TCDEBUG) std::cout << "TreeContainer -- Made new Tree Called " << treeNam << std::endl;
}
int TreeContainer::getTreeVal() {
return treeVal;
}
void TreeContainer::AddTreeBranch(std::string name,int type){
if (type==0){ // Int_t
int_branches.insert(std::pair<std::string,int> (name,-999) );
tr_->Branch(name.c_str(),&(int_branches[name]),Form("%s/Int_t",name.c_str()));
if (TCDEBUG) std::cout << "TreeContainer -- Creating Int Branch " << name << std::endl;
}
else if (type==1){ // Unsigned Int_t
uint_branches.insert(std::pair<std::string,unsigned int> (name,999.) );
tr_->Branch(name.c_str(),&(uint_branches[name]),Form("%s/UInt_t",name.c_str()));
if (TCDEBUG) std::cout << "TreeContainer -- Creating UInt Branch " << name << std::endl;
}
else if (type==2){ // Float_t
float_branches.insert(std::pair<std::string,float> (name,-999.) );
tr_->Branch(name.c_str(),&(float_branches[name]),Form("%s/Float_t",name.c_str()));
if (TCDEBUG) std::cout << "TreeContainer -- Creating Float Branch " << name << std::endl;
}
else if (type==3){ // Double_t
double_branches.insert(std::pair<std::string,double> (name,-999.) );
tr_->Branch(name.c_str(),&(double_branches[name]),Form("%s/Double_t",name.c_str()));
if (TCDEBUG) std::cout << "TreeContainer -- Creating Double Branch " << name << std::endl;
}
else if (type==4) { // std::string
string_branches.insert(std::pair<std::string, std::string> (name, ""));
//tr_->Branch(name.c_str(), &(string_branches[name]),Form("%s/C",name.c_str()));
tr_->Branch(name.c_str(), "std::string", &(string_branches[name]));
if (TCDEBUG) std::cout << "TreeContainer -- Creating String Branch " << name << std::endl;
}
else if (type==5){
bool_branches.insert(std::pair<std::string,bool> (name,false) );
tr_->Branch(name.c_str(),&(bool_branches[name]),Form("%s/Bool_t",name.c_str()));
if (TCDEBUG) std::cout << "TreeContainer -- Creating Bool Branch " << name << std::endl;
} else {
std::cerr << "TreeContainer -- No Type " << type << std::endl;
}
}
void TreeContainer::FillFloat(std::string name, float x){
std::map<std::string,float>::iterator it = float_branches.find(name);
if (it!=float_branches.end()){
(*it).second = x;
} else {
std::map<std::string,double>::iterator jt = double_branches.find(name);
if (jt!=double_branches.end()) {
(*jt).second = x;
}
}
}
void TreeContainer::FillInt(std::string name, int x){
std::map<std::string,int>::iterator it = int_branches.find(name);
if (it!=int_branches.end()){
(*it).second = x;
} else {
std::cerr << "TreeContainer -- No Int Tree " << name << std::endl;
}
}
void TreeContainer::FillUInt(std::string name, unsigned int x){
std::map<std::string,unsigned int>::iterator it = uint_branches.find(name);
if (it!=uint_branches.end()){
(*it).second = x;
} else {
std::cerr << "TreeContainer -- No UInt Tree " << name << std::endl;
}
}
void TreeContainer::FillDouble(std::string name, double x){
std::map<std::string,double>::iterator it = double_branches.find(name);
if (it!=double_branches.end()){
(*it).second = x;
} else {
std::map<std::string,float>::iterator jt = float_branches.find(name);
if (jt!=float_branches.end()){
(*jt).second = x;
}
}
}
void TreeContainer::FillString(std::string name, std::string x) {
std::map<std::string,std::string>::iterator it = string_branches.find(name);
if (it!=string_branches.end()){
(*it).second = x;
} else {
std::cerr << "TreeContainer -- No Double Tree " << name << std::endl;
}
}
void TreeContainer::FillBool(std::string name, bool x){
std::map<std::string,bool>::iterator it = bool_branches.find(name);
if (it!=bool_branches.end()){
(*it).second = x;
} else {
std::cerr << "TreeeContainer -- No Bool Tree " << name << std::endl;
}
}
void TreeContainer::FillTree(){
tr_->Fill();
resetDefaults();
}
void TreeContainer::Save(TFile* f) {
if (dirName != "") {
TDirectory* dir = (TDirectory*)f->Get(dirName.c_str());
if (!dir) dir = f->mkdir(dirName.c_str(), dirName.c_str());
dir->cd();
tr_->Write(0,TObject::kWriteDelete);
delete tr_;
f->cd();
} else {
tr_->Write(0,TObject::kWriteDelete);
delete tr_;
}
}
void TreeContainer::resetDefaults(){
for (std::map<std::string,int>::iterator it = int_branches.begin();it!=int_branches.end() ;it++){
(*it).second=-999;
}
for (std::map<std::string,unsigned int>::iterator it = uint_branches.begin();it!=uint_branches.end() ;it++){
(*it).second=999;
}
for (std::map<std::string,float>::iterator it = float_branches.begin();it!=float_branches.end() ;it++){
(*it).second=-999.;
}
for (std::map<std::string,double>::iterator it = double_branches.begin();it!=double_branches.end() ;it++){
(*it).second=-999.;
}
for (std::map<std::string,bool>::iterator it = bool_branches.begin();it!=bool_branches.end() ;it++){
(*it).second=-999.;
}
}