CXBgdUtility.cpp 22.5 KB
Newer Older
Jérémie Dudouet's avatar
Jérémie Dudouet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include "CXBgdUtility.h"

#include <iostream>
#include <iomanip>

#include "TFrame.h"
#include "TGCanvas.h"
#include "TGNumberEntry.h"
#include "TGLabel.h"
#include "TSpectrum.h"
#include "TSpectrum2.h"

using namespace std;

CXBgdUtility::CXBgdUtility(const TGCompositeFrame *MotherFrame, UInt_t w, UInt_t h) : TGVerticalFrame(MotherFrame, w, h, kFixedWidth)
{

    gClient->GetColorByName("red", red);
    gClient->GetColorByName("blue", blue);
    gClient->GetColorByName("black", black);

    fListOfButtons1D = new TList();

24 25
    auto *GroupHints = new TGLayoutHints(kLHintsCenterY | kLHintsLeft | kLHintsExpandX, -10, -10, 0, 0);
    auto *HFHints = new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,8,0);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
26

27
    auto *f1DGroupFrame = new TGGroupFrame(MotherFrame, "1D background", kVerticalFrame);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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
    f1DGroupFrame->SetTextColor(red);
    f1DGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    AddFrame(f1DGroupFrame, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 0, 0, 0, 0));

    //Activation du mode background supress

    fGroupFrame = new TGGroupFrame(f1DGroupFrame, "Activate utility", kVerticalFrame);
    fGroupFrame->SetTextColor(blue);
    fGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    f1DGroupFrame->AddFrame(fGroupFrame, GroupHints);

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "On"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 0, 0, 0, 0));
    fActivate = new TGCheckButton(fHorizontalFrame,"");
    fActivate->Connect("Clicked()","CXBgdUtility", this, "ToggleBckSupp()");
    fHorizontalFrame->AddFrame(fActivate, HFHints);
    fGroupFrame->AddFrame(fHorizontalFrame, GroupHints);


    ///Paramers

    fGroupFrame = new TGGroupFrame(f1DGroupFrame, "Parameters", kVerticalFrame);
    fGroupFrame->SetTextColor(blue);
    fGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    f1DGroupFrame->AddFrame(fGroupFrame, GroupHints);


    /// Iterations

    fNumberIterations1D = 20;
    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "Iterations"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 0, 0, 0));
    fNItersButton1D = new TGNumberEntry(fHorizontalFrame, fNumberIterations1D, 6,0, TGNumberFormat::kNESInteger, TGNumberFormat::kNEANonNegative);
    fNItersButton1D->Connect("ValueSet(Long_t)","CXBgdUtility", this, "GetParams()");
    fHorizontalFrame->AddFrame(fNItersButton1D,new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,0,0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);

    fListOfButtons1D->Add(fNItersButton1D);

    /// Window

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "Window direction :"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 0, 0, 0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);
    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fDirectionButton1D[0] = new TGRadioButton(fHorizontalFrame, "Increasing", 0);
    fDirectionButton1D[0]->Connect("Clicked()", "CXBgdUtility", this, "HandleWindowButtons()");
    fDirectionButton1D[0]->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
    fHorizontalFrame->AddFrame(fDirectionButton1D[0],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,20,0,0,0));
    fDirectionButton1D[1] = new TGRadioButton(fHorizontalFrame, "Decreasing", 1);
    fDirectionButton1D[1]->SetState(kButtonDown);
    fDirection1D = TSpectrum::kBackDecreasingWindow;
    fDirectionButton1D[1]->Connect("Clicked()", "CXBgdUtility", this, "HandleWindowButtons()");
    fDirectionButton1D[1]->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
    fHorizontalFrame->AddFrame(fDirectionButton1D[1],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,0,0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);

    fListOfButtons1D->Add(fDirectionButton1D[0]);
    fListOfButtons1D->Add(fDirectionButton1D[1]);

    /// Filter order

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "Filter order"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 0, 0, 0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);
    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fFilterOrderButton1D[0] = new TGRadioButton(fHorizontalFrame, "2", 0);
    fFilterOrderButton1D[0]->Connect("Clicked()", "CXBgdUtility", this, "HandleFilterButtons()");
    fFilterOrderButton1D[0]->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
    fHorizontalFrame->AddFrame(fFilterOrderButton1D[0],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,20,0,0,0));
    fFilterOrderButton1D[0]->SetState(kButtonDown);
    fFilterOrder1D = TSpectrum::kBackOrder2;

    fFilterOrderButton1D[1] = new TGRadioButton(fHorizontalFrame, "4", 1);
    fFilterOrderButton1D[1]->Connect("Clicked()", "CXBgdUtility", this, "HandleFilterButtons()");
    fFilterOrderButton1D[1]->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
    fHorizontalFrame->AddFrame(fFilterOrderButton1D[1],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,0,0));

    fFilterOrderButton1D[2] = new TGRadioButton(fHorizontalFrame, "6", 2);
    fFilterOrderButton1D[2]->Connect("Clicked()", "CXBgdUtility", this, "HandleFilterButtons()");
    fFilterOrderButton1D[2]->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
    fHorizontalFrame->AddFrame(fFilterOrderButton1D[2],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,0,0));

    fFilterOrderButton1D[3] = new TGRadioButton(fHorizontalFrame, "8", 3);
    fFilterOrderButton1D[3]->Connect("Clicked()", "CXBgdUtility", this, "HandleFilterButtons()");
    fFilterOrderButton1D[3]->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
    fHorizontalFrame->AddFrame(fFilterOrderButton1D[3],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,0,0));

    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);


    fListOfButtons1D->Add(fFilterOrderButton1D[0]);
    fListOfButtons1D->Add(fFilterOrderButton1D[1]);
    fListOfButtons1D->Add(fFilterOrderButton1D[2]);
    fListOfButtons1D->Add(fFilterOrderButton1D[3]);

    /// Smoothing

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "Smoothing : "),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 0, 0, 0));
    fDoSmoothing = new TGCheckButton(fHorizontalFrame,"",-2);
    fDoSmoothing->Connect("Clicked()", "CXBgdUtility", this, "HandleSmoothButtons()");
    fDoSmoothing->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
    fDoSmoothing->SetState(kButtonDown);
    fHorizontalFrame->AddFrame(fDoSmoothing, new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,0,0));
    fGroupFrame->AddFrame(fHorizontalFrame, HFHints);
    fSmoothing = true;
    fSmoothingWindow = 3;

    fListOfButtons1D->Add(fDoSmoothing);

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "Smoothing order : "),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 0, 0, 0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);
    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);

144 145 146 147 148 149 150 151 152
    for(int i=0 ; i<7 ; i++) {
        fSmoothingButton1D.at(i) = new TGRadioButton(fHorizontalFrame, Form("%d",3+2*i), i);
        if(i==0) fSmoothingButton1D.at(i)->SetState(kButtonDown);
        fSmoothingButton1D.at(i)->Connect("Clicked()", "CXBgdUtility", this, "HandleSmoothButtons()");
        fSmoothingButton1D.at(i)->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
        if(i==0) fHorizontalFrame->AddFrame(fSmoothingButton1D.at(i),new TGLayoutHints(kLHintsCenterY | kLHintsLeft,20,0,0,0));
        else fHorizontalFrame->AddFrame(fSmoothingButton1D.at(i),new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,0,0));

        fListOfButtons1D->Add(fSmoothingButton1D.at(i));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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
    }

    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);


    /// Compton

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "Compton : "),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 0, 0, 0));
    fDoCompton = new TGCheckButton(fHorizontalFrame,"");
    fDoCompton->Connect("Clicked()","CXBgdUtility", this, "GetParams()");
    fHorizontalFrame->AddFrame(fDoCompton, new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,0,0));
    fGroupFrame->AddFrame(fHorizontalFrame, HFHints);
    fCompton = false;

    fListOfButtons1D->Add(fDoCompton);


    /// Do subtract

    fGroupFrame = new TGGroupFrame(f1DGroupFrame, "Do subtract", kVerticalFrame);
    fGroupFrame->SetTextColor(blue);
    fGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    f1DGroupFrame->AddFrame(fGroupFrame, GroupHints);

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fSubtractButton = new TGTextButton(fHorizontalFrame, "Subtract");
    fSubtractButton->Connect("Clicked()", "CXBgdUtility", this, "DoSubtract()");
    fHorizontalFrame->AddFrame(fSubtractButton,new TGLayoutHints(kLHintsCenterY | kLHintsLeft | kLHintsExpandX,10,10,10,10));
    fGroupFrame->AddFrame(fHorizontalFrame, GroupHints);

    fListOfButtons1D->Add(fSubtractButton);

186
    SetButtonsStatus(false);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
187 188 189


    /// Background 2D
190
    auto *f2DGroupFrame = new TGGroupFrame(MotherFrame, "2D background", kVerticalFrame);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    f2DGroupFrame->SetTextColor(red);
    f2DGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    AddFrame(f2DGroupFrame, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 0, 0, 0, 0));

    fGroupFrame = new TGGroupFrame(f2DGroupFrame, "Parameters", kVerticalFrame);
    fGroupFrame->SetTextColor(blue);
    fGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    f2DGroupFrame->AddFrame(fGroupFrame, GroupHints);

    /// Iterations

    fNumberIterations2DX = 20;
    fNumberIterations2DY = 20;
    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "IterationsX"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 5, 0, 0));
    fNItersButton2D[0] = new TGNumberEntry(fHorizontalFrame, fNumberIterations2DX, 4,0, TGNumberFormat::kNESInteger, TGNumberFormat::kNEANonNegative);
    fHorizontalFrame->AddFrame(fNItersButton2D[0],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,10,5,0));
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "IterationsY"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 5, 0, 0));
    fNItersButton2D[1] = new TGNumberEntry(fHorizontalFrame, fNumberIterations2DY, 4,0, TGNumberFormat::kNESInteger, TGNumberFormat::kNEANonNegative);
    fHorizontalFrame->AddFrame(fNItersButton2D[1],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,0,5,0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);

    /// Window

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "Window direction :"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 0, 0, 0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);
    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fDirectionButton2D[0] = new TGRadioButton(fHorizontalFrame, "Increasing", 10);
    fDirectionButton2D[0]->Connect("Clicked()", "CXBgdUtility", this, "HandleWindowButtons()");
    fHorizontalFrame->AddFrame(fDirectionButton2D[0],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,20,0,0,0));
    fDirectionButton2D[1] = new TGRadioButton(fHorizontalFrame, "Decreasing", 11);
    fDirectionButton2D[1]->SetState(kButtonDown);
    fDirection2D = TSpectrum::kBackDecreasingWindow;
    fDirectionButton2D[1]->Connect("Clicked()", "CXBgdUtility", this, "HandleWindowButtons()");
    fHorizontalFrame->AddFrame(fDirectionButton2D[1],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,20,0,0,0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);

    /// Filter

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fHorizontalFrame->AddFrame(fLabel = new TGLabel(fHorizontalFrame, "Filter algorithm :"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 0, 0, 0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);
    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    fFilterOrderButton2D[0] = new TGRadioButton(fHorizontalFrame, "Successive", 10);
    fFilterOrderButton2D[0]->Connect("Clicked()", "CXBgdUtility", this, "HandleFilterButtons()");
    fFilterOrderButton2D[0]->SetState(kButtonDown);
    fFilterOrder2D = TSpectrum2::kBackSuccessiveFiltering;
    fHorizontalFrame->AddFrame(fFilterOrderButton2D[0],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,20,0,0,0));
    fFilterOrderButton2D[1] = new TGRadioButton(fHorizontalFrame, "OneStep", 11);
    fFilterOrderButton2D[1]->Connect("Clicked()", "CXBgdUtility", this, "HandleFilterButtons()");
    fHorizontalFrame->AddFrame(fFilterOrderButton2D[1],new TGLayoutHints(kLHintsCenterY | kLHintsLeft,20,0,0,0));
    fGroupFrame->AddFrame(fHorizontalFrame,HFHints);

    /// Eval

    fGroupFrame = new TGGroupFrame(f2DGroupFrame, "Evaluate background", kVerticalFrame);
    fGroupFrame->SetTextColor(blue);
    fGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    f2DGroupFrame->AddFrame(fGroupFrame, GroupHints);

    fHorizontalFrame = new TGCompositeFrame(fGroupFrame, 60, 20, kHorizontalFrame);
    f2DEvalButton = new TGTextButton(fHorizontalFrame, "Evaluate");
    f2DEvalButton->Connect("Clicked()", "CXBgdUtility", this, "Do2DEvaluation()");
    fHorizontalFrame->AddFrame(f2DEvalButton,new TGLayoutHints(kLHintsCenterY | kLHintsLeft | kLHintsExpandX,10,10,10,10));
    fGroupFrame->AddFrame(fHorizontalFrame, GroupHints);
}

259
CXBgdUtility::~CXBgdUtility() = default;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
260 261 262 263 264 265 266 267 268 269 270 271 272

void CXBgdUtility::DoSubtract()
{
    gPad = fMainWindow->GetSelectedPad();

    TH1 *hist = fMainWindow->GetHisto();

    if(hist == nullptr || hist->InheritsFrom("TH2"))
    {
        cout<<"No 1D istogram found, ignored"<<endl;
        return;
    }

273
    TH1 *bkgrd = dynamic_cast<TH1*>(gPad->GetListOfPrimitives()->FindObject(Form("%s_BG",hist->GetName())));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

    if(bkgrd == nullptr)
    {
        cout<<"No background histogram built in the pad for hist: "<<hist->GetName()<<endl;
        return;
    }

    hist->Add(bkgrd,-1.);
    delete bkgrd;
    hist->Draw(hist->GetDrawOption());

    gPad->Update();
    gPad->Modified();

    gPad->SetBit(TPad::kCannotMove);
    gPad->GetFrame()->SetBit(TObject::kCannotPick);

    fActivate->SetState(kButtonUp);
}

void CXBgdUtility::ToggleBckSupp()
{
    gPad = fMainWindow->GetSelectedPad();

    if(gPad == nullptr)
        return;

    if(fActivate->GetState() == kButtonDown)
    {
303
        SetButtonsStatus(true);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
304 305 306 307
        GetParams();
    }
    else
    {
308
        SetButtonsStatus(false);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

        for(int i=gPad->GetListOfPrimitives()->GetEntries()-1 ; i>=0 ; i--)
        {
            TObject *o = gPad->GetListOfPrimitives()->At(i);
            if(o->InheritsFrom("TH1") && ((TString)o->GetName()).EndsWith("_BG"))
            {
                delete o;
                break;
            }
        }
    }

    gPad->Modified();
    gPad->Update();
}

void CXBgdUtility::SetButtonsStatus(bool on)
{
    fNItersButton1D->SetState(on);

    for(int i=1 ; i<fListOfButtons1D->GetEntries() ; i++)
    {
331
        auto *btn = dynamic_cast<TGButton*>(fListOfButtons1D->At(i));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

        if(!fSmoothing && i>=8 && i<=14)
            continue;
        if(!on)
            btn->SetState(kButtonDisabled);
        else if(btn->IsDown())
            btn->SetState(kButtonDown);
        else
            btn->SetState(kButtonUp);
    }
}

void CXBgdUtility::GetParams()
{
    gPad = fMainWindow->GetSelectedPad();

    fNumberIterations1D = fNItersButton1D->GetIntNumber();
    fCompton = fDoCompton->GetState();

    TString Option = "same";

    if(fDirection1D == TSpectrum::kBackDecreasingWindow) Option += "BackDecreasingWindow";
    else if(fDirection1D == TSpectrum::kBackIncreasingWindow) Option += "BackIncreasingWindow";

    if(fFilterOrder1D == TSpectrum::kBackOrder2) Option += "BackOrder2";
    else if(fFilterOrder1D == TSpectrum::kBackOrder4) Option += "BackOrder4";
    else if(fFilterOrder1D == TSpectrum::kBackOrder6) Option += "BackOrder6";
    else if(fFilterOrder1D == TSpectrum::kBackOrder8) Option += "BackOrder8";

    if(!fSmoothing) Option += "nosmoothing";
    else
    {
        if(fSmoothingWindow == TSpectrum::kBackSmoothing3) Option += "BackSmoothing3";
        else if(fSmoothingWindow == TSpectrum::kBackSmoothing5) Option += "BackSmoothing5";
        else if(fSmoothingWindow == TSpectrum::kBackSmoothing7) Option += "BackSmoothing7";
        else if(fSmoothingWindow == TSpectrum::kBackSmoothing9) Option += "BackSmoothing9";
        else if(fSmoothingWindow == TSpectrum::kBackSmoothing11) Option += "BackSmoothing11";
        else if(fSmoothingWindow == TSpectrum::kBackSmoothing13) Option += "BackSmoothing13";
        else if(fSmoothingWindow == TSpectrum::kBackSmoothing15) Option += "BackSmoothing15";
        else Option += "nosmoothing";
    }
    if(fCompton) Option += "Compton";

375
    TH1 *hist = dynamic_cast<TH1F*>(fMainWindow->GetHisto());
Jérémie Dudouet's avatar
Jérémie Dudouet committed
376 377 378 379 380 381 382

    if(hist == nullptr || hist->InheritsFrom("TH2"))
    {
        cout<<"No 1D istogram found, ignored"<<endl;
        return;
    }

383
    TH1D *clone = dynamic_cast<TH1D*>(hist->Clone(Form("%s_clone",hist->GetName())));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
384 385 386
    for(int i=0 ; i<clone->GetNbinsX() ; i++)
        clone->SetBinContent(i+1,hist->GetBinContent(i+1));

387 388
    TH1 *OldBG = dynamic_cast<TH1*>(gPad->GetListOfPrimitives()->FindObject((TString)hist->GetName() + "_BG"));
    delete OldBG;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
389

390
    auto *tspec = new TSpectrum;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

    TH1 *Backgd = tspec->Background(clone,fNumberIterations1D,Option.Data());
    TString bgname = hist->GetName();
    bgname += "_BG";
    Backgd->SetName(bgname.Data());

    delete clone;
    delete tspec;

    gPad->Modified();
    gPad->Update();
}

void CXBgdUtility::HandleWindowButtons(Int_t id)
{
    // Handle different buttons
407 408
    auto *btn = static_cast<TGButton*>(gTQSender);

Jérémie Dudouet's avatar
Jérémie Dudouet committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    if (id == -1)
        id = btn->WidgetId();

    if(id == 0)
    {
        fDirectionButton1D[1]->SetState(kButtonUp);
        fDirection1D = TSpectrum::kBackIncreasingWindow;
    }
    if(id == 1)
    {
        fDirectionButton1D[0]->SetState(kButtonUp);
        fDirection1D = TSpectrum::kBackDecreasingWindow;
    }

    if(id == 10)
    {
        fDirectionButton2D[1]->SetState(kButtonUp);
        fDirection2D = TSpectrum::kBackIncreasingWindow;
    }
    if(id == 11)
    {
        fDirectionButton2D[0]->SetState(kButtonUp);
        fDirection2D = TSpectrum::kBackDecreasingWindow;
    }
}

void CXBgdUtility::HandleFilterButtons(Int_t id)
{
    // Handle different buttons
438
    auto *btn = static_cast<TGButton*>(gTQSender);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
439 440 441
    if (id == -1)
        id = btn->WidgetId();

442 443
    if(id<4) {
        for(int i=0 ; i<4 ; i++) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
444
            if(id != i)
445
                fFilterOrderButton1D.at(i)->SetState(kButtonUp);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
446 447 448 449 450
            else
                fFilterOrder1D = i;
        }
    }

451
    if(id == 10) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
452 453 454
        fFilterOrderButton2D[1]->SetState(kButtonUp);
        fFilterOrder2D = TSpectrum2::kBackSuccessiveFiltering;
    }
455
    if(id == 11) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
456 457 458 459 460 461 462 463
        fFilterOrderButton2D[0]->SetState(kButtonUp);
        fFilterOrder2D = TSpectrum2::kBackOneStepFiltering;
    }
}

void CXBgdUtility::HandleSmoothButtons(Int_t id)
{
    // Handle different buttons
464
    auto *btn = static_cast<TGButton*>(gTQSender);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
465 466 467 468 469 470 471 472 473 474
    if (id == -1)
        id = btn->WidgetId();

    if(id == -2)
    {
        if(btn->GetState() == kButtonDown)
        {
            fSmoothing = true;
            for(int i=0 ; i<7 ; i++)
            {
475
                if(fSmoothingButton1D.at(i)->IsDown())
Jérémie Dudouet's avatar
Jérémie Dudouet committed
476
                {
477
                    fSmoothingButton1D.at(i)->SetState(kButtonDown);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
478 479 480
                    fSmoothingWindow = 3+2*i;
                }
                else
481
                    fSmoothingButton1D.at(i)->SetState(kButtonUp);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
482 483 484 485 486 487
            }
        }
        else
        {
            fSmoothing = false;
            for(int i=0 ; i<7 ; i++)
488
                fSmoothingButton1D.at(i)->SetState(kButtonDisabled);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
489 490 491 492 493 494 495
        }
        return;
    }

    for(int i=0 ; i<7 ; i++)
    {
        if(id != i)
496
            fSmoothingButton1D.at(i)->SetState(kButtonUp);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
        else
            fSmoothingWindow = 3+2*i;
    }
}

void CXBgdUtility::Do2DEvaluation(TH1 *hist_in)
{
    TH1 *hist;

    if(hist_in == nullptr)
        hist = fMainWindow->GetHisto();
    else
        hist = hist_in;

    if(hist && hist->InheritsFrom("TH2"))
        fCurrent2DHist = dynamic_cast<TH2*>(hist);
    else
    {
        cout<<"No 2D histogram found in the current pad, ignored"<<endl;
        fCurrent2DHist = nullptr;
        return;
    }

    TString draw_opt = hist->GetDrawOption();
    if(draw_opt=="")
        draw_opt = "col";

    fCurrent2DBackd = dynamic_cast<TH2*>(fCurrent2DHist->Clone());
    fCurrent2DBackd->SetName(Form("%s_bck",fCurrent2DBackd->GetName()));
    fCurrent2DBackd->Reset();

    fCurrent2DSubtract = dynamic_cast<TH2*>(fCurrent2DHist->Clone());
    fCurrent2DSubtract->SetName(Form("%s_sub",fCurrent2DBackd->GetName()));
    fCurrent2DSubtract->Reset();

532
    auto *spectrum = new TSpectrum2;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
533 534 535 536 537

    Int_t nbinsx, nbinsy;
    nbinsx = fCurrent2DHist->GetNbinsX();
    nbinsy = fCurrent2DHist->GetNbinsY();

538
    auto ** source = new Double_t*[nbinsx];
Jérémie Dudouet's avatar
Jérémie Dudouet committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    for (int i=0;i<nbinsx;i++)
       source[i]=new Double_t[nbinsy];

    for (int i = 0; i < nbinsx; i++){
       for (int j = 0; j < nbinsy; j++){
          source[i][j] = fCurrent2DHist->GetBinContent(i + 1,j + 1);
       }
    }

    spectrum->Background(source,nbinsx,nbinsy,fNumberIterations2DX,fNumberIterations2DY,fDirection2D,fFilterOrder2D);

    for (int i = 0; i < nbinsx; i++){
       for (int j = 0; j < nbinsy; j++)
          fCurrent2DBackd->SetBinContent(i + 1,j + 1, source[i][j]);
    }

    fCurrent2DSubtract->Add(fCurrent2DHist,fCurrent2DBackd,1,-1);

    for (int i=0;i<nbinsx;i++)
       delete[] source[i];

    fMainWindow->NewTab(1,2,"Bckd");

    TVirtualPad *pad = fMainWindow->GetCanvas()->cd(1);
    fCurrent2DSubtract->Draw(draw_opt);
    pad->Update();
    pad->SetBit(TPad::kCannotMove);
    pad->GetFrame()->SetBit(TObject::kCannotPick);

    pad = fMainWindow->GetCanvas()->cd(2);
    fCurrent2DBackd->Draw(draw_opt);
    pad->Update();
    pad->SetBit(TPad::kCannotMove);
    pad->GetFrame()->SetBit(TObject::kCannotPick);
}

ClassImp(CXBgdUtility)