CXTH1Proj.cpp 11.2 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
#include "CXTH1Proj.h"

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>

#include "TH2.h"
#include "TCanvas.h"
#include "KeySymbols.h"
#include "TPadPainter.h"
#include "TColor.h"
#include "TROOT.h"
#include "TFrame.h"
15
#include "TError.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
16 17 18 19 20 21 22

#include "CXMainWindow.h"
#include "CXHist2DPlayer.h"
#include "CXGateBox.h"

using namespace std;

Jérémie Dudouet's avatar
Jérémie Dudouet committed
23 24
CXTH1Proj::CXTH1Proj(const TH1D &hist): TH1D::TH1D(hist)
{
25
    GetXaxis()->SetTitle("Energy (kev) [X axis]");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
26 27 28 29 30 31 32 33 34 35
    GetXaxis()->SetTitleSize(0.05);
    GetXaxis()->SetTitleOffset(0.8);
    GetXaxis()->SetTitleFont(132);
    GetXaxis()->SetLabelFont(132);

    GetYaxis()->SetTitle(Form("Counts/%g kev",GetBinWidth(1)));
    GetYaxis()->SetTitleSize(0.05);
    GetYaxis()->SetTitleOffset(0.5);
    GetYaxis()->SetTitleFont(132);
    GetYaxis()->SetLabelFont(132);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
36 37 38 39 40

    fListOfGates = new TList;
    fListOfGates->SetOwner();
    fListOfGates->SetName("ListOfGates");

Jérémie Dudouet's avatar
Jérémie Dudouet committed
41 42
    SetStats();
}
Jérémie Dudouet's avatar
Jérémie Dudouet committed
43

Jérémie Dudouet's avatar
Jérémie Dudouet committed
44 45
CXTH1Proj::CXTH1Proj(): TH1D::TH1D()
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
46 47 48
    fListOfGates = new TList;
    fListOfGates->SetOwner();
    fListOfGates->SetName("ListOfGates");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
49 50

    SetStats();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
51 52 53 54
}

CXTH1Proj::~CXTH1Proj()
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
55
    delete fListOfGates;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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
void CXTH1Proj::UpdateProjection(Int_t Axis)
{
    if(fGGHist->GetXaxis()->GetNbins() != fGGHist->GetYaxis()->GetNbins()) {
        WARN_MESS << "X and Y axis needs to have the same number of bins for GG projections" << ENDL;
        return;
    }

    if(Axis == 0) {
        GetXaxis()->SetTitle("Energy (kev) [X axis]");
        TH1 *projtmp = fGGHist->ProjectionX();
        for(int i=0 ; i<projtmp->GetNbinsX()+2 ; i++)
            SetBinContent(i,projtmp->GetBinContent(i));
        delete projtmp;
    }
    else if(Axis == 1) {
        GetXaxis()->SetTitle("Energy (kev) [Y axis]");
        TH1 *projtmp = fGGHist->ProjectionY();
        for(int i=0 ; i<projtmp->GetNbinsX()+2 ; i++)
            SetBinContent(i,projtmp->GetBinContent(i));
        delete projtmp;
    }

    fProjectionAxis = Axis;

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

Jérémie Dudouet's avatar
Jérémie Dudouet committed
86 87
void CXTH1Proj::SetPlayer(CXHist2DPlayer *p)
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
88 89 90
    f2DPlayer = p;
}

Jérémie Dudouet's avatar
Jérémie Dudouet committed
91 92
void CXTH1Proj::SetMainWindow(CXMainWindow *w)
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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
    fMainWindow = w;
}

void CXTH1Proj::SetTH2(TH2 *hist)
{
    if(fGGHist) delete fGGHist;
    fGGHist = (TH2*)hist->Clone();
}

void CXTH1Proj::SetProjPad(TPad *pad)
{
    fProjPad = pad;
}

void CXTH1Proj::SetCurrentPad(TPad *pad)
{
    fCurrentPad = pad;
    fCurrentPad->GetCanvas()->Connect("ProcessedEvent(Int_t, Int_t, Int_t, TObject*)", "CXTH1Proj", this, "HandleMovement(Int_t,Int_t,Int_t, TObject*)");
    fCurrentPad->Connect("RangeChanged()", "CXTH1Proj", this, "UpdateGates()");
    fCurrentPad->Connect("RangeAxisChanged()", "CXTH1Proj", this, "UpdateGates()");
    fCurrentPad->Connect("Resized()", "CXTH1Proj", this, "UpdateGates()");
    fCurrentPad->Connect("UnZoomed()", "CXTH1Proj", this, "UpdateGates()");
}


Jérémie Dudouet's avatar
Jérémie Dudouet committed
118 119
void CXTH1Proj::AddBackgd(Float_t Mean, Float_t Width)
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
120 121 122 123 124 125 126 127
    CXGateBox *box = new CXGateBox(Mean,Width,fCurrentPad);
    box->SetBGD();
    fListOfGates->Add(box);
    box->Draw();

    fAddNewBacground = false;
}

Jérémie Dudouet's avatar
Jérémie Dudouet committed
128 129
void CXTH1Proj::AddGate1(Float_t Mean, Float_t Width)
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
130
    CXGateBox *box = new CXGateBox(Mean,Width,fCurrentPad);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
131
    box->SetGate1();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    fListOfGates->Add(box);

    fCurrentPad->cd();
    box->Draw();

    fAddNewGate = false;
}

void CXTH1Proj::Project(Bool_t FixRange){

    Float_t SavedRange[2];
    if( FixRange ){
        if(fMainWindow->GetHisto(fProjPad)){
        SavedRange[0] = fProjPad->GetUxmin();
        SavedRange[1] = fProjPad->GetUxmax();
        }
    }

    Float_t TotalGateWidth = 0;
    Float_t TotalBGDWidth = 0;

    TString ProjName = Form("%s_Proj",fGGHist->GetName());
    TString NameGates  = "";
    TString NameBGD  = "";

    for(int i=0 ; i<fListOfGates->GetEntries() ; i++)
    {
        CXGateBox* box =  static_cast<CXGateBox*>(fListOfGates->At(i));

        if(box->IsBGD()){
            TotalBGDWidth += box->GetWidth();
            NameBGD += Form("_B%.1f(%.1f)",box->GetCentroid(),box->GetWidth());
        }
Jérémie Dudouet's avatar
Jérémie Dudouet committed
165 166 167
        else if(box->IsGate2()){
            WARN_MESS << "Cube gates not possible in GG mode, ignores" << ENDL;
        }
Jérémie Dudouet's avatar
Jérémie Dudouet committed
168 169 170 171 172 173 174 175 176 177 178 179
        else{
            TotalGateWidth += box->GetWidth();
            NameGates += Form("_E%.1f(%.1f)",box->GetCentroid(),box->GetWidth());
        }
    }

    ProjName += NameGates + NameBGD;

    if(gROOT->FindObject(ProjName.Data()))
        delete gROOT->FindObject(ProjName.Data());

    gErrorIgnoreLevel = kError;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
180 181 182 183 184 185 186 187
    TH1D *FinalProj = fGGHist->ProjectionX("tempproj");
    FinalProj->SetStats();
    FinalProj->Reset();
    FinalProj->SetName(ProjName.Data());
    FinalProj->SetTitle(ProjName.Data());

    Float_t WidthRef = FinalProj->GetBinWidth(1);

188 189 190 191 192 193 194
    TString XAxis = GetXaxis()->GetTitle();
    if(XAxis.Contains("[X"))
        XAxis.ReplaceAll("[X","[Y");
    else if(XAxis.Contains("[Y"))
        XAxis.ReplaceAll("[Y","[X");

    FinalProj->GetXaxis()->SetTitle(XAxis);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
195 196 197 198 199 200 201 202 203 204
    FinalProj->GetXaxis()->SetTitleSize(GetXaxis()->GetTitleSize());
    FinalProj->GetXaxis()->SetTitleOffset(GetXaxis()->GetTitleOffset());
    FinalProj->GetXaxis()->SetTitleFont(GetXaxis()->GetTitleFont());
    FinalProj->GetXaxis()->SetLabelFont(GetXaxis()->GetLabelFont());

    FinalProj->GetYaxis()->SetTitle(GetYaxis()->GetTitle());
    FinalProj->GetYaxis()->SetTitleSize(GetYaxis()->GetTitleSize());
    FinalProj->GetYaxis()->SetTitleOffset(GetYaxis()->GetTitleOffset());
    FinalProj->GetYaxis()->SetTitleFont(GetYaxis()->GetTitleFont());
    FinalProj->GetYaxis()->SetLabelFont(GetYaxis()->GetLabelFont());
Jérémie Dudouet's avatar
Jérémie Dudouet committed
205 206 207 208 209

    for(int i=0 ; i<fListOfGates->GetEntries() ; i++)
    {
        CXGateBox* box =  static_cast<CXGateBox*>(fListOfGates->At(i));

Jérémie Dudouet's avatar
Jérémie Dudouet committed
210 211 212 213 214 215 216 217
        Float_t X1 = box->GetX1();
        Float_t X2 = box->GetX2();

        Float_t BinX1 = fGGHist->GetXaxis()->FindBin(X1);
        Float_t BinX2 = fGGHist->GetXaxis()->FindBin(X2);

//        cout<<"add_gate("<<X1<<","<<X2<<"): ilo="<<BinX1<<" ; ihi="<<BinX2<<endl;

Jérémie Dudouet's avatar
Jérémie Dudouet committed
218 219 220
        Float_t Weight = 1.;
        if(box->IsBGD())
            Weight = -TotalGateWidth/TotalBGDWidth;
Jérémie Dudouet's avatar
Jérémie Dudouet committed
221 222 223 224
        if(box->IsGate2())
            continue;

        for(int ibin=BinX1 ; ibin<BinX2 ; ibin++) {
225 226 227 228 229 230
            TH1D *temp = nullptr;
            if(fProjectionAxis==0)
                temp = fGGHist->ProjectionY("tmp",ibin,ibin);
            else if(fProjectionAxis==1)
                temp = fGGHist->ProjectionX("tmp",ibin,ibin);

Jérémie Dudouet's avatar
Jérémie Dudouet committed
231 232 233 234
            temp->Scale(FinalProj->GetBinWidth(ibin)/WidthRef);
            FinalProj->Add(temp,Weight);
            delete temp;
        }
Jérémie Dudouet's avatar
Jérémie Dudouet committed
235 236
    }

Jérémie Dudouet's avatar
Jérémie Dudouet committed
237 238
    INFO_MESS<<"Integral of projection: "<<FinalProj->Integral()<<ENDL;

Jérémie Dudouet's avatar
Jérémie Dudouet committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    fProjPad->cd();
    FinalProj->Draw("hist");
    if(FixRange)
        FinalProj->GetXaxis()->SetRangeUser(SavedRange[0],SavedRange[1]);
    gErrorIgnoreLevel = kPrint;

    fProjPad->Update();
    fProjPad->Modified();

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

void CXTH1Proj::ClearGates(){
    fListOfGates->Clear();
    UpdateGates();
}

void CXTH1Proj::RemoveGate(CXGateBox *box){
    fCurrentPad->GetListOfPrimitives()->Remove(box);
    fListOfGates->Remove(box);
    UpdateGates();
}

void CXTH1Proj::HandleMovement(Int_t EventType, Int_t EventX, Int_t EventY, TObject *selected)
{
    //    cout<<EventType<<" "<<EventX<<" "<<EventY<<" "<<selected<<endl;

    bool CTRL = ( EventX == EventY-96);

    switch (EventType){
    case kKeyPress:{
        if(CTRL)
            break;

        EKeySym KeySym = (EKeySym)EventY;

        if( KeySym == kKey_g )
            fAddNewGate = true;
        if( KeySym == kKey_b )
            fAddNewBacground = true;
        if( KeySym == kKey_c && fMainWindow->GetSelectedPad() == fCurrentPad)
            ClearGates();
        if( KeySym == kKey_p )
Antoine Lemasson's avatar
Antoine Lemasson committed
283
            f2DPlayer->Project();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
284 285
        if( KeySym == kKey_d  && selected && selected->InheritsFrom("CXGateBox"))
            RemoveGate(((CXGateBox*)selected));
Antoine Lemasson's avatar
Antoine Lemasson committed
286 287
        if( KeySym == kKey_l )
            f2DPlayer->ApplyLastGate();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

        UpdateGates();

        break;
    }
    case kButton1Down:{
        if(!fAddNewGate && !fAddNewBacground)
            break;

        if(selected && selected->InheritsFrom("TAxis"))
            break;

        gPad = fCurrentPad;

        gVirtualX->SetLineWidth(1);

        gPad->GetCanvas()->FeedbackMode(kTRUE);   // to draw in rubberband mode

        oldx = gPad->AbsPixeltoX(gPad->GetCanvas()->GetEventX());
        oldy = gPad->AbsPixeltoY(gPad->GetCanvas()->GetEventY());
        xinit = oldx;
        yinit = oldy;
        ((TPadPainter*)gPad->GetPainter())->DrawBox(xinit,yinit,xinit,yinit, TVirtualPadPainter::kHollow);

        fGateVirtualBox = new TBox(xinit, yinit, xinit, yinit);
        Int_t ci;
        if(fAddNewBacground)
            ci = TColor::GetColor("#7d7dff");
        else
            ci = TColor::GetColor("#ff7d7d");

        TColor *zoomcolor = gROOT->GetColor(ci);
        if (!TCanvas::SupportAlpha() || !zoomcolor)
            fGateVirtualBox->SetFillStyle(3002);
        else
            zoomcolor->SetAlpha(0.5);

        fGateVirtualBox->SetFillColor(ci);
        fGateVirtualBox->Draw();

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

        moved = true;

        break;
    }
    case kButton1Motion:{
        if(!fAddNewGate && !fAddNewBacground)
            break;

        if(moved==false)
            break;

        gPad = fCurrentPad;    // don't use cd() because we won't draw in pad

        gPad->GetPainter()->SetFillStyle(kFDotted1);
        gPad->GetPainter()->SetFillColor(kRed);

        ((TPadPainter*)gPad->GetPainter())->DrawBox(xinit,yinit,oldx,oldy, TVirtualPadPainter::kHollow);
        oldx = gPad->AbsPixeltoX(gPad->GetCanvas()->GetEventX());
        oldy = gPad->AbsPixeltoY(gPad->GetCanvas()->GetEventY());

        if (fGateVirtualBox){
            fGateVirtualBox->SetX1(xinit);
            fGateVirtualBox->SetY1(yinit);
            fGateVirtualBox->SetX2(oldx);
            fGateVirtualBox->SetY2(oldy);
        }

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

        xmax = oldx;
        ymax = oldy;

        xmin = xinit;
        ymin = yinit;

        Double_t toto = 0;
        if (xmax < xmin){
            toto = xmax;
            xmax = xmin;
            xmin = toto;
        }
        if (ymax < ymin){
            toto = ymax;
            ymax = ymin;
            ymin = toto;
        }

        break;
    }
    case kButton1Up:{
        if(!fAddNewGate && !fAddNewBacground){
            UpdateGates();
            break;
        }

        if(moved){
            gPad = fCurrentPad;

            Float_t Mean = (xmax+xmin)/2;
            Float_t Width = (xmax-xmin)/2.;

            if(fAddNewGate)
Jérémie Dudouet's avatar
Jérémie Dudouet committed
394
                AddGate1(Mean,Width);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 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
            if(fAddNewBacground)
                AddBackgd(Mean,Width);

            xmax = xmin = ymax = ymin = 0.;
        }

        moved = false;

        if (fGateVirtualBox) {
            fGateVirtualBox->Delete();
            fGateVirtualBox = nullptr;
        }

        UpdateGates();

        break;
    }
    default:{
        break;
    }
    }

}

void CXTH1Proj::UpdateGates()
{
    fCurrentPad->Modified();
    fCurrentPad->Update();

    for(int i=0 ; i<fListOfGates->GetEntries() ; i++){
        CXGateBox* box =  static_cast<CXGateBox*>(fListOfGates->At(i));
        box->Update();
    }

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

ClassImp(CXTH1Proj)