-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathhistHelpersTest.cxx
More file actions
210 lines (170 loc) · 8.49 KB
/
histHelpersTest.cxx
File metadata and controls
210 lines (170 loc) · 8.49 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
// 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 "Framework/AnalysisTask.h"
#include "AnalysisCore/HistHelpers.h"
#include "Framework/AnalysisDataModel.h"
#include "Framework/runDataProcessing.h"
#include <cmath>
using namespace o2;
using namespace o2::framework;
using namespace o2::framework::expressions;
using namespace o2::experimental::histhelpers;
struct HistHelpersTest {
// some unique names for the histograms
enum HistNamesTest : uint8_t {
test_1d_TH1D,
test_2d_TH2F,
test_3d_TH3I,
test_1d_TH1D_Weight,
test_2d_TH2F_VarBinningY,
test_3d_THnI,
test_5d_THnSparseI,
test_1d_TProfile,
test_1d_TProfile_Weight,
test_2d_TProfile2D,
test_3d_TProfile3D,
test_7d_THnF_first,
test_7d_THnF_second,
test_8d_THnC_third,
};
enum HistNamesKine : uint8_t {
pt,
eta,
phi,
};
enum HistNamesTPC : uint8_t {
tpcNClsFound,
tpcNClsCrossedRows,
tpcChi2NCl,
};
enum HistNamesITS : uint8_t {
itsChi2NCl,
};
OutputObj<HistArray> test{HistArray("Test"), OutputObjHandlingPolicy::QAObject};
OutputObj<HistArray> kine{HistArray("Kine"), OutputObjHandlingPolicy::QAObject};
OutputObj<HistFolder> tpc{HistFolder("TPC"), OutputObjHandlingPolicy::QAObject};
OutputObj<HistList> its{HistList("ITS"), OutputObjHandlingPolicy::QAObject};
OutputObj<THnF> standaloneHist{"standaloneHist", OutputObjHandlingPolicy::QAObject};
void init(o2::framework::InitContext&)
{
// add some plain and simple histograms
test->Add<test_1d_TH1D>(new TH1D("test_1d_TH1D", ";x", 100, 0., 50.));
test->Add<test_2d_TH2F>(new TH2F("test_2d_TH2F", ";x;y", 100, -0.5, 0.5, 100, -0.5, 0.5));
test->Add<test_3d_TH3I>(new TH3I("test_3d_TH3I", ";x;y;z", 100, 0., 20., 100, 0., 20., 100, 0., 20.));
// alternatively use Hist to generate the histogram and add it to container afterwards
Hist sameAsBefore;
sameAsBefore.AddAxis("x", "x", 100, 0., 50.);
// via Hist::Create() we can generate the actual root histogram with the requested axes
// Parameters: the name and optionally the decision wether to call SumW2 in case we want to fill this histogram with weights
test->Add<test_1d_TH1D_Weight>(sameAsBefore.Create<TH1D>("test_1d_TH1D_Weight", true));
// this helper enables us to have combinations of flexible + fixed binning in 2d or 3d histograms
// (which are not available via default root constructors)
Hist sameButDifferent;
sameButDifferent.AddAxis("x", "x", 100, -0.5, 0.5);
sameButDifferent.AddAxis("y", "y", {-0.5, -0.48, -0.3, 0.4, 0.5}); // use variable binning for y axsis this time
test->Add<test_2d_TH2F_VarBinningY>(sameButDifferent.Create<TH2F>("test_2d_TH2F_VarBinningY"));
// also for n dimensional histograms things become much simpler:
std::vector<double> ptBins = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
1.1, 1.2, 1.3, 1.4, 1.5, 2.0, 5.0, 10.0, 20.0, 50.0};
std::vector<double> centBins = {0., 30., 60., 90.};
// varaiable binning
Axis ptAxis = {"pt", "#it{p}_{T} (GeV/c)", ptBins};
Axis centAxis = {"cent", "centrality", centBins};
// equidistant binning
Axis etaAxis = {"eta", "#eta", {-0.8, 0.8}, 5};
Axis phiAxis = {"phi", "#phi", {0., 2. * M_PI}, 4}; // 36 to see tpc sectors
const int nCuts = 5;
Axis cutAxis = {"cut", "cut setting", {-0.5, nCuts - 0.5}, nCuts};
Hist myHistogram({ptAxis, etaAxis, {"signed1Pt", "q/p_{T}", {-8, 8}, 200}});
test->Add<test_3d_THnI>(myHistogram.Create<THnI>("test_3d_THnI"));
Hist testSparseHist({ptAxis, etaAxis, phiAxis, centAxis, cutAxis});
test->Add<test_5d_THnSparseI>(testSparseHist.Create<THnSparseI>("test_5d_THnSparseI"));
Hist testProfile({ptAxis});
test->Add<test_1d_TProfile>(testProfile.Create<TProfile>("test_1d_TProfile"));
test->Get<TProfile>(test_1d_TProfile)->GetYaxis()->SetTitle("eta profile");
// now add same histogram but intended for weighted filling
test->Add<test_1d_TProfile_Weight>(testProfile.Create<TProfile>("test_1d_TProfile_Weight", true));
Hist testProfile2d;
testProfile2d.AddAxis(ptAxis);
testProfile2d.AddAxis(etaAxis);
test->Add<test_2d_TProfile2D>(testProfile2d.Create<TProfile2D>("test_2d_TProfile2D"));
Hist testProfile3d;
testProfile3d.AddAxis(ptAxis);
testProfile3d.AddAxis(etaAxis);
testProfile3d.AddAxis(phiAxis);
test->Add<test_3d_TProfile3D>(testProfile3d.Create<TProfile3D>("test_3d_TProfile3D"));
// we can also re-use axis definitions in case they are similar in many histograms:
Hist baseDimensions({ptAxis, etaAxis, phiAxis, centAxis, cutAxis, centAxis});
Hist firstHist{baseDimensions};
firstHist.AddAxis("something", "v (m/s)", 10, -1, 1);
test->Add<test_7d_THnF_first>(firstHist.Create<THnF>("test_7d_THnF_first"));
Hist secondHist{baseDimensions};
secondHist.AddAxis("somethingElse", "a (m/(s*s))", 10, -1, 1);
test->Add<test_7d_THnF_second>(secondHist.Create<THnF>("test_7d_THnF_second"));
// or if we want to have the baseDimensions somewhere in between:
Hist thirdHist;
thirdHist.AddAxis("myFirstDimension", "a (m/(s*s))", 10, -1, 1);
thirdHist.AddAxes(baseDimensions);
thirdHist.AddAxis("myLastDimension", "a (m/(s*s))", 10, -1, 1);
test->Add<test_8d_THnC_third>(thirdHist.Create<THnC>("test_8d_THnC_third"));
// we can also use the Hist helper tool independent of the HistCollections:
Hist myHist;
myHist.AddAxis(ptAxis);
myHist.AddAxis(etaAxis);
myHist.AddAxis(phiAxis);
standaloneHist.setObject(myHist.Create<THnF>("standaloneHist"));
// now add some more useful histograms
kine->Add<pt>(new TH1F("pt", "p_{T};p_{T} [GeV/c]", 100, 0., 5.));
kine->Add<eta>(new TH1F("eta", "#eta;#eta", 101, -1.0, 1.0));
kine->Add<phi>(new TH1F("phi", "#phi;#phi [rad]", 100, 0., 2 * M_PI));
tpc->Add<tpcNClsFound>(new TH1F("tpcNClsFound", "number of found TPC clusters;# clusters TPC", 165, -0.5, 164.5));
tpc->Add<tpcNClsCrossedRows>(new TH1F("tpcNClsCrossedRows", "number of crossed TPC rows;# crossed rows TPC", 165, -0.5, 164.5));
tpc->Add<tpcChi2NCl>(new TH1F("tpcChi2NCl", "chi2 per cluster in TPC;chi2 / cluster TPC", 100, 0, 10));
its->Add<itsChi2NCl>(new TH1F("itsChi2NCl", "chi2 per ITS cluster;chi2 / cluster ITS", 100, 0, 40));
}
void process(soa::Join<aod::Tracks, aod::TracksExtra>::iterator const& track)
{
test->Fill<test_1d_TH1D>(20.);
test->Fill<test_2d_TH2F>(0.1, 0.3);
test->Fill<test_3d_TH3I>(10, 10, 15.5);
// this time fill the 1d histogram with weight of 10:
test->FillWeight<test_1d_TH1D_Weight>(20., 10.);
test->Fill<test_2d_TH2F_VarBinningY>(0.1, 0.3);
test->Fill<test_3d_THnI>(1., 0., 1.5);
test->Fill<test_5d_THnSparseI>(1., 0., 1.5, 30, 1);
// we can also directly access to the underlying histogram
// for this the correct type has to be known (TH1, TH2, TH3, THn, THnSparse, TProfile, TProfile2D or TProfile3D)
test->Get<TH2>(test_2d_TH2F)->Fill(0.2, 0.2);
test->Fill<test_1d_TProfile>(track.pt(), track.eta());
// now fill same histogram, but with random weight
test->FillWeight<test_1d_TProfile_Weight>(track.pt(), track.eta(), std::rand());
test->Fill<test_2d_TProfile2D>(track.pt(), track.eta(), track.phi());
test->Fill<test_3d_TProfile3D>(track.pt(), track.eta(), track.phi(), track.tpcNClsFound());
kine->Fill<pt>(track.pt());
kine->Fill<eta>(track.eta());
kine->Fill<phi>(track.phi());
tpc->Fill<tpcNClsFound>(track.tpcNClsFound());
tpc->Fill<tpcNClsCrossedRows>(track.tpcNClsCrossedRows());
tpc->Fill<tpcChi2NCl>(track.tpcChi2NCl());
its->Fill<itsChi2NCl>(track.itsChi2NCl());
double dummyArray[] = {track.pt(), track.eta(), track.phi()};
standaloneHist->Fill(dummyArray);
}
};
//****************************************************************************************
/**
* Workflow definition.
*/
//****************************************************************************************
WorkflowSpec defineDataProcessing(ConfigContext const& cfgc)
{
return WorkflowSpec{
adaptAnalysisTask<HistHelpersTest>(cfgc, TaskName{"hist-helpers-test"})};
}