7#include "TVirtualPad.h"
13#include "KeySymbols.h"
24GH1D::GH1D(
const TH1& source) : fParent(nullptr), fProjectionAxis(-1)
29GH1D::GH1D(
const TH1* source) : fParent(nullptr), fProjectionAxis(-1)
34GH1D::GH1D(
const TF1& function, Int_t nbinsx, Double_t xlow, Double_t xup)
35 : TH1D(Form(
"%s_hist", function.GetName()), Form(
"%s_hist", function.GetName()), nbinsx, xlow, xup), fParent(nullptr),
38 for(
int i = 0; i < nbinsx; i++) {
39 Fill(GetBinCenter(i), function.Eval(i));
45 if(strlen(outFile) < 1) {
52 if(!(out.is_open())) {
56 for(
int i = 0; i < GetNbinsX(); i++) {
57 out << GetXaxis()->GetBinCenter(i) <<
"\t" << GetBinContent(i) << std::endl;
98 std::cout <<
"\tParent: " <<
fParent.GetObject() << std::endl;
111 if(option.Contains(
"new", TString::kIgnoreCase)) {
112 option.ReplaceAll(
"new",
"");
115 TH1D::Draw(option.Data());
117 if(gPad !=
nullptr) {
119 gPad->GetFrame()->SetBit(TBox::kCannotMove);
126 TH1*
hist = TH1D::DrawCopy(opt, name_postfix);
127 if(gPad !=
nullptr) {
129 gPad->GetFrame()->SetBit(TBox::kCannotMove);
136 TH1*
hist = TH1D::DrawNormalized(opt, norm);
137 if(gPad !=
nullptr) {
139 gPad->GetFrame()->SetBit(TBox::kCannotMove);
146 if((
fParent.GetObject() !=
nullptr) &&
fParent.GetObject()->InheritsFrom(GH2Base::Class())) {
148 int first = GetXaxis()->GetFirst();
149 int last = GetXaxis()->GetLast();
151 prev->GetXaxis()->SetRange(first, last);
159 if((
fParent.GetObject() !=
nullptr) &&
fParent.GetObject()->InheritsFrom(GH2Base::Class())) {
161 int first = GetXaxis()->GetFirst();
162 int last = GetXaxis()->GetLast();
164 next->GetXaxis()->SetRange(first, last);
174 if(value_low > value_high) {
175 std::swap(value_low, value_high);
179 int bin_low = gpar->GetXaxis()->FindBin(value_low);
180 int bin_high = gpar->GetXaxis()->FindBin(value_high);
181 return gpar->
ProjectionY(
"_py", bin_low, bin_high);
183 int bin_low = gpar->GetYaxis()->FindBin(value_low);
184 int bin_high = gpar->GetYaxis()->FindBin(value_high);
185 return gpar->
ProjectionX(
"_px", bin_low, bin_high);
194 if(value_low > value_high) {
195 std::swap(value_low, value_high);
197 if(bg_value_low > bg_value_high) {
198 std::swap(bg_value_low, bg_value_high);
203 int bin_low = gpar->GetXaxis()->FindBin(value_low);
204 int bin_high = gpar->GetXaxis()->FindBin(value_high);
205 int bg_bin_low = gpar->GetXaxis()->FindBin(bg_value_low);
206 int bg_bin_high = gpar->GetXaxis()->FindBin(bg_value_high);
210 int bin_low = gpar->GetYaxis()->FindBin(value_low);
211 int bin_high = gpar->GetYaxis()->FindBin(value_high);
212 int bg_bin_low = gpar->GetYaxis()->FindBin(bg_value_low);
213 int bg_bin_high = gpar->GetYaxis()->FindBin(bg_value_high);
222 GH1D* proj =
nullptr;
223 double ymax = GetMinimum();
224 double ymin = GetMaximum();
226 bins =
static_cast<int>(std::abs(ymax - ymin));
231 proj =
new GH1D(Form(
"%s_y_axis_projection", GetName()), Form(
"%s_y_axis_projection", GetName()), bins, ymin, ymax);
232 for(
int i = 0; i < GetNbinsX(); i++) {
233 if(GetBinContent(i) != 0) {
234 proj->Fill(GetBinContent(i));
244 fPad->GetCanvas()->Connect(
"ProcessedEvent(Int_t, Int_t, Int_t, TObject*)",
"GH1D",
this,
"HandleMovement(Int_t,Int_t,Int_t, TObject*)");
245 gClient->Connect(
"ProcessedEvent(Event_t*, Window_t)",
"GH1D",
this,
"HandleEvent(Event_t*, Window_t)");
250 double currentX =
fPad->PixeltoX(eventX);
251 double currentY =
fPad->PixeltoY(eventY -
fPad->GetWh());
253 if(selected !=
nullptr && selected->InheritsFrom(TRegion::Class())) {
254 auto* region =
static_cast<TRegion*
>(selected);
255 if(eventType == kKeyPress && eventX == kKey_d) {
257 }
else if(eventType == kButton1Down) {
261 std::cout <<
"button 1 down at " << currentX <<
", " << currentY << std::endl;
263 }
else if(eventType == kButton1Motion) {
265 std::cout <<
"button 1 motion at " << currentX <<
", " << currentY << std::endl;
267 }
else if(eventType == kButton1Up) {
268 region->Update(
fStartX, currentX);
270 std::cout <<
"button 1 up at " << currentX <<
", " << currentY << std::endl;
274 std::cout <<
"nullptr selected at " << currentX <<
", " << currentY << std::endl;
286 UInt_t keySymbol = 0;
287 std::array<char, 2> str;
288 gVirtualX->LookupString(event, str.data(), str.size(), keySymbol);
293 std::cout << __PRETTY_FUNCTION__ <<
", event " <<
event <<
", window " << window <<
", type " <<
event->fType <<
", code " <<
event->fCode <<
", state " <<
event->fState <<
", x " <<
event->fX <<
", root-x " <<
event->fXRoot <<
", y " <<
event->fY <<
", root-y " <<
event->fYRoot <<
", x/y coordinates: x " <<
fPad->PixeltoX(event->fX) <<
", y " <<
fPad->PixeltoY(event->fY -
fPad->GetWh()) <<
", root-x " <<
fPad->PixeltoX(event->fXRoot) <<
", root-y " <<
fPad->PixeltoY(event->fYRoot -
fPad->GetWh()) <<
", key symbol " << keySymbol <<
" = " <<
hex(keySymbol) <<
"; fPad " <<
fPad <<
" = \"" <<
fPad->GetName() <<
"\", gPad " << gPad <<
" = \"" << gPad->GetName() <<
"\"" << std::endl;
296 switch(event->fType) {
303 gROOT->SetInterrupt();
318 if(
fPad->GetLogy() == 0) {
332 GetXaxis()->UnZoom();
333 GetYaxis()->UnZoom();
343 GetYaxis()->UnZoom();
357 std::cout <<
"Escape!" << std::endl;
366 std::cout <<
"Moving histogram" << std::endl;
382 switch(event->fCode) {
405 if(event->fState == 0) {
408 double factor = 0.90;
409 if(event->fCode == 4) {
413 std::cout <<
"factor " << factor <<
": maximum " << GetMinimum() + (GetMaximum() - GetMinimum()) * factor <<
", old range " << (GetMaximum() - GetMinimum()) <<
" = " << GetMaximum() <<
" - " << GetMinimum() << std::endl;
415 SetMaximum(GetMinimum() + (GetMaximum() - GetMinimum()) * factor);
417 std::cout <<
"new range: " << (GetMaximum() - GetMinimum()) <<
" = " << GetMaximum() <<
" - " << GetMinimum() << std::endl;
419 }
else if(event->fState == 1) {
420 if(event->fCode == 4) {
440 double currentX =
fPad->PixeltoX(event->fX);
441 double currentY =
fPad->PixeltoY(event->fY -
fPad->GetWh());
443 std::cout <<
"current box: " <<
fStartX <<
" - " <<
fStartY <<
", " << currentX <<
" - " << currentY <<
", canvas: " <<
fPad->PixeltoX(
fPad->GetCanvas()->GetEventX()) <<
" - " <<
fPad->PixeltoY(
fPad->GetCanvas()->GetEventY()) << std::endl;
455 if(event->fCode == 1) {
456 if(event->fState == 0) {
459 double currentX =
fPad->PixeltoX(event->fX);
460 double currentY =
fPad->PixeltoY(event->fY -
fPad->GetWh());
462 if(
Pad()->GetFrame()->GetX1() < currentX && currentX <
Pad()->GetFrame()->GetX2() &&
463 Pad()->GetFrame()->GetY1() < currentY && currentY <
Pad()->GetFrame()->GetY2()) {
465 std::cout <<
"inside frame" << std::endl;
469 GetYaxis()->UnZoom();
471 std::cout <<
"outside frame" << std::endl;
482 double stopX =
fPad->PixeltoX(event->fX);
483 double stopY =
fPad->PixeltoY(event->fY -
fPad->GetWh());
488 std::cout <<
"new gate: " <<
fStartX <<
" - " << stopX << std::endl;
494 std::cout <<
"new background: " <<
fStartX <<
" - " << stopX << std::endl;
500 std::cout <<
"new region: " <<
fStartX <<
" - " << stopX << std::endl;
523 static_cast<TRegion*
>(obj)->Update();
536 std::cout << GetName() <<
"/" << GetTitle() <<
" - Regions:" << std::endl;
538 for(
auto* obj : *(
Pad()->GetListOfPrimitives())) {
539 if(obj->InheritsFrom(TRegion::Class())) {
540 std::cout << index <<
": " << obj <<
" ";
559 fPad->GetListOfPrimitives()->Remove(region);
564 : TBox(*box), fParent(parent), fType(type), fLowX(box->GetX1()), fHighX(box->GetX2())
567 if(GetX1() > GetX2()) {
568 double tmpX = GetX1();
574 auto* axis = parent->GetXaxis();
575 fLowX = axis->GetBinLowEdge(axis->FindBin(
fLowX));
590 std::cout <<
"Frame: " << frame->GetX1() <<
" - " << frame->GetX2() <<
", " << frame->GetY1() <<
" - " << frame->GetY2() <<
" -> updating y-range from " << GetY1() <<
" - " << GetY2();
594 SetY1(frame->GetY1());
595 SetY2(frame->GetY2());
598 SetY1(TMath::Power(10., frame->GetY1()));
599 SetY2(TMath::Power(10., frame->GetY2()));
602 std::cout <<
" to " << GetY1() <<
" - " << GetY2() << std::endl;
606 std::cout <<
"Updating x-range from " << GetX1() <<
" - " << GetX2() <<
" (" <<
fLowX <<
" - " <<
fHighX <<
")";
611 std::cout <<
" (whole region)";
621 std::cout <<
" (out of " << frame->GetX1() <<
" - " << frame->GetX2() <<
")";
627 std::cout <<
" (high part)";
629 SetX1(frame->GetX1());
631 }
else if(frame->GetX1() <
fLowX) {
634 std::cout <<
" (low part)";
637 SetX2(frame->GetX2());
641 std::cout <<
" (region too big)";
643 SetX1(frame->GetX1());
644 SetX2(frame->GetX2());
647 std::cout <<
" to " << GetX1() <<
" - " << GetX2() << std::endl;
659 throw std::runtime_error(
"Can't draw region without parent histogram!");
663 if(
fParent->
Pad()->GetListOfPrimitives()->FindObject(
this) ==
nullptr) {
666 std::cout <<
this <<
" region " <<
fLowX <<
" - " <<
fHighX <<
" already has been drawn" << std::endl;
678 throw std::runtime_error(
"Can't draw region without parent histogram!");
681 fParent->
Pad()->GetListOfPrimitives()->Remove(
this);
683 std::cout <<
"hid region " <<
fLowX <<
" - " <<
fHighX << std::endl;
692 std::cout <<
"region " <<
fLowX <<
" - " <<
fHighX <<
", start x " << startX <<
", stop x " << stopX;
694 if(std::abs(startX -
fLowX) < std::abs(startX -
fHighX)) {
700 std::cout <<
" => " <<
fLowX <<
" - " <<
fHighX << std::endl;
bool Move1DHistogram(const Int_t &key, TH1 *histogram=nullptr)
std::string hex(T val, int width=-1)
double fStartX
! initial x-position of new region
GH1D * Project_Background(double value_low, double value_high, double bg_value_low, double bg_value_high, EBackgroundSubtraction mode=EBackgroundSubtraction::kRegionBackground) const
TVirtualPad * Pad() const
void SetPad(TVirtualPad *pad)
void DrawRegions(Option_t *opt="")
std::array< int, 3 > fRegionColor
TH1 * DrawNormalized(Option_t *opt="", Double_t norm=1) const override
void HandleMovement(Int_t eventType, Int_t eventX, Int_t eventY, TObject *selected)
void RemoveCurrentRegion()
double fStartY
! initial y-position of new region
GH1D * GetNext(bool DrawEmpty=false) const
static EVerbosity fVerboseLevel
! level of verbosity
void Copy(TObject &obj) const override
size_t fNofRegions
! counts number of regions in this histogram, only used to set the color of the region
void Print(Option_t *opt="") const override
bool WriteDatFile(const char *outFile)
void Clear(Option_t *opt="") override
TBox * fCurrentRegion
! box for the current region
GH1D * GetPrevious(bool DrawEmpty=false) const
static EVerbosity VerboseLevel()
void HandleEvent(Event_t *event, Window_t window)
GH1D * Project(int bins=-1)
TH1 * DrawCopy(Option_t *opt="", const char *name_postfix="copy") const override
void RemoveRegion(TRegion *region)
void Draw(Option_t *opt="") override
GH1D * GetPrevious(const GH1D *curr, bool DrawEmpty=true)
GH1D * ProjectionY_Background(int firstbin=0, int lastbin=-1, int first_bg_bin=0, int last_bg_bin=-1, EBackgroundSubtraction mode=EBackgroundSubtraction::kRegionBackground)
GH1D * GetNext(const GH1D *curr, bool DrawEmpty=true)
GH1D * ProjectionX_Background(int firstbin=0, int lastbin=-1, int first_bg_bin=0, int last_bg_bin=-1, EBackgroundSubtraction mode=EBackgroundSubtraction::kRegionBackground)
GH1D * ProjectionX(const char *name="_px", int firstbin=0, int lastbin=-1, Option_t *option="")
GH1D * ProjectionY(const char *name="_py", int firstbin=0, int lastbin=-1, Option_t *option="")
void Draw(Option_t *opt="") override