CXHist2DPlayer.cpp 10.3 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"
17
#include "TGComboBox.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
18 19 20

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

using namespace std;

Jérémie Dudouet's avatar
Jérémie Dudouet committed
26
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
27
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
28 29
    fMainWindow = window;

Jérémie Dudouet's avatar
Jérémie Dudouet committed
30 31 32 33 34 35
    /// 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
36
    TGGroupFrame *fSubGroupFrame = new TGGroupFrame(fGroupFrame, "Projections", kVerticalFrame);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
37 38 39 40 41
    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);
42 43 44 45 46 47 48 49 50 51
    fHorizontalFrame->AddFrame(new TGLabel(fHorizontalFrame, "Projection axis: "), new TGLayoutHints(kLHintsCenterY | kLHintsLeft,5,10,0,0));
    fProjectionAxis = new TGComboBox(fHorizontalFrame);
    fHorizontalFrame->AddFrame(fProjectionAxis,new TGLayoutHints(kLHintsCenterY | kLHintsLeft  | kLHintsExpandX | kLHintsExpandY ,1,3,0,0));
    fProjectionAxis->AddEntry("X",0);
    fProjectionAxis->AddEntry("Y",1);
    fProjectionAxis->Select(0);
    fProjectionAxis->Connect("Selected(Int_t)", "CXHist2DPlayer", this, "UpdateProjection()");
    fSubGroupFrame->AddFrame(fHorizontalFrame,new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,-10,-10,5,5));

    fHorizontalFrame = new TGCompositeFrame(fSubGroupFrame, 60, 20, kHorizontalFrame);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
52 53 54
    TGTextButton *GateButton = new TGTextButton(fHorizontalFrame, "Gate");
    GateButton->SetTextColor(CXred);
    GateButton->Connect("Clicked()", "CXHist2DPlayer", this, "AddGate()");
55
    fHorizontalFrame->AddFrame(GateButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,2,2,0,0));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
56 57 58 59

    TGTextButton *BackgroundButton = new TGTextButton(fHorizontalFrame, "Backgd");
    BackgroundButton->SetTextColor(CXblue);
    BackgroundButton->Connect("Clicked()", "CXHist2DPlayer", this, "AddBackgd()");
60
    fHorizontalFrame->AddFrame(BackgroundButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,2,2,0,0));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
61 62 63 64

    TGTextButton *ClearButton = new TGTextButton(fHorizontalFrame, "Clear");
    ClearButton->SetTextColor(CXgreen);
    ClearButton->Connect("Clicked()", "CXHist2DPlayer", this, "ClearGates()");
65 66 67 68 69
    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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    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);
90
    fHorizontalFrame->AddFrame(new TGLabel(fHorizontalFrame, "Draw Options:"),new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 0, 5, 0, 0));
Antoine Lemasson's avatar
Antoine Lemasson committed
91
    fDrawOpt = new TGTextEntry(fHorizontalFrame, "hist");
92 93 94
    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
95 96 97

    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
98 99
    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
100 101 102

    fHorizontalFrame = new TGCompositeFrame(fSubGroupFrame, 60, 20, kHorizontalFrame);
    TGTextButton *Button = new TGTextButton(fHorizontalFrame, "Remove");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
103
    Button->Connect("Clicked()", "CXSavedList", fMainWindow->GetSaveList(), "RemoveSelectedEntry()");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
104 105
    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
106
    Button->Connect("Clicked()", "CXSavedList", fMainWindow->GetSaveList(), "ClearStoredList()");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
107 108
    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
109
    Button->Connect("Clicked()", "CXSavedList", fMainWindow->GetSaveList(), "SaveStoredList()");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
110 111 112
    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
113 114
    fListOfStoredSpectra = fMainWindow->GetSaveList()->GetListOfStoredSpectra();
    fMainWindow->GetSaveList()->AddListBox(fStoredSpectraBox);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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
}

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();

143
    if(fCurrentProj){
Jérémie Dudouet's avatar
Jérémie Dudouet committed
144
        fCurrentProj->Project(fFixRange->GetState());
145 146

        TList *gates = fCurrentProj->GetGatesList();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
147
        fMainWindow->GetSavedGatesList()->Clear();
148
        for(int i=0 ; i<gates->GetEntries() ; i++){
Jérémie Dudouet's avatar
Jérémie Dudouet committed
149
            fMainWindow->GetSavedGatesList()->Add(gates->At(i)->Clone());
150 151 152 153 154 155 156 157 158 159 160 161
        }
    }
}

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
162 163
        for(int i=0 ; i<fMainWindow->GetSavedGatesList()->GetEntries() ; i++){
            CXGateBox *box = (CXGateBox*)fMainWindow->GetSavedGatesList()->At(i)->Clone();
164 165 166 167 168 169
            box->SetPad(fCurrentProj->fCurrentPad);
            gates->Add(box);
            box->Draw();
        }
        fCurrentProj->UpdateGates();
    }
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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
}

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;
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
void CXHist2DPlayer::UpdateProjection()
{
    if(fAxisProj == fProjectionAxis->GetSelected())
        return;

    if(fProjectionAxis->GetSelected()==0)
        fAxisProj = 0;
    else
        fAxisProj = 1;

    GetProj()->UpdateProjection(fAxisProj);

    Project();
}

Jérémie Dudouet's avatar
Jérémie Dudouet committed
213 214 215 216 217 218 219 220 221 222 223
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;

224
    if(hist == nullptr) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
225 226 227 228
        cout<<"No 2D histogram found in the current pad, ignored"<<endl;
        return;
    }

229
    if(fAxisProj==0) {
Jérémie Dudouet's avatar
Jérémie Dudouet committed
230
        TotalProj = hist->ProjectionX(Form("%s_2DTotProjX",hist->GetName()));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
231 232 233
        TotalProj->SetName(Form("%s_TotProjX",hist->GetName()));
        TotalProj->SetTitle(Form("%s TotProjX",hist->GetTitle()));
    }
234
    else if(fAxisProj==1){
Jérémie Dudouet's avatar
Jérémie Dudouet committed
235
        TotalProj = hist->ProjectionY(Form("%s_2DTotProjY",hist->GetName()));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
236 237 238
        TotalProj->SetName(Form("%s_TotProjY",hist->GetName()));
        TotalProj->SetTitle(Form("%s TotProjY",hist->GetTitle()));
    }
239 240 241 242
    else {
        ERR_MESS << "Unkown projection axis... " << ENDL;
        return;
    }
Jérémie Dudouet's avatar
Jérémie Dudouet committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

    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
263
void CXHist2DPlayer::UpdateDrawOpt()
Jérémie Dudouet's avatar
Jérémie Dudouet committed
264
{
Jérémie Dudouet's avatar
Jérémie Dudouet committed
265
    fMainWindow->GetSaveList()->SetListDrawOption(fDrawOpt->GetText());
Jérémie Dudouet's avatar
Jérémie Dudouet committed
266 267 268
}

ClassImp(CXHist2DPlayer)