CXHist2DPlayer.cpp 13.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
#include "CXHist2DPlayer.h"

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

#include "TGButton.h"
#include "TFile.h"
#include "TGLabel.h"
#include "TROOT.h"
#include "TFrame.h"
#include "TGListBox.h"
14
#include "TError.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
15 16 17

#include "CXMainWindow.h"
#include "CXTH1Proj.h"
18
#include "CXGateBox.h"
Jérémie Dudouet's avatar
Jérémie Dudouet committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

using namespace std;

CXHist2DPlayer::CXHist2DPlayer(const TGCompositeFrame *MotherFrame, UInt_t w, UInt_t h) : TGVerticalFrame(MotherFrame, w, h, kFixedWidth)
{
    /// 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));

    TGGroupFrame *fSubGroupFrame = new TGGroupFrame(fGroupFrame, "Init", kVerticalFrame);
    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 *InitButton = new TGTextButton(fHorizontalFrame, "Init GxG");
    InitButton->Connect("Clicked()", "CXHist2DPlayer", this, "InitGG()");
    fHorizontalFrame->AddFrame(InitButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,5,10,0,0));
    fSubGroupFrame->AddFrame(fHorizontalFrame,new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,-10,-10,0,0));

    fSubGroupFrame = new TGGroupFrame(fGroupFrame, "Projections", kVerticalFrame);
    fSubGroupFrame->SetTextColor(CXblue);
    fSubGroupFrame->SetTitlePos(TGGroupFrame::kLeft); // right aligned
    fGroupFrame->AddFrame(fSubGroupFrame, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, -10, -10, 0, 0));

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

    TGTextButton *BackgroundButton = new TGTextButton(fHorizontalFrame, "Backgd");
    BackgroundButton->SetTextColor(CXblue);
    BackgroundButton->Connect("Clicked()", "CXHist2DPlayer", this, "AddBackgd()");
55
    fHorizontalFrame->AddFrame(BackgroundButton,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,2,2,0,0));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
56 57 58 59

    TGTextButton *ClearButton = new TGTextButton(fHorizontalFrame, "Clear");
    ClearButton->SetTextColor(CXgreen);
    ClearButton->Connect("Clicked()", "CXHist2DPlayer", this, "ClearGates()");
60 61 62 63 64
    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
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

    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);
    fDrawSame = new TGCheckButton(fHorizontalFrame, "Draw same", 0);
    fDrawSame->SetState(kButtonUp);
    fHorizontalFrame->AddFrame(fDrawSame,new TGLayoutHints(kLHintsLeft | kLHintsCenterY ,5,10,0,0));
    fSubGroupFrame->AddFrame(fHorizontalFrame,new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,-10,-10,5,0));

    fStoredSpectraBox = new TGListBox(fSubGroupFrame);
    fSubGroupFrame->AddFrame(fStoredSpectraBox, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,-10,-10,0,0));
    fStoredSpectraBox->Connect("DoubleClicked(Int_t)", "CXHist2DPlayer", this, "DoubleClicked(Int_t)");
    fStoredSpectraBox->GetContainer()->Connect("ProcessedEvent(Event_t*)", "CXHist2DPlayer", this, "ProcessedButtonEvent(Event_t*)");

    fHorizontalFrame = new TGCompositeFrame(fSubGroupFrame, 60, 20, kHorizontalFrame);
    TGTextButton *Button = new TGTextButton(fHorizontalFrame, "Remove");
    Button->Connect("Clicked()", "CXHist2DPlayer", this, "RemoveSelectedEntry()");
    fHorizontalFrame->AddFrame(Button,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,5,10,0,0));
    Button = new TGTextButton(fHorizontalFrame, "Clear");
    Button->Connect("Clicked()", "CXHist2DPlayer", this, "ClearStoredList()");
    fHorizontalFrame->AddFrame(Button,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,5,10,0,0));
    Button = new TGTextButton(fHorizontalFrame, "Save");
    Button->Connect("Clicked()", "CXHist2DPlayer", this, "SaveStoredList()");
    fHorizontalFrame->AddFrame(Button,new TGLayoutHints(kLHintsCenterY | kLHintsExpandX,5,10,0,0));
    fSubGroupFrame->AddFrame(fHorizontalFrame,new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX,-10,-10,5,0));

    fListOfStoredSpectra = new TList;
    fListOfStoredSpectra->SetOwner();
109 110 111 112

    fListOfSavedGates = new TList;
    fListOfSavedGates->SetOwner();
    fListOfSavedGates->SetName("ListOfGates");
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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
}

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

141
    if(fCurrentProj){
Jérémie Dudouet's avatar
Jérémie Dudouet committed
142
        fCurrentProj->Project(fFixRange->GetState());
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

        TList *gates = fCurrentProj->GetGatesList();
        fListOfSavedGates->Clear();
        for(int i=0 ; i<gates->GetEntries() ; i++){
            fListOfSavedGates->Add(gates->At(i)->Clone());
        }
    }
}

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

    ClearGates();

    if(fCurrentProj){
        TList *gates = fCurrentProj->GetGatesList();
        gates->Clear();
        for(int i=0 ; i<fListOfSavedGates->GetEntries() ; i++){
            CXGateBox *box = (CXGateBox*)fListOfSavedGates->At(i)->Clone();
            box->SetPad(fCurrentProj->fCurrentPad);
            gates->Add(box);
            box->Draw();
        }
        fCurrentProj->UpdateGates();
    }
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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 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
}

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){
        TotalProj = hist->ProjectionX();
        TotalProj->SetName(Form("%s_TotProjX",hist->GetName()));
        TotalProj->SetTitle(Form("%s TotProjX",hist->GetTitle()));
    }
    else{
        TotalProj = hist->ProjectionY();
        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));
}

void CXHist2DPlayer::AddToStoredList(TObject *o){

    fListOfStoredSpectra->Remove(fListOfStoredSpectra->FindObject(o->GetName()));
    fListOfStoredSpectra->Add(o);

    UpdateStoredList();
}

void CXHist2DPlayer::UpdateStoredList(){
    fStoredSpectraBox->RemoveAll();

    for(int i=0 ; i<fListOfStoredSpectra->GetEntries() ; i++){
255
        TGTextLBEntry *entry = new TGTextLBEntry(fStoredSpectraBox->GetContainer(), new TGString(fListOfStoredSpectra->At(i)->GetName()), fStoredSpectraBox->GetNumberOfEntries()+1);
Jérémie Dudouet's avatar
Jérémie Dudouet committed
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 283 284 285 286 287 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
        entry->SetBackgroundColor((Pixel_t)0x90f269);
        fStoredSpectraBox->AddEntry((TGLBEntry *)entry, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX));
    }

    fStoredSpectraBox->Layout();
}

void CXHist2DPlayer::RemoveSelectedEntry(){
    if(fStoredSpectraBox->GetSelectedEntry()){
        fListOfStoredSpectra->Remove(fListOfStoredSpectra->FindObject(fStoredSpectraBox->GetSelectedEntry()->GetTitle()));
        UpdateStoredList();
    }
    else
        cout<<"No selected entry"<<endl;
}

void CXHist2DPlayer::SaveStoredList(){
    if(fListOfStoredSpectra->GetEntries()==0){
        cout<<"No histogram to save"<<endl;
    }

    const char* SaveAsTypes[] = {
        "ROOT files",   "*.root",
        0,              0
    };

    TString workdir = gSystem->WorkingDirectory();
    static TString dir(".");
    static Int_t typeidx = 0;
    static Bool_t overwr = kFALSE;
    TGFileInfo fi;
    fi.fFileTypes   = SaveAsTypes;
    fi.fIniDir      = StrDup(dir);
    fi.fFileTypeIdx = typeidx;
    fi.fOverwrite   = overwr;
    new TGFileDialog(gClient->GetDefaultRoot(), gClient->GetDefaultRoot(), kFDSave, &fi);
    gSystem->ChangeDirectory(workdir.Data());
    if (!fi.fFilename) return;
    TString fn = fi.fFilename;
    dir     = fi.fIniDir;
    typeidx = fi.fFileTypeIdx;
    overwr  = fi.fOverwrite;

    TFile *FileOut = new TFile(fn.Data(),"recreate");
    for(int i=0 ; i<fListOfStoredSpectra->GetEntries() ; i++)
        fListOfStoredSpectra->At(i)->Write();
    FileOut->Close();
}

void CXHist2DPlayer::ClearStoredList(){
    fListOfStoredSpectra->Clear();
    UpdateStoredList();
}

void CXHist2DPlayer::ProcessedButtonEvent(Event_t *event)
{
//    char input[10];
//    UInt_t keysym;

//    gVirtualX->LookupString(event, input, sizeof(input), keysym);

//    //        std::cout << "event : " << event->fCode << " " << event->fState <<" ; "<< event->fType  << "; " << keysym << std::endl;

//    //    if(event->fType == kGKeyPress && keysym == kKey_Control)
//    //        fCtrl_On = true;

//    if(event->fType == kButtonPress){

//        if(fLastSelected){
//            fLastSelected->SetBackgroundColor((Pixel_t)0x90f269);
//            fLastSelected = nullptr;
//            fStoredSpectraBox->Layout();
//        }

//        if(fStoredSpectraBox->GetSelectedEntry()){
//            fStoredSpectraBox->GetSelectedEntry()->SetBackgroundColor((Pixel_t) 0x87a7d2);
//            fLastSelected = fStoredSpectraBox->GetSelectedEntry();
//            fStoredSpectraBox->Select(fStoredSpectraBox->GetSelected(),false);
//            fStoredSpectraBox->Layout();
//        }
//    }
}

void CXHist2DPlayer::DoubleClicked(Int_t id){

    TObject *o = fListOfStoredSpectra->FindObject(fStoredSpectraBox->GetSelectedEntry()->GetTitle());

    if(fMainWindow->GetCanvas()->GetUniqueID() == 666)
    {
        cout<<"Cannot draw on this Canvas"<<endl;
        return;
    }

    TString Options = "";
    if(o->InheritsFrom("TH2"))
        Options += "col";
    else if(o->InheritsFrom("TH1"))
        Options += "hist";

    if(fDrawSame->GetState() == kButtonDown)
        Options += " same";

    gErrorIgnoreLevel = kError;
    TObject *clone = o->Clone();
    clone->Draw(Options);
    gErrorIgnoreLevel = kPrint;

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

ClassImp(CXHist2DPlayer)