Skip to content

Commit d3559b5

Browse files
ferdymercuryguitargeek
authored andcommitted
[hist] Add "width" option to TH2/3 Projection methods
TH2::ProjectionX,Y, TH2::ProfileX,Y TH3::ProjectionX,Y,Z, TH3::Project3D, TH3:Project3DProfile Fixes https://its.cern.ch/jira/browse/ROOT-10121 See https://root-forum.cern.ch/t/projections-of-histograms-width-option-extension-proposal/34071 See https://root-forum.cern.ch/t/integral-over-2d-histogram/34048
1 parent 9ddc709 commit d3559b5

File tree

6 files changed

+100
-65
lines changed

6 files changed

+100
-65
lines changed

hist/hist/inc/TH3.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,17 @@ class TH3 : public TH1, public TAtt3D {
151151
virtual TH1D *DoProject1D(const char* name, const char * title, int imin1, int imax1, int imin2, int imax2,
152152
const TAxis* projAxis, const TAxis * axis1, const TAxis * axis2, Option_t * option) const;
153153
virtual TH1D *DoProject1D(const char *name, const char *title, const TAxis *projAxis, const TAxis *axis1,
154-
const TAxis *axis2, bool computeErrors, bool originalRange, bool useUF, bool useOF) const;
154+
const TAxis *axis2, bool computeErrors, bool originalRange, bool useUF, bool useOF, bool useWidth) const;
155155
virtual TH2D *DoProject2D(const char* name, const char * title, const TAxis* projX, const TAxis* projY,
156-
bool computeErrors, bool originalRange, bool useUF, bool useOF) const;
156+
bool computeErrors, bool originalRange, bool useUF, bool useOF, bool useWidth) const;
157157
virtual TProfile2D *DoProjectProfile2D(const char* name, const char * title, const TAxis* projX, const TAxis* projY,
158-
bool originalRange, bool useUF, bool useOF) const;
158+
bool originalRange, bool useUF, bool useOF, bool useWidth) const;
159159

160160
// these functions are need to be used inside TProfile3D::DoProjectProfile2D
161161
static TH1D *DoProject1D(const TH3 & h, const char* name, const char * title, const TAxis* projX,
162-
bool computeErrors, bool originalRange, bool useUF, bool useOF);
162+
bool computeErrors, bool originalRange, bool useUF, bool useOF, bool useWidth);
163163
static TH2D *DoProject2D(const TH3 & h, const char* name, const char * title, const TAxis* projX, const TAxis* projY,
164-
bool computeErrors, bool originalRange, bool useUF, bool useOF);
164+
bool computeErrors, bool originalRange, bool useUF, bool useOF, bool useWidth);
165165

166166
ClassDefOverride(TH3,6) //3-Dim histogram base class
167167
};

hist/hist/inc/TProfile3D.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class TProfile3D : public TH3D {
6767
Double_t GetBinErrorSqUnchecked(Int_t bin) const override { Double_t err = GetBinError(bin); return err*err; }
6868

6969
TProfile2D *DoProjectProfile2D(const char* name, const char * title, const TAxis* projX, const TAxis* projY,
70-
bool originalRange, bool useUF, bool useOF) const override;
70+
bool originalRange, bool useUF, bool useOF, bool useWidth) const override;
7171

7272
private:
7373
Double_t *GetB() {return &fBinEntries.fArray[0];}

hist/hist/src/TH2.cxx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@ TProfile *TH2::DoProfile(bool onX, const char *name, Int_t firstbin, Int_t lastb
18511851
}
18521852
opt.ToLower();
18531853
bool originalRange = opt.Contains("o");
1854+
bool useWidth = opt.Contains("width");
18541855

18551856
const TAxis& outAxis = ( onX ? fXaxis : fYaxis );
18561857
const TAxis& inAxis = ( onX ? fYaxis : fXaxis );
@@ -1990,13 +1991,13 @@ TProfile *TH2::DoProfile(bool onX, const char *name, Int_t firstbin, Int_t lastb
19901991
}
19911992
Int_t bin = GetBin(binx, biny);
19921993
Double_t cxy = RetrieveBinContent(bin);
1993-
1994+
double step = useWidth ? inAxis.GetBinWidth(inbin) : 1;
19941995

19951996
if (cxy) {
19961997
Double_t tmp = 0;
19971998
// the following fill update wrongly the fBinSumw2- need to save it before
19981999
if ( useWeights ) tmp = binSumw2.fArray[binOut];
1999-
h1->Fill( xOut, inAxis.GetBinCenter(inbin), cxy );
2000+
h1->Fill( xOut, inAxis.GetBinCenter(inbin), cxy * step);
20002001
if ( useWeights ) binSumw2.fArray[binOut] = tmp + fSumw2.fArray[bin];
20012002
}
20022003

@@ -2026,9 +2027,9 @@ TProfile *TH2::DoProfile(bool onX, const char *name, Int_t firstbin, Int_t lastb
20262027

20272028

20282029
////////////////////////////////////////////////////////////////////////////////
2029-
/// Project a 2-D histogram into a profile histogram along X.
2030+
/// Project a 2-D histogram into a profile histogram along X (integration along Y).
20302031
///
2031-
/// The projection is made from the channels along the Y axis
2032+
/// The projection is made from summing the channels along the Y axis
20322033
/// ranging from firstybin to lastybin included.
20332034
/// By default, bins 1 to ny are included
20342035
/// When all bins are included, the number of entries in the projection
@@ -2040,6 +2041,9 @@ TProfile *TH2::DoProfile(bool onX, const char *name, Int_t firstbin, Int_t lastb
20402041
/// if option "o" original axis range of the target axes will be
20412042
/// kept, but only bins inside the selected range will be filled.
20422043
///
2044+
/// if option "width" is specified, each bin content is multiplied
2045+
/// by its Y bin-width during projection
2046+
///
20432047
/// The option can also be used to specify the projected profile error type.
20442048
/// Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
20452049
///
@@ -2076,9 +2080,9 @@ TProfile *TH2::ProfileX(const char *name, Int_t firstybin, Int_t lastybin, Optio
20762080

20772081

20782082
////////////////////////////////////////////////////////////////////////////////
2079-
/// Project a 2-D histogram into a profile histogram along Y.
2083+
/// Project a 2-D histogram into a profile histogram along Y (integration along X).
20802084
///
2081-
/// The projection is made from the channels along the X axis
2085+
/// The projection is made from summing the channels along the X axis
20822086
/// ranging from firstxbin to lastxbin included.
20832087
/// By default, bins 1 to nx are included
20842088
/// When all bins are included, the number of entries in the projection
@@ -2090,6 +2094,9 @@ TProfile *TH2::ProfileX(const char *name, Int_t firstybin, Int_t lastybin, Optio
20902094
/// if option "o" , the original axis range of the target axis will be
20912095
/// kept, but only bins inside the selected range will be filled.
20922096
///
2097+
/// if option "width" is specified, each bin content is multiplied
2098+
/// by its X bin-width during projection
2099+
///
20932100
/// The option can also be used to specify the projected profile error type.
20942101
/// Values which can be used are 's', 'i', or 'g'. See TProfile::BuildOptions for details
20952102
/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
@@ -2144,6 +2151,7 @@ TH1D *TH2::DoProjection(bool onX, const char *name, Int_t firstbin, Int_t lastbi
21442151
}
21452152
opt.ToLower(); //must be called after having parsed the cut name
21462153
bool originalRange = opt.Contains("o");
2154+
bool useWidth = opt.Contains("width");
21472155

21482156
if ( onX )
21492157
{
@@ -2286,9 +2294,10 @@ TH1D *TH2::DoProjection(bool onX, const char *name, Int_t firstbin, Int_t lastbi
22862294
if (!fPainter->IsInside(binx,biny)) continue;
22872295
}
22882296
// sum bin content and error if needed
2289-
cont += GetBinContent(binx,biny);
2297+
double step = useWidth ? inAxis->GetBinWidth(inbin) : 1;
2298+
cont += GetBinContent(binx,biny)*step;
22902299
if (computeErrors) {
2291-
Double_t exy = GetBinError(binx,biny);
2300+
Double_t exy = GetBinError(binx,biny)*step;
22922301
err2 += exy*exy;
22932302
}
22942303
}
@@ -2363,10 +2372,10 @@ TH1D *TH2::DoProjection(bool onX, const char *name, Int_t firstbin, Int_t lastbi
23632372

23642373

23652374
////////////////////////////////////////////////////////////////////////////////
2366-
/// Project a 2-D histogram into a 1-D histogram along X.
2375+
/// Project a 2-D histogram into a 1-D histogram along X (integration along Y).
23672376
///
23682377
/// The projection is always of the type TH1D.
2369-
/// The projection is made from the channels along the Y axis
2378+
/// The projection is made from summing the channels along the Y axis
23702379
/// ranging from firstybin to lastybin included.
23712380
/// By default, all bins including under- and overflow are included.
23722381
/// The number of entries in the projection is estimated from the
@@ -2380,6 +2389,9 @@ TH1D *TH2::DoProjection(bool onX, const char *name, Int_t firstbin, Int_t lastbi
23802389
/// if option "o" original axis range of the target axes will be
23812390
/// kept, but only bins inside the selected range will be filled.
23822391
///
2392+
/// if option "width" is specified, each bin content is multiplied
2393+
/// by its Y bin-width during projection
2394+
///
23832395
/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
23842396
/// One must create a graphical cut (mouse or C++) and specify the name
23852397
/// of the cut between [] in the option.
@@ -2402,10 +2414,10 @@ TH1D *TH2::ProjectionX(const char *name, Int_t firstybin, Int_t lastybin, Option
24022414

24032415

24042416
////////////////////////////////////////////////////////////////////////////////
2405-
/// Project a 2-D histogram into a 1-D histogram along Y.
2417+
/// Project a 2-D histogram into a 1-D histogram along Y (integration along X).
24062418
///
24072419
/// The projection is always of the type TH1D.
2408-
/// The projection is made from the channels along the X axis
2420+
/// The projection is made from summing the channels along the X axis
24092421
/// ranging from firstxbin to lastxbin included.
24102422
/// By default, all bins including under- and overflow are included.
24112423
/// The number of entries in the projection is estimated from the
@@ -2419,6 +2431,9 @@ TH1D *TH2::ProjectionX(const char *name, Int_t firstybin, Int_t lastybin, Option
24192431
/// if option "o" original axis range of the target axes will be
24202432
/// kept, but only bins inside the selected range will be filled.
24212433
///
2434+
/// if option "width" is specified, each bin content is multiplied
2435+
/// by its X bin-width during projection
2436+
///
24222437
/// Using a TCutG object, it is possible to select a sub-range of a 2-D histogram.
24232438
/// One must create a graphical cut (mouse or C++) and specify the name
24242439
/// of the cut between [] in the option.

0 commit comments

Comments
 (0)