HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
HGCSSGeometryConversion.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 #include <vector>
5 #include <cmath>
6 #include "TH2Poly.h"
7 #include "TMath.h"
8 
9 #include "HGCal/TBStandaloneSimulator/interface/HGCSSGeometryConversion.hh"
10 
11 
12 HGCSSGeometryConversion::HGCSSGeometryConversion(const int model,
13  const double width,
14  const double cellsize)
15  : model_(model),
16  width_(width),
17  cellsize_(cellsize)
18 {
19 }
20 
21 HGCSSGeometryConversion::~HGCSSGeometryConversion()
22 {
23  hexaGeom.clear();
24  squareGeom.clear();
25 }
26 
27 void HGCSSGeometryConversion::initialiseSquareMap(const double xymin,
28  const double side)
29 {
30  initialiseSquareMap(squareMap(), xymin, side, true);
31  fillXY(squareMap(), squareGeom);
32 }
33 
34 void HGCSSGeometryConversion::initialiseSquareMap(TH2Poly *map,
35  const double xymin,
36  const double side,
37  bool print)
38 {
39  unsigned nx = static_cast<unsigned>(xymin * 2. / side);
40  unsigned ny = nx;
41  unsigned i, j;
42  Double_t x1, y1, x2, y2;
43  Double_t dx = side, dy = side;
44  x1 = -1.*xymin;
45  x2 = x1 + dx;
46 
47  for (i = 0; i < nx; i++) {
48  y1 = -1.*xymin;
49  y2 = y1 + dy;
50  for (j = 0; j < ny; j++) {
51  map->AddBin(x1, y1, x2, y2);
52  y1 = y2;
53  y2 = y1 + dy;
54  }
55  x1 = x2;
56  x2 = x1 + dx;
57  }
58 
59  if (print) {
60  std::cout << " -- Initialising squareMap with parameters: " << std::endl
61  << " ---- xymin = " << -1.*xymin << ", side = " << side
62  << ", nx = " << nx << ", ny=" << ny
63  << std::endl;
64  }
65 
66 }
67 
68 void HGCSSGeometryConversion::initialiseHoneyComb(const double width,
69  const double side)
70 {
71  initialiseHoneyComb(hexagonMap(), width, side, true);
72  fillXY(hexagonMap(), hexaGeom);
73 }
74 
75 void HGCSSGeometryConversion::initialiseHoneyComb(TH2Poly *map,
76  const double width,
77  const double side,
78  bool print)
79 {
80  // Center a cell at (x,y)=(0,0) and ensure
81  // coverage up to/past width/2 in all 4 directions,
82  // assuming each cell is lying on a side.
83 
84  unsigned ncellwide = 11;
85  unsigned ny = ncellwide + 1;
86  unsigned nx = ncellwide + 4;
87  double xstart = -((double)ncellwide + 0.5) * side;
88  double ystart = -((double)ncellwide + 1) * side * sqrt(3) / 2;
89  if (print) {
90  std::cout << " -- Initialising HoneyComb with parameters: " << std::endl
91  << " ---- (xstart,ystart) = (" << xstart << "," << ystart << ")"
92  << ", side = " << side << ", nx = " << nx << ", ny=" << ny << std::endl;
93  }
94  myHoneycomb(map, xstart, ystart, side, ny, nx);
95 }
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Copied and modified from TH2Poly.cxx
99 /// Bins the histogram using a honeycomb structure
100 /// 90 degree rotation, side up instead of corner up
101 
102 void HGCSSGeometryConversion::myHoneycomb(TH2Poly* map,
103  Double_t xstart, Double_t ystart,
104  Double_t a, // side length
105  Int_t k, // # hexagons in a column
106  Int_t s) // # columns
107 {
108  // Add the bins
109  Double_t numberOfHexagonsInAColumn;
110  Double_t x[6], y[6];
111  Double_t xloop, yloop, ytemp;
112  xloop = xstart;
113  yloop = ystart + a * TMath::Sqrt(3) / 2.0;
114  for (int sCounter = 0; sCounter < s; sCounter++) {
115 
116  ytemp = yloop; // Resets the temp variable
117 
118  // Determine the number of hexagons in that column
119  if(sCounter % 2 == 0) {
120  numberOfHexagonsInAColumn = k;
121  } else {
122  numberOfHexagonsInAColumn = k - 1;
123  }
124 
125  for (int kCounter = 0; kCounter < numberOfHexagonsInAColumn; kCounter++) {
126 
127  // Go around the hexagon
128  x[0] = xloop;
129  y[0] = ytemp;
130  x[1] = x[0] + a / 2.0;
131  y[1] = y[0] + a * TMath::Sqrt(3) / 2.0;
132  x[2] = x[1] + a;
133  y[2] = y[1];
134  x[3] = x[2] + a / 2.0;
135  y[3] = y[1] - a * TMath::Sqrt(3) / 2.0;;
136  x[4] = x[2];
137  y[4] = y[3] - a * TMath::Sqrt(3) / 2.0;;
138  x[5] = x[1];
139  y[5] = y[4];
140 
141  map->AddBin(6, x, y);
142 
143  // Go up
144  ytemp += a * TMath::Sqrt(3);
145  }
146 
147  // Increment the starting position
148  if (sCounter % 2 == 0) yloop += a * TMath::Sqrt(3) / 2.0;
149  else yloop -= a * TMath::Sqrt(3) / 2.0;
150  xloop += 1.5 * a;
151  }
152 }
153 
154 
155 void HGCSSGeometryConversion::fillXY(TH2Poly* hist,
156  std::map<int, std::pair<double, double> >&
157  geom)
158 {
159  TIter next(hist->GetBins());
160  TObject *obj = 0;
161  TH2PolyBin *polyBin = 0;
162  geom.clear();
163 
164  while ((obj = next())) {
165  polyBin = (TH2PolyBin*)obj;
166  int id = polyBin->GetBinNumber();
167  std::pair<double, double> xy = std::pair<double, double>((polyBin->GetXMax() + polyBin->GetXMin()) / 2., (polyBin->GetYMax() + polyBin->GetYMin()) / 2.);
168  geom.insert(std::pair<unsigned, std::pair<double, double> >(id, xy));
169  }
170  std::cout << " -- Check geomMap: size = " << geom.size() << std::endl;
171 }
172 
173 
174 
175 
176 
177