4444#include " TSpectrum2.h"
4545#include " TPolyMarker.h"
4646#include " TList.h"
47- #include " TH1 .h"
47+ #include " TH2 .h"
4848#include " TMath.h"
49+ #include " TVirtualPad.h"
4950#define PEAK_WINDOW 1024
5051
5152Int_t TSpectrum2::fgIterations = 3 ;
@@ -122,28 +123,15 @@ void TSpectrum2::SetDeconIterations(Int_t n)
122123// /
123124// / Function parameters:
124125// / - h: input 2-d histogram
125- // / - numberIterations, (default value = 20)
126- // / Increasing numberIterations make the result smoother and lower.
126+ // / - nIterX, nIterY, (default value = 20), iterations for X and Y
127+ // / Increasing number of iterations make the result smoother and lower.
127128// / - option: may contain one of the following options
128- // / - to set the direction parameter
129- // / "BackIncreasingWindow". By default the direction is BackDecreasingWindow
130- // / - filterOrder-order of clipping filter. Possible values:
131- // / - "BackOrder2" (default)
132- // / - "BackOrder4"
133- // / - "BackOrder6"
134- // / - "BackOrder8"
135- // / - "nosmoothing"- if selected, the background is not smoothed
136- // / By default the background is smoothed.
137- // / - smoothWindow-width of smoothing window. Possible values:
138- // / - "BackSmoothing3" (default)
139- // / - "BackSmoothing5"
140- // / - "BackSmoothing7"
141- // / - "BackSmoothing9"
142- // / - "BackSmoothing11"
143- // / - "BackSmoothing13"
144- // / - "BackSmoothing15"
145- // / - "Compton" if selected the estimation of Compton edge
146- // / will be included.
129+ // / - direction of change of clipping window
130+ // / - possible values=kBackIncreasingWindow
131+ // / kBackDecreasingWindow
132+ // / - filterType-determines the algorithm of the filtering
133+ // / - possible values=kBackSuccessiveFiltering
134+ // / kBackOneStepFiltering
147135// / - "same" : if this option is specified, the resulting background
148136// / histogram is superimposed on the picture in the current pad.
149137// /
@@ -153,12 +141,67 @@ void TSpectrum2::SetDeconIterations(Int_t n)
153141// / as the input histogram h, but only bins from binmin to binmax will be filled
154142// / with the estimated background.
155143
156- TH1 *TSpectrum2::Background (const TH1 * h, Int_t number_of_iterations,
157- Option_t * option)
144+ TH1 *TSpectrum2::Background (const TH1 *h, Int_t nIterX, Int_t nIterY, Option_t *option)
158145{
159- Error (" Background" ," function not yet implemented: h=%s, iter=%d, option=%sn"
160- , h->GetName (), number_of_iterations, option);
161- return nullptr ;
146+ if (h == nullptr )
147+ return nullptr ;
148+ Int_t dimension = h->GetDimension ();
149+ if (dimension != 2 ) {
150+ Error (" Background" , " Only implemented for 2-d histograms" );
151+ return nullptr ;
152+ }
153+ TString opt = option;
154+ opt.ToLower ();
155+
156+ // set options
157+ Int_t direction = kBackDecreasingWindow ;
158+ if (opt.Contains (" backincreasingwindow" ))
159+ direction = kBackIncreasingWindow ;
160+ Int_t filterType = kBackSuccessiveFiltering ;
161+ if (opt.Contains (" backonestepfiltering" ))
162+ filterType = kBackOneStepFiltering ;
163+ Int_t firstX = h->GetXaxis ()->GetFirst ();
164+ Int_t lastX = h->GetXaxis ()->GetLast ();
165+ Int_t sizeX = lastX - firstX + 1 ;
166+ Int_t firstY = h->GetYaxis ()->GetFirst ();
167+ Int_t lastY = h->GetYaxis ()->GetLast ();
168+ Int_t sizeY = lastY - firstY + 1 ;
169+ Int_t i, j;
170+ Double_t **source = new Double_t *[sizeX];
171+ for (i = 0 ; i < sizeX; i++) {
172+ source[i] = new Double_t[sizeY];
173+ for (j = 0 ; j < sizeY; j++)
174+ source[i][j] = h->GetBinContent (i + firstX, j + firstY);
175+ }
176+
177+ // find background (source is input and in output contains the background
178+ Background (source, sizeX, sizeY, nIterX, nIterY, direction, filterType);
179+
180+ // create output histogram containing background
181+ // only bins in the range of the input histogram are filled
182+ Int_t nch = strlen (h->GetName ());
183+ char *hbname = new char [nch + 20 ];
184+ snprintf (hbname, nch + 20 , " %s_background" , h->GetName ());
185+ TH2 *hb = (TH2 *)h->Clone (hbname);
186+ hb->Reset ();
187+ hb->GetListOfFunctions ()->Delete ();
188+ for (i = 0 ; i < sizeX; i++)
189+ for (j = 0 ; j < sizeY; j++)
190+ hb->SetBinContent (i + firstX, j + firstY, source[i][j]);
191+ hb->SetEntries (sizeX * sizeY);
192+
193+ // if option "same is specified, draw the result in the pad
194+ if (opt.Contains (" same" )) {
195+ if (gPad )
196+ delete gPad ->GetPrimitive (hbname);
197+ hb->Draw (" same" );
198+ }
199+ for (i = 0 ; i < sizeX; i++) {
200+ delete[] source[i];
201+ }
202+ delete[] source;
203+ delete[] hbname;
204+ return hb;
162205}
163206
164207// //////////////////////////////////////////////////////////////////////////////
@@ -1706,7 +1749,7 @@ Int_t TSpectrum2::SearchHighRes(Double_t **source, Double_t **dest, Int_t ssizex
17061749}
17071750
17081751// //////////////////////////////////////////////////////////////////////////////
1709- // / static function (called by TH1 ), interface to TSpectrum2::Search
1752+ // / static function (called by TH2 ), interface to TSpectrum2::Search
17101753
17111754Int_t TSpectrum2::StaticSearch (const TH1 *hist, Double_t sigma, Option_t *option, Double_t threshold)
17121755{
@@ -1715,10 +1758,10 @@ Int_t TSpectrum2::StaticSearch(const TH1 *hist, Double_t sigma, Option_t *option
17151758}
17161759
17171760// //////////////////////////////////////////////////////////////////////////////
1718- // / static function (called by TH1 ), interface to TSpectrum2::Background
1761+ // / static function (called by TH2 ), interface to TSpectrum2::Background
17191762
1720- TH1 *TSpectrum2::StaticBackground (const TH1 *hist,Int_t niter , Option_t *option)
1763+ TH1 *TSpectrum2::StaticBackground (const TH1 *hist, Int_t nIterX, Int_t nIterY , Option_t *option)
17211764{
17221765 TSpectrum2 s;
1723- return s.Background (hist,niter, option);
1766+ return s.Background (hist, nIterX, nIterY, option);
17241767}
0 commit comments