Logo ROOT  
Reference Guide
TProfile2Poly.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Filip Ilic
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TProfile2Poly.h"
13#include "TProfileHelper.h"
14
15#include "TMultiGraph.h"
16#include "TGraph.h"
17#include "TList.h"
18#include "TMath.h"
19#include "Riostream.h"
20
21#include <cassert>
22#include <cmath>
23#include <set>
24
25/** \class TProfile2Poly
26 \ingroup Hist
272D Profile Histogram with Polygonal Bins.
28
29tprofile2polyRealisticModuleError.C and tprofile2polyRealistic.C illustrate how
30to use this class.
31*/
32
33/** \class TProfile2PolyBin
34 \ingroup Hist
35Helper class to represent a bin in the TProfile2Poly histogram
36*/
37
39
40////////////////////////////////////////////////////////////////////////////////
41/// TProfile2PolyBin constructor.
42
44{
45 fSumw = 0;
46 fSumvw = 0;
47 fSumw2 = 0;
48 fSumwv2 = 0;
49 fError = 0;
50 fAverage = 0;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// TProfile2PolyBin constructor.
56
57TProfile2PolyBin::TProfile2PolyBin(TObject *poly, Int_t bin_number) : TH2PolyBin(poly, bin_number)
58{
59 fSumw = 0;
60 fSumvw = 0;
61 fSumw2 = 0;
62 fSumwv2 = 0;
63 fError = 0;
64 fAverage = 0;
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Merge.
70
72{
73 this->fSumw += toMerge->fSumw;
74 this->fSumvw += toMerge->fSumvw;
75 this->fSumw2 += toMerge->fSumw2;
76 this->fSumwv2 += toMerge->fSumwv2;
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/// Update.
81
83{
86 SetChanged(true);
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Update average.
91
93{
94 if (fSumw != 0) fAverage = fSumvw / fSumw;
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Update error.
99
101{
102 Double_t tmp = 0;
103 if (fSumw != 0) tmp = std::sqrt((fSumwv2 / fSumw) - (fAverage * fAverage));
104
105 fError = tmp;
106
107 return;
108
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Clear statistics.
113
115{
116 fSumw = 0;
117 fSumvw = 0;
118 fSumw2 = 0;
119 fSumwv2 = 0;
120 fError = 0;
121 fAverage = 0;
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Fill.
126
128{
129 fSumw += weight;
130 fSumvw += value * weight;
131 fSumw2 += weight * weight;
132 fSumwv2 += weight * value * value;
133 this->Update();
134}
135
136////////////////////////////////////////////////////////////////////////////////
137/// TProfile2Poly constructor.
138
139TProfile2Poly::TProfile2Poly(const char *name, const char *title, Double_t xlow, Double_t xup, Double_t ylow,
140 Double_t yup)
141 : TH2Poly(name, title, xlow, xup, ylow, yup)
142{
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// TProfile2Poly constructor.
147
148TProfile2Poly::TProfile2Poly(const char *name, const char *title, Int_t nX, Double_t xlow, Double_t xup, Int_t nY,
149 Double_t ylow, Double_t yup)
150 : TH2Poly(name, title, nX, xlow, xup, nY, ylow, yup)
151{
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Create bin.
156
158{
159 if (!poly) return 0;
160
161 if (fBins == 0) {
162 fBins = new TList();
163 fBins->SetOwner();
164 }
165
166 fNcells++;
167 Int_t ibin = fNcells - kNOverflow;
168 return new TProfile2PolyBin(poly, ibin);
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Fill
173
175{
176 return Fill(xcoord, ycoord, value, 1);
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Fill
181
183{
184 // Find region in which the hit occurred
185 Int_t tmp = GetOverflowRegionFromCoordinates(xcoord, ycoord);
186 if (tmp < 0) {
187 Int_t overflow_idx = OverflowIdxToArrayIdx(tmp);
188 fOverflowBins[overflow_idx].Fill(value, weight);
189 fOverflowBins[overflow_idx].SetContent(fOverflowBins[overflow_idx].fAverage );
190 }
191
192 // Find the cell to which (x,y) coordinates belong to
193 Int_t n = (Int_t)(floor((xcoord - fXaxis.GetXmin()) / fStepX));
194 Int_t m = (Int_t)(floor((ycoord - fYaxis.GetXmin()) / fStepY));
195
196 // Make sure the array indices are correct.
197 if (n >= fCellX) n = fCellX - 1;
198 if (m >= fCellY) m = fCellY - 1;
199 if (n < 0) n = 0;
200 if (m < 0) m = 0;
201
202 // ------------ Update global (per histo) statistics
203 fTsumw += weight;
204 fTsumw2 += weight * weight;
205 fTsumwx += weight * xcoord;
206 fTsumwx2 += weight * xcoord * xcoord;
207 fTsumwy += weight * ycoord;
208 fTsumwy2 += weight * ycoord * ycoord;
209 fTsumwxy += weight * xcoord * ycoord;
210 fTsumwz += weight * value;
211 fTsumwz2 += weight * value * value;
212
213 // ------------ Update local (per bin) statistics
214 TProfile2PolyBin *bin;
215 TIter next(&fCells[n + fCellX * m]);
216 TObject *obj;
217 while ((obj = next())) {
218 bin = (TProfile2PolyBin *)obj;
219 if (bin->IsInside(xcoord, ycoord)) {
220 fEntries++;
221 bin->Fill(value, weight);
222 bin->Update();
223 bin->SetContent(bin->fAverage);
224 }
225 }
226
227 return tmp;
228}
229
230////////////////////////////////////////////////////////////////////////////////
231/// Merge
232
234{
235 Int_t size = in->GetSize();
236
237 std::vector<TProfile2Poly *> list;
238 list.reserve(size);
239
240 for (int i = 0; i < size; i++) {
241 list.push_back((TProfile2Poly *)((TList *)in)->At(i));
242 }
243 return this->Merge(list);
244}
245
246////////////////////////////////////////////////////////////////////////////////
247/// Merge
248
249Long64_t TProfile2Poly::Merge(const std::vector<TProfile2Poly *> &list)
250{
251 if (list.size() == 0) {
252 std::cout << "[FAIL] TProfile2Poly::Merge: No objects to be merged " << std::endl;
253 return -1;
254 }
255
256 // ------------ Check that bin numbers of TP2P's to be merged are equal
257 std::set<Int_t> numBinUnique;
258 for (const auto &histo : list) {
259 if (histo->fBins) numBinUnique.insert(histo->fBins->GetSize());
260 }
261 if (numBinUnique.size() != 1) {
262 std::cout << "[FAIL] TProfile2Poly::Merge: Bin numbers of TProfile2Polys to be merged differ!" << std::endl;
263 return -1;
264 }
265 Int_t nbins = *numBinUnique.begin();
266
267 // ------------ Update global (per histo) statistics
268 for (const auto &histo : list) {
269 this->fEntries += histo->fEntries;
270 this->fTsumw += histo->fTsumw;
271 this->fTsumw2 += histo->fTsumw2;
272 this->fTsumwx += histo->fTsumwx;
273 this->fTsumwx2 += histo->fTsumwx2;
274 this->fTsumwy += histo->fTsumwy;
275 this->fTsumwy2 += histo->fTsumwy2;
276 this->fTsumwxy += histo->fTsumwxy;
277 this->fTsumwz += histo->fTsumwz;
278 this->fTsumwz2 += histo->fTsumwz2;
279
280 // Merge overflow bins
281 for (Int_t i = 0; i < kNOverflow; ++i) {
282 this->fOverflowBins[i].Merge(&histo->fOverflowBins[i]);
283 }
284 }
285
286 // ------------ Update local (per bin) statistics
287 TProfile2PolyBin *dst = nullptr;
288 TProfile2PolyBin *src = nullptr;
289 for (Int_t i = 0; i < nbins; i++) {
290 dst = (TProfile2PolyBin *)fBins->At(i);
291
292 for (const auto &e : list) {
293 src = (TProfile2PolyBin *)e->fBins->At(i);
294 dst->Merge(src);
295 }
296
297 dst->Update();
298 }
299
300 this->SetContentToAverage();
301 return 1;
302}
303
304////////////////////////////////////////////////////////////////////////////////
305/// Set content to average.
306
308{
309 Int_t nbins = fBins ? fBins->GetSize() : 0;
310 for (Int_t i = 0; i < nbins; i++) {
312 bin->Update();
313 bin->SetContent(bin->fAverage);
314 }
315 for (Int_t i = 0; i < kNOverflow; ++i) {
317 bin.Update();
318 bin.SetContent(bin.fAverage);
319 }
320}
321
322////////////////////////////////////////////////////////////////////////////////
323/// Set content to error.
324
326{
327 Int_t nbins = fBins ? fBins->GetSize() : 0;
328 for (Int_t i = 0; i < nbins; i++) {
330 bin->Update();
331 bin->SetContent(bin->fError);
332 }
333 for (Int_t i = 0; i < kNOverflow; ++i) {
335 bin.Update();
336 bin.SetContent(bin.fError);
337 }
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// Get bin content.
342
344{
345 if (bin > GetNumberOfBins() || bin == 0 || bin < -kNOverflow) return 0;
346 if (bin<0) return fOverflowBins[-bin - 1].GetContent();
347 return ((TProfile2PolyBin*) fBins->At(bin-1))->GetContent();
348}
349
350
351////////////////////////////////////////////////////////////////////////////////
352/// Get bin effective entries.
353
355{
356 if (bin > GetNumberOfBins() || bin == 0 || bin < -kNOverflow) return 0;
357 if (bin < 0) return fOverflowBins[-bin - 1].GetEffectiveEntries();
358 return ((TProfile2PolyBin *)fBins->At(bin - 1))->GetEffectiveEntries();
359}
360
361////////////////////////////////////////////////////////////////////////////////
362/// Get bin entries.
363
365{
366 if (bin > GetNumberOfBins() || bin == 0 || bin < -kNOverflow) return 0;
367 if (bin < 0) return fOverflowBins[-bin - 1].GetEntries();
368 return ((TProfile2PolyBin *)fBins->At(bin - 1))->GetEntries();
369}
370
371////////////////////////////////////////////////////////////////////////////////
372/// Get bin entries W2.
373
375{
376 if (bin > GetNumberOfBins() || bin == 0 || bin < -kNOverflow) return 0;
377 if (bin < 0) return fOverflowBins[-bin - 1].GetEntriesW2();
378 return ((TProfile2PolyBin *)fBins->At(bin - 1))->GetEntriesW2();
379}
380
381////////////////////////////////////////////////////////////////////////////////
382/// Get bin entries VW.
383
385{
386 if (bin > GetNumberOfBins() || bin == 0 || bin < -kNOverflow) return 0;
387 if (bin < 0) return fOverflowBins[-bin - 1].GetEntriesVW();
388 return ((TProfile2PolyBin *)fBins->At(bin - 1))->GetEntriesVW();
389}
390
391////////////////////////////////////////////////////////////////////////////////
392/// Get bin entries WV2.
393
395{
396 if (bin > GetNumberOfBins() || bin == 0 || bin < -kNOverflow) return 0;
397 if (bin < 0) return fOverflowBins[-bin - 1].GetEntriesWV2();
398 return ((TProfile2PolyBin *)fBins->At(bin - 1))->GetEntriesWV2();
399}
400
401////////////////////////////////////////////////////////////////////////////////
402/// Get bin error.
403
405{
406 Double_t tmp = 0;
407 if (bin > GetNumberOfBins() || bin == 0 || bin < -kNOverflow) return 0;
408 if (bin < 0)
409 tmp = fOverflowBins[-bin - 1].GetError();
410 else
411 tmp = ((TProfile2PolyBin *)fBins->At(bin - 1))->GetError();
412
413 return (fErrorMode == kERRORSPREAD) ? tmp : tmp / std::sqrt(GetBinEffectiveEntries(bin));
414
415}
416
417////////////////////////////////////////////////////////////////////////////////
418/// Fill the array stats from the contents of this profile.
419/// The array stats must be correctly dimensioned in the calling program.
420///
421/// - stats[0] = sumw
422/// - stats[1] = sumw2
423/// - stats[2] = sumwx
424/// - stats[3] = sumwx2
425/// - stats[4] = sumwy
426/// - stats[5] = sumwy2
427/// - stats[6] = sumwxy
428/// - stats[7] = sumwz
429/// - stats[8] = sumwz2
430///
431/// If no axis-subrange is specified (via TAxis::SetRange), the array stats
432/// is simply a copy of the statistics quantities computed at filling time.
433/// If a sub-range is specified, the function recomputes these quantities
434/// from the bin contents in the current axis range.
435
437{
438 stats[0] = fTsumw;
439 stats[1] = fTsumw2;
440 stats[2] = fTsumwx;
441 stats[3] = fTsumwx2;
442 stats[4] = fTsumwy;
443 stats[5] = fTsumwy2;
444 stats[6] = fTsumwxy;
445 stats[7] = fTsumwz;
446 stats[8] = fTsumwz2;
447}
448
449////////////////////////////////////////////////////////////////////////////////
450/// Print overflow regions.
451
453{
454 Double_t total = 0;
455 Double_t cont = 0;
456 for (Int_t i = 0; i < kNOverflow; ++i) {
457 cont = GetOverflowContent(i);
458 total += cont;
459 std::cout << "\t" << cont << "\t";
460 if ((i + 1) % 3 == 0) std::cout << std::endl;
461 }
462
463 std::cout << "Total: " << total << std::endl;
464}
465
466////////////////////////////////////////////////////////////////////////////////
467/// Reset
468
470{
471 TIter next(fBins);
472 TObject *obj;
473 TProfile2PolyBin *bin;
474
475 // Clears bin contents
476 while ((obj = next())) {
477 bin = (TProfile2PolyBin *)obj;
478 bin->ClearContent();
479 bin->ClearStats();
480 }
481 TH2::Reset(opt);
482}
483
484////////////////////////////////////////////////////////////////////////////////
485/// The overflow regions are calculated by considering x, y coordinates.
486/// The Middle bin at -5 contains all the TProfile2Poly bins.
487///
488/// ~~~ {.cpp}
489/// -0 -1 -2
490/// ________
491/// -1: |__|__|__|
492/// -4: |__|__|__|
493/// -7: |__|__|__|
494/// ~~~
495
497{
498
499
500 Int_t region = 0;
501
502 if (fNcells <= kNOverflow) return 0;
503
504 // --- y offset
505 if (y > fYaxis.GetXmax())
506 region += -1;
507 else if (y > fYaxis.GetXmin())
508 region += -4;
509 else
510 region += -7;
511
512 // --- x offset
513 if (x > fXaxis.GetXmax())
514 region += -2;
515 else if (x > fXaxis.GetXmin())
516 region += -1;
517 else
518 region += 0;
519
520 return region;
521}
522
523////////////////////////////////////////////////////////////////////////////////
524/// Set error option.
525
527{
529}
#define e(i)
Definition: RSha256.hxx:103
int Int_t
Definition: RtypesCore.h:43
double Double_t
Definition: RtypesCore.h:57
long long Long64_t
Definition: RtypesCore.h:71
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
static unsigned int total
char name[80]
Definition: TGX11.cxx:109
int type
Definition: TGX11.cxx:120
double floor(double)
double sqrt(double)
EErrorType
Definition: TProfile.h:28
@ kERRORSPREAD
Definition: TProfile.h:28
@ kERRORMEAN
Definition: TProfile.h:28
Double_t GetXmax() const
Definition: TAxis.h:134
Double_t GetXmin() const
Definition: TAxis.h:133
Collection abstract base class.
Definition: TCollection.h:63
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
Int_t fNcells
number of bins(1D), cells (2D) +U/Overflows
Definition: TH1.h:86
Double_t fTsumw
Total Sum of weights.
Definition: TH1.h:93
Double_t fTsumw2
Total Sum of squares of weights.
Definition: TH1.h:94
Double_t fTsumwx2
Total Sum of weight*X*X.
Definition: TH1.h:96
Double_t fEntries
Number of entries.
Definition: TH1.h:92
TAxis fXaxis
X axis descriptor.
Definition: TH1.h:87
TAxis fYaxis
Y axis descriptor.
Definition: TH1.h:88
Double_t fTsumwx
Total Sum of weight*X.
Definition: TH1.h:95
Helper class to represent a bin in the TH2Poly histogram.
Definition: TH2Poly.h:25
void ClearContent()
Definition: TH2Poly.h:32
Bool_t IsInside(Double_t x, Double_t y) const
Return "true" if the point (x,y) is inside the bin.
Definition: TH2Poly.cxx:1542
void SetContent(Double_t content)
Definition: TH2Poly.h:45
Double_t GetContent() const
Definition: TH2Poly.h:35
void SetChanged(Bool_t flag)
Definition: TH2Poly.h:44
2D Histogram with Polygonal Bins
Definition: TH2Poly.h:66
TList * fBins
List of bins. The list owns the contained objects.
Definition: TH2Poly.h:167
Int_t GetNumberOfBins() const
Definition: TH2Poly.h:110
@ kNOverflow
Definition: TH2Poly.h:154
Double_t fStepX
Definition: TH2Poly.h:161
Int_t fCellX
Number of partition cells in the x-direction of the histogram.
Definition: TH2Poly.h:157
Double_t fStepY
Dimensions of a partition cell.
Definition: TH2Poly.h:161
TList * fCells
[fNCells] The array of TLists that store the bins that intersect with each cell. List do not own the ...
Definition: TH2Poly.h:160
Int_t fCellY
Number of partition cells in the y-direction of the histogram.
Definition: TH2Poly.h:158
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition: TH2.cxx:2464
Double_t fTsumwxy
Definition: TH2.h:36
Double_t fTsumwy2
Definition: TH2.h:35
Double_t fTsumwy
Definition: TH2.h:34
A doubly linked list.
Definition: TList.h:44
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:356
Mother of all ROOT objects.
Definition: TObject.h:37
Helper class to represent a bin in the TProfile2Poly histogram.
Definition: TProfile2Poly.h:18
Double_t GetEntriesW2() const
Definition: TProfile2Poly.h:33
void Update()
Update.
Double_t GetEntries() const
Definition: TProfile2Poly.h:32
void Fill(Double_t value, Double_t weight)
Fill.
Double_t GetError() const
Definition: TProfile2Poly.h:36
Double_t GetEffectiveEntries() const
Definition: TProfile2Poly.h:31
TProfile2PolyBin()
TProfile2PolyBin constructor.
void UpdateAverage()
Update average.
Double_t GetEntriesVW() const
Definition: TProfile2Poly.h:34
void UpdateError()
Update error.
EErrorType fErrorMode
Definition: TProfile2Poly.h:46
Double_t GetEntriesWV2() const
Definition: TProfile2Poly.h:35
void Merge(const TProfile2PolyBin *toMerge)
Merge.
void ClearStats()
Clear statistics.
2D Profile Histogram with Polygonal Bins.
Definition: TProfile2Poly.h:57
void PrintOverflowRegions()
Print overflow regions.
Double_t GetOverflowContent(Int_t idx)
Definition: TProfile2Poly.h:98
EErrorType fErrorMode
virtual Int_t Fill(Double_t xcoord, Double_t ycoord, Double_t value) override
Fill.
Long64_t Merge(const std::vector< TProfile2Poly * > &list)
Merge.
Double_t fTsumwz2
Double_t GetBinEffectiveEntries(Int_t bin) const
Get bin effective entries.
virtual Double_t GetBinError(Int_t bin) const override
Get bin error.
Double_t GetBinEntriesVW(Int_t bin) const
Get bin entries VW.
Double_t GetBinEntriesW2(Int_t bin) const
Get bin entries W2.
Double_t fTsumwz
friend class TProfile2PolyBin
Definition: TProfile2Poly.h:58
void SetContentToError()
Set content to error.
virtual Double_t GetBinContent(Int_t bin) const override
Get bin content.
Int_t GetOverflowRegionFromCoordinates(Double_t x, Double_t y)
The overflow regions are calculated by considering x, y coordinates.
virtual void GetStats(Double_t *stats) const override
Fill the array stats from the contents of this profile.
virtual void Reset(Option_t *option="") override
Reset.
void SetContentToAverage()
Set content to average.
Double_t GetBinEntriesWV2(Int_t bin) const
Get bin entries WV2.
Double_t GetBinEntries(Int_t bin) const
Get bin entries.
void SetErrorOption(EErrorType type)
Set error option.
Int_t OverflowIdxToArrayIdx(Int_t val)
TProfile2PolyBin fOverflowBins[kNOverflow]
virtual TProfile2PolyBin * CreateBin(TObject *poly) override
Create bin.
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
auto * m
Definition: textangle.C:8