GSI Object Oriented Online Offline (Go4) GO4-6.4.0
Loading...
Searching...
No Matches
TGo4PolyCond.cxx
Go to the documentation of this file.
1// $Id$
2//-----------------------------------------------------------------------
3// The GSI Online Offline Object Oriented (Go4) Project
4// Experiment Data Processing at EE department, GSI
5//-----------------------------------------------------------------------
6// Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7// Planckstr. 1, 64291 Darmstadt, Germany
8// Contact: http://go4.gsi.de
9//-----------------------------------------------------------------------
10// This software can be used under the license agreements as stated
11// in Go4License.txt file which is part of the distribution.
12//-----------------------------------------------------------------------
13
14#include "TGo4PolyCond.h"
15
16#include <vector>
17
18#include "TMath.h"
19#include "TROOT.h"
20#include "TH2.h"
21#include "TList.h"
22#include "TCutG.h"
23
24#include "TGo4PolyCondPainter.h"
25#include "TGo4Log.h"
26
29//#define POLYCOND_UPDATE_WITHCLONE 1
30
31
32TString TGo4PolyCond::fgxURL_NPOINTS="npolygon";
35
37{
38 TString res;
39 Int_t cnt = 0;
40 do {
41 res.Form("CutG_%d",cnt++);
42 } while (gROOT->GetListOfSpecials()->FindObject(res));
43 return res;
44}
45
46// ----------------------------------------------------------
49 fxCut(nullptr),
50 fxCutHis(nullptr)
51{
52 SetDimension(2);
53 //SetBit(kCanDelete, kFALSE);
54}
55// ----------------------------------------------------------
56TGo4PolyCond::TGo4PolyCond(const char *name, const char *title) :
57 TGo4Condition(name,title),
58 fxCut(nullptr),
59 fxCutHis(nullptr)
60{
61 SetDimension(2);
62 //SetBit(kCanDelete, kFALSE);
63}
64// ----------------------------------------------------------
66{
67 // UnDraw(); // JAM 2016 - do it before cut has vanished!
68
70
71 if (fxCut) {
72 delete fxCut;
73 fxCut = nullptr; // JAM2016: reset for Unpaint in PolyCondView?
74 }
75}
76
77// ----------------------------------------------------------
79{
81 ClearCutHis(); // discard internal cut histogram
82}
83
84// ----------------------------------------------------------
86{
87 if (fxCutHis) {
88 delete fxCutHis;
89 fxCutHis = nullptr;
90 }
91}
92
93// ----------------------------------------------------------
94Double_t TGo4PolyCond::GetXLow() const
95{
96 if(!fxCut) return 0.;
97 Int_t n = fxCut->GetN();
98 auto xarr = fxCut->GetX();
99 fxCut->SetBit(kMustCleanup,0);
100 Int_t nxmin = TMath::LocMin(n,xarr);
101 return xarr[nxmin];
102}
103
104Double_t TGo4PolyCond::GetXUp() const
105{
106 if(!fxCut) return 0.;
107 Int_t n=fxCut->GetN();
108 auto xarr=fxCut->GetX();
109 Int_t nxmax=TMath::LocMax(n,xarr);
110 return xarr[nxmax];
111}
112
113Double_t TGo4PolyCond::GetYLow() const
114{
115 if(!fxCut) return 0;
116 Int_t n=fxCut->GetN();
117 auto yarr=fxCut->GetY();
118 Int_t nymin=TMath::LocMin(n,yarr);
119 return yarr[nymin];
120}
121
122Double_t TGo4PolyCond::GetYUp() const
123{
124 if(!fxCut) return 0.;
125 Int_t n = fxCut->GetN();
126 auto yarr = fxCut->GetY();
127 Int_t nymax = TMath::LocMax(n,yarr);
128 return yarr[nymax];
129}
130
132{
133 return kTRUE;
134}
135
136// ----------------------------------------------------------
137TCutG *TGo4PolyCond::GetCut(Bool_t changeowner)
138{
139 TCutG *tempcut = fxCut;
140
141 if(changeowner) {
142 fxCut = nullptr;
143 ClearCutHis(); // discard internal histogram
144 }
145 return tempcut;
146}
147
148// ----------------------------------------------------------
150{
151 TCutG *tempcut = source->GetCut(false); // get fxCut pointer
152 if(tempcut) {
153 CleanupSpecials(); // JAM2016: Clone might delete cut of same name from list of specials, remove it first
154 TCutG *ret= (TCutG *)tempcut->Clone(GetName());
156 return ret;
157 }
158
159 return nullptr;
160}
161// ----------------------------------------------------------
162void TGo4PolyCond::SetValues(TCutG *newcut)
163{
164 if(!newcut) return;
165#ifdef POLYCOND_UPDATE_WITHCLONE
166 if(fxCut) delete fxCut;
167 CleanupSpecials(); // JAM2016: Clone might delete cut of same name from list of specials, remove it first
168 //fxCut = (TCutG*) newcut->Clone(NextAvailableName());
169 fxCut = (TCutG*) newcut->Clone(GetName());
170 fxCut->SetBit(kCanDelete,kFALSE);
172#else
173 Int_t pn = newcut->GetN();
174 if(!fxCut) {
175 fxCut = new TCutG(GetName(),pn);
176 fxCut->SetBit(kMustCleanup);
178 }
179 else
180 {
181 fxCut->Set(pn);
182 }
183 Double_t xp = 0, yp = 0;
184 for (Int_t i = 0; i < pn; ++i) {
185 newcut->GetPoint(i, xp, yp);
186 fxCut->SetPoint(i, xp, yp);
187 }
188
189
190#endif
191 // when updated from view, we store graphical attributes:
192 SetLineColor(newcut->GetLineColor());
193 SetLineWidth(newcut->GetLineWidth());
194 SetLineStyle(newcut->GetLineStyle());
195 SetFillColor(newcut->GetFillColor());
196 SetFillStyle(newcut->GetFillStyle());
197
198 ClearCutHis(); // fxCut changed, so discard previous fxCut histogram
199}
200 // ----------------------------------------------------------
202{
203 if(!newcut) return;
204 if(fxCut && fxCut!=newcut) delete fxCut;
205
206 fxCut = newcut;
207 //fxCut->SetName(NextAvailableName()); // JAM2016
208 fxCut->SetName(GetName());
209
210 // when updated from view, we store graphical attributes:
211 SetLineColor(newcut->GetLineColor());
212 SetLineWidth(newcut->GetLineWidth());
213 SetLineStyle(newcut->GetLineStyle());
214 SetFillColor(newcut->GetFillColor());
215 SetFillStyle(newcut->GetFillStyle());
216
217 ClearCutHis(); // fxCut changed, so discard previous fxCut histogram
218}
219
220// ----------------------------------------------------------
221void TGo4PolyCond::SetValues(Double_t * x, Double_t * y, Int_t len)
222{
223 if(fxCut) delete fxCut;
225 //fxCut = new TCutG(NextAvailableName(), len, x, y);
226 fxCut = new TCutG(GetName(), len, x, y);
227 fxCut->SetBit(kMustCleanup);
228 //fxCut->SetBit(kCanDelete,kFALSE);
230
231 ClearCutHis(); // discard previous fxCut histogram
232}
233
234// ----------------------------------------------------------
235Bool_t TGo4PolyCond::Test(Double_t x, Double_t y)
236{
237 IncCounts();
238 if(!IsEnabled() || !fxCut){
240 return FixedResult();
241 }
242 Bool_t outside = (fxCut->IsInside(x,y) == 0);
243 if(outside) return IsFalse();
245 return IsTrue();
246}
247
248// ----------------------------------------------------------
250{
252 if(points) {
253 if(!fxCut)
254 std::cout << "No polygon specified!" << std::endl;
255 else
256 fxCut->Print(nullptr);
257 }
258}
259
260// -----------------------------------------------
261// PrintBar switch is handled in condition baseclass now.
262//void TGo4PolyCond::Print(Option_t *opt) const{
264// TGo4PolyCond *const localthis= const_cast<TGo4PolyCond *const>(this);
265// localthis->PrintBar();
266//}
267// ----------------------------------------------------------
268Bool_t TGo4PolyCond::UpdateFrom(TGo4Condition *cond, Bool_t counts)
269{
270 if(!TGo4Condition::UpdateFrom(cond,counts)) return kFALSE;
271 if(cond->InheritsFrom(TGo4PolyCond::Class())) {
272
273#ifdef POLYCOND_UPDATE_WITHCLONE
274 TCutG * temp = CloneCut((TGo4PolyCond*)cond); // get clone from source, still valid there!
275 CleanupSpecials(); // remove all references to cloned TCutG from list of specials
276 if(temp)
277 {
278 TCutG *old = fxCut; // JAM2016 change cut before deleting the old one!
279 fxCut = temp;
280 if(old) delete old;
281 ClearCutHis();
282 return kTRUE;
283 }
284 else
285 {
286 return kFALSE;
287 }
288#else
289 /* JAM2016: try to avoid streaming the cut multiple times when updating condition:*/
290 TGo4PolyCond *source=(TGo4PolyCond*)cond;
291 TCutG * srccut = source->GetCut(false);
292
293 if(!srccut) return kFALSE;
294 CleanupSpecials(); // redundant? do it to get rid of entries from streamer!?
295 Int_t pn = srccut->GetN();
296 fxCut->Set(pn);
297 Double_t xp = 0, yp = 0;
298 for (Int_t i = 0; i < pn; ++i) {
299 srccut->GetPoint(i, xp, yp);
300 fxCut->SetPoint(i, xp, yp);
301 }
302
303 // still need this to reassign the bins of statistics histogram:
304 ClearCutHis();
305
306 return kTRUE;
307#endif
308
309 }
310 else
311 {
312 std::cout << "Cannot update " << GetName() << " from " << cond->ClassName() << std::endl;
313 return kFALSE;
314 }
315}
316
317
318Bool_t TGo4PolyCond::UpdateFromUrl(const char *rest_url_opt)
319{
320 if (!TGo4Condition::UpdateFromUrl(rest_url_opt))
321 return kFALSE;
322 TString message;
323 message.Form("TGo4PolyCond::UpdateFromUrl - condition %s: with url:%s", GetName(), rest_url_opt);
324 TGo4Log::Message(1, "%s", message.Data());
325
326 // evaluate options that change array of points
328 message = "";
330 if (npoints >= 0) {
331 TString xname, yname;
332 std::vector<Double_t> X(npoints), Y(npoints);
333 for (Int_t i = 0; i < npoints; ++i) {
334 xname.Form("%s%d", TGo4PolyCond::fgxURL_XPRE.Data(), i);
335 yname.Form("%s%d", TGo4PolyCond::fgxURL_YPRE.Data(), i);
336 X[i] = GetUrlOptionAsDouble(xname.Data(), 0.);
337 Y[i] = GetUrlOptionAsDouble(yname.Data(), 0.);
338 message.Append(TString::Format(" X[%i]=%f, Y[%i]=%f\n", i, X[i], i, Y[i]));
339 }
340 SetValues(X.data(), Y.data(), npoints);
341 }
342 message.Append(" - setting Polygon condition to new values!");
343 TGo4Log::Message(1, "%s", message.Data());
344 }
345 return kTRUE;
346}
347
348Double_t TGo4PolyCond::GetIntegral(TH1 *histo, Option_t *opt)
349{
350 return fxCut ? fxCut->IntegralHist(dynamic_cast<TH2 *>(histo),opt) : 0.;
351}
352
353Double_t TGo4PolyCond::GetMean(TH1 *histo, Int_t axis)
354{
355 if (IsCutHis(histo))
356 return fxCutHis->GetMean(axis);
357
358 return -1;
359}
360
361Double_t TGo4PolyCond::GetRMS(TH1 *histo, Int_t axis)
362{
363 if (IsCutHis(histo))
364 return fxCutHis->GetRMS(axis);
365
366 return -1;
367}
368
369Double_t TGo4PolyCond::GetSkewness(TH1 *histo, Int_t axis)
370{
371 if (IsCutHis(histo))
372 return fxCutHis->GetSkewness(axis);
373
374 return -1;
375}
376
377Double_t TGo4PolyCond::GetCurtosis(TH1 *histo, Int_t axis)
378{
379 if (IsCutHis(histo))
380 return fxCutHis->GetKurtosis(axis);
381
382 return -1;
383}
384
385Double_t TGo4PolyCond::GetXMax(TH1 *histo)
386{
387 if (IsCutHis(histo)) {
388 TAxis *xax = fxCutHis->GetXaxis();
389 Int_t maxbin = fxCutHis->GetMaximumBin();
390 Int_t xmaxbin = maxbin % (fxCutHis->GetNbinsX() + 2);
391 return xax->GetBinCenter(xmaxbin);
392 }
393 return -1;
394}
395
396Double_t TGo4PolyCond::GetYMax(TH1 *histo)
397{
398 if (IsCutHis(histo)) {
399 TAxis *yax = fxCutHis->GetYaxis();
400 Int_t maxbin = fxCutHis->GetMaximumBin();
401 Int_t maxybin = maxbin / (fxCutHis->GetNbinsX() + 2);
402 return yax->GetBinCenter(maxybin);
403 }
404
405 return -1;
406}
407
408Double_t TGo4PolyCond::GetCMax(TH1 *histo)
409{
410 if (IsCutHis(histo))
411 return fxCutHis->GetMaximum();
412
413 return -1;
414}
415
417{
418 if (!painter)
419 return;
420 if (painter->InheritsFrom(TGo4PolyCondPainter::Class())) {
421 if (fxPainter)
422 delete fxPainter;
423 fxPainter = painter;
424 fxPainter->SetCondition(this);
425 } else {
426 TGo4Log::Warn("Could not set painter of class %s for TGo4PolyCond %s", painter->ClassName(), GetName());
427 }
428}
429
430
432{
433 TGo4ConditionPainter *painter=new TGo4PolyCondPainter(GetName());
434 painter->SetCondition(this);
435 return painter;
436}
437
438
439Bool_t TGo4PolyCond::IsCutHis(TH1 *source)
440{
441 if (fxCutHis)
442 return kTRUE;
443
444 TH2 *his = dynamic_cast<TH2 *>(source);
445 if (!his)
446 return kFALSE;
447
448 fxCutHis = (TH2 *) his->Clone();
449 Int_t nx = fxCutHis->GetNbinsX();
450 Int_t ny = fxCutHis->GetNbinsY();
451 TAxis *xaxis = fxCutHis->GetXaxis();
452 TAxis *yaxis = fxCutHis->GetYaxis();
453 xaxis->SetRange(0, 0); // expand work histogram to full range
454 yaxis->SetRange(0, 0);
455 // set all bins outside fxCut to zero:
456 for (Int_t i = 0; i < nx; ++i) {
457 Double_t x = xaxis->GetBinCenter(i);
458 for (Int_t j = 0; j < ny; ++j) {
459 Double_t y = yaxis->GetBinCenter(j);
460 if (fxCut && !(fxCut->IsInside(x, y)))
461 fxCutHis->SetBinContent(i, j, 0);
462 }
463 }
464 // prepare statistics:
465 Stat_t s[11] = {0}; // dimension is kNstat of TH1.cxx
466 fxCutHis->PutStats(s); // reset previous stats
467 fxCutHis->GetStats(s); // recalculate
468 fxCutHis->PutStats(s); // put back
469 fxCutHis->SetDirectory(nullptr); // important for first draw from marker setup file!
470
471 return kTRUE;
472}
473
474
475// ----------------------------------------------------------
477{
478 TSeqCollection *specials = gROOT->GetListOfSpecials();
479 TIter iter(specials);
480 while(auto ob = iter()) {
481 if(ob->InheritsFrom(TCutG::Class()))
482 specials->Remove(ob);
483 }
484}
485
487{
488 Int_t size = sizeof(*this);
489 if (GetName()) size += strlen(GetName());
490 if (GetTitle()) size += strlen(GetTitle());
491 if (fxCut) {
492 size += sizeof(*fxCut);
493 size += fxCut->GetMaxSize()*2*sizeof(Double_t);
494 }
495 return size;
496}
497
498void TGo4PolyCond::SavePrimitive(std::ostream &out, Option_t *opt)
499{
500 static int cnt = 0;
501 TString line, varname = MakeScript(out, TString::Format("polycond%d", cnt++).Data(), opt);
502
503 if (!fxCut || (fxCut->GetN() == 0)) {
504 line.Form(" %s->SetValues(0, 0, 0);", varname.Data());
505 } else {
506 TString xname = varname;
507 xname.ReplaceAll("->At(","_sub");
508 xname.ReplaceAll(")","");
509 TString yname = xname + "_y";
510 xname = xname + "_x";
511 line.Form(" Double_t %s[%d], %s[%d];", xname.Data(), fxCut->GetN(), yname.Data(), fxCut->GetN());
512 out << line << std::endl;
513 for (Int_t n = 0; n < fxCut->GetN(); n++) {
514 Double_t x, y;
515 fxCut->GetPoint(n, x, y);
516 line.Form(" %s[%d] = %f; %s[%d] = %f;", xname.Data(), n, x, yname.Data(), n, y);
517 out << line << std::endl;
518 }
519 line.Form(" %s->SetValues(%s, %s, %d);", varname.Data(), xname.Data(), yname.Data(), fxCut->GetN());
520 }
521
522 out << line << std::endl;
523}
virtual void SetCondition(TGo4Condition *con)
Not inline, because we overwrite this for polygon condition.
virtual Bool_t UpdateFrom(TGo4Condition *cond, Bool_t counts)
Copy values from cond to this.
Double_t GetUrlOptionAsDouble(const char *key, Double_t def_value)
Scan list of url options for key.
virtual Bool_t UpdateFromUrl(const char *rest_url_opt)
Method used by HTTP server to update some fields, specified in URL syntax.
Int_t GetUrlOptionAsInt(const char *key, Int_t def_value)
Scan list of url options for key.
void IncCounts()
Increment the test counter.
TGo4ConditionPainter * fxPainter
Painter instance to display the condition in root pad.
Bool_t FixedResult() const
Returns the value set by Disable(value).
void SetDimension(Int_t d)
void IncTrueCounts()
Increment the "test was true" counter.
Bool_t IsTrue() const
Use this as return, if test was true.
Bool_t UrlOptionHasKey(const char *key)
returns true if key is present in list of url options.
virtual void PrintCondition(Bool_t full=kTRUE)
Prints counters and boolean members.
Bool_t IsFalse() const
Use this as return, if test was false.
const char * MakeScript(std::ostream &out, const char *varname, Option_t *opt="", const char *arrextraargs=nullptr)
virtual void SetWorkHistogram(TH1 *histo)
Set reference to work histogram for statistics functions.
Bool_t IsEnabled() const
Returns true if condition is enabled.
static void Warn(const char *text,...) GO4_PRINTF_ARGS
User shortcut for message with prio 2.
Definition TGo4Log.cxx:307
static const char * Message(Int_t prio, const char *text,...) GO4_PRINTF2_ARGS
Display a message.
Definition TGo4Log.cxx:206
TCutG * fxCut
Cut pointer.
Double_t GetCMax(TH1 *histo) override
Calculate value for histogram inside condition limits.
TGo4ConditionPainter * CreatePainter() override
Factory method to generate the subclass implementation for painter.
Double_t GetRMS(TH1 *histo, Int_t axis=1) override
Calculate value for histogram inside condition limits.
Bool_t IsCutHis(TH1 *source)
Returns true if fxCutHis can be used, create if possible.
void PrintCondition(Bool_t points=kTRUE) override
Printout values.
virtual ~TGo4PolyCond()
Double_t GetSkewness(TH1 *histo, Int_t axis=1) override
Calculate value for histogram inside condition limits.
Double_t GetCurtosis(TH1 *histo, Int_t axis=1) override
Calculate value for histogram inside condition limits.
Double_t GetXUp() const override
maximum extension of polygon in x axis
static TString fgxURL_YPRE
web condition editor keyword used in UpdateFromUrl.
void SetPainter(TGo4ConditionPainter *painter) override
Replace default painter of this condition by external one.
void ClearCutHis()
Set new cut histogram.
Bool_t UpdateFrom(TGo4Condition *cond, Bool_t counts) override
Copy values from cond to this.
Bool_t UpdateFromUrl(const char *rest_url_opt) override
Method used by HTTP server to update some fields, specified in URL syntax.
TH2 * fxCutHis
temporary histogram used to calculate statistics on polygon cuts.
virtual void SetValues()
Set values needed, i.e.
Double_t GetYUp() const override
maximum extension of polygon in y axis
void SavePrimitive(std::ostream &fs, Option_t *opt="") override
Standard way to store parameter in form of macro.
Double_t GetYLow() const override
minimum extension of polygon in y axis
static TString fgxURL_XPRE
web condition editor keyword used in UpdateFromUrl.
virtual Bool_t Test()
Test if condition is true.
static TString NextAvailableName()
Double_t GetMean(TH1 *histo, Int_t axis=1) override
Calculate value for histogram inside condition limits.
void SetValuesDirect(TCutG *newcut)
Delete old cut and get ownership over newcut.
Double_t GetXMax(TH1 *histo) override
Calculate value for histogram inside condition limits.
Double_t GetIntegral(TH1 *histo, Option_t *opt="") override
Calculate value for histogram inside condition limits.
static void CleanupSpecials()
Remove all references to any TCutGs from ROOT list of specials.
static TString fgxURL_NPOINTS
web condition editor keyword used in UpdateFromUrl.
Int_t GetMemorySize() const override
void SetWorkHistogram(TH1 *histo) override
Set reference to work histogram for statistics functions.
Bool_t IsPolygonType() const override
Double_t GetYMax(TH1 *histo) override
Calculate value for histogram inside condition limits.
TCutG * GetCut(Bool_t changeowner) override
Used to return the cut pointer of a TGo4PolyCond.
Double_t GetXLow() const override
minimum extension of polygon in x axis
TCutG * CloneCut(TGo4PolyCond *source)
Returns pointer to a cloned cut from source.