CMS 3D CMS Logo

BuilderUtils.cc
Go to the documentation of this file.
4 
6 
7 #include "TGeoBBox.h"
8 #include "TColor.h"
9 #include "TROOT.h"
10 #include "TEveBox.h"
11 #include "TEveScalableStraightLineSet.h"
12 #include "TEveStraightLineSet.h"
13 #include "TEveTrans.h"
14 #include "TEveGeoNode.h"
15 #include "TEveGeoShape.h"
16 
17 #include <cmath>
18 #include <ctime>
19 
20 namespace fireworks {
21  std::pair<double, double> getPhiRange(const std::vector<double>& phis, double phi) {
22  double min = 100;
23  double max = -100;
24 
25  for (std::vector<double>::const_iterator i = phis.begin(); i != phis.end(); ++i) {
26  double aphi = *i;
27  // make phi continuous around jet phi
28  if (aphi - phi > M_PI)
29  aphi -= 2 * M_PI;
30  if (phi - aphi > M_PI)
31  aphi += 2 * M_PI;
32  if (aphi > max)
33  max = aphi;
34  if (aphi < min)
35  min = aphi;
36  }
37 
38  if (min > max)
39  return std::pair<double, double>(0, 0);
40 
41  return std::pair<double, double>(min, max);
42  }
43 
44  TEveGeoShape* getShape(const char* name, TGeoBBox* shape, Color_t color) {
45  TEveGeoShape* egs = new TEveGeoShape(name);
46  TColor* c = gROOT->GetColor(color);
47  Float_t rgba[4] = {1, 0, 0, 1};
48  if (c) {
49  rgba[0] = c->GetRed();
50  rgba[1] = c->GetGreen();
51  rgba[2] = c->GetBlue();
52  }
53  egs->SetMainColorRGB(rgba[0], rgba[1], rgba[2]);
54  egs->SetShape(shape);
55  return egs;
56  }
57 
59  TEveElement* container,
60  double r_ecal,
61  double z_ecal,
62  double theta_min,
63  double theta_max,
64  double phi) {
65  TEveGeoManagerHolder gmgr(TEveGeoShape::GetGeoMangeur());
66  double z1 = r_ecal / tan(theta_min);
67  if (z1 > z_ecal)
68  z1 = z_ecal;
69  if (z1 < -z_ecal)
70  z1 = -z_ecal;
71  double z2 = r_ecal / tan(theta_max);
72  if (z2 > z_ecal)
73  z2 = z_ecal;
74  if (z2 < -z_ecal)
75  z2 = -z_ecal;
76  double r1 = z_ecal * fabs(tan(theta_min));
77  if (r1 > r_ecal)
78  r1 = r_ecal;
79  if (phi < 0)
80  r1 = -r1;
81  double r2 = z_ecal * fabs(tan(theta_max));
82  if (r2 > r_ecal)
83  r2 = r_ecal;
84  if (phi < 0)
85  r2 = -r2;
86 
87  if (fabs(r2 - r1) > 1) {
88  TGeoBBox* sc_box = new TGeoBBox(0., fabs(r2 - r1) / 2, 1);
89  TEveGeoShape* element = new TEveGeoShape("r-segment");
90  element->SetShape(sc_box);
91  TEveTrans& t = element->RefMainTrans();
92  t(1, 4) = 0;
93  t(2, 4) = (r2 + r1) / 2;
94  t(3, 4) = fabs(z2) > fabs(z1) ? z2 : z1;
95  pb->setupAddElement(element, container);
96  }
97  if (fabs(z2 - z1) > 1) {
98  TGeoBBox* sc_box = new TGeoBBox(0., 1, (z2 - z1) / 2);
99  TEveGeoShape* element = new TEveGeoShape("z-segment");
100  element->SetShape(sc_box);
101  TEveTrans& t = element->RefMainTrans();
102  t(1, 4) = 0;
103  t(2, 4) = fabs(r2) > fabs(r1) ? r2 : r1;
104  t(3, 4) = (z2 + z1) / 2;
105  pb->setupAddElement(element, container);
106  }
107  }
108 
110  time_t t(event.time().value() >> 32);
111  std::string text(asctime(gmtime(&t)));
112  size_t pos = text.find('\n');
113  if (pos != std::string::npos)
114  text = text.substr(0, pos);
115  text += " GMT";
116  return text;
117  }
118 
120  time_t t(event.time().value() >> 32);
121  struct tm* xx = localtime(&t);
122  std::string text(asctime(xx));
123  size_t pos = text.find('\n');
124  if (pos != std::string::npos)
125  text = text.substr(0, pos);
126  text += " ";
127  if (xx->tm_isdst)
128  text += tzname[1];
129  else
130  text += tzname[0];
131  return text;
132  }
133 
134  void invertBox(std::vector<float>& corners) {
135  std::swap(corners[0], corners[9]);
136  std::swap(corners[1], corners[10]);
137  std::swap(corners[2], corners[11]);
138 
139  std::swap(corners[3], corners[6]);
140  std::swap(corners[4], corners[7]);
141  std::swap(corners[5], corners[8]);
142 
143  std::swap(corners[12], corners[21]);
144  std::swap(corners[13], corners[22]);
145  std::swap(corners[14], corners[23]);
146 
147  std::swap(corners[15], corners[18]);
148  std::swap(corners[16], corners[19]);
149  std::swap(corners[17], corners[20]);
150  }
151 
152  void addBox(const std::vector<float>& corners, TEveElement* comp, FWProxyBuilderBase* pb) {
153  TEveBox* eveBox = new TEveBox("Box");
154  eveBox->SetDrawFrame(false);
155  eveBox->SetPickable(true);
156  eveBox->SetVertices(&corners[0]);
157 
158  pb->setupAddElement(eveBox, comp);
159  }
160 
161  void addCircle(double eta,
162  double phi,
163  double radius,
164  const unsigned int nLineSegments,
165  TEveElement* comp,
166  FWProxyBuilderBase* pb) {
167  TEveStraightLineSet* container = new TEveStraightLineSet;
168 
169  for (unsigned int iphi = 0; iphi < nLineSegments; ++iphi) {
170  container->AddLine(eta + radius * cos(2 * M_PI / nLineSegments * iphi),
171  phi + radius * sin(2 * M_PI / nLineSegments * iphi),
172  0.01,
173  eta + radius * cos(2 * M_PI / nLineSegments * (iphi + 1)),
174  phi + radius * sin(2 * M_PI / nLineSegments * (iphi + 1)),
175  0.01);
176  }
177  pb->setupAddElement(container, comp);
178  }
179 
180  void addDashedArrow(double phi, double size, TEveElement* comp, FWProxyBuilderBase* pb) {
181  TEveScalableStraightLineSet* marker = new TEveScalableStraightLineSet;
182  marker->SetLineWidth(1);
183  marker->SetLineStyle(2);
184  marker->AddLine(0, 0, 0, size * cos(phi), size * sin(phi), 0);
185  marker->AddLine(size * 0.9 * cos(phi + 0.03), size * 0.9 * sin(phi + 0.03), 0, size * cos(phi), size * sin(phi), 0);
186  marker->AddLine(size * 0.9 * cos(phi - 0.03), size * 0.9 * sin(phi - 0.03), 0, size * cos(phi), size * sin(phi), 0);
187  pb->setupAddElement(marker, comp);
188  }
189 
190  void addDashedLine(double phi, double theta, double size, TEveElement* comp, FWProxyBuilderBase* pb) {
191  double r(0);
192  if (theta < pb->context().caloTransAngle() || M_PI - theta < pb->context().caloTransAngle())
193  r = pb->context().caloZ2() / fabs(cos(theta));
194  else
195  r = pb->context().caloR1() / sin(theta);
196 
197  TEveStraightLineSet* marker = new TEveStraightLineSet;
198  marker->SetLineWidth(2);
199  marker->SetLineStyle(2);
200  marker->AddLine(r * cos(phi) * sin(theta),
201  r * sin(phi) * sin(theta),
202  r * cos(theta),
203  (r + size) * cos(phi) * sin(theta),
204  (r + size) * sin(phi) * sin(theta),
205  (r + size) * cos(theta));
206  pb->setupAddElement(marker, comp);
207  }
208 
209  void addDoubleLines(double phi, TEveElement* comp, FWProxyBuilderBase* pb) {
210  TEveStraightLineSet* mainLine = new TEveStraightLineSet;
211  mainLine->AddLine(-5.191, phi, 0.01, 5.191, phi, 0.01);
212  pb->setupAddElement(mainLine, comp);
213 
214  phi = phi > 0 ? phi - M_PI : phi + M_PI;
215  TEveStraightLineSet* secondLine = new TEveStraightLineSet;
216  secondLine->SetLineStyle(7);
217  secondLine->AddLine(-5.191, phi, 0.01, 5.191, phi, 0.01);
218  pb->setupAddElement(secondLine, comp);
219  }
220 
221  //______________________________________________________________________________
222  void energyScaledBox3DCorners(const float* corners, float scale, std::vector<float>& scaledCorners, bool invert) {
223  std::vector<float> centre(3, 0);
224 
225  for (unsigned int i = 0; i < 24; i += 3) {
226  centre[0] += corners[i];
227  centre[1] += corners[i + 1];
228  centre[2] += corners[i + 2];
229  }
230 
231  for (unsigned int i = 0; i < 3; ++i)
232  centre[i] *= 1.0f / 8.0f;
233 
234  // Coordinates for a scaled version of the original box
235  for (unsigned int i = 0; i < 24; i += 3) {
236  scaledCorners[i] = centre[0] + (corners[i] - centre[0]) * scale;
237  scaledCorners[i + 1] = centre[1] + (corners[i + 1] - centre[1]) * scale;
238  scaledCorners[i + 2] = centre[2] + (corners[i + 2] - centre[2]) * scale;
239  }
240 
241  if (invert)
242  invertBox(scaledCorners);
243  }
244 
245  void drawEnergyScaledBox3D(const float* corners, float scale, TEveElement* comp, FWProxyBuilderBase* pb, bool invert) {
246  std::vector<float> scaledCorners(24);
247  energyScaledBox3DCorners(corners, scale, scaledCorners, invert);
248  addBox(scaledCorners, comp, pb);
249  }
250  //______________________________________________________________________________
251 
253  const float* corners, float energy, float maxEnergy, std::vector<float>& scaledCorners, bool invert) {
254  std::vector<float> centre(3, 0);
255 
256  for (unsigned int i = 0; i < 24; i += 3) {
257  centre[0] += corners[i];
258  centre[1] += corners[i + 1];
259  centre[2] += corners[i + 2];
260  }
261 
262  for (unsigned int i = 0; i < 3; ++i)
263  centre[i] *= 1.0f / 8.0f;
264 
265  TEveVector c(centre[0], centre[1], centre[2]);
266  float scale = energy / maxEnergy * sin(c.Theta());
267 
268  // Coordinates for a scaled version of the original box
269  for (unsigned int i = 0; i < 24; i += 3) {
270  scaledCorners[i] = centre[0] + (corners[i] - centre[0]) * scale;
271  scaledCorners[i + 1] = centre[1] + (corners[i + 1] - centre[1]) * scale;
272  scaledCorners[i + 2] = centre[2] + (corners[i + 2] - centre[2]) * scale;
273  }
274 
275  if (invert)
276  invertBox(scaledCorners);
277  }
278 
280  const float* corners, float energy, float maxEnergy, TEveElement* comp, FWProxyBuilderBase* pb, bool invert) {
281  std::vector<float> scaledCorners(24);
282  etScaledBox3DCorners(corners, energy, maxEnergy, scaledCorners, invert);
283  addBox(scaledCorners, comp, pb);
284  }
285 
286  //______________________________________________________________________________
287  void energyTower3DCorners(const float* corners, float scale, std::vector<float>& scaledCorners, bool reflect) {
288  for (int i = 0; i < 24; ++i)
289  scaledCorners[i] = corners[i];
290  // Coordinates of a front face scaled
291  if (reflect) {
292  // We know, that an ES rechit geometry in -Z needs correction.
293  // The back face is actually its front face.
294  for (unsigned int i = 0; i < 12; i += 3) {
295  TEveVector diff(
296  corners[i] - corners[i + 12], corners[i + 1] - corners[i + 13], corners[i + 2] - corners[i + 14]);
297  diff.Normalize();
298  diff *= scale;
299 
300  scaledCorners[i] = corners[i] + diff.fX;
301  scaledCorners[i + 1] = corners[i + 1] + diff.fY;
302  scaledCorners[i + 2] = corners[i + 2] + diff.fZ;
303  }
304  } else {
305  for (unsigned int i = 0; i < 12; i += 3) {
306  TEveVector diff(
307  corners[i + 12] - corners[i], corners[i + 13] - corners[i + 1], corners[i + 14] - corners[i + 2]);
308  diff.Normalize();
309  diff *= scale;
310 
311  scaledCorners[i] = corners[i + 12];
312  scaledCorners[i + 1] = corners[i + 13];
313  scaledCorners[i + 2] = corners[i + 14];
314 
315  scaledCorners[i + 12] = corners[i + 12] + diff.fX;
316  scaledCorners[i + 13] = corners[i + 13] + diff.fY;
317  scaledCorners[i + 14] = corners[i + 14] + diff.fZ;
318  }
319  }
320  }
321 
322  void drawEnergyTower3D(const float* corners, float scale, TEveElement* comp, FWProxyBuilderBase* pb, bool reflect) {
323  std::vector<float> scaledCorners(24);
324  energyTower3DCorners(corners, scale, scaledCorners, reflect);
325  addBox(scaledCorners, comp, pb);
326  }
327 
328  //______________________________________________________________________________
329 
330  void etTower3DCorners(const float* corners, float scale, std::vector<float>& scaledCorners, bool reflect) {
331  for (int i = 0; i < 24; ++i)
332  scaledCorners[i] = corners[i];
333  // Coordinates of a front face scaled
334  if (reflect) {
335  // We know, that an ES rechit geometry in -Z needs correction.
336  // The back face is actually its front face.
337  for (unsigned int i = 0; i < 12; i += 3) {
338  TEveVector diff(
339  corners[i] - corners[i + 12], corners[i + 1] - corners[i + 13], corners[i + 2] - corners[i + 14]);
340  diff.Normalize();
341  diff *= (scale * sin(diff.Theta()));
342 
343  scaledCorners[i] = corners[i] + diff.fX;
344  scaledCorners[i + 1] = corners[i + 1] + diff.fY;
345  scaledCorners[i + 2] = corners[i + 2] + diff.fZ;
346  }
347  } else {
348  for (unsigned int i = 0; i < 12; i += 3) {
349  TEveVector diff(
350  corners[i + 12] - corners[i], corners[i + 13] - corners[i + 1], corners[i + 14] - corners[i + 2]);
351  diff.Normalize();
352  diff *= (scale * sin(diff.Theta()));
353 
354  scaledCorners[i] = corners[i + 12];
355  scaledCorners[i + 1] = corners[i + 13];
356  scaledCorners[i + 2] = corners[i + 14];
357 
358  scaledCorners[i + 12] = corners[i + 12] + diff.fX;
359  scaledCorners[i + 13] = corners[i + 13] + diff.fY;
360  scaledCorners[i + 14] = corners[i + 14] + diff.fZ;
361  }
362  }
363  }
364 
365  void drawEtTower3D(const float* corners, float scale, TEveElement* comp, FWProxyBuilderBase* pb, bool reflect) {
366  std::vector<float> scaledCorners(24);
367  etTower3DCorners(corners, scale, scaledCorners, reflect);
368  addBox(scaledCorners, comp, pb);
369  }
370 
371 } // namespace fireworks
size
Write out results.
std::pair< double, double > getPhiRange(const std::vector< double > &phis, double phi)
Definition: BuilderUtils.cc:21
void addDoubleLines(double phi, TEveElement *comp, FWProxyBuilderBase *pb)
void setupAddElement(TEveElement *el, TEveElement *parent, bool set_color=true) const
void etTower3DCorners(const float *corners, float scale, std::vector< float > &, bool reflect=false)
void drawEnergyScaledBox3D(const float *corners, float scale, TEveElement *, FWProxyBuilderBase *, bool invert=false)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
static float caloZ2(bool offset=true)
Definition: Context.cc:192
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
const fireworks::Context & context() const
void addDashedLine(double phi, double theta, double size, TEveElement *comp, FWProxyBuilderBase *pb)
void drawEtTower3D(const float *corners, float scale, TEveElement *, FWProxyBuilderBase *, bool reflect=false)
void invertBox(std::vector< float > &corners)
void addDashedArrow(double phi, double size, TEveElement *comp, FWProxyBuilderBase *pb)
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
static float caloR1(bool offset=true)
Definition: Context.cc:187
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
double f[11][100]
void addCircle(double eta, double phi, double radius, const unsigned int nLineSegments, TEveElement *comp, FWProxyBuilderBase *pb)
#define M_PI
std::string getTimeGMT(const edm::EventBase &event)
void energyTower3DCorners(const float *corners, float scale, std::vector< float > &, bool reflect=false)
void addRhoZEnergyProjection(FWProxyBuilderBase *, TEveElement *, double r_ecal, double z_ecal, double theta_min, double theta_max, double phi)
Definition: BuilderUtils.cc:58
void energyScaledBox3DCorners(const float *corners, float scale, std::vector< float > &, bool invert=false)
void addBox(const std::vector< float > &corners, TEveElement *, FWProxyBuilderBase *)
std::string getLocalTime(const edm::EventBase &event)
void etScaledBox3DCorners(const float *corners, float energy, float maxEnergy, std::vector< float > &scaledCorners, bool reflect=false)
void drawEnergyTower3D(const float *corners, float scale, TEveElement *, FWProxyBuilderBase *, bool reflect=false)
TEveGeoShape * getShape(const char *name, TGeoBBox *shape, Color_t color)
Definition: BuilderUtils.cc:44
Geom::Theta< T > theta() const
Definition: event.py:1
void drawEtScaledBox3D(const float *corners, float energy, float maxEnergy, TEveElement *, FWProxyBuilderBase *, bool reflect=false)