Project
Loading...
Searching...
No Matches
StepTHn.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef StepTHn_H
13#define StepTHn_H
14
15// optimized data container reusing functionality of THn
16// A THnSparse is used to have the axis functionality "for free"
17
18#include "TNamed.h"
19#include "THnSparse.h"
20#include "TAxis.h"
21#include "TArray.h"
22
23#include "Framework/Logger.h"
24
25class TArray;
26class TArrayF;
27class TArrayD;
28class TCollection;
29
30class StepTHn : public TNamed
31{
32 public:
33 StepTHn();
34 StepTHn(const Char_t* name, const Char_t* title, const Int_t nSteps, const Int_t nAxes);
35 ~StepTHn() override;
36
37 template <typename... Ts>
38 void Fill(int iStep, const Ts&... valuesAndWeight);
39 void Fill(int iStep, int nParams, double positionAndWeight[]);
40
41 THnBase* getTHn(Int_t step, Bool_t sparse = kFALSE)
42 {
43 if (!mTarget || !mTarget[step]) {
44 createTarget(step, sparse);
45 }
46 return mTarget[step];
47 }
48 Int_t getNSteps() { return mNSteps; }
49 Int_t getNVar() { return mNVars; }
50
51 TArray* getValues(Int_t step) { return mValues[step]; }
52 TArray* getSumw2(Int_t step) { return mSumw2[step]; }
53
54 StepTHn(const StepTHn& c);
55 StepTHn& operator=(const StepTHn& corr);
56 void Copy(TObject& c) const override;
57
58 virtual Long64_t Merge(TCollection* list) = 0;
59
60 TAxis* GetAxis(int i) { return mPrototype->GetAxis(i); }
61 void Sumw2() {}; // TODO: added for compatibiltiy with registry, but maybe it would be useful also in StepTHn as toggle for error weights
62
63 protected:
64 void init();
65 virtual TArray* createArray(const TArray* src = nullptr) const = 0;
66 void createTarget(Int_t step, Bool_t sparse);
67 void deleteContainers();
68
69 Long64_t getGlobalBinIndex(const Int_t* binIdx);
70 virtual void updateBin(int iStep, Long64_t bin, double weight) = 0;
71
72 Long64_t mNBins; // number of total bins
73 Int_t mNVars; // number of variables
74 Int_t mNSteps; // number of selection steps
75 TArray** mValues; //[mNSteps] data container
76 TArray** mSumw2; //[mNSteps] data container
77
78 THnBase** mTarget;
79
80 TAxis** mAxisCache;
81 Int_t* mNbinsCache;
82 Double_t* mLastVars;
83 Int_t* mLastBins;
84
85 // Fast bin lookup table: for each axis, maps a quantized position to an approximate bin.
86 static constexpr Int_t kLookupSize = 1024; // number of slots per axis
87 struct AxisLookup {
88 Double_t invSlotWidth; // 1.0 / slot width for fast index computation
89 Double_t xmin; // axis minimum
90 Double_t xmax; // axis maximum
91 const Double_t* edges; // pointer to bin edges array (nBins+1 entries)
92 Int_t nBins; // number of bins
93 Int_t table[kLookupSize]; // slot -> bin index (1-based, TAxis convention)
94 };
96
97 THnSparse* mPrototype; // not filled used as prototype histogram for axis functionality etc.
98
99 ClassDef(StepTHn, 1) // THn like container
100};
101
102template <class TemplateArray>
103class StepTHnT : public StepTHn
104{
105 public:
107 StepTHnT(const Char_t* name, const Char_t* title, const Int_t nSteps, const Int_t nAxes, Int_t* nBins, std::vector<Double_t> binEdges[], const char** axisTitles);
108 StepTHnT(const char* name, const char* title, const int nSteps, const int nAxes, const int* nBins, const double* xmin, const double* xmax);
109 ~StepTHnT() override = default;
110
111 Long64_t Merge(TCollection* list) override;
112
113 protected:
114 TArray* createArray(const TArray* src = nullptr) const override
115 {
116 if (src == nullptr) {
117 return new TemplateArray(mNBins);
118 } else {
119 return new TemplateArray(*((TemplateArray*)src));
120 }
121 }
122
123 void updateBin(int iStep, Long64_t bin, double weight) override
124 {
125 if (!mValues[iStep]) {
126 mValues[iStep] = createArray();
127 LOGF(info, "Created values container for step %d", iStep);
128 }
129
130 if (weight != 1.) {
131 if (!mSumw2[iStep]) {
132 mSumw2[iStep] = createArray(mValues[iStep]);
133 LOGF(info, "Created sumw2 container for step %d", iStep);
134 }
135 }
136
137 auto* arr = static_cast<TemplateArray*>(mValues[iStep])->GetArray();
138 arr[bin] += weight;
139 if (mSumw2[iStep]) {
140 auto* sw2 = static_cast<TemplateArray*>(mSumw2[iStep])->GetArray();
141 sw2[bin] += weight * weight;
142 }
143 }
144
145 ClassDef(StepTHnT, 1) // THn like container
146};
147
150
151template <typename... Ts>
152void StepTHn::Fill(int iStep, const Ts&... valuesAndWeight)
153{
154 constexpr int nArgs = sizeof...(Ts);
155 double tempArray[] = {static_cast<double>(valuesAndWeight)...};
156 Fill(iStep, nArgs, tempArray);
157}
158
159#endif
int32_t i
uint32_t c
Definition RawData.h:2
StepTHnT< TArrayF > StepTHnF
Definition StepTHn.h:148
StepTHnT< TArrayD > StepTHnD
Definition StepTHn.h:149
StepTHnT()
Definition StepTHn.h:106
void updateBin(int iStep, Long64_t bin, double weight) override
Definition StepTHn.h:123
TArray * createArray(const TArray *src=nullptr) const override
Definition StepTHn.h:114
~StepTHnT() override=default
Long64_t Merge(TCollection *list) override
Definition StepTHn.cxx:229
Double_t * mLastVars
cache Nbins per axis
Definition StepTHn.h:82
Int_t * mLastBins
caching of last used bins (in many loops some vars are the same for a while)
Definition StepTHn.h:83
Int_t getNSteps()
Definition StepTHn.h:48
void Fill(int iStep, const Ts &... valuesAndWeight)
Definition StepTHn.h:152
Int_t getNVar()
Definition StepTHn.h:49
StepTHn()
Definition StepTHn.cxx:32
Long64_t mNBins
Definition StepTHn.h:72
TAxis * GetAxis(int i)
Definition StepTHn.h:60
TArray ** mValues
Definition StepTHn.h:75
TAxis ** mAxisCache
target histogram
Definition StepTHn.h:80
Long64_t getGlobalBinIndex(const Int_t *binIdx)
Definition StepTHn.cxx:285
Int_t mNSteps
Definition StepTHn.h:74
StepTHn & operator=(const StepTHn &corr)
Definition StepTHn.cxx:184
virtual TArray * createArray(const TArray *src=nullptr) const =0
Int_t * mNbinsCache
cache axis pointers (about 50% of the time in Fill is spent in GetAxis otherwise)
Definition StepTHn.h:81
virtual void updateBin(int iStep, Long64_t bin, double weight)=0
virtual Long64_t Merge(TCollection *list)=0
void deleteContainers()
Definition StepTHn.cxx:162
void createTarget(Int_t step, Bool_t sparse)
Definition StepTHn.cxx:300
TArray * getValues(Int_t step)
Definition StepTHn.h:51
AxisLookup * mLookup
Definition StepTHn.h:95
THnBase * getTHn(Int_t step, Bool_t sparse=kFALSE)
Definition StepTHn.h:41
THnSparse * mPrototype
per-axis lookup tables
Definition StepTHn.h:97
TArray ** mSumw2
Definition StepTHn.h:76
TArray * getSumw2(Int_t step)
Definition StepTHn.h:52
static constexpr Int_t kLookupSize
caching of last used bins (in many loops some vars are the same for a while)
Definition StepTHn.h:86
void init()
Definition StepTHn.cxx:112
THnBase ** mTarget
Definition StepTHn.h:78
Int_t mNVars
Definition StepTHn.h:73
~StepTHn() override
Definition StepTHn.cxx:145
void Sumw2()
Definition StepTHn.h:61
GLenum src
Definition glcorearb.h:1767
GLuint const GLchar * name
Definition glcorearb.h:781
GLuint GLuint GLfloat weight
Definition glcorearb.h:5477
Int_t table[kLookupSize]
Definition StepTHn.h:93
const Double_t * edges
Definition StepTHn.h:91
Double_t invSlotWidth
Definition StepTHn.h:88