GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4WinCond.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 "TGo4WinCond.h"
15 
16 #include "TH1.h"
17 
18 #include "TGo4WinCondPainter.h"
19 #include "TGo4Log.h"
20 
21 TString TGo4WinCond::fgxURL_XLOW="xmin";
22 TString TGo4WinCond::fgxURL_XUP="xmax";
23 TString TGo4WinCond::fgxURL_YLOW="ymin";
24 TString TGo4WinCond::fgxURL_YUP="ymax";
25 
26 
27 // -----------------------------------------------
28 // Constructors
29 // -----------------------------------------------
31  TGo4Condition(),
32  fLow1(0),
33  fUp1(0),
34  fLow2(0),
35  fUp2(0),
36  fiSaveXMin(0),
37  fiSaveXMax(0),
38  fiSaveYMin(0),
39  fiSaveYMax(0)
40 {
41  GO4TRACE((15,"TGo4WinCond::TGo4WinCond()",__LINE__, __FILE__));
42 }
43 // -----------------------------------------------
44 TGo4WinCond::TGo4WinCond(const char *name, const char *title) :
45  TGo4Condition(name,title),
46  fLow1(0),
47  fUp1(0),
48  fLow2(0),
49  fUp2(0),
50  fiSaveXMin(0),
51  fiSaveXMax(0),
52  fiSaveYMin(0),
53  fiSaveYMax(0)
54 {
55 GO4TRACE((15,"TGo4WinCond::TGo4WinCond(name,title)",__LINE__, __FILE__));
56 }
57 
58 // -----------------------------------------------
60 {
61 GO4TRACE((15,"TGo4WinCond::~TGo4WinCond()",__LINE__, __FILE__));
62 }
63 
64 // -----------------------------------------------
65 Bool_t TGo4WinCond::Test(Double_t v1, Double_t v2)
66 {
67  IncCounts();
68  if (!IsEnabled()) {
69  if (FixedResult())
70  IncTrueCounts();
71  return FixedResult();
72  }
73  if (v1 < fLow1)
74  return IsFalse();
75  if (v1 >= fUp1)
76  return IsFalse();
77  if (v2 < fLow2)
78  return IsFalse();
79  if (v2 >= fUp2)
80  return IsFalse();
81  IncTrueCounts();
82  return IsTrue();
83 }
84 
85 // -----------------------------------------------
86 Bool_t TGo4WinCond::Test(Double_t v1)
87 {
88  IncCounts();
89  if (!IsEnabled()) {
90  if (FixedResult())
91  IncTrueCounts();
92  return FixedResult();
93  }
94  if (v1 < fLow1)
95  return IsFalse();
96  if (v1 >= fUp1)
97  return IsFalse();
98  IncTrueCounts();
99  return IsTrue();
100 }
101 // -----------------------------------------------
102 void TGo4WinCond::SetValues(Double_t low1, Double_t up1, Double_t low2, Double_t up2)
103 {
104  fLow1 = low1;
105  fUp1 = up1;
106  fLow2 = low2;
107  fUp2 = up2;
108  SetDimension(2);
109 }
110 
111 // -----------------------------------------------
112 void TGo4WinCond::SetValues(Double_t low1, Double_t up1)
113 {
114  fLow1 = low1;
115  fUp1 = up1;
116  SetDimension(1);
117 }
118 
119 // -----------------------------------------------
120 void TGo4WinCond::GetValues(Int_t &dim, Double_t &x1, Double_t &y1, Double_t &x2, Double_t &y2)
121 {
122  x1 = fLow1;
123  y1 = fUp1;
124  x2 = fLow2;
125  y2 = fUp2;
126  dim = GetDimension();
127 }
128 
129 // -----------------------------------------------
130 void TGo4WinCond::PrintCondition(Bool_t limits)
131 {
133  if (limits) {
134  if (GetDimension() == 1)
135  std::cout << TString::Format("[%8.2f,%8.2f]", fLow1, fUp1) << std::endl;
136  else
137  std::cout << TString::Format("[%8.2f,%8.2f][%8.2f,%8.2f]", fLow1, fUp1, fLow2, fUp2) << std::endl;
138  }
139 }
140 
141 // -----------------------------------------------
142 Double_t TGo4WinCond::GetIntegral(TH1 *histo, Option_t *opt)
143 {
144  if(!histo) return 0;
145  SetHistogramRanges(histo);
146  Double_t result = histo->Integral(opt);
147  RestoreHistogramRanges(histo);
148  return result;
149 }
150 
151 // -----------------------------------------------
152 Double_t TGo4WinCond::GetMean(TH1 *histo, Int_t axis)
153 {
154  if(!histo) return 0;
155  SetHistogramRanges(histo);
156  Double_t result = histo->GetMean(axis);
157  RestoreHistogramRanges(histo);
158  return result;
159 }
160 
161 // -----------------------------------------------
162 Double_t TGo4WinCond::GetRMS(TH1 *histo, Int_t axis)
163 {
164  if(!histo) return 0;
165  SetHistogramRanges(histo);
166  Double_t result = histo->GetRMS(axis);
167  RestoreHistogramRanges(histo);
168  return result;
169 }
170 
171 // -----------------------------------------------
172 Double_t TGo4WinCond::GetSkewness(TH1 *histo, Int_t axis)
173 {
174  if(!histo) return 0;
175  SetHistogramRanges(histo);
176  Double_t result = histo->GetSkewness(axis);
177  RestoreHistogramRanges(histo);
178  return result;
179 }
180 
181 // -----------------------------------------------
182 Double_t TGo4WinCond::GetCurtosis(TH1 *histo, Int_t axis)
183 {
184  if(!histo) return 0;
185  SetHistogramRanges(histo);
186  Double_t result = histo->GetKurtosis(axis);
187  RestoreHistogramRanges(histo);
188  return result;
189 }
190 
191 // -----------------------------------------------
192 Double_t TGo4WinCond::GetXMax(TH1 *histo)
193 {
194  if(!histo) return 0;
195  Double_t result = 0.;
196  SetHistogramRanges(histo);
197  TAxis *xax = histo->GetXaxis();
198  Int_t maxbin = histo->GetMaximumBin();
199  if (histo->GetDimension() == 1) {
200  result = xax->GetBinCenter(maxbin);
201  } else if (histo->GetDimension() == 2) {
202  Int_t xmaxbin = maxbin % (histo->GetNbinsX() + 2);
203  result = xax->GetBinCenter(xmaxbin);
204  } else {
205  result = 0; // no support for 3d histos at the moment!
206  }
207  RestoreHistogramRanges(histo);
208  return result;
209 }
210 
211 // -----------------------------------------------
212 Double_t TGo4WinCond::GetYMax(TH1 *histo)
213 {
214  if(!histo) return 0;
215  Double_t result = 0;
216  SetHistogramRanges(histo);
217  if (histo->GetDimension() == 1) {
218  result = histo->GetMaximum();
219  } else if (histo->GetDimension() == 2) {
220  TAxis *yax = histo->GetYaxis();
221  Int_t maxbin = histo->GetMaximumBin();
222  Int_t maxybin = maxbin / (histo->GetNbinsX() + 2);
223  result = yax->GetBinCenter(maxybin);
224  } else {
225  result = 0; // no support for 3d histos at the moment!
226  }
227  RestoreHistogramRanges(histo);
228  return result;
229 }
230 
231 // -----------------------------------------------
232 Double_t TGo4WinCond::GetCMax(TH1 *histo)
233 {
234  if(!histo) return 0;
235  SetHistogramRanges(histo);
236  Double_t result = histo->GetMaximum();
237  RestoreHistogramRanges(histo);
238  return result;
239 }
240 
241 // -----------------------------------------------
243 {
244  if(!histo) return;
245  Double_t xmin = fLow1;
246  Double_t xmax = fUp1;
247  Double_t ymin = fLow2;
248  Double_t ymax = fUp2;
249  TAxis *xax = histo->GetXaxis();
250  fiSaveXMin = xax->GetFirst();
251  fiSaveXMax = xax->GetLast();
252  Int_t xminbin = xax->FindBin(xmin);
253  Int_t xmaxbin = xax->FindBin(xmax);
254  Int_t yminbin = 0;
255  Int_t ymaxbin = 0;
256  TAxis *yax = histo->GetYaxis();
257  if (yax && histo->GetDimension() > 1) {
258  fiSaveYMin = yax->GetFirst();
259  fiSaveYMax = yax->GetLast();
260  yminbin = yax->FindBin(ymin);
261  ymaxbin = yax->FindBin(ymax);
262  }
263  // set histo range to condition limits
264  xax->SetRange(xminbin, xmaxbin);
265  if (yax && histo->GetDimension() > 1)
266  yax->SetRange(yminbin, ymaxbin);
267 }
268 
269 // -----------------------------------------------
271 {
272  if(!histo) return;
273  TAxis *xax=histo->GetXaxis();
274  TAxis *yax=histo->GetYaxis();
275  xax->SetRange(fiSaveXMin,fiSaveXMax);
276  if(yax&& histo->GetDimension()>1)
277  yax->SetRange(fiSaveYMin,fiSaveYMax);
278 }
279 
280 // -----------------------------------------------
281 Bool_t TGo4WinCond::UpdateFrom(TGo4Condition *cond, Bool_t counts)
282 {
283  if(!TGo4Condition::UpdateFrom(cond,counts)) return kFALSE;
284  if(!cond->InheritsFrom(TGo4WinCond::Class())) {
285  std::cout << "Cannot update " << GetName() << " from " << cond->ClassName() << std::endl;
286  return kFALSE;
287  }
288  Int_t dimension = 0;
289  ((TGo4WinCond*)cond)->GetValues(dimension,fLow1,fUp1,fLow2,fUp2); // get limits from source
290  SetDimension(dimension);
291  return kTRUE;
292 }
293 
294 // -----------------------------------------------
295 
296 Bool_t TGo4WinCond::UpdateFromUrl(const char *rest_url_opt)
297 {
298  if (!TGo4Condition::UpdateFromUrl(rest_url_opt))
299  return kFALSE;
300  TGo4Log::Message(1, "TGo4WinCond::UpdateFromUrl - condition %s: with url:%s", GetName(), rest_url_opt);
301  TString message;
307  message.Form("Set Window condition %s:", GetName());
308  Int_t dim = GetDimension();
309  switch (dim) {
310  case 1:
311  SetValues(xmin, xmax);
312  message.Append(TString::Format(", set limits to (%f, %f)", xmin, xmax));
313  break;
314  case 2:
315  SetValues(xmin, xmax, ymin, ymax);
316  message.Append(TString::Format(", set limits to (%f, %f) (%f, %f)", xmin, xmax, ymin, ymax));
317  break;
318  default:
319  message.Append(TString::Format(" !wrong condition dimension %d, NEVER COME HERE", dim));
320  break;
321  };
322  } else {
323  std::cout << "DEBUG- no limits to change received" << std::endl;
324  }
325  if (message.Length() > 0)
326  TGo4Log::Message(1, "%s", message.Data());
327  return kTRUE;
328 }
329 
331 {
332  // delete old painter, replace by the new one
333  // overwritten method in subclass may check if painter is correct type
334  if(!painter) return;
335  if(painter->InheritsFrom(TGo4WinCondPainter::Class())) {
336  if(fxPainter) delete fxPainter;
337  fxPainter = painter;
338  fxPainter->SetCondition(this);
339  } else {
340  TGo4Log::Warn("Could not set painter of class %s for TGo4WinCond %s",
341  painter->ClassName(),GetName());
342  }
343 }
344 
346 {
347  auto painter = new TGo4WinCondPainter(GetName());
348  painter->SetCondition(this);
349  return painter;
350 }
351 
353 {
354  Int_t size = sizeof(*this);
355  if (GetName()) size += strlen(GetName());
356  if (GetTitle()) size += strlen(GetTitle());
357  return size;
358 }
359 
360 void TGo4WinCond::SavePrimitive(std::ostream &out, Option_t *opt)
361 {
362  static int cnt = 0;
363 
364  TString varname = MakeScript(out, TString::Format("wincond%d", cnt++), opt);
365 
366  Int_t dim;
367  Double_t xl,xu,yl,yu;
368  GetValues(dim,xl,xu,yl,yu);
369 
370  if(dim==1) out << TString::Format(" %s->SetValues(%f, %f);", varname.Data(), xl, xu) << std::endl;
371  else out << TString::Format(" %s->SetValues(%f, %f, %f, %f);", varname.Data(), xl, xu, yl, yu) << std::endl;
372 }
373 
374 
static TString fgxURL_YLOW
Definition: TGo4WinCond.h:112
void SavePrimitive(std::ostream &fs, Option_t *opt="") override
Double_t GetIntegral(TH1 *histo, Option_t *opt="") override
Bool_t UrlOptionHasKey(const char *key)
Double_t GetCMax(TH1 *histo) override
Double_t GetMean(TH1 *histo, Int_t axis=1) override
Int_t fiSaveYMax
Definition: TGo4WinCond.h:140
virtual Bool_t UpdateFrom(TGo4Condition *cond, Bool_t counts)
Int_t fiSaveXMin
Definition: TGo4WinCond.h:131
Double_t fUp2
Definition: TGo4WinCond.h:128
Int_t GetMemorySize() const override
Bool_t UpdateFrom(TGo4Condition *cond, Bool_t counts) override
virtual void PrintCondition(Bool_t full=kTRUE)
Double_t GetYMax(TH1 *histo) override
virtual void SetValues()
Definition: TGo4Condition.h:93
Int_t fiSaveYMin
Definition: TGo4WinCond.h:137
TGo4ConditionPainter * CreatePainter() override
const char * MakeScript(std::ostream &out, const char *varname, Option_t *opt="", const char *arrextraargs=nullptr)
Double_t GetYLow() const override
Definition: TGo4WinCond.h:59
static TString fgxURL_XUP
Definition: TGo4WinCond.h:110
void GetValues(Int_t &dim, Double_t &x1, Double_t &y1, Double_t &x2, Double_t &y2) override
void SetHistogramRanges(TH1 *histo)
Double_t GetXUp() const override
Definition: TGo4WinCond.h:58
Int_t fiSaveXMax
Definition: TGo4WinCond.h:134
static const char * Message(Int_t prio, const char *text,...) GO4_PRINTF2_ARGS
Definition: TGo4Log.cxx:206
Double_t fLow1
Definition: TGo4WinCond.h:119
Double_t fLow2
Definition: TGo4WinCond.h:125
virtual Bool_t UpdateFromUrl(const char *rest_url_opt)
Bool_t IsFalse() const
static TString fgxURL_XLOW
Definition: TGo4WinCond.h:108
Bool_t UpdateFromUrl(const char *rest_url_opt) override
void PrintCondition(Bool_t full=kTRUE) override
Double_t GetSkewness(TH1 *histo, Int_t axis=1) override
Double_t GetYUp() const override
Definition: TGo4WinCond.h:60
static TString fgxURL_YUP
Definition: TGo4WinCond.h:114
Double_t fUp1
Definition: TGo4WinCond.h:122
Double_t GetRMS(TH1 *histo, Int_t axis=1) override
void SetPainter(TGo4ConditionPainter *painter) override
virtual void SetCondition(TGo4Condition *con)
virtual Bool_t Test()
#define GO4TRACE(X)
Definition: TGo4Log.h:25
TGo4ConditionPainter * fxPainter
Bool_t IsEnabled() const
Definition: TGo4Condition.h:82
virtual void GetValues(Int_t &dim, Double_t &x1, Double_t &y1, Double_t &x2, Double_t &y2)
Double_t GetXLow() const override
Definition: TGo4WinCond.h:57
Bool_t FixedResult() const
Int_t GetDimension() const
void RestoreHistogramRanges(TH1 *histo)
static void Warn(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:307
Double_t GetCurtosis(TH1 *histo, Int_t axis=1) override
virtual ~TGo4WinCond()
Definition: TGo4WinCond.cxx:59
Double_t GetUrlOptionAsDouble(const char *key, Double_t def_value)
void SetDimension(Int_t d)
Bool_t IsTrue() const
Double_t GetXMax(TH1 *histo) override