HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
createIDMap.cc
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // File: createIDMap
3 //
4 // Description: consolidate various mappings into a single text file
5 // containing
6 // sim cell id (cellid), (u, v), (x, y), position id (posid),
7 // SKIROC number (skiroc), channel number (channel). The text
8 // HGCal/TBStandaloneSimulator/data/sensor_cellid_uv_map.txt
9 // is read by HGCCellMap, which provides between various numbers.
10 //
11 // Created April 2016 Harrison B. Prosper
12 // --------------------------------------------------------------------------
13 #include <cmath>
14 #include <fstream>
15 #include <iostream>
16 #include <map>
17 
18 #include "TSystem.h"
19 #include "TCanvas.h"
20 #include "TStyle.h"
21 #include "TH2Poly.h"
22 #include "TIterator.h"
23 #include "TList.h"
24 #include "TText.h"
25 
26 #include "HGCal/CondObjects/interface/HGCalElectronicsMap.h"
27 #include "HGCal/CondObjects/interface/HGCalCondObjectTextIO.h"
28 #include "HGCal/DataFormats/interface/SKIROCParameters.h"
29 #include "HGCal/DataFormats/interface/HGCalTBElectronicsId.h"
30 #include "HGCal/DataFormats/interface/HGCalTBDetId.h"
31 #include "HGCal/Geometry/interface/HGCalTBCellParameters.h"
32 #include "HGCal/Geometry/interface/HGCalTBCellVertices.h"
33 #include "HGCal/Geometry/interface/HGCalTBTopology.h"
34 #include "HGCal/TBStandaloneSimulator/interface/HGCSSGeometryConversion.hh"
35 // --------------------------------------------------------------------------
36 using namespace std;
37 
38 namespace
39 {
40 int MODEL = 5; // TB2016 model
41 int NCELL = 11; // number of pixels from side to side in sensor
42 // side length of one pixel (cell) in mm
43 double CELL_SIDE = 10 * HGCAL_TB_CELL::FULL_CELL_SIDE;
44 // side length of sensor
45 double SENSOR_SIDE = NCELL * CELL_SIDE;
46 double WIDTH = 2 * SENSOR_SIDE; // width of sensor corner to corner
47 struct Cell {
48  int skiroc;
49  int channel;
50  int sensor_u;
51  int sensor_v;
52  int celltype;
53 };
54 };
55 // --------------------------------------------------------------------------
56 int main(int argc, char **argv)
57 {
58  // electronics id to (u, v) mapping file
59  string electronicsmapfile("map_FNAL_1234.txt");
60  if ( argc > 1 )
61  electronicsmapfile = string(argv[1]);
62 
63  cout << endl
64  << "=> Using (u, v) to (skiroc, channel) map file "
65  << endl
66  << " HGCal/CondObjects/data/"
67  << electronicsmapfile
68  << endl << endl;
69 
70  // ----------------------------------------------------------
71  // load mapping between (layer, u, v) tp (SKIROC, CHANNEL)
72  // ----------------------------------------------------------
73  char mfile[1024];
74  sprintf(mfile, "$CMSSW_BASE/src/HGCal/CondObjects/data/%s",
75  electronicsmapfile.c_str());
76  sprintf(mfile, "%s", gSystem->ExpandPathName(mfile));
77  ifstream mapin(mfile);
78  if ( ! mapin.good() ) {
79  cout << "*** cannot open file " << mfile << endl;
80  exit(0);
81  }
82  // dismiss header
83  string line;
84  getline(mapin, line);
85  int skiroc, channel, layer, sensor_u, sensor_v, u, v, celltype;
86  map<int, map<pair<int, int>, Cell> > layer2uv2eid;
87  vector<int> layers;
88  int lastlayer = -1;
89  while (mapin
90  >> skiroc >> channel >> layer
91  >> sensor_u >> sensor_v
92  >> u >> v
93  >> celltype) {
94  Cell cell;
95  cell.skiroc = skiroc;
96  cell.channel = channel;
97  cell.sensor_u = sensor_u;
98  cell.sensor_v = sensor_v;
99  cell.celltype = celltype;
100  pair<int, int> uv(u, v);
101 
102  if ( layer2uv2eid.find(layer) == layer2uv2eid.end() )
103  layer2uv2eid[layer] = map<pair<int, int>, Cell>();
104  layer2uv2eid[layer][uv] = cell;
105 
106  if ( layer != lastlayer ) layers.push_back(layer);
107  lastlayer = layer;
108  }
109 
110  // ----------------------------------------------------------
111  // create a 2-D histogram with hexagonal bins, a
112  // subset of which lie within the hexagonal boundary
113  // that defines a sensor
114  // ----------------------------------------------------------
115  HGCSSGeometryConversion geom(MODEL, WIDTH, CELL_SIDE);
116  geom.initialiseHoneyComb(WIDTH, CELL_SIDE);
117  TH2Poly* map = geom.hexagonMap();
118 
119  // ----------------------------------------------------------
120  // create a single hexagonal bin to represent sensor.
121  // we shall use this hexagon region to determine which
122  // sim cellids lie within a sensor.
123  // ----------------------------------------------------------
124  TH2Poly hsensor;
125  hsensor.SetName("hsensor");
126  hsensor.SetTitle("sensor");
127 
128  // make slightly smaller so that we identify
129  // mouse bitten cells
130  double S = 0.98 * SENSOR_SIDE;
131  double H = S * sqrt(3) / 2; // center to side distance
132  double x[7], y[7];
133  x[0] = -S / 2;
134  y[0] = -H;
135  x[1] = -S;
136  y[1] = 0;
137  x[2] = -S / 2;
138  y[2] = H;
139  x[3] = S / 2;
140  y[3] = H;
141  x[4] = S;
142  y[4] = 0;
143  x[5] = S / 2;
144  y[5] = -H;
145  x[6] = -S / 2;
146  y[6] = -H;
147  hsensor.AddBin(7, x, y);
148 
149  //get the single hexagonal bin that represents
150  //the boundary of sensor
151  TIter it(hsensor.GetBins());
152  TH2PolyBin* sensor = (TH2PolyBin*)it();
153 
154  // loop over bins in 2-D histogram, determine which
155  // ones lie within sensor, and write out the bin
156  // numbers (cellid) and the (x,y) centers of each
157  // pixel.
158  gStyle->SetPalette(1);
159  gStyle->SetOptStat("");
160 
161  TCanvas csensor("sensor_cellids", "cellid", 600, 600);
162  map->GetXaxis()->CenterTitle();
163  map->GetXaxis()->SetTitle("#font[12]{x} axis");
164  map->GetYaxis()->CenterTitle();
165  map->GetYaxis()->SetTitle("#font[12]{y} axis");
166 
167  // make a sensor with the correct size (for plotting)
168  TH2Poly hsensortrue;
169  hsensortrue.SetName("hsensortrue");
170  S = SENSOR_SIDE;
171  H = S * sqrt(3) / 2; // center to side distance
172  x[0] = -S / 2;
173  y[0] = -H;
174  x[1] = -S;
175  y[1] = 0;
176  x[2] = -S / 2;
177  y[2] = H;
178  x[3] = S / 2;
179  y[3] = H;
180  x[4] = S;
181  y[4] = 0;
182  x[5] = S / 2;
183  y[5] = -H;
184  x[6] = -S / 2;
185  y[6] = -H;
186  hsensortrue.AddBin(7, x, y);
187  hsensortrue.SetMinimum(0.0);
188  hsensortrue.SetMaximum(1.0);
189  hsensortrue.SetBinContent(1, 0.7);
190 
191  csensor.cd();
192  map->Draw();
193  hsensortrue.Draw("col same");
194  map->Draw("same");
195 
196  // (u, v) plot
197  TCanvas cuv("sensor_u_v", "u, v", 600, 600);
198  hsensortrue.SetBinContent(1, 0.0);
199  hsensortrue.Draw();
200  hsensortrue.SetBinContent(1, 0.7);
201 
202  cuv.cd();
203  map->Draw();
204  hsensortrue.Draw("col same");
205  map->Draw("same");
206 
207  // ----------------------------------------------------------
208  // loop over cells
209  // ----------------------------------------------------------
210  char record[80];
211  ofstream sout("sensor_map.txt");
212  sprintf(record,
213  "%5s %5s %5s %5s %5s %5s %5s "
214  "%5s %5s %5s "
215  "%9s %9s",
216  "layer", "posid", "cid", "s_u", "s_v", "u", "v",
217  "ski", "chan", "ctype",
218  "x", "y");
219  cout << record << endl;
220  sout << record << endl;
221 
222  TList* bins = map->GetBins();
223  TText text;
224  text.SetTextSize(0.02);
225  text.SetTextAlign(22); // centered
226 
227  HGCalTBTopology topology;
228  HGCalTBCellVertices vertices;
229 
230  // in offline code, layers start at 1
231  // hard code layer and channel count for now
232  int ncells = 128;
233  for(size_t c = 0; c < layers.size(); c++) {
234  TIter next(bins);
235  int layer = layers[c];
236 
237  // number of cells in y (either 12 or 11)
238  bool new_column = true;
239  bool odd_column = true;
240  int colnumber = 0;
241  int u_start = 1;
242  int v_start = 8;
243  int number = 1;
244  u = 0;
245  v = 0;
246  while ( TH2PolyBin* bin = (TH2PolyBin*)next() ) {
247  // We get the starting
248  // values of (u, v) per column as follows:
249  // 1. every two columns, increment u
250  // 2. every column, decrement v
251  // Thereafter:
252  // 1. decrement u
253 
254  int binnumber = bin->GetBinNumber();
255  new_column = binnumber == number;
256  if ( new_column ) {
257  // decrement v
258  v_start--;
259 
260  colnumber++;
261  if ( colnumber % 2 == 1 ) u_start++;
262 
263  // initialize (u, v) for current column
264  u = u_start + 1;
265  v = v_start;
266 
267  new_column = false;
268  if ( odd_column )
269  number += 12;
270  else
271  number += 11;
272  odd_column = !odd_column;
273  }
274 
275  // decrement u
276  u--;
277 
278  // map to skiroc number and channel
279  pair<int, int> uv(u, v);
280  Cell cell = layer2uv2eid[layer][uv];
281 
282  if ( ! topology.iu_iv_valid(layer,
283  cell.sensor_u,
284  cell.sensor_v,
285  u, v,
286  ncells) ) continue;
287 
288  pair<double, double> pos
289  = vertices.GetCellCentreCoordinates(layer,
290  cell.sensor_u,
291  cell.sensor_v,
292  u, v,
293  ncells);
294  double x = pos.first;
295  double y = pos.second;
296  x *= 10; // change to mm
297  y *= 10;
298 
299  // posid is the position identifier, which specifies whether the
300  // cell is an interior cell or a boundary cell.
301  int posid = 0;
302  if ( ! sensor->IsInside(x, y) ) {
303  // boundary cell, determine type
304  if ( v > 3 ) {
305  // either lower or upper left
306  if ( u < -3 )
307  posid = 3; // upper
308  else
309  posid = 4; // lower
310  } else if ( v < -3 ) {
311  // either lower or upper right
312  if ( u > 3 )
313  posid = 6; // lower
314  else
315  posid = 1; // upper
316  } else {
317  // either lower or upper
318  if ( u > 0 )
319  posid = 5; // lower
320  else
321  posid = 2; // upper
322  }
323  }
324 
325  csensor.cd();
326  sprintf(record, "%d", binnumber);
327  text.DrawText(x, y, record);
328  cuv.cd();
329  sprintf(record, "%d,%d", u, v);
330  text.DrawText(x, y, record);
331 
332  sprintf(record,
333  "%5d %5d %5d %5d %5d %5d %5d "
334  "%5d %5d %5d "
335  "%9.3f %9.3f",
336  layer, posid, binnumber,
337  cell.sensor_u, cell.sensor_v,
338  u, v,
339  cell.skiroc, cell.channel, cell.celltype,
340  x, y);
341  cout << record << endl;
342  sout << record << endl;
343  }
344  }
345  sout.close();
346 
347  map->SetTitle("TB2016 Standalone Simulator Cell IDs");
348  csensor.Update();
349  csensor.SaveAs(".png");
350 
351  map->SetTitle("TB2016 Sensor (u,v) Coordinates");
352  cuv.Update();
353  cuv.SaveAs(".png");
354 
355  return 0;
356 }
std::pair< double, double > GetCellCentreCoordinates(int layer, int sensor_iu, int sensor_iv, int iu, int iv, int sensorsize, bool flipX=false)
returns the coordinates of each vertex of cell in the lab frame (x,y)
bool iu_iv_valid(int layer, int sensor_iu, int sensor_iv, int iu, int iv, int sensorSize) const
int main(int argc, char *argv[])