-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathHistogramManager.cxx
More file actions
754 lines (697 loc) · 30 KB
/
HistogramManager.cxx
File metadata and controls
754 lines (697 loc) · 30 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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "Analysis/HistogramManager.h"
#include <iostream>
#include <fstream>
using namespace std;
#include <TObject.h>
#include <TObjArray.h>
#include <THashList.h>
#include <TH1F.h>
#include <TH2F.h>
#include <TH3F.h>
#include <TProfile.h>
#include <TProfile2D.h>
#include <TProfile3D.h>
#include <THn.h>
#include <THnSparse.h>
#include <TIterator.h>
#include <TClass.h>
ClassImp(HistogramManager);
//_______________________________________________________________________________
HistogramManager::HistogramManager() : TNamed("", ""),
fMainList(nullptr),
fNVars(0),
fUsedVars(nullptr),
fVariablesMap(),
fUseDefaultVariableNames(false),
fBinsAllocated(0),
fVariableNames(nullptr),
fVariableUnits(nullptr)
{
//
// Constructor
//
}
//_______________________________________________________________________________
HistogramManager::HistogramManager(const char* name, const char* title, const int maxNVars) : TNamed(name, title),
fMainList(),
fNVars(maxNVars),
fUsedVars(),
fVariablesMap(),
fUseDefaultVariableNames(kFALSE),
fBinsAllocated(0),
fVariableNames(),
fVariableUnits()
{
//
// Constructor
//
fMainList = new THashList;
fMainList->SetOwner(kTRUE);
fMainList->SetName(name);
fUsedVars = new bool[maxNVars];
fVariableNames = new TString[maxNVars];
fVariableUnits = new TString[maxNVars];
}
//_______________________________________________________________________________
HistogramManager::~HistogramManager()
{
//
// De-constructor
//
delete fMainList;
delete fUsedVars;
}
//_______________________________________________________________________________
void HistogramManager::SetDefaultVarNames(TString* vars, TString* units)
{
//
// Set default variable names
//
for (int i = 0; i < fNVars; ++i) {
fVariableNames[i] = vars[i];
fVariableUnits[i] = units[i];
}
};
//__________________________________________________________________
void HistogramManager::AddHistClass(const char* histClass)
{
//
// Add a new histogram list
//
if (fMainList->FindObject(histClass)) {
cout << "Warning in HistogramManager::AddHistClass(): Cannot add histogram class " << histClass
<< " because it already exists." << endl;
return;
}
TList* hList = new TList;
hList->SetOwner(kTRUE);
hList->SetName(histClass);
fMainList->Add(hList);
std::list<std::vector<int>> varList;
fVariablesMap[histClass] = varList;
cout << "Adding histogram class " << histClass << endl;
cout << "Variable map size :: " << fVariablesMap.size() << endl;
}
//_________________________________________________________________
void HistogramManager::AddHistogram(const char* histClass, const char* hname, const char* title, bool isProfile,
int nXbins, double xmin, double xmax, int varX,
int nYbins, double ymin, double ymax, int varY,
int nZbins, double zmin, double zmax, int varZ,
const char* xLabels, const char* yLabels, const char* zLabels,
int varT, int varW)
{
//
// add a histogram (this function can define TH1F,TH2F,TH3F,TProfile,TProfile2D, and TProfile3D)
//
// TODO: replace the cout warning messages with LOG (same for all the other functions)
// get the list to which the histogram should be added
TList* hList = (TList*)fMainList->FindObject(histClass);
if (!hList) {
cout << "Warning in HistogramManager::AddHistogram(): Histogram list " << histClass << " not found!" << endl;
cout << " Histogram not created" << endl;
return;
}
// check whether this histogram name was used before
if (hList->FindObject(hname)) {
cout << "Warning in HistogramManager::AddHistogram(): Histogram " << hname << " already exists" << endl;
return;
}
// deduce the dimension of the histogram from parameters
// NOTE: in case of profile histograms, one extra variable is needed
int dimension = 1;
if (varY > kNothing)
dimension = 2;
if (varZ > kNothing)
dimension = 3;
// tokenize the title string; the user may include in it axis titles which will overwrite the defaults
TString titleStr(title);
TObjArray* arr = titleStr.Tokenize(";");
// mark required variables as being used
if (varX > kNothing)
fUsedVars[varX] = kTRUE;
if (varY > kNothing)
fUsedVars[varY] = kTRUE;
if (varZ > kNothing)
fUsedVars[varZ] = kTRUE;
if (varT > kNothing)
fUsedVars[varT] = kTRUE;
if (varW > kNothing)
fUsedVars[varW] = kTRUE;
// encode needed variable identifiers in a vector and push it to the std::list corresponding to the current histogram list
std::vector<int> varVector;
varVector.push_back(isProfile ? 1 : 0); // whether the histogram is a profile
varVector.push_back(0); // whether it is a THn
varVector.push_back(varW); // variable used for weighting
varVector.push_back(varX); // variables on each axis
varVector.push_back(varY);
varVector.push_back(varZ);
varVector.push_back(varT); // variable used for profiling in case of TProfile3D
std::list varList = fVariablesMap[histClass];
varList.push_back(varVector);
cout << "Adding histogram " << hname << endl;
cout << "size of array :: " << varList.size() << endl;
fVariablesMap[histClass] = varList;
// create and configure histograms according to required options
TH1* h = nullptr;
switch (dimension) {
case 1: // TH1F
h = new TH1F(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xmin, xmax);
fBinsAllocated += nXbins + 2;
// TODO: possibly make the call of Sumw2() optional for all histograms
h->Sumw2();
if (fVariableNames[varX][0])
h->GetXaxis()->SetTitle(Form("%s %s", fVariableNames[varX].Data(),
(fVariableUnits[varX][0] ? Form("(%s)", fVariableUnits[varX].Data()) : "")));
if (arr->At(1))
h->GetXaxis()->SetTitle(arr->At(1)->GetName());
if (xLabels[0] != '\0')
MakeAxisLabels(h->GetXaxis(), xLabels);
hList->Add(h);
h->SetDirectory(nullptr);
break;
case 2: // either TH2F or TProfile
if (isProfile) {
h = new TProfile(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xmin, xmax);
fBinsAllocated += nXbins + 2;
h->Sumw2();
// if requested, build the profile using the profile widths instead of stat errors
// TODO: make this option more transparent to the user ?
if (titleStr.Contains("--s--"))
((TProfile*)h)->BuildOptions(0., 0., "s");
} else {
h = new TH2F(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xmin, xmax, nYbins, ymin, ymax);
fBinsAllocated += (nXbins + 2) * (nYbins + 2);
h->Sumw2();
}
if (fVariableNames[varX][0])
h->GetXaxis()->SetTitle(Form("%s %s", fVariableNames[varX].Data(),
(fVariableUnits[varX][0] ? Form("(%s)", fVariableUnits[varX].Data()) : "")));
if (arr->At(1))
h->GetXaxis()->SetTitle(arr->At(1)->GetName());
if (xLabels[0] != '\0')
MakeAxisLabels(h->GetXaxis(), xLabels);
if (fVariableNames[varY][0])
h->GetYaxis()->SetTitle(Form("%s %s", fVariableNames[varY].Data(),
(fVariableUnits[varY][0] ? Form("(%s)", fVariableUnits[varY].Data()) : "")));
if (fVariableNames[varY][0] && isProfile)
h->GetYaxis()->SetTitle(Form("<%s> %s", fVariableNames[varY].Data(),
(fVariableUnits[varY][0] ? Form("(%s)", fVariableUnits[varY].Data()) : "")));
if (arr->At(2))
h->GetYaxis()->SetTitle(arr->At(2)->GetName());
if (yLabels[0] != '\0')
MakeAxisLabels(h->GetYaxis(), yLabels);
hList->Add(h);
h->SetDirectory(nullptr);
break;
case 3: // TH3F, TProfile2D or TProfile3D
if (isProfile) {
if (varT > kNothing) { // TProfile3D
h = new TProfile3D(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xmin, xmax, nYbins, ymin, ymax, nZbins, zmin, zmax);
fBinsAllocated += (nXbins + 2) * (nYbins + 2) * (nZbins + 2);
h->Sumw2();
if (titleStr.Contains("--s--"))
((TProfile3D*)h)->BuildOptions(0., 0., "s");
} else { // TProfile2D
h = new TProfile2D(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xmin, xmax, nYbins, ymin, ymax);
fBinsAllocated += (nXbins + 2) * (nYbins + 2);
h->Sumw2();
if (titleStr.Contains("--s--"))
((TProfile2D*)h)->BuildOptions(0., 0., "s");
}
} else { // TH3F
h = new TH3F(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xmin, xmax, nYbins, ymin, ymax, nZbins, zmin, zmax);
fBinsAllocated += (nXbins + 2) * (nYbins + 2) * (nZbins + 2);
h->Sumw2();
}
if (fVariableNames[varX][0])
h->GetXaxis()->SetTitle(Form("%s %s", fVariableNames[varX].Data(),
(fVariableUnits[varX][0] ? Form("(%s)", fVariableUnits[varX].Data()) : "")));
if (arr->At(1))
h->GetXaxis()->SetTitle(arr->At(1)->GetName());
if (xLabels[0] != '\0')
MakeAxisLabels(h->GetXaxis(), xLabels);
if (fVariableNames[varY][0])
h->GetYaxis()->SetTitle(Form("%s %s", fVariableNames[varY].Data(),
(fVariableUnits[varY][0] ? Form("(%s)", fVariableUnits[varY].Data()) : "")));
if (arr->At(2))
h->GetYaxis()->SetTitle(arr->At(2)->GetName());
if (yLabels[0] != '\0')
MakeAxisLabels(h->GetYaxis(), yLabels);
if (fVariableNames[varZ][0])
h->GetZaxis()->SetTitle(Form("%s %s", fVariableNames[varZ].Data(),
(fVariableUnits[varZ][0] ? Form("(%s)", fVariableUnits[varZ].Data()) : "")));
if (fVariableNames[varZ][0] && isProfile && varT < 0) // for TProfile2D
h->GetZaxis()->SetTitle(Form("<%s> %s", fVariableNames[varZ].Data(),
(fVariableUnits[varZ][0] ? Form("(%s)", fVariableUnits[varZ].Data()) : "")));
if (arr->At(3))
h->GetZaxis()->SetTitle(arr->At(3)->GetName());
if (zLabels[0] != '\0')
MakeAxisLabels(h->GetZaxis(), zLabels);
h->SetDirectory(nullptr);
hList->Add(h);
break;
} // end switch
}
//_________________________________________________________________
void HistogramManager::AddHistogram(const char* histClass, const char* hname, const char* title, bool isProfile,
int nXbins, double* xbins, int varX,
int nYbins, double* ybins, int varY,
int nZbins, double* zbins, int varZ,
const char* xLabels, const char* yLabels, const char* zLabels,
int varT, int varW)
{
//
// add a histogram
//
// get the list to which the histogram should be added
TList* hList = (TList*)fMainList->FindObject(histClass);
if (!hList) {
cout << "Warning in HistogramManager::AddHistogram(): Histogram list " << histClass << " not found!" << endl;
cout << " Histogram not created" << endl;
return;
}
// check whether this histogram name was used before
if (hList->FindObject(hname)) {
cout << "Warning in HistogramManager::AddHistogram(): Histogram " << hname << " already exists" << endl;
return;
}
// deduce the dimension of the histogram from parameters
// NOTE: in case of profile histograms, one extra variable is needed
int dimension = 1;
if (varY > kNothing)
dimension = 2;
if (varZ > kNothing)
dimension = 3;
// mark required variables as being used
if (varX > kNothing)
fUsedVars[varX] = kTRUE;
if (varY > kNothing)
fUsedVars[varY] = kTRUE;
if (varZ > kNothing)
fUsedVars[varZ] = kTRUE;
if (varT > kNothing)
fUsedVars[varT] = kTRUE;
if (varW > kNothing)
fUsedVars[varW] = kTRUE;
// tokenize the title string; the user may include in it axis titles which will overwrite the defaults
TString titleStr(title);
TObjArray* arr = titleStr.Tokenize(";");
// encode needed variable identifiers in a vector and push it to the std::list corresponding to the current histogram list
std::vector<int> varVector;
varVector.push_back(isProfile ? 1 : 0); // whether the histogram is a profile
varVector.push_back(0); // whether it is a THn
varVector.push_back(varW); // variable used for weighting
varVector.push_back(varX); // variables on each axis
varVector.push_back(varY);
varVector.push_back(varZ);
varVector.push_back(varT); // variable used for profiling in case of TProfile3D
std::list varList = fVariablesMap[histClass];
varList.push_back(varVector);
cout << "Adding histogram " << hname << endl;
cout << "size of array :: " << varList.size() << endl;
fVariablesMap[histClass] = varList;
TH1* h = nullptr;
switch (dimension) {
case 1:
h = new TH1F(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xbins);
fBinsAllocated += nXbins + 2;
h->Sumw2();
if (fVariableNames[varX][0])
h->GetXaxis()->SetTitle(Form("%s %s", fVariableNames[varX].Data(),
(fVariableUnits[varX][0] ? Form("(%s)", fVariableUnits[varX].Data()) : "")));
if (arr->At(1))
h->GetXaxis()->SetTitle(arr->At(1)->GetName());
if (xLabels[0] != '\0')
MakeAxisLabels(h->GetXaxis(), xLabels);
h->SetDirectory(nullptr);
hList->Add(h);
break;
case 2:
if (isProfile) {
h = new TProfile(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xbins);
fBinsAllocated += nXbins + 2;
h->Sumw2();
if (titleStr.Contains("--s--"))
((TProfile*)h)->BuildOptions(0., 0., "s");
} else {
h = new TH2F(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xbins, nYbins, ybins);
fBinsAllocated += (nXbins + 2) * (nYbins + 2);
h->Sumw2();
}
if (fVariableNames[varX][0])
h->GetXaxis()->SetTitle(Form("%s (%s)", fVariableNames[varX].Data(),
(fVariableUnits[varX][0] ? Form("(%s)", fVariableUnits[varX].Data()) : "")));
if (arr->At(1))
h->GetXaxis()->SetTitle(arr->At(1)->GetName());
if (xLabels[0] != '\0')
MakeAxisLabels(h->GetXaxis(), xLabels);
if (fVariableNames[varY][0])
h->GetYaxis()->SetTitle(Form("%s (%s)", fVariableNames[varY].Data(),
(fVariableUnits[varY][0] ? Form("(%s)", fVariableUnits[varY].Data()) : "")));
if (fVariableNames[varY][0] && isProfile)
h->GetYaxis()->SetTitle(Form("<%s> (%s)", fVariableNames[varY].Data(),
(fVariableUnits[varY][0] ? Form("(%s)", fVariableUnits[varY].Data()) : "")));
if (arr->At(2))
h->GetYaxis()->SetTitle(arr->At(2)->GetName());
if (yLabels[0] != '\0')
MakeAxisLabels(h->GetYaxis(), yLabels);
h->SetDirectory(nullptr);
hList->Add(h);
break;
case 3:
if (isProfile) {
if (varT > kNothing) {
h = new TProfile3D(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xbins, nYbins, ybins, nZbins, zbins);
fBinsAllocated += (nXbins + 2) * (nYbins + 2) * (nZbins + 2);
h->Sumw2();
if (titleStr.Contains("--s--"))
((TProfile3D*)h)->BuildOptions(0., 0., "s");
} else {
h = new TProfile2D(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xbins, nYbins, ybins);
fBinsAllocated += (nXbins + 2) * (nYbins + 2);
h->Sumw2();
if (titleStr.Contains("--s--"))
((TProfile2D*)h)->BuildOptions(0., 0., "s");
}
} else {
h = new TH3F(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xbins, nYbins, ybins, nZbins, zbins);
fBinsAllocated += (nXbins + 2) * (nYbins + 2) * (nZbins + 2);
h->Sumw2();
}
if (fVariableNames[varX][0])
h->GetXaxis()->SetTitle(Form("%s %s", fVariableNames[varX].Data(),
(fVariableUnits[varX][0] ? Form("(%s)", fVariableUnits[varX].Data()) : "")));
if (arr->At(1))
h->GetXaxis()->SetTitle(arr->At(1)->GetName());
if (xLabels[0] != '\0')
MakeAxisLabels(h->GetXaxis(), xLabels);
if (fVariableNames[varY][0])
h->GetYaxis()->SetTitle(Form("%s %s", fVariableNames[varY].Data(),
(fVariableUnits[varY][0] ? Form("(%s)", fVariableUnits[varY].Data()) : "")));
if (arr->At(2))
h->GetYaxis()->SetTitle(arr->At(2)->GetName());
if (yLabels[0] != '\0')
MakeAxisLabels(h->GetYaxis(), yLabels);
if (fVariableNames[varZ][0])
h->GetZaxis()->SetTitle(Form("%s %s", fVariableNames[varZ].Data(),
(fVariableUnits[varZ][0] ? Form("(%s)", fVariableUnits[varZ].Data()) : "")));
if (fVariableNames[varZ][0] && isProfile && varT < 0) // TProfile2D
h->GetZaxis()->SetTitle(Form("<%s> %s", fVariableNames[varZ].Data(),
(fVariableUnits[varZ][0] ? Form("(%s)", fVariableUnits[varZ].Data()) : "")));
if (arr->At(3))
h->GetZaxis()->SetTitle(arr->At(3)->GetName());
if (zLabels[0] != '\0')
MakeAxisLabels(h->GetZaxis(), zLabels);
hList->Add(h);
break;
} // end switch(dimension)
}
//_________________________________________________________________
void HistogramManager::AddHistogram(const char* histClass, const char* hname, const char* title,
int nDimensions, int* vars, int* nBins, double* xmin, double* xmax,
TString* axLabels, int varW, bool useSparse)
{
//
// add a multi-dimensional histogram THnF or THnFSparseF
//
// get the list to which the histogram should be added
TList* hList = (TList*)fMainList->FindObject(histClass);
if (!hList) {
cout << "Warning in HistogramManager::AddHistogram(): Histogram list " << histClass << " not found!" << endl;
cout << " Histogram not created" << endl;
return;
}
// check whether this histogram name was used before
if (hList->FindObject(hname)) {
cout << "Warning in HistogramManager::AddHistogram(): Histogram " << hname << " already exists" << endl;
return;
}
// tokenize the title string; the user may include in it axis titles which will overwrite the defaults
TString titleStr(title);
TObjArray* arr = titleStr.Tokenize(";");
if (varW > kNothing)
fUsedVars[varW] = kTRUE;
// encode needed variable identifiers in a vector and push it to the std::list corresponding to the current histogram list
std::vector<int> varVector;
varVector.push_back(0); // whether the histogram is a profile
varVector.push_back(nDimensions); // number of dimensions
varVector.push_back(varW); // variable used for weighting
for (int idim = 0; idim < nDimensions; ++idim)
varVector.push_back(vars[idim]); // axes variables
std::list varList = fVariablesMap[histClass];
varList.push_back(varVector);
cout << "Adding histogram " << hname << endl;
cout << "size of array :: " << varList.size() << endl;
fVariablesMap[histClass] = varList;
unsigned long int nbins = 1;
THnBase* h = nullptr;
if (useSparse)
h = new THnSparseF(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nDimensions, nBins, xmin, xmax);
else
h = new THnF(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nDimensions, nBins, xmin, xmax);
h->Sumw2();
// configure the THn histogram and count the allocated bins
for (int idim = 0; idim < nDimensions; ++idim) {
nbins *= (nBins[idim] + 2);
TAxis* axis = h->GetAxis(idim);
if (fVariableNames[vars[idim]][0])
axis->SetTitle(Form("%s %s", fVariableNames[vars[idim]].Data(),
(fVariableUnits[vars[idim]][0] ? Form("(%s)", fVariableUnits[vars[idim]].Data()) : "")));
if (arr->At(1 + idim))
axis->SetTitle(arr->At(1 + idim)->GetName());
if (axLabels && !axLabels[idim].IsNull())
MakeAxisLabels(axis, axLabels[idim].Data());
fUsedVars[vars[idim]] = kTRUE;
}
if (useSparse)
hList->Add((THnSparseF*)h);
else
hList->Add((THnF*)h);
fBinsAllocated += nbins;
}
//_________________________________________________________________
void HistogramManager::AddHistogram(const char* histClass, const char* hname, const char* title,
int nDimensions, int* vars, TArrayD* binLimits,
TString* axLabels, int varW, bool useSparse)
{
//
// add a multi-dimensional histogram THnF or THnSparseF with equal or variable bin widths
//
// get the list to which the histogram should be added
TList* hList = (TList*)fMainList->FindObject(histClass);
if (!hList) {
cout << "Warning in HistogramManager::AddHistogram(): Histogram list " << histClass << " not found!" << endl;
cout << " Histogram not created" << endl;
return;
}
// check whether this histogram name was used before
if (hList->FindObject(hname)) {
cout << "Warning in HistogramManager::AddHistogram(): Histogram " << hname << " already exists" << endl;
return;
}
// tokenize the title string; the user may include in it axis titles which will overwrite the defaults
TString titleStr(title);
TObjArray* arr = titleStr.Tokenize(";");
if (varW > kNothing)
fUsedVars[varW] = kTRUE;
// encode needed variable identifiers in a vector and push it to the std::list corresponding to the current histogram list
std::vector<int> varVector;
varVector.push_back(0); // whether the histogram is a profile
varVector.push_back(nDimensions); // number of dimensions
varVector.push_back(varW); // variable used for weighting
for (int idim = 0; idim < nDimensions; ++idim)
varVector.push_back(vars[idim]); // axes variables
std::list varList = fVariablesMap[histClass];
varList.push_back(varVector);
cout << "Adding histogram " << hname << endl;
cout << "size of array :: " << varList.size() << endl;
fVariablesMap[histClass] = varList;
// get the min and max for each axis
double* xmin = new double[nDimensions];
double* xmax = new double[nDimensions];
int* nBins = new int[nDimensions];
for (int idim = 0; idim < nDimensions; ++idim) {
nBins[idim] = binLimits[idim].GetSize() - 1;
xmin[idim] = binLimits[idim][0];
xmax[idim] = binLimits[idim][nBins[idim]];
}
// initialize the THn with equal spaced bins
THnBase* h = nullptr;
if (useSparse)
h = new THnSparseF(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nDimensions, nBins, xmin, xmax);
else
h = new THnF(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nDimensions, nBins, xmin, xmax);
// rebin the axes according to the user requested binning
for (int idim = 0; idim < nDimensions; ++idim) {
TAxis* axis = h->GetAxis(idim);
axis->Set(nBins[idim], binLimits[idim].GetArray());
}
h->Sumw2();
unsigned long int bins = 1;
for (int idim = 0; idim < nDimensions; ++idim) {
bins *= (nBins[idim] + 2);
TAxis* axis = h->GetAxis(idim);
if (fVariableNames[vars[idim]][0])
axis->SetTitle(Form("%s %s", fVariableNames[vars[idim]].Data(),
(fVariableUnits[vars[idim]][0] ? Form("(%s)", fVariableUnits[vars[idim]].Data()) : "")));
if (arr->At(1 + idim))
axis->SetTitle(arr->At(1 + idim)->GetName());
if (axLabels && !axLabels[idim].IsNull())
MakeAxisLabels(axis, axLabels[idim].Data());
fUsedVars[vars[idim]] = kTRUE;
}
if (useSparse)
hList->Add((THnSparseF*)h);
else
hList->Add((THnF*)h);
fBinsAllocated += bins;
}
//__________________________________________________________________
void HistogramManager::FillHistClass(const char* className, Float_t* values)
{
//
// fill a class of histograms
//
// get the needed histogram list
TList* hList = (TList*)fMainList->FindObject(className);
if (!hList) {
// TODO: add some meaningfull error message
/*cout << "Warning in HistogramManager::FillHistClass(): Histogram list " << className << " not found!" << endl;
cout << " Histogram list not filled" << endl; */
return;
}
// get the corresponding std::list containng identifiers to the needed variables to be filled
list varList = fVariablesMap[className];
TIter next(hList);
auto varIter = varList.begin();
TObject* h = nullptr;
bool isProfile;
bool isTHn;
int dimension = 0;
bool isSparse = kFALSE;
// TODO: At the moment, maximum 20 dimensions are foreseen for the THn histograms. We should make this more dynamic
// But maybe its better to have it like to avoid dynamically allocating this array in the histogram loop
double fillValues[20] = {0.0};
int varX = -1, varY = -1, varZ = -1, varT = -1, varW = -1;
// loop over the histogram and std::list
// NOTE: these two should contain the same number of elements and be synchronized, otherwise its a mess
for (auto varIter = varList.begin(); varIter != varList.end(); varIter++) {
h = next(); // get the histogram
// decode information from the vector of indices
isProfile = (varIter->at(0) == 1 ? true : false);
isTHn = (varIter->at(1) > 0 ? true : false);
if (isTHn)
dimension = varIter->at(1);
else
dimension = ((TH1*)h)->GetDimension();
// get the various variable indices
varW = varIter->at(2);
if (isTHn) {
for (int i = 0; i < dimension; i++)
fillValues[i] = values[varIter->at(3 + i)];
} else {
varX = varIter->at(3);
varY = varIter->at(4);
varZ = varIter->at(5);
varT = varIter->at(6);
}
if (!isTHn) {
switch (dimension) {
case 1:
if (isProfile) {
if (varW > kNothing)
((TProfile*)h)->Fill(values[varX], values[varY], values[varW]);
else
((TProfile*)h)->Fill(values[varX], values[varY]);
} else {
if (varW > kNothing)
((TH1F*)h)->Fill(values[varX], values[varW]);
else
((TH1F*)h)->Fill(values[varX]);
}
break;
case 2:
if (isProfile) {
if (varW > kNothing)
((TProfile2D*)h)->Fill(values[varX], values[varY], values[varZ], values[varW]);
else
((TProfile2D*)h)->Fill(values[varX], values[varY], values[varZ]);
} else {
if (varW > kNothing)
((TH2F*)h)->Fill(values[varX], values[varY], values[varW]);
else
((TH2F*)h)->Fill(values[varX], values[varY]);
}
break;
case 3:
if (isProfile) {
if (varW > kNothing)
((TProfile3D*)h)->Fill(values[varX], values[varY], values[varZ], values[varT], values[varW]);
else
((TProfile3D*)h)->Fill(values[varX], values[varY], values[varZ], values[varT]);
} else {
if (varW > kNothing)
((TH3F*)h)->Fill(values[varX], values[varY], values[varZ], values[varW]);
else
((TH3F*)h)->Fill(values[varX], values[varY], values[varZ]);
}
break;
default:
break;
} // end switch
} // end if(!isTHn)
else {
if (varW > kNothing) {
if (isSparse)
((THnSparseF*)h)->Fill(fillValues, values[varW]);
else
((THnF*)h)->Fill(fillValues, values[varW]);
} else {
if (isSparse)
((THnSparseF*)h)->Fill(fillValues);
else
((THnF*)h)->Fill(fillValues);
}
} // end else
} // end loop over histograms
}
//____________________________________________________________________________________
void HistogramManager::MakeAxisLabels(TAxis* ax, const char* labels)
{
//
// add bin labels to an axis
//
TString labelsStr(labels);
TObjArray* arr = labelsStr.Tokenize(";");
for (int ib = 1; ib <= ax->GetNbins(); ++ib) {
if (ib >= arr->GetEntries() + 1)
break;
ax->SetBinLabel(ib, arr->At(ib - 1)->GetName());
}
}
//____________________________________________________________________________________
void HistogramManager::Print(Option_t*) const
{
//
// Print the defined histograms
//
cout << "###################################################################" << endl;
cout << "HistogramManager:: " << fMainList->GetName() << endl;
for (int i = 0; i < fMainList->GetEntries(); ++i) {
TList* list = (TList*)fMainList->At(i);
cout << "************** List " << list->GetName() << endl;
for (int j = 0; j < list->GetEntries(); ++j) {
TObject* obj = list->At(j);
cout << obj->GetName() << ": " << obj->IsA()->GetName() << endl;
}
}
}