-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiller.h
More file actions
322 lines (268 loc) · 5.48 KB
/
Filler.h
File metadata and controls
322 lines (268 loc) · 5.48 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#ifndef FILLER_H
#define FILLER_H
//Standard includes
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
//Root includes
#include "TROOT.h"
#include "TSystem.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TNtuple.h"
#include "TH1.h"
#include "TF1.h"
//Local includes
#include "Binner.h"
//Definitions
using namespace std;
class Filler: public Binner
{
private:
StrFunction<float>* fill_strfunc;
public:
string fill_name;
string fill_expr;
void Free()
{
//Free memory that would have been allocated previously
for(i = 0; i < (int)var_strfuncs.size(); i++)
{
if(var_strfuncs[i] != 0x0)
{
var_strfuncs[i]->Free();
delete var_strfuncs[i];
var_strfuncs[i] = 0x0;
}
}
var_strfuncs.clear();
if(fill_strfunc != 0x0)
{
fill_strfunc->Free();
delete fill_strfunc;
fill_strfunc = 0x0;
}
if(branch_vals != 0x0)
{
delete branch_vals;
branch_vals = 0x0;
}
}
string Write(int k = -1, string f = "_%s_%+08.3f_%+08.3f")
{
if(k > -1)Index(k);
char temp[MAX_CHAR_LEN];
string str = fill_name;
for(i = 0; i < NumBinners(); i++)
{
j = indexes[i];
sprintf(temp, f.c_str(), var_binners[i].name.c_str(), var_binners[i].min(j), var_binners[i].max(j));
for(j = 0; j < MAX_CHAR_LEN; j++)
{
if(temp[j] == '+' or temp[j] == '.')
{
temp[j] = 'p';
}
else if(temp[j] == '-')
{
temp[j] = 'm';
}
}
str += temp;
}
return str;
}
void PrintVarVals()
{
if(fill_strfunc == 0x0)
{
cout << "Fill StrFunction was null during call, skipping" << endl;
}
else
{
cout << "Fill value \"" << fill_name << "\": " << fill_strfunc->Evaluate() << endl;
}
for(i = 0; i < var_binners.size(); i++)
{
if(var_strfuncs[i] == 0x0)
{
cout << "var_binners[" << i << "] was null, skipping" << endl;
continue;
}
cout << var_binners[i].name << ": " << var_strfuncs[i]->Evaluate() << endl;
}
}
void SetUp()
{
Free();
int k;
bool b = false;
vector<string> vars = {};
branch_names.clear();
for(i = 0; i < NumBinners(); i++)
{
vars = GetExprVars(var_binners[i].expr);
for(j = 0; j < vars.size(); j++)
{
b = true;
for(k = 0; k < branch_names.size(); k++)
{
if(branch_names[k] == vars[j])
{
b = false;
break;
}
}
if(b)
{
branch_names.push_back(vars[j]);
}
}
vars.clear();
}
vars = GetExprVars(fill_expr);
for(j = 0; j < vars.size(); j++)
{
b = true;
for(k = 0; k < branch_names.size(); k++)
{
if(branch_names[k] == vars[j])
{
b = false;
break;
}
}
if(b)
{
branch_names.push_back(vars[j]);
}
}
vars.clear();
branch_vals = new float[branch_names.size()];
}
void SetNtuple(TNtuple* ntuple)
{
nt = ntuple;
if(nt == 0x0)
{
cout << "In Binner::SetNtuple(TNtuple* ntuple)" << endl;
cout << "ntuple was null, exiting" << endl;
return;
}
nt->ResetBranchAddresses();
nt->SetBranchStatus("*", 0);
for(i = 0; i < branch_names.size(); i++)
{
if(nt->GetBranch(branch_names[i].c_str()) == 0x0)
{
cout << "In Binner::SetNtuple(TNtuple* ntuple)" << endl;
cout << "branch \"" << branch_names[i] << "\" not found in ntuple, continuing" << endl;
continue;
}
nt->SetBranchStatus(branch_names[i].c_str(), 1);
nt->SetBranchAddress(branch_names[i].c_str(), &(branch_vals[i]));
}
for(i = 0; i < NumBinners(); i++)
{
var_strfuncs.push_back(new StrFunction<float>(var_binners[i].expr, cf->common_funcs, nt));
}
fill_strfunc = new StrFunction<float>(fill_expr, cf->common_funcs, nt);
}
float GetFillVal()
{
if(fill_strfunc == 0x0)
{
cout << "In Filler::GetFillVal, fill_strfunc was null at call" << endl;
return 0.0;
}
return fill_strfunc->Evaluate();
}
void TryFill(TH1* h, int k=-1)
{
if(h == 0x0)
{
cout << "In Filler::TryFill, histogram is null" << endl;
cout << endl;
return;
}
if(fill_strfunc == 0x0)
{
cout << "In Filler::TryFill, fill_strfunc is null" << endl;
cout << endl;
return;
}
if(Check(k))h->Fill(fill_strfunc->Evaluate());
}
Filler()
{
cf = new CommonFunctions<float>();
nt = 0x0;
i = 0; j = 0;
f = 0.0;
b = false;
branch_vals = 0x0;
fill_strfunc = 0x0;
}
Filler(string config_filename)
{
cout << "Reading Filler from config file " << config_filename << endl;
cf = new CommonFunctions<float>();
nt = 0x0;
i = 0; j = 0;
f = 0.0;
b = false;
branch_vals = 0x0;
fill_strfunc = 0x0;
string config_str;
ifstream config(config_filename);
config >> config_str; //First line is reserved for histogram
if(config.eof())return;
config >> config_str; //Second line contains information about the fill quantity
if(config.eof())return;
cout << "Name: ";
fill_name = "";
while(i < config_str.length())
{
if(config_str.substr(i, 1) == ":")
{
break;
}
fill_name += config_str.substr(i,1);
i++;
}
cout << fill_name << endl;
i++;
cout << "Expression: ";
fill_expr = "";
while(i < config_str.length())
{
if(config_str.substr(i, 1) == ":")
{
break;
}
fill_expr += config_str.substr(i,1);
i++;
}
cout << fill_expr << endl;
cout << endl;
while(true)
{
config >> config_str;
if(config.eof())break;
AddBinner(VarBinner(config_str));
}
SetUp();
}
~Filler()
{
Free();
if(cf != 0x0)
{
delete cf;
cf = 0x0;
}
}
};
#endif