-
Notifications
You must be signed in to change notification settings - Fork 6
/
TTreeProjection.cc
445 lines (421 loc) · 16 KB
/
TTreeProjection.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* @file TTreeProjection.cc
* @brief Tree projection definitions
*
* @date Created : 2014-03-05 10:15:05 JST
* Last Modified : 2023-08-16 16:19:00 JST (fendo)
* @author Shinsuke OTA <ota@cns.s.u-tokyo.ac.jp>
*
* (C) 2014 Shinsuke OTA
*/
#include "TTreeProjection.h"
#include <TObjArray.h>
#include <yaml-cpp/yaml.h>
#include <TSystem.h>
#include <TObjString.h>
#include <TTreeProjGroup.h>
#include <TH1FTreeProj.h>
#include <TH2FTreeProj.h>
#include <TH3FTreeProj.h>
#include <TAxisTreeProj.h>
#include <TTree.h>
#include <fstream>
#include <sstream>
using art::TTreeProjection;
ClassImp(TTreeProjection)
// definition of constant strings for the key of each node
namespace {
const char* kNodeKeyGroup = "group";
const char* kNodeKeyInclude = "include";
const char* kNodeKeyAlias = "alias";
const char* kNodeKeyReplace = "replace";
const char* kNodeKeyCut = "cut";
const char* kNodeKeyName = "name";
const char* kNodeKeyTitle = "title";
const char* kNodeKeyType = "type";
const char* kNodeKeyContents = "contents";
const char* kNodeKeyClone = "clone";
const char* kNodeKeySuffix = "suffix";
const char* kNodeKeyX = "x";
const char* kNodeKeyY = "y";
const char* kNodeKeyZ = "z";
const char* kNodeKeyAsync = "async";
}
TTreeProjection::TTreeProjection()
: fProjGroups(NULL), fIncludes(NULL), fAliases(NULL)
{
fProjGroups = new TObjArray;
fIncludes = new TObjArray;
fAliases = new TObjArray;
}
TTreeProjection::~TTreeProjection()
{
fProjGroups->Delete();
delete fProjGroups;
fIncludes->Delete();
delete fIncludes;
fAliases->Delete();
delete fAliases;
}
TTreeProjection::TTreeProjection(const TTreeProjection& rhs)
{
// not implemented
MayNotUse("TTreeProjection");
}
TTreeProjection& TTreeProjection::operator=(const TTreeProjection& rhs)
{
// not implemented
MayNotUse("operator=");
if (this != &rhs) {
}
return *this;
}
Bool_t TTreeProjection::LoadYAMLNode(const YAML::Node &node)
{
// local variables
const char *kMethodName = "LoadYAMLNode";
// node shoud have at least group and may have 'include' and 'cut'
const YAML::Node groupNode = node[kNodeKeyGroup];
const YAML::Node aliasNode = node[kNodeKeyAlias];
const YAML::Node includeNode = node[kNodeKeyInclude];
Info(kMethodName,"loading tree projection");
if (fBaseDir.IsNull()) {
fBaseDir = gSystem->DirName(fCurrentFile);
}
#if 0
// node itself exists since the Node::operator[] ensure its existense
if (!groupNode && !includeNode) {
}
#endif
if (!groupNode && !includeNode) {
Error(kMethodName,"At least one group or include node should be contained");
return kFALSE;
}
//======================================================================
// include node
//======================================================================
if (includeNode) {
printf("include\n");
// prepare includes before continue
TString filename;
try {
for (YAML::const_iterator it = includeNode.begin(); it != includeNode.end(); ++it) {
std::string name;
// load template
if (it->Type() == YAML::NodeType::Map) {
const YAML::Node replaceNode = (*it)[kNodeKeyReplace];
const YAML::Node nameNode = (*it)[kNodeKeyName];
if (nameNode.IsNull()) {
Error("LoadFile","no 'name' node is found");
return kFALSE;
}
name = nameNode.as<std::string>();
if (replaceNode.IsNull()) {
Warning("LoadFile","no 'replace' node is found while it is expected in '%s'",name.c_str());
}
filename = gSystem->ConcatFileName(fBaseDir,name.c_str());
std::ifstream fin(filename.Data());
if (!fin.is_open()) {
Error("LoadFile", "Cannot open file: %s",filename.Data());
return kFALSE;
}
YAML::Node doc;
TString lines;
lines.ReadFile(fin);
if (replaceNode) {
for (YAML::const_iterator it = replaceNode.begin(), itend = replaceNode.end();
it != itend; ++it) {
std::string key, value;
key = it->first.as<std::string>();
value = it->second.as<std::string>();
lines.ReplaceAll(TString::Format("@%s@",key.c_str()),value);
}
}
if (!LoadYAMLNode(YAML::Load(lines.Data()))) {
return kFALSE;
}
} else {
// load file simply
printf("loading file\n");
name = it->as<std::string>();
filename = gSystem->ConcatFileName(fBaseDir,name.c_str());
TString dirsaved = fBaseDir;
// gSystem->FindFile(fSearchPath,filename);
if (filename.IsNull()) {
Warning(kMethodName,"File '%s' in include node does not exist",name.c_str());
continue;
}
if (LoadYAMLFile(filename)) {
fIncludes->Add(new TObjString(filename));
} else {
Error("LoadYAMLNode","Error while loading file '%s'",filename.Data());
return kFALSE;
}
fBaseDir = dirsaved;
}
}
} catch (YAML::Exception &e){
Error(kMethodName,"In %s, %s",filename.Data(),e.what());
return kFALSE;
}
}
//======================================================================
// alias node
//======================================================================
if (aliasNode) {
for (YAML::const_iterator it = aliasNode.begin(); it != aliasNode.end(); it++) {
try {
TString name = it->first.as<std::string>().c_str();
TString title = it->second.as<std::string>().c_str();
fAliases->Add(new TNamed(name,title));
} catch (YAML::Exception &e) {
Error(kMethodName,"%s",e.what());
return kFALSE;
}
}
}
if (!groupNode) return kTRUE;
//======================================================================
// group node
//======================================================================
TDirectory *saved = gDirectory;
for (YAML::const_iterator it = groupNode.begin(); it != groupNode.end(); it++) {
saved->cd();
TString name, title, cut, suffix;
TTreeProjGroup *group = NULL;
try {
name = (*it)[kNodeKeyName].as<std::string>().c_str();
title = (*it)[kNodeKeyTitle].as<std::string>().c_str();
} catch (YAML::Exception &e) {
Warning(kMethodName,"group skipped since name or title is not defined (%s)",e.what());
continue;
}
if (const YAML::Node c = (*it)[kNodeKeyCut]) {
cut = c.as<std::string>().c_str();
}
// check if the cloning the object or not
const YAML::Node contents = (*it)[kNodeKeyContents];
const YAML::Node clone = (*it)[kNodeKeyClone];
const YAML::Node nodeSuffix = (*it)[kNodeKeySuffix];
if (nodeSuffix) {
suffix = nodeSuffix.as<std::string>().c_str();
}
if (clone) {
if (TObject *obj = fProjGroups->FindObject(clone.as<std::string>().c_str())) {
// cloning the existing group;
group = dynamic_cast<TTreeProjGroup*>(obj->Clone(name));
group->SetTitle(title);
group->GetCut() += cut;
} else {
Warning(kMethodName,"group skipped since the group '%s' does not exist",name.Data());
continue;
}
} else if (contents) {
// new group is created
Int_t nSame = 0;
TString groupName = name;
while (gDirectory->Get(groupName)) {
++nSame;
groupName = Form("%s%d",name.Data(),nSame);
}
group = new TTreeProjGroup(groupName,title,cut);
} else {
// invalid declaration of group
Warning(kMethodName,"group skipped since the group '%s' does not contain any clones or contents",
name.Data());
continue;
}
// group should be created here
Info(kMethodName,"new group '%s'",name.Data());
fProjGroups->Add(group);
if (!contents) {
// changing name of histogram will destroy the structure of link
TList *savedlist = new TList;
TObjLink *link = group->GetList()->FirstLink();
while (link){
savedlist->Add(link->GetObject());
link = link->Next();
}
link = savedlist->FirstLink();
while (link) {
TH1* h1 = dynamic_cast<TH1*>(link->GetObject());
if (!h1) continue; // Object may be group. not implemented yet
h1->SetName(TString(h1->GetName()).Append(suffix));
link = link->Next();
}
saved->cd();
continue;
}
group->cd();
for (YAML::const_iterator itcont = contents.begin(); itcont != contents.end(); itcont++) {
TString hname, htitle, hcut, hasync;
TAttTreeProj *hproj = NULL;
Int_t nDim = 0;
const YAML::Node axisnode[3] =
{(*itcont)[kNodeKeyX],
(*itcont)[kNodeKeyY],
(*itcont)[kNodeKeyZ]};
try {
hname = (*itcont)["name"].as<std::string>().c_str();
htitle = (*itcont)["title"].as<std::string>().c_str();
} catch (YAML::Exception &e) {
Warning(kMethodName,"projection skipped since name or title is not defined (%s)",e.what());
continue;
}
if (const YAML::Node c = (*itcont)[kNodeKeyCut]) {
hcut = c.as<std::string>().c_str();
}
if (const YAML::Node n = (*itcont)[kNodeKeyAsync]) {
hasync = n.as<std::string>().c_str();
}
if (const YAML::Node n = (*itcont)[kNodeKeySuffix]) {
hname.Append(n.as<std::string>().c_str());
}
// apped group suffix
hname.Append(suffix);
// check if the cloning the object or not
if (const YAML::Node clone = (*itcont)[kNodeKeyClone]) {
if (TObject *obj = group->FindObject(clone.as<std::string>().c_str())) {
// cloning the existing group;
hproj = dynamic_cast<TAttTreeProj*>(obj->Clone(hname));
hproj->SetProjTitle(htitle);
hproj->GetCut() += hcut;
hproj->SetAxisAsync(hasync.Atoi());
} else {
Warning(kMethodName,"projection skipped since the group '%s' does not exist",name.Data());
continue;
}
} else {
// check if axisnode has enough number of inputs
for (Int_t i = 0; i < 3; ++i) {
if (axisnode[i] && axisnode[i].size() < 4) {
// insufficient number of input for axis
Error("LoadYAMLNode","\n histgram '%s': insufficient number of axis input for '%c' (size = %d)\n",
hname.Data(),'x'+i,axisnode[i].size());
saved->cd();
return kFALSE;
}
}
if (axisnode[2]) {
// three dimensional not implimented yet
if (!axisnode[0] || ! axisnode[1]) {
Warning(kMethodName,"projection '%s' in group '%s' skipped since x or y axis is not defined",
hname.Data(),name.Data());
continue;
}
hproj = new TH3FTreeProj(hname,htitle,hcut);
hproj->SetAxisAsync(hasync.Atoi());
nDim = 3;
} else if (axisnode[1]) {
if (!axisnode[0]) {
Warning(kMethodName,"projection '%s' in group '%s' skipped since x axis is not defined",
hname.Data(),name.Data());
continue;
}
hproj = new TH2FTreeProj(hname,htitle,hcut);
hproj->SetAxisAsync(hasync.Atoi());
nDim = 2;
} else if (axisnode[0]) {
// 1D histogram
hproj = new TH1FTreeProj(hname,htitle,hcut);
hproj->SetAxisAsync(hasync.Atoi());
nDim = 1;
} else {
Warning(kMethodName,"projection '%s' in group '%s' skipped since no axis is defined",
hname.Data(),name.Data());
continue;
}
}
for (Int_t i=0; i!=nDim; i++) {
const YAML::Node &a = axisnode[i];
try {
TCut axiscut;
TString axistitle;
if (a.size()>5) axiscut = a[5].as<std::string>().c_str();
if (a.size()>4) axistitle = a[4].as<std::string>().c_str();
TAxisTreeProj *axis = new TAxisTreeProj(a[0].as<std::string>().c_str(),
a[1].as<int>(),
a[2].as<double>(),
a[3].as<double>(),
axiscut);
hproj->SetAxisDef(i,axis);
// if (!axistitle.IsNull()) {
// switch (i) {
// case 0:
// hproj->SetXtitle(axistitle);
// break;
// case 1:
// hproj->SetYtitle(axistitle);
// break;
// case 2:
// hproj->SetZtitle(axistitle);
// break;
// }
// }
} catch (YAML::Exception &e) {
Warning(kMethodName,"projection '%s' in group '%s' skipped axis %d is invalid",
hname.Data(),name.Data(),i);
delete hproj;
hproj = NULL;
break;
}
}
}
}
saved->cd();
return kTRUE;
}
Bool_t TTreeProjection::Sync(TTree *tree)
{
Info("Sync","synchronizing");
Int_t nAlias = fAliases->GetEntriesFast();
for (Int_t i = 0; i!=nAlias; i++) {
TNamed *named = (TNamed*)fAliases->At(i);
tree->SetAlias(named->GetName(),named->GetTitle());
}
Int_t nGroup = fProjGroups->GetEntriesFast();
for (Int_t i = 0; i != nGroup; i++) {
TTreeProjGroup *group = (TTreeProjGroup*)fProjGroups->At(i);
Sync(group,tree,group->GetCut());
}
return kFALSE;
}
Bool_t TTreeProjection::Sync(TTreeProjGroup *group, TTree *tree, TCut cut)
{
TIter next(group->GetList());
while (TObject *obj = next()) {
if (obj->InheritsFrom(TTreeProjGroup::Class())) {
TTreeProjGroup *group = (TTreeProjGroup*)obj;
Sync((TTreeProjGroup*)obj,tree,cut + group->GetCut());
} else if (obj->InheritsFrom(TH1::Class())) {
Int_t nDim = ((TH1*)obj)->GetDimension();
TAttTreeProj *proj = dynamic_cast<TAttTreeProj*>(obj);
for (Int_t i=0; i!=nDim; i++) {
TAxisTreeProj* axis = proj->GetAxisDef(i);
TCut totalcut = axis->GetCut() + proj->GetCut() + cut;
TTreeFormula *axisFormula =new TTreeFormula(TString::Format("V%c",'x'+i),axis->GetTitle(),tree);
if (axisFormula->GetNdim() == 0) {
axisFormula->Delete();
proj = NULL;
break;
}
axis->SetVariableFormula(axisFormula);
if (totalcut != "") {
TTreeFormula *cutFormula = new TTreeFormula(TString::Format("S%c",'x'+i),
axis->GetCut()+proj->GetCut()+cut,tree);
if (cutFormula->GetNdim() == 0) {
cutFormula->Delete();
proj = NULL;
break;
}
axis->SetSelectionFormula(cutFormula);
}
}
if (proj) {
proj->Sync();
fProjections.push_back(proj);
}
}
}
return kTRUE;
}