ARTEMIS-CRIB
 
Loading...
Searching...
No Matches
TCatCmdTCutG.cc
Go to the documentation of this file.
1/**
2 * @file TCatCmdTCutG.cc
3 * @brief
4 * @author Kodai Okawa <okawa@cns.s.u-tokyo.ac.jp>
5 * @date 2023-06-13 18:34:21
6 * @note last modified: 2024-08-23 21:35:53
7 * @details
8 */
9
10#include "TCatCmdTCutG.h"
11
12#include <TCatHistManager.h>
13#include <TCutG.h>
14#include <TFile.h>
15#include <TH2.h>
16#include <TLine.h>
17#include <TPad.h>
18#include <chrono>
19#include <iostream>
20#include <thread>
21
23
25
26TCatCmdTCutG::TCatCmdTCutG() : fX(0.), fY(0.), fEventtype(0), fisFirst(true) {
27 SetName("tcutg");
28 SetTitle("get TCutG object (using mouse)");
29}
30
32
34 static TCatCmdTCutG instance;
35 return &instance;
36}
37
38Long_t TCatCmdTCutG::Cmd(vector<TString>) {
39 if (gPad == nullptr) {
40 Info("Cmd", "warning: no pad");
41 return 1;
42 }
43
44 TObject *hist_now = gPad->GetPrimitive("htemp");
45 if (!hist_now) {
46 hist_now = TCatHistManager::Instance()->GetCurrent();
47 if (!hist_now || !hist_now->InheritsFrom("TH1")) {
48 Info("Cmd", "warning: no hist, please check using ls command");
49 return 1;
50 }
51 }
52
53 if (!hist_now->InheritsFrom(TH2::Class())) {
54 Info("Cmd", "warning: The current hist does not inherit from TH2 class");
55 return 1;
56 }
57
58 TString xname = ((TH2 *)hist_now)->GetXaxis()->GetTitle();
59 TString yname = ((TH2 *)hist_now)->GetYaxis()->GetTitle();
60 if (xname == "" || yname == "") {
61 TString title = ((TH2 *)hist_now)->GetTitle();
62 yname = title(0, title.First(":"));
63 if (title.Contains(' ')) {
64 TString tmp = title(title.First(":") + 1, title.Length());
65 xname = tmp(0, tmp.First(' '));
66 } else
67 xname = title(title.First(":") + 1, title.Length());
68 }
69
70 Info("Cmd", "Xaxis name : %s Yaxis name : %s", xname.Data(), yname.Data());
71
72 Double_t ix = 0.0, iy = 0.0, x = 0.0, y = 0.0, bx = 0.0, by = 0.0;
73 Int_t i = 0;
74 std::vector<TLine *> lines;
75 TCutG *cutg = new TCutG();
76 cutg->SetVarX(xname);
77 cutg->SetVarY(yname);
78
79 Info("Cmd", "When you have finished specifying the area (last point), double-click on it.");
80 fisFirst = true;
81 fEventtype = 0;
82 while (fEventtype != kButton1Double) {
83 TCatCmdTCutG::Instance()->Run(&x, &y);
84 Info("Cmd", "(x, y) = (%lf, %lf)", x, y);
85 if (!fisFirst) {
86 TLine *line = new TLine(bx, by, x, y);
87 lines.push_back(line);
88 lines[i - 1]->SetLineColor(kRed);
89 lines[i - 1]->SetLineWidth(2);
90 lines[i - 1]->Draw("same");
91 }
92 if (fisFirst) {
93 ix = x;
94 iy = y;
95 fisFirst = false;
96 }
97 gPad->Update();
98 cutg->SetPoint(i, x, y);
99 bx = x;
100 by = y;
101 i++;
102 }
103 cutg->RemovePoint(i - 1);
104 cutg->SetPoint(i - 1, ix, iy);
105
106 for (Size_t l = 0; l < lines.size(); l++) {
107 delete lines[l];
108 }
109 cutg->SetLineColor(kRed);
110 cutg->SetLineWidth(2);
111 cutg->Draw("l same");
112 std::this_thread::sleep_for(std::chrono::milliseconds(10));
113 gPad->Update();
114
115 TString input;
116 std::cout << "if you want to save it, input the TCutG name [name/exit] ";
117 std::cin >> input;
118 if (input == "exit" || input == "") {
119 Info("Cmd", "exit, not saved");
120 return 1;
121 }
122
123 cutg->SetName(input);
124 TFile *file = new TFile("gate/" + input + ".root", "recreate");
125 Info("Cmd", "Created gate/%s.root", input.Data());
126 cutg->Write();
127 file->Close();
128
129 return 1;
130}
131
132Long_t TCatCmdTCutG::Run(TPad *pad, Double_t *x, Double_t *y) {
133 if (!pad)
134 return 0;
135 pad->cd();
136
137 pad->AddExec("ex_setxy", "TCatCmdTCutG::Instance()->GetEvent()");
138 if (TObject *const obj = gPad->WaitPrimitive("setxy")) {
139 gPad->GetListOfPrimitives()->Remove(obj);
140 delete obj;
141 } else {
142 fX = fY = 0;
143 return 0;
144 }
145
146 if (!x && !y) {
147 // display coordinates when both of x and y are nullptr.
148 printf("[xval] X: %f, Y: %f\n", fX, fY);
149 } else {
150 if (x)
151 *x = fX;
152 if (y)
153 *y = fY;
154 }
155
156 return 1;
157}
158
159Long_t TCatCmdTCutG::Run(Double_t *x, Double_t *y) {
160 return Run((TPad *)gPad, x, y);
161}
162
164 const Int_t event = gPad->GetEvent();
165 const Int_t px = gPad->GetEventX();
166 const Double_t xx = gPad->AbsPixeltoX(px);
167 const Double_t x = gPad->PadtoX(xx);
168 const Int_t py = gPad->GetEventY();
169 const Double_t yy = gPad->AbsPixeltoY(py);
170 const Double_t y = gPad->PadtoY(yy);
171
172 if (!fisFirst) {
173 TLine *line = new TLine(fX, fY, x, y);
174 line->SetLineColor(kBlack);
175 line->Draw("same");
176 gPad->Update();
177 delete line;
178 }
179
180 if (event == kButton1Double) {
181 fEventtype = event;
182 return;
183 } else if (event != kButton1Up)
184 return;
185
186 gPad->DeleteExec("ex_setxy");
187
188 fX = x;
189 fY = y;
190 gPad->GetListOfPrimitives()->AddLast(new TNamed("setxy", "setxy"));
191}
192
194 std::cout << "make TCutG object" << std::endl;
195}
ClassImp(TCatCmdTCutG)
Long_t Cmd(vector< TString >) override
Long_t Run(TPad *pad, Double_t *x=nullptr, Double_t *y=nullptr)
static TCatCmdTCutG * Instance()
return to the guide