GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4ShapedCond.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 "TGo4ShapedCond.h"
15 
16 #include "TMath.h"
17 
18 #include "TGo4Log.h"
19 
20 
21 TString TGo4ShapedCond::fgxURL_RESOLUTION ="ellinpts";
22 TString TGo4ShapedCond::fgxURL_CX ="ellicx";
23 TString TGo4ShapedCond::fgxURL_CY ="ellicy";
24 TString TGo4ShapedCond::fgxURL_A1 ="ellia1";
25 TString TGo4ShapedCond::fgxURL_A2 ="ellia2";
26 TString TGo4ShapedCond::fgxURL_TH ="ellith";
27 TString TGo4ShapedCond::fgxURL_SHAPE ="ellishape";
28 
29 // ----------------------------------------------------------
31  TGo4PolyCond(), fiResolution(GO4ELLIPSECOND_DEFAULTRESOLUTION),fdCenterX(0),fdCenterY(0), fdRadius1(0),fdRadius2(0), fdTheta(0),fiShapeType(Go4Cond_Shape_None)
32 {
33 }
34 // ----------------------------------------------------------
35 TGo4ShapedCond::TGo4ShapedCond(const char *name, const char *title) :
37 {
38 }
39 // ----------------------------------------------------------
41 {
42 }
43 
44 // ----------------------------------------------------------
45 void TGo4ShapedCond::PrintCondition(Bool_t points)
46 {
48  std::cout << "**** Geometrical shaped condition with parameters:" << std::endl;
49  std::cout << "**** Shape Type: "<<fiShapeType << "("<<GetShapeName()<<")"<<std::endl;
50  std::cout << "**** Center: ("<<fdCenterX<<","<< fdCenterY<<")" << std::endl;
51  std::cout << "**** Radius: ("<<fdRadius1<<","<< fdRadius2<<")" << std::endl;
52  std::cout << "**** Tilt: "<<fdTheta<<" deg" << std::endl;
53  std::cout << "**** Resolution: "<<fiResolution<<" points" << std::endl;
54 }
55 
56 // ----------------------------------------------------------
57 Bool_t TGo4ShapedCond::UpdateFrom(TGo4Condition *cond, Bool_t counts)
58 {
59  if (!TGo4PolyCond::UpdateFrom(cond, counts))
60  return kFALSE;
61  if (cond->InheritsFrom(TGo4ShapedCond::Class())) {
62  TGo4ShapedCond *from = (TGo4ShapedCond*) cond;
63  fdCenterX = from->fdCenterX;
64  fdCenterY = from->fdCenterY;
65  fdRadius1 = from->fdRadius1;
66  fdRadius2 = from->fdRadius2;
67  fdTheta = from->fdTheta;
68  fiResolution = from->fiResolution;
69 
70  fiShapeType = from->fiShapeType;
71  // TODO: probably later we only update ellipse parameters and render polygon points from that.
72  //PrintCondition();
73  return kTRUE;
74  }
75  std::cout << "Cannot update " << GetName() << " from " << cond->ClassName()
76  << std::endl;
77  return kFALSE;
78 }
79 
80 
81 Bool_t TGo4ShapedCond::UpdateFromUrl(const char *rest_url_opt){
82  //if(!TGo4PolyCond::UpdateFromUrl(rest_url_opt)) return kFALSE;
83  // do not always update polygon points, but first reconstruct from shape parameters!
84 
85  if(!TGo4Condition::UpdateFromUrl(rest_url_opt)) return kFALSE; // still may change condition base class flags
86 
87  TString message;
88  message.Form("TGo4ShapedCond::UpdateFromUrl - condition %s: with url:%s\n", GetName(), rest_url_opt);
89  Double_t x = 0, y = 0, a = 0, b = 0;
90  GetCenter(x, y);
91  GetRadius(a, b); // old values as defaults
92  Double_t theta = GetTheta();
97  theta = GetUrlOptionAsInt(TGo4ShapedCond::fgxURL_TH.Data(), theta);
100 
101  if (shapetype == Go4Cond_Shape_Circle) {
102  message.Append(TString::Format("Set to Circle shape with x:%f, y:%f, r:%f, points=%d", x, y, a, resolution));
103  SetCircle(x, y, a, resolution);
104  } else if (shapetype == Go4Cond_Shape_Ellipse) {
105  message.Append(TString::Format("Set to Ellipse shape with x:%f, y:%f, a:%f b:%f,theta:%f, points=%d", x, y, a, b,
106  theta, resolution));
107  SetEllipse(x, y, a, b, theta, resolution);
108  } else if (shapetype == Go4Cond_Shape_Box) {
109  message.Append(TString::Format("Set to Box shape with x:%f, y:%f, a:%f b:%f,theta:%f", x, y, a, b, theta));
110  SetBox(x, y, a, b, theta);
111  } else if (shapetype == Go4Cond_Shape_Free) {
112  message.Append("Set to Free polygon shape!");
113  SetFreeShape();
114  if (!TGo4PolyCond::UpdateFromUrl(rest_url_opt))
115  return kFALSE; // here we might get also different point coordinates...
116  }
117  TGo4Log::Message(1, "%s", message.Data());
118  return kTRUE;
119 }
120 
121 
122 void TGo4ShapedCond::SetEllipse(Double_t cx, Double_t cy, Double_t a1, Double_t a2, Double_t theta, Int_t npoints)
123 {
124  fiResolution = npoints;
125  fdCenterX = cx;
126  fdCenterY = cy;
127  fdRadius1 = a1;
128  fdRadius2 = a2;
129  fdTheta = theta;
130  SetEllipse();
131  ResetPolygon();
132 }
133 
134 void TGo4ShapedCond::SetCircle(Double_t cx, Double_t cy, Double_t r, Int_t npoints)
135 {
136  fiResolution = npoints;
137  fdCenterX = cx;
138  fdCenterY = cy;
139  fdRadius1 = r;
140  fdRadius2 = r;
141  fdTheta = 0.;
142  SetCircle();
143  ResetPolygon();
144 }
145 
146 void TGo4ShapedCond::SetBox(Double_t cx, Double_t cy, Double_t a1, Double_t a2, Double_t theta)
147 {
148  fiResolution = 4;
149  fdCenterX = cx;
150  fdCenterY = cy;
151  fdRadius1 = a1;
152  fdRadius2 = a2;
153  fdTheta = theta;
154  SetBox();
155  ResetPolygon();
156 }
157 
158 void TGo4ShapedCond::SetCenter(Double_t x, Double_t y)
159 {
160  fdCenterX = x;
161  fdCenterY = y;
162  ResetPolygon();
163 }
164 
165 void TGo4ShapedCond::SetRadius(Double_t a1, Double_t a2)
166 {
167  fdRadius1 = a1;
168  fdRadius2 = a2;
169  ResetPolygon();
170 }
171 
172 void TGo4ShapedCond::SetTheta(Double_t angle)
173 {
174  fdTheta = angle;
175  ResetPolygon();
176 }
177 
179 {
180  // stolen from ROOT TEllipse rendering code. we use it just to calculate polygon points
181  // thanks to Sven Augustin, MPI Heidelberg, for the idea and first version of the code!
182  if (fiResolution == 0)
184  Double_t *x = new Double_t[fiResolution + 1];
185  Double_t *y = new Double_t[fiResolution + 1];
186  if (IsEllipse() || IsCircle())
188  else if (IsBox())
189  DefineBox(x, y, fiResolution);
190  else {
191  delete[] x;
192  delete[] y;
193  return;
194  }
195  SetValues(x, y, fiResolution + 1);
196 
197  delete[] x;
198  delete[] y;
199 }
200 
201 void TGo4ShapedCond::DefineEllipse(Double_t *x, Double_t *y, Int_t n)
202 {
203  Double_t dphi = TMath::TwoPi()/n;
204  Double_t ct = TMath::Cos(TMath::Pi()*fdTheta/180);
205  Double_t st = TMath::Sin(TMath::Pi()*fdTheta/180);
206  for (Int_t i = 0; i < n; i++)
207  {
208  Double_t dx = fdRadius1 * TMath::Cos((Double_t) i * dphi);
209  Double_t dy = fdRadius2 * TMath::Sin((Double_t) i * dphi);
210  x[i] = fdCenterX + dx * ct - dy * st;
211  y[i] = fdCenterY + dx * st + dy * ct;
212  }
213  x[n] = x[0];
214  y[n] = y[0];
215 }
216 
217 void TGo4ShapedCond::DefineBox(Double_t *x, Double_t *y, Int_t n)
218 {
219  Double_t ct = TMath::Cos(TMath::Pi()*fdTheta/180);
220  Double_t st = TMath::Sin(TMath::Pi()*fdTheta/180);
221  x[0] = fdCenterX + fdRadius1 * ct + fdRadius2 * st;
222  y[0] = fdCenterY + fdRadius1 * st - fdRadius2 * ct;
223  x[1] = fdCenterX + fdRadius1 * ct - fdRadius2 * st;
224  y[1] = fdCenterY + fdRadius1 * st + fdRadius2 * ct;
225  x[2] = fdCenterX - (fdRadius1 * ct + fdRadius2 * st);
226  y[2] = fdCenterY - (fdRadius1 * st - fdRadius2 * ct);
227  x[3] = fdCenterX - (fdRadius1 * ct - fdRadius2 * st);
228  y[3] = fdCenterY - (fdRadius1 * st + fdRadius2 * ct);
229  for(Int_t i=4; i< n+1; ++i)
230  {
231  x[i] = x[0];
232  y[i] = y[0];
233  }
234 }
void SetTheta(Double_t angle)
Double_t fdCenterX
static TString fgxURL_TH
void DefineEllipse(Double_t *x, Double_t *y, Int_t n)
static TString fgxURL_SHAPE
void GetCenter(Double_t &x, Double_t &y) const
Double_t fdRadius2
static TString fgxURL_A1
void PrintCondition(Bool_t points=kTRUE) override
void DefineBox(Double_t *x, Double_t *y, Int_t n)
virtual void SetValues()
Definition: TGo4Condition.h:93
void SetRadius(Double_t a1, Double_t a2)
const char * GetShapeName() const
void SetCenter(Double_t x, Double_t y)
Bool_t UpdateFrom(TGo4Condition *cond, Bool_t counts) override
Bool_t IsCircle() const
Bool_t UpdateFromUrl(const char *rest_url_opt) override
Bool_t IsBox() const
Bool_t IsEllipse() const
static const char * Message(Int_t prio, const char *text,...) GO4_PRINTF2_ARGS
Definition: TGo4Log.cxx:206
virtual Bool_t UpdateFromUrl(const char *rest_url_opt)
static TString fgxURL_CY
static TString fgxURL_CX
static TString fgxURL_A2
Double_t GetTheta() const
Int_t GetResolution() const
void GetRadius(Double_t &a1, Double_t &a2) const
Double_t fdRadius1
static TString fgxURL_RESOLUTION
Double_t fdCenterY
Go4CondShape_t fiShapeType
virtual ~TGo4ShapedCond()
Int_t GetUrlOptionAsInt(const char *key, Int_t def_value)
Bool_t UpdateFrom(TGo4Condition *cond, Bool_t counts) override
#define GO4ELLIPSECOND_DEFAULTRESOLUTION
Bool_t UpdateFromUrl(const char *rest_url_opt) override
Double_t GetUrlOptionAsDouble(const char *key, Double_t def_value)
void PrintCondition(Bool_t points=kTRUE) override