Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
xRooNode_interactive.cxx
Go to the documentation of this file.
1/*
2 * Project: xRooFit
3 * Author:
4 * Will Buttinger, RAL 2022
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13// Interactive methods of xRooNode
14
15#include "xRooFit/xRooNode.h"
16
17#include "RooArgList.h"
18#include "RooArgSet.h"
19
20#include "TCanvas.h"
21#include "TVirtualX.h"
22#include "TH1F.h"
23#include "TStyle.h"
24#include "TGraphAsymmErrors.h"
25#include "TMultiGraph.h"
26#include "TSystem.h"
27
29
30void xRooNode::Interactive_Pull()
31{
32 static bool doRestore = false;
33 auto select = dynamic_cast<TGraph *>(gPad->GetSelected());
34 // if (!select) return;
35 int event = gPad->GetEvent();
36 if (event == 1) {
37 doRestore = false;
38 // if this is one of the 'black' points, color it temporarily
39 } else if (event == 11 || doRestore) {
40 if (!select || doRestore) {
41 // now need to assemble a snapshot corresponding to the current variation
42 auto _h = static_cast<TGraph *>(gPad->GetPrimitive("nominal"))->GetHistogram();
43 for (int i = 1; i <= _h->GetNbinsX(); i++) {
44 std::string parName = _h->GetXaxis()->GetBinLabel(i);
45 // ensure point is back .. sometimes editing mode allows point to drift
46 auto _gr = static_cast<TGraph *>(static_cast<TMultiGraph *>(gPad->GetPrimitive("editables"))
47 ->GetListOfGraphs()
48 ->FindObject(parName.c_str()));
49 _gr->SetPoint(0, i - 1, static_cast<TGraph *>(gPad->GetPrimitive("nominal"))->GetPointY(i - 1));
50 }
51 gPad->GetMother()->GetMother()->cd();
52 doRestore=false;
53 return;
54 }
55 doRestore=true;
56 // mouse up event, if this was an original point it needs snapping back
57 // double _y = select->GetPointY(0);
58 TString _name = select->GetName();
59 TString _varyName = "";
60 if (_name.Contains(";")) {
61 _varyName = TString(_name(_name.Index(";") + 1, _name.Length()));
62 _name = _name(0, _name.Index(";"));
63 }
64 auto _h = static_cast<TGraph *>(gPad->GetPrimitive("nominal"))->GetHistogram();
65 for (int i = 1; i <= _h->GetNbinsX(); i++) {
66 if (_name == _h->GetXaxis()->GetBinLabel(i)) {
67 auto _gr = static_cast<TGraph *>(gPad->GetPrimitive("nominal"));
68 if (_varyName == "") {
69 int vNum = 1;
70 TGraphAsymmErrors *newPoint = dynamic_cast<TGraphAsymmErrors *>(
71 gPad->GetPrimitive(TString::Format("%s;variation %d", select->GetName(), vNum)));
72 while (newPoint && newPoint->GetN() > 0) {
73 vNum++;
74 newPoint = static_cast<TGraphAsymmErrors *>(
75 gPad->GetPrimitive(TString::Format("%s;variation %d", select->GetName(), vNum)));
76 }
77 _varyName = TString::Format("variation %d", vNum);
78 if (!newPoint)
79 newPoint = static_cast<TGraphAsymmErrors *>(
80 select->Clone(TString::Format("%s;%s", select->GetName(), _varyName.Data())));
81 newPoint->SetPointX(0, _gr->GetPointX(i - 1));
82 newPoint->SetMarkerColor(860 + (vNum-1)*20);
83 newPoint->SetLineColor(newPoint->GetMarkerColor());
84 newPoint->SetPointEYlow(0, 0);
85 newPoint->SetPointEYhigh(0, 0); // remove errors because currently meaningless!
86 newPoint->Draw("z0p");
87 select->SetPoint(0, _gr->GetPointX(i - 1), _gr->GetPointY(i - 1));
88 select = newPoint;
89 } else {
90 select->SetPointX(0, _gr->GetPointX(i - 1));
91 }
92 static_cast<TGraph *>(static_cast<TMultiGraph *>(gPad->GetPrimitive("editables"))
93 ->GetListOfGraphs()->FindObject(_name))->SetPoint(0, i - 1, _gr->GetPointY(i - 1));
94 break;
95 }
96 }
97
98 // then do an overlay update
99 auto _node = dynamic_cast<xRooNode *>(gPad->GetPrimitive("node"));
100 if (!_node)
101 return;
102 RooArgSet _pars(_node->pars().argList());
103 std::unique_ptr<RooArgSet> snap(_pars.snapshot());
104
105 // now need to assemble a snapshot corresponding to the current variation
106 for (int i = 1; i <= _h->GetNbinsX(); i++) {
107 std::string parName = _h->GetXaxis()->GetBinLabel(i);
108 // ensure point is back .. sometimes editing mode allows point to drift
109 // dynamic_cast<TGraph*>(gPad->GetPrimitive(parName.c_str()))->SetPoint(0,i-1,dynamic_cast<TGraph*>(gPad->GetPrimitive("nominal"))->GetPointY(i-1));
110 if (auto g =
111 dynamic_cast<TGraph *>(gPad->GetPrimitive(TString::Format("%s;%s", parName.c_str(), _varyName.Data())));
112 g && g->GetN() > 0) {
113 double _val =
114 g->GetPointY(0) * _h->GetBinError(i) + _h->GetBinContent(i); // error is scale, content is offset
115 _pars.setRealValue(parName.c_str(), _val);
116 g->SetTitle(TString::Format("%s=%g", parName.c_str(), _val));
117 } else {
118 _pars.setRealValue(parName.c_str(),
119 static_cast<TGraph *>(gPad->GetPrimitive("nominal"))->GetPointY(i - 1) *
120 _h->GetBinError(i) +
121 _h->GetBinContent(i));
122 }
123 }
124 TAttLine bak = *gStyle;
125 TAttFill bak2 = *gStyle;
128 gStyle->SetLineColor(select->GetMarkerColor());
129 auto _tmpPad = gPad;
130 gPad->GetMother()->GetMother()->cd(1);
131 _node->Draw(TString::Format("same overlay%s", _varyName.Data()));
132 // TODO: find the drawn variation and set its title equal to a _pars value string
133 static_cast<TAttLine&>(*gStyle) = bak;
134 static_cast<TAttFill&>(*gStyle) = bak2;
135 _pars = *snap;
136 _tmpPad->GetCanvas()->cd();
137 }
138}
139
140void xRooNode::Interactive_PLLPlot()
141{
142
143 // TObject *select = gPad->GetSelected();
144 // if(!select) return;
145 // if (!select->InheritsFrom(TGraph::Class())) {gPad->SetUniqueID(0); return;}
146 // gPad->GetCanvas()->FeedbackMode(kTRUE);
147
148 auto _pull_pad = gPad->GetPad(1);
149 auto _hidden_pad = gPad->GetPad(2);
150
151 if (!_pull_pad || strcmp(_pull_pad->GetName(), "pulls") != 0)
152 return;
153 if (!_hidden_pad)
154 return;
155
156 // erase old position and draw a line at current position
157 // int pxold = gPad->GetUniqueID();
158 int px = gPad->GetEventX();
159 // int py = gPad->GetEventY();
160 // int pymin = gPad->YtoAbsPixel(gPad->GetUymin());
161 // int pymax = gPad->YtoAbsPixel(gPad->GetUymax());
162 // if(pxold) gVirtualX->DrawLine(pxold,pymin,pxold,pymax);
163 // gVirtualX->DrawLine(px,pymin,px,pymax);
164 gPad->SetUniqueID(px);
165 Float_t upx = gPad->AbsPixeltoX(px);
166 Float_t x = gPad->PadtoX(upx);
167
168 // find which graph in the hidden pad best reflects current x value
169 TObject *foundGraph = nullptr;
170 for (auto g : *_hidden_pad->GetListOfPrimitives()) {
171 double midpoint = TString(g->GetName()).Atof();
172 if (!foundGraph)
173 foundGraph = g;
174 else {
175 midpoint = (midpoint + TString(foundGraph->GetName()).Atof()) / 2.;
176 }
177 if (midpoint >= x)
178 break;
179 foundGraph = g;
180 }
181 if (foundGraph) {
182 auto _x = TString(foundGraph->GetName()).Atof();
183 if (auto line = dynamic_cast<TGraph *>(gPad->GetListOfPrimitives()->FindObject("markerLine")); line) {
184 line->SetPointX(0, _x);
185 line->SetPointX(1, _x);
186 } else {
187 line = new TGraph;
188 line->SetLineStyle(2);
189 line->SetName("markerLine");
191 line->SetPoint(0, _x, -100);
192 line->SetPoint(1, _x, 100);
193 line->Draw("Lsame");
194 }
195 gPad->Modified();
196 gPad->Update();
197 for (auto o : *_pull_pad->GetListOfPrimitives()) {
198 if (!o->InheritsFrom("TGraph"))
199 continue;
200 if (_hidden_pad->GetListOfPrimitives()->FindObject(o) || TString(o->GetName()).EndsWith("_pull")) {
201 // todo: might need to delete the "_pull" plot if removing for first time
202 _pull_pad->GetListOfPrimitives()->Remove(o);
203 break;
204 }
205 }
206 auto tmp = gPad;
207 _pull_pad->cd();
208 foundGraph->Draw("pz0 same");
209 tmp->cd();
210 _pull_pad->Modified();
211 _pull_pad->Update();
212 }
213}
214
215void xRooNode::InteractiveObject::Interactive_PLLPlot(TVirtualPad *pad, TObject *obj, Int_t x, Int_t /*y*/)
216{
217
218 if (auto g = dynamic_cast<TGraph *>(obj); g && pad && pad->GetMother() && pad->GetNumber() == 1) {
219 auto frPad = pad->GetMother()->GetPad(2);
220 if (frPad) {
221 if (!g->IsHighlight())
222 x = -1;
223 else if (x >= 0)
224 x += 1;
225 // x is the point index
226 TVirtualPad *_pad = frPad->GetPad(x);
227 auto selPad = dynamic_cast<TVirtualPad *>(frPad->GetPrimitive("selected"));
228 if (_pad && selPad) {
229 auto prim = selPad->GetListOfPrimitives();
230 prim->Remove(prim->At(0));
231 prim->Add(_pad);
232
233 // for (auto p: *pad->GetListOfPrimitives()) {
234 // if (auto _p = dynamic_cast<TPad *>(p)) {
235 // _p->Modified();
236 // }
237 // }
238 // pad->Modified();
239 // pad->Update();
240 selPad->Modified();
241 selPad->Update();
243 }
244 }
245 }
246}
247
#define g(i)
Definition RSha256.hxx:105
float Float_t
Definition RtypesCore.h:57
@ kCanDelete
Definition TObject.h:369
R__EXTERN TStyle * gStyle
Definition TStyle.h:414
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
#define gPad
bool setRealValue(const char *name, double newVal=0.0, bool verbose=false)
Set value of a RooAbsRealLValye stored in set with given name to newVal No error messages are printed...
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:178
Fill Area Attributes class.
Definition TAttFill.h:19
Line Attributes class.
Definition TAttLine.h:18
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition TAttLine.h:42
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition TAttMarker.h:31
const char * GetBinLabel(Int_t bin) const
Return label for bin.
Definition TAxis.cxx:440
TGraph with asymmetric error bars.
virtual void SetPointEYlow(Int_t i, Double_t eyl)
Set EYlow for point i.
virtual void SetPointEYhigh(Int_t i, Double_t eyh)
Set EYhigh for point i.
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2325
Int_t GetN() const
Definition TGraph.h:129
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:808
TAxis * GetXaxis() const
Get x axis of the graph.
Definition TGraph.cxx:1550
virtual void SetPointX(Int_t i, Double_t x)
Set x value for point i.
Definition TGraph.cxx:2349
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition TList.cxx:822
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition TMultiGraph.h:34
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:439
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:403
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:774
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:785
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:274
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:421
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2222
Double_t Atof() const
Return floating-point value contained in string.
Definition TString.cxx:2032
const char * Data() const
Definition TString.h:380
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2356
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:636
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:419
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual TList * GetListOfPrimitives() const =0
virtual TVirtualPad * GetMother() const =0
virtual TVirtualPad * GetPad(Int_t subpadnumber) const =0
virtual Int_t GetNumber() const =0
TLine * line
Double_t x[n]
Definition legend1.C:17
#define BEGIN_XROOFIT_NAMESPACE
Definition Config.h:24
#define END_XROOFIT_NAMESPACE
Definition Config.h:25