ARTEMIS-CRIB
 
Loading...
Searching...
No Matches
TCmdXYblow.cc
Go to the documentation of this file.
1/**
2 * @file TCmdXYblow.cc
3 * @brief
4 * @author Kodai Okawa <okawa@cns.s.u-tokyo.ac.jp>
5 * @date 2023-06-13 17:38:35
6 * @note last modified: 2024-08-23 21:46:06
7 * @details
8 */
9
10#include "TCmdXYblow.h"
11
12#include <TCatCmdXval.h>
13#include <TCatHistManager.h>
14#include <TF1.h>
15#include <TH2.h>
16#include <TPad.h>
17
19
21
22const char *TCmdXYblow::kFuncNameBase = "f";
23const char *TCmdXYblow::kLabelNameBase = "l";
24
26 SetName("xyblow");
27 SetTitle("Crop histogram (using mouse)");
28}
29
31
32Long_t TCmdXYblow::Cmd(vector<TString> args) {
33 const TString opt = args.size() > 2 ? args[2] : "";
34
35 if (gPad == nullptr) {
36 Info("Cmd", "warning: no pad");
37 return 1;
38 }
39
40 TObject *hist_now = gPad->GetPrimitive("htemp");
41 if (!hist_now) {
42 hist_now = TCatHistManager::Instance()->GetCurrent();
43 if (!hist_now || !hist_now->InheritsFrom("TH1")) {
44 Info("Cmd", "warning: no hist, please check using ls command");
45 return 1;
46 }
47 }
48
49 if (!hist_now->InheritsFrom(TH2::Class())) {
50 Info("Cmd", "warning: The current hist does not inherit from TH2 class");
51 return 1;
52 }
53
54 Int_t nid = (gDirectory->GetList())->GetEntries();
55 Run((TH2 *)hist_now, opt);
56 Info("Run", "id = %d hist is created", nid);
57 TCatHistManager::Instance()->DrawObject(nid, "colz");
58
59 return 1;
60}
61
62TH2 *TCmdXYblow::Run(TH2 *h2, Option_t *) {
63
64 Double_t x1 = 0., y1 = 0., x2 = 0., y2 = 0.;
65 Info("Run", "click on one corner: ");
66 TCatCmdXval::Instance()->Run(&x1, &y1);
67 Info("Run", "X1: %g, Y1: %g", x1, y1);
68
69 Info("Run", "click on the other corner: ");
70 TCatCmdXval::Instance()->Run(&x2, &y2);
71 Info("Run", "X2: %g, Y2: %g", x2, y2);
72
73 Double_t temp;
74 if (x1 > x2) {
75 temp = x1;
76 x1 = x2;
77 x2 = temp;
78 }
79 if (y1 > y2) {
80 temp = y1;
81 y1 = y2;
82 y2 = temp;
83 }
84
85 Int_t x1bin, x2bin, y1bin, y2bin, nbinsx, nbinsy;
86 Float_t bc, btotal;
87
88 const TString namesuffix = " blo", titlesuffix = " blo";
89
90 x1bin = h2->GetXaxis()->FindBin(x1);
91 x2bin = h2->GetXaxis()->FindBin(x2);
92 y1bin = h2->GetYaxis()->FindBin(y1);
93 y2bin = h2->GetYaxis()->FindBin(y2);
94
95 nbinsx = (x2bin - x1bin) + 1;
96 nbinsy = (y2bin - y1bin) + 1;
97
98 TH2F *hnew = new TH2F("name", "title", nbinsx, x1, x2, nbinsy, y1, y2);
99
100 btotal = 0.;
101 for (Int_t nx = x1bin; nx <= x2bin; nx++) {
102 for (Int_t ny = y1bin; ny <= y2bin; ny++) {
103 bc = h2->GetBinContent(nx, ny);
104 hnew->SetBinContent(nx - x1bin, ny - y1bin, bc);
105 btotal += bc;
106 }
107 }
108
109 hnew->SetName(TString(h2->GetName()) + namesuffix);
110 hnew->SetTitle(TString(h2->GetTitle()) + titlesuffix);
111 hnew->SetEntries(btotal);
112
113 return hnew;
114}
115
117 std::cout << "crop some part of the histogram" << std::endl;
118}
ClassImp(TCmdXYblow)
TH2 * Run(TH2 *h2, Option_t *)
Definition TCmdXYblow.cc:62
static const char * kFuncNameBase
Definition TCmdXYblow.h:26
static const char * kLabelNameBase
Definition TCmdXYblow.h:27
Long_t Cmd(vector< TString >) override
Definition TCmdXYblow.cc:32
void Help() override
return to the guide