-
Notifications
You must be signed in to change notification settings - Fork 0
/
gHist.h
154 lines (141 loc) · 4.04 KB
/
gHist.h
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
#ifndef __GHIST_H__
#define __GHIST_H__
// Allows to do
// gHist.Fill("histName", "title of histgram", 100, 0, 1,
// value)
// instead of
//
// somewhere
// TH1* hmuell;
// somewhere else
// hmuell = new TH1D("histName", "title of histogram", 100, 0, 1);
// yet somewhere else
// hmuell->Fill(value);
//
// In order to save some typing when filling the same histogram
// several times, a pointer to the histogram can be obtained via
// getHist.
//
// Some trivialities remain:
//
// TODO: give Fill a return value? Either return hist or whatever
// TH1::Fill returns
#include <map>
#include <stdint.h>
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
namespace {
class gHistClass
{
private:
std::map<uint64_t, TH1*> mHists;
// Calculate an FNV hash in order to not have to search through
// the map by comparing strings. Collisions should be absurdely
// unlikely.
uint64_t hashString(const char* str) const {
const uint64_t FNV_offset_basis = 14695981039346656037ULL;
const uint64_t FNV_prime = 1099511628211ULL;
uint64_t hash = FNV_offset_basis;
const char* p = str;
while (*p)
{
hash = hash ^ *p;
hash = hash * FNV_prime;
p++;
}
return hash;
}
public:
TH1* getHist(const char* name)
{
uint64_t hash = hashString(name);
std::map<uint64_t, TH1*>::iterator it = mHists.find(hash);
if (it == mHists.end())
return 0;
return dynamic_cast<TH1*>(it->second);
}
TH1* operator[](const char* name)
{
return this->getHist(name);
}
TH1* getHist(const char* name, const char* title,
Int_t nBinsX, Double_t lowX, Double_t highX)
{
uint64_t hash = hashString(name);
std::map<uint64_t, TH1*>::iterator it = mHists.find(hash);
if (it == mHists.end())
{
return (TH1*)(mHists[hash] = new TH1D(name, title,
nBinsX, lowX, highX));
}
return dynamic_cast<TH1*>(it->second);
}
void Fill(const char* name, const char* title,
Int_t nBinsX, Double_t lowX, Double_t highX,
Double_t valueX, Double_t weight = 1)
{
getHist(name, title, nBinsX, lowX, highX)
->Fill(valueX, weight);
}
TH2* getHist(const char* name, const char* title,
Int_t nBinsX, Double_t lowX, Double_t highX,
Int_t nBinsY, Double_t lowY, Double_t highY)
{
uint64_t hash = hashString(name);
std::map<uint64_t, TH1*>::iterator it = mHists.find(hash);
if (it == mHists.end())
{
return (TH2*)(mHists[hash] = new TH2D(name, title,
nBinsX, lowX, highX,
nBinsY, lowY, highY));
}
return dynamic_cast<TH2*>(it->second);
}
void Fill(const char* name, const char* title,
Int_t nBinsX, Double_t lowX, Double_t highX,
Int_t nBinsY, Double_t lowY, Double_t highY,
Double_t valueX, Double_t valueY, Double_t weight = 1)
{
getHist(name, title, nBinsX, lowX, highX, nBinsY, lowY, highY)
->Fill(valueX, valueY, weight);
}
TH3* getHist(const char* name, const char* title,
Int_t nBinsX, Double_t lowX, Double_t highX,
Int_t nBinsY, Double_t lowY, Double_t highY,
Int_t nBinsZ, Double_t lowZ, Double_t highZ)
{
uint64_t hash = hashString(name);
std::map<uint64_t, TH1*>::iterator it = mHists.find(hash);
if (it == mHists.end())
{
return (TH3*)(mHists[hash] = new TH3D(name, title,
nBinsX, lowX, highX,
nBinsY, lowY, highY,
nBinsZ, lowZ, highZ));
}
return dynamic_cast<TH3*>(it->second);
}
void Fill(const char* name, const char* title,
Int_t nBinsX, Double_t lowX, Double_t highX,
Int_t nBinsY, Double_t lowY, Double_t highY,
Int_t nBinsZ, Double_t lowZ, Double_t highZ,
Double_t valueX, Double_t valueY, Double_t valueZ, Double_t weight = 1)
{
getHist(name, title, nBinsX, lowX, highX, nBinsY, lowY, highY, nBinsZ, lowZ, highZ)
->Fill(valueX, valueY, valueZ, weight);
}
/*
// Syntactic sugar.
gHistClass* operator->()
{
return this;
}
*/
void clear()
{
mHists.clear();
}
} gHist;
}
#endif