CXHist2DPlayer.cpp 9.21 KB
Newer Older
Jérémie Dudouet's avatar
Jérémie Dudouet committed
1 2 3 4 5 6 7 8
#include "CXHist2DPlayer.h"

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

#include "TGButton.h"
9
#include "TGTextEntry.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
10 11 12 13 14
#include "TFile.h"
#include "TGLabel.h"
#include "TROOT.h"
#include "TFrame.h"
#include "TGListBox.h"
15
#include "TError.h"
16
#include "TObjArray.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
17 18 19

#include "CXMainWindow.h"
#include "CXTH1Proj.h"
20
#include "CXGateBox.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
21
#include "CXSavedList.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
22 23 24

using namespace std;

Jérémie Dudouet's avatar
Jérémie Dudouet committed
25
CXHist2DPlayer::CXHist2DPlayer(const TGCompositeFrame *MotherFrame, UInt_t w, UInt_t h, CXMainWindow *window) : TGVerticalFrame(MotherFrame, w, h, kFixedWidth)
Jérémie Dudouet's avatar
Jérémie Dudouet committed
26
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
27 28
    fMainWindow = window;

Jérémie Dudouet's avatar
Jérémie Dudouet committed
29 30 31 32 33 34
    /// GxG
    TGGroupFrame *fGroupFrame = new TGGroupFrame(MotherFrame, "Gamma Gamma utility", kVerticalFrame);
    fGroupFrame->SetTextColor(CXred);
    fGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    AddFrame(fGroupFrame, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 0, 0, 0, 0));

Jérémie Dudouet's avatar
Jérémie Dudouet committed
35
    TGGroupFrame *fSubGroupFrame = new TGGroupFrame(fGroupFrame, "Projections", kVerticalFrame);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
36 37 38 39 40 41 42 43
    fSubGroupFrame->SetTextColor(CXblue);
    fSubGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    fGroupFrame->AddFrame(fSubGroupFrame, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, -10, -10, 0, 0));

    TGCompositeFrame *fHorizontalFrame = new TGCompositeFrame(fSubGroupFrame, 60, 20, kHorizontalFrame);
    TGTextButton *GateButton = new TGTextButton(fHorizontalFrame, "Gate");
    GateButton->SetTextColor(CXred);
    GateButton->Connect("Clicked()", "CXHist2DPlayer", this, "AddGate()");
44
    fHorizontalFrame->AddFrame(GateButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,2,2,0,0));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
45 46 47 48

    TGTextButton *BackgroundButton = new TGTextButton(fHorizontalFrame, "Backgd");
    BackgroundButton->SetTextColor(CXblue);
    BackgroundButton->Connect("Clicked()", "CXHist2DPlayer", this, "AddBackgd()");
49
    fHorizontalFrame->AddFrame(BackgroundButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,2,2,0,0));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
50 51 52 53

    TGTextButton *ClearButton = new TGTextButton(fHorizontalFrame, "Clear");
    ClearButton->SetTextColor(CXgreen);
    ClearButton->Connect("Clicked()", "CXHist2DPlayer", this, "ClearGates()");
54 55 56 57 58
    fHorizontalFrame->AddFrame(ClearButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,2,2,0,0));

    TGTextButton *LastGateButton = new TGTextButton(fHorizontalFrame, "Last");
    LastGateButton->Connect("Clicked()", "CXHist2DPlayer", this, "ApplyLastGate()");
    fHorizontalFrame->AddFrame(LastGateButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,2,2,0,0));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    fSubGroupFrame->AddFrame(fHorizontalFrame,new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,-10,-10,5,0));

    fHorizontalFrame = new TGCompositeFrame(fSubGroupFrame, 60, 20, kHorizontalFrame);
    TGTextButton *ProjectButton = new TGTextButton(fHorizontalFrame, "Project");
    ProjectButton->Connect("Clicked()", "CXHist2DPlayer", this, "Project()");
    fHorizontalFrame->AddFrame(ProjectButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,5,10,0,0));

    fFixRange = new TGCheckButton(fHorizontalFrame, "Fix Range", 0);
    fFixRange->SetState(kButtonUp);
    fHorizontalFrame->AddFrame(fFixRange,new TGLayoutHints(kLHintsLeft | kLHintsCenterY ,5,10,0,0));

    fSubGroupFrame->AddFrame(fHorizontalFrame,new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,-10,-10,5,0));

    fSubGroupFrame = new TGGroupFrame(MotherFrame, "Stored spectra", kVerticalFrame);
    fSubGroupFrame->SetTextColor(CXblue);
    fSubGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    AddFrame(fSubGroupFrame, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandY | kLHintsExpandX, 3, 3, 0, 0));

    fHorizontalFrame = new TGCompositeFrame(fSubGroupFrame, 60, 20, kHorizontalFrame);
79
    fHorizontalFrame->AddFrame(new TGLabel(fHorizontalFrame, "Draw Options:"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 0, 5, 0, 0));
Antoine Lemasson's avatar
Antoine Lemasson committed
80
    fDrawOpt = new TGTextEntry(fHorizontalFrame, "hist");
81 82 83
    fDrawOpt->SetToolTipText("hist, same, norm, add, add(fact)");
    fHorizontalFrame->AddFrame(fDrawOpt,new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsCenterY ,0,0,0,0));
    fSubGroupFrame->AddFrame(fHorizontalFrame,new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,-10,-10,5,5));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
84 85 86

    fStoredSpectraBox = new TGListBox(fSubGroupFrame);
    fSubGroupFrame->AddFrame(fStoredSpectraBox, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,-10,-10,0,0));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
87 88
    fStoredSpectraBox->Connect("DoubleClicked(Int_t)", "CXHist2DPlayer", this, "UpdateDrawOpt()");
    fStoredSpectraBox->Connect("DoubleClicked(Int_t)", "CXSavedList", fMainWindow->GetSaveList(), "DoubleClicked(Int_t)");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
89 90 91

    fHorizontalFrame = new TGCompositeFrame(fSubGroupFrame, 60, 20, kHorizontalFrame);
    TGTextButton *Button = new TGTextButton(fHorizontalFrame, "Remove");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
92
    Button->Connect("Clicked()", "CXSavedList", fMainWindow->GetSaveList(), "RemoveSelectedEntry()");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
93 94
    fHorizontalFrame->AddFrame(Button,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,5,10,0,0));
    Button = new TGTextButton(fHorizontalFrame, "Clear");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
95
    Button->Connect("Clicked()", "CXSavedList", fMainWindow->GetSaveList(), "ClearStoredList()");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
96 97
    fHorizontalFrame->AddFrame(Button,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,5,10,0,0));
    Button = new TGTextButton(fHorizontalFrame, "Save");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
98
    Button->Connect("Clicked()", "CXSavedList", fMainWindow->GetSaveList(), "SaveStoredList()");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
99 100 101
    fHorizontalFrame->AddFrame(Button,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,5,10,0,0));
    fSubGroupFrame->AddFrame(fHorizontalFrame,new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,-10,-10,5,0));

Jérémie Dudouet's avatar
Jérémie Dudouet committed
102 103
    fListOfStoredSpectra = fMainWindow->GetSaveList()->GetListOfStoredSpectra();
    fMainWindow->GetSaveList()->AddListBox(fStoredSpectraBox);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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
}

CXHist2DPlayer::~CXHist2DPlayer(){
}

CXTH1Proj *CXHist2DPlayer::GetProj(){

    if(((TString)fMainWindow->GetCanvas()->GetName()).BeginsWith("GxG")){
        fMainWindow->SetSelectedPad(fMainWindow->GetCanvas()->GetPad(1));
        fMainWindow->GetCanvas()->SetSelectedPad((TPad*)fMainWindow->GetCanvas()->GetPad(1));
        fMainWindow->GetCanvas()->SetClickSelectedPad((TPad*)fMainWindow->GetCanvas()->GetPad(1));
        gPad = fMainWindow->GetCanvas()->GetSelectedPad();
    }

    TH1 *hist = fMainWindow->GetHisto();

    if(hist && hist->InheritsFrom("CXTH1Proj")){
        return dynamic_cast<CXTH1Proj*>(hist);
    }
    else{
        cout<<"No Projection for GxG found in the current pad"<<endl;
        return nullptr;
    }
}

void CXHist2DPlayer::Project(){
    fCurrentProj = GetProj();

132
    if(fCurrentProj){
Jérémie Dudouet's avatar
Jérémie Dudouet committed
133
        fCurrentProj->Project(fFixRange->GetState());
134 135

        TList *gates = fCurrentProj->GetGatesList();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
136
        fMainWindow->GetSavedGatesList()->Clear();
137
        for(int i=0 ; i<gates->GetEntries() ; i++){
Jérémie Dudouet's avatar
Jérémie Dudouet committed
138
            fMainWindow->GetSavedGatesList()->Add(gates->At(i)->Clone());
139 140 141 142 143 144 145 146 147 148 149 150
        }
    }
}

void CXHist2DPlayer::ApplyLastGate(){
    fCurrentProj = GetProj();

    ClearGates();

    if(fCurrentProj){
        TList *gates = fCurrentProj->GetGatesList();
        gates->Clear();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
151 152
        for(int i=0 ; i<fMainWindow->GetSavedGatesList()->GetEntries() ; i++){
            CXGateBox *box = (CXGateBox*)fMainWindow->GetSavedGatesList()->At(i)->Clone();
153 154 155 156 157 158
            box->SetPad(fCurrentProj->fCurrentPad);
            gates->Add(box);
            box->Draw();
        }
        fCurrentProj->UpdateGates();
    }
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
}

void CXHist2DPlayer::AddBackgd(){
    fCurrentProj = GetProj();

    if(fCurrentProj)
        fCurrentProj->AddBackgd();
}

void CXHist2DPlayer::AddGate(){
    fCurrentProj = GetProj();

    if(fCurrentProj)
        fCurrentProj->AddGate();
}

void CXHist2DPlayer::ClearGates(){
    fCurrentProj = GetProj();

    if(fCurrentProj)
        fCurrentProj->ClearGates();
}

void CXHist2DPlayer::SetMainWindow(CXMainWindow *w)
{
    fMainWindow = w;
}

void CXHist2DPlayer::InitGG(TH2 *hist_in)
{
    TH2 *hist;

    TH1D *TotalProj = nullptr;

    if(hist_in == nullptr)
        hist = dynamic_cast<TH2*>(fMainWindow->GetHisto());
    else
        hist = hist_in;

    if(hist == nullptr)
    {
        cout<<"No 2D histogram found in the current pad, ignored"<<endl;
        return;
    }

    if(fTotProjX){
Jérémie Dudouet's avatar
Jérémie Dudouet committed
205
        TotalProj = hist->ProjectionX(Form("%s_2DTotProjX",hist->GetName()));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
206 207 208 209
        TotalProj->SetName(Form("%s_TotProjX",hist->GetName()));
        TotalProj->SetTitle(Form("%s TotProjX",hist->GetTitle()));
    }
    else{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
210
        TotalProj = hist->ProjectionY(Form("%s_2DTotProjY",hist->GetName()));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        TotalProj->SetName(Form("%s_TotProjY",hist->GetName()));
        TotalProj->SetTitle(Form("%s TotProjY",hist->GetTitle()));
    }

    CXTH1Proj *NewProj = new CXTH1Proj(*TotalProj);
    NewProj->SetMainWindow(fMainWindow);
    NewProj->SetPlayer(this);
    NewProj->SetTH2(hist);
    fMainWindow->NewTab(1,2,"GxG");

    TVirtualPad *pad = fMainWindow->GetCanvas()->cd(1);
    NewProj->Draw("hist");
    NewProj->SetCurrentPad(dynamic_cast<TPad*>(pad));
    pad->Update();
    pad->SetBit(TPad::kCannotMove);
    pad->GetFrame()->SetBit(TObject::kCannotPick);

    pad = fMainWindow->GetCanvas()->GetPad(2);
    NewProj->SetProjPad(dynamic_cast<TPad*>(pad));

    gROOT->SetSelectedPad(fMainWindow->GetCanvas()->GetPad(1));
}

Jérémie Dudouet's avatar
Jérémie Dudouet committed
234
void CXHist2DPlayer::UpdateDrawOpt()
Jérémie Dudouet's avatar
Jérémie Dudouet committed
235
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
236
    fMainWindow->GetSaveList()->SetListDrawOption(fDrawOpt->GetText());
Jérémie Dudouet's avatar
Jérémie Dudouet committed
237 238 239
}

ClassImp(CXHist2DPlayer)