CMS 3D CMS Logo

LayerEncoding.cc
Go to the documentation of this file.
3 
4 #include <vector>
5 #include <set>
6 #include <algorithm>
7 #include <cmath>
8 #include <sstream>
9 #include <fstream>
10 
11 using namespace std;
12 using namespace tt;
13 
14 namespace trackerTFP {
15 
16  LayerEncoding::LayerEncoding(const DataFormats* dataFormats)
17  : setup_(dataFormats->setup()),
18  dataFormats_(dataFormats),
19  zT_(&dataFormats->format(Variable::zT, Process::zht)),
20  cot_(&dataFormats->format(Variable::cot, Process::zht)),
21  layerEncoding_(setup_->numSectorsEta(),
22  vector<vector<vector<int>>>(pow(2, zT_->width()), vector<vector<int>>(pow(2, cot_->width())))),
23  maybeLayer_(setup_->numSectorsEta(),
24  vector<vector<vector<int>>>(pow(2, zT_->width()), vector<vector<int>>(pow(2, cot_->width())))) {
25  // number of boundaries of fiducial area in r-z plane for a given set of rough r-z track parameter
26  static constexpr int boundaries = 2;
27  // find unique sensor mouldes in r-z
28  // allowed distance in r and z in cm between modules to consider them not unique
29  static constexpr double delta = 1.e-3;
30  vector<const SensorModule*> sensorModules;
31  sensorModules.reserve(setup_->sensorModules().size());
32  for (const SensorModule& sm : setup_->sensorModules())
33  sensorModules.push_back(&sm);
34  auto smallerR = [](const SensorModule* lhs, const SensorModule* rhs) { return lhs->r() < rhs->r(); };
35  auto smallerZ = [](const SensorModule* lhs, const SensorModule* rhs) { return lhs->z() < rhs->z(); };
36  auto equalRZ = [](const SensorModule* lhs, const SensorModule* rhs) {
37  return abs(lhs->r() - rhs->r()) < delta && abs(lhs->z() - rhs->z()) < delta;
38  };
39  stable_sort(sensorModules.begin(), sensorModules.end(), smallerR);
40  stable_sort(sensorModules.begin(), sensorModules.end(), smallerZ);
41  sensorModules.erase(unique(sensorModules.begin(), sensorModules.end(), equalRZ), sensorModules.end());
42  // find set of moudles for each set of rough r-z track parameter
43  // loop over eta sectors
44  for (int binEta = 0; binEta < setup_->numSectorsEta(); binEta++) {
45  // cotTheta of eta sector centre
46  const double sectorCot = (sinh(setup_->boundarieEta(binEta + 1)) + sinh(setup_->boundarieEta(binEta))) / 2.;
47  // z at radius choenRofZ of eta sector centre
48  const double sectorZT = setup_->chosenRofZ() * sectorCot;
49  // loop over bins in zT
50  for (int binZT = 0; binZT < pow(2, zT_->width()); binZT++) {
51  // z at radius chosenRofZ wrt zT of sectorZT of this bin centre
52  const double zT = zT_->floating(zT_->toSigned(binZT));
53  // z at radius chosenRofZ wrt zT of sectorZT of this bin boundaries
54  const vector<double> zTs = {sectorZT + zT - zT_->base() / 2., sectorZT + zT + zT_->base() / 2.};
55  // loop over bins in cotTheta
56  for (int binCot = 0; binCot < pow(2, cot_->width()); binCot++) {
57  // cotTheta wrt sectorCot of this bin centre
58  const double cot = cot_->floating(cot_->toSigned(binCot));
59  // layer ids crossed by left and right rough r-z parameter shape boundaries
60  vector<set<int>> layers(boundaries);
61  // cotTheta wrt sectorCot of this bin boundaries
62  const vector<double> cots = {sectorCot + cot - cot_->base() / 2., sectorCot + cot + cot_->base() / 2.};
63  // loop over all unique modules
64  for (const SensorModule* sm : sensorModules) {
65  // check if module is crossed by left and right rough r-z parameter shape boundaries
66  for (int i = 0; i < boundaries; i++) {
67  const int j = boundaries - i - 1;
68  const double zTi = zTs[sm->r() > setup_->chosenRofZ() ? i : j];
69  const double coti = cots[sm->r() > setup_->chosenRofZ() ? j : i];
70  // distance between module and boundary in moudle tilt angle direction
71  const double d =
72  (zTi - sm->z() + (sm->r() - setup_->chosenRofZ()) * coti) / (sm->cosTilt() - sm->sinTilt() * coti);
73  // compare distance with module size and add module layer id to layers if module is crossed
74  if (abs(d) < sm->numColumns() * sm->pitchCol() / 2.)
75  layers[i].insert(sm->layerId());
76  }
77  }
78  // mayber layers are given by layer ids crossed by only one booundary
79  set<int> maybeLayer;
80  set_symmetric_difference(layers[0].begin(),
81  layers[0].end(),
82  layers[1].begin(),
83  layers[1].end(),
84  inserter(maybeLayer, maybeLayer.end()));
85  // layerEncoding is given by sorted layer ids crossed by any booundary
86  set<int> layerEncoding;
87  set_union(layers[0].begin(),
88  layers[0].end(),
89  layers[1].begin(),
90  layers[1].end(),
91  inserter(layerEncoding, layerEncoding.end()));
92  vector<int>& le = layerEncoding_[binEta][binZT][binCot];
93  le = vector<int>(layerEncoding.begin(), layerEncoding.end());
94  vector<int>& ml = maybeLayer_[binEta][binZT][binCot];
95  ml.reserve(maybeLayer.size());
96  for (int m : maybeLayer) {
97  int layer = distance(le.begin(), find(le.begin(), le.end(), m));
98  if (layer >= setup_->numLayers())
99  layer = setup_->numLayers() - 1;
100  ml.push_back(layer);
101  }
102  }
103  }
104  }
105  const bool print = false;
106  if (!print)
107  return;
108  static constexpr int widthLayer = 3;
109  static constexpr auto layerIds = {1, 2, 3, 4, 5, 6, 11, 12, 13, 14, 15};
110  stringstream ss;
111  for (int layer : layerIds) {
112  auto encode = [layer, this](const vector<int>& layers, int& l) {
113  const auto it = find(layers.begin(), layers.end(), layer);
114  if (it == layers.end())
115  return false;
116  l = distance(layers.begin(), it);
117  if (l >= setup_->numLayers())
118  l = setup_->numLayers() - 1;
119  return true;
120  };
121  for (int binEta = 0; binEta < setup_->numSectorsEta(); binEta++) {
122  for (int binZT = 0; binZT < pow(2, zT_->width()); binZT++) {
123  for (int binCot = 0; binCot < pow(2, cot_->width()); binCot++) {
124  const int zT =
125  binZT < pow(2, zT_->width() - 1) ? binZT + pow(2, zT_->width() - 1) : binZT - pow(2, zT_->width() - 1);
126  const int cot = binCot < pow(2, cot_->width() - 1) ? binCot + pow(2, cot_->width() - 1)
127  : binCot - pow(2, cot_->width() - 1);
128  const vector<int>& layers = layerEncoding_[binEta][zT][cot];
129  const vector<int>& maybes = maybeLayer_[binEta][zT][cot];
130  int layerKF(-1);
131  if (encode(layers, layerKF))
132  ss << "1" << TTBV(layerKF, widthLayer) << (encode(maybes, layerKF) ? "1" : "0");
133  else
134  ss << "00000";
135  ss << endl;
136  }
137  }
138  }
139  }
140  fstream file;
141  file.open("layerEncoding.txt", ios::out);
142  file << ss.rdbuf();
143  file.close();
144  }
145 
146  // encoded layer id for given eta sector, bin in zT, bin in cotThea and decoed layer id, returns -1 if layer incositent with track
147  const int LayerEncoding::layerIdKF(int binEta, int binZT, int binCot, int layerId) const {
148  const vector<int>& layers = layerEncoding_[binEta][binZT][binCot];
149  const auto it = find(layers.begin(), layers.end(), layerId);
150  if (it == layers.end())
151  return -1;
152  int layer = distance(layers.begin(), it);
153  if (layer >= setup_->numLayers())
154  layer = setup_->numLayers() - 1;
155  return layer;
156  }
157 
158  // pattern of maybe layers for given eta sector, bin in zT and bin in cotThea
159  TTBV LayerEncoding::maybePattern(int binEta, int binZT, int binCot) const {
160  TTBV ttBV(0, setup_->numLayers());
161  const vector<int>& layers = layerEncoding_[binEta][binZT][binCot];
162  const vector<int>& maybes = maybeLayer_[binEta][binZT][binCot];
163  for (int m : maybes)
164  ttBV.set(distance(layers.begin(), find(layers.begin(), layers.end(), m)));
165  return ttBV;
166  }
167 
168 } // namespace trackerTFP
const std::vector< int > & maybeLayer(int binEta, int binZT, int binCot) const
Definition: LayerEncoding.h:28
double base() const
Definition: DataFormats.h:117
double chosenRofZ() const
Definition: Setup.h:413
Bit vector used by Track Trigger emulators. Mainly used to convert integers into arbitrary (within ma...
Definition: TTBV.h:20
double boundarieEta(int eta) const
Definition: Setup.h:417
const tt::Setup * setup_
Definition: LayerEncoding.h:38
const std::vector< int > & layerEncoding(int binEta, int binZT, int binCot) const
Definition: LayerEncoding.h:24
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
TTBV maybePattern(int binEta, int binZT, int binCot) const
int numSectorsEta() const
Definition: Setup.h:411
Definition: TTTypes.h:54
const std::vector< SensorModule > & sensorModules() const
Definition: Setup.h:126
def unique(seq, keepstr=True)
Definition: tier0.py:24
const DataFormat * cot_
Definition: LayerEncoding.h:44
double floating(int i) const
Definition: DataFormats.h:94
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double r() const
Definition: SensorModule.h:45
d
Definition: ztail.py:151
int numLayers() const
Definition: Setup.h:215
int toSigned(int i) const
Definition: DataFormats.h:100
double z() const
Definition: SensorModule.h:49
def encode(args, files)
std::vector< std::vector< std::vector< std::vector< int > > > > layerEncoding_
Definition: LayerEncoding.h:46
std::vector< std::vector< std::vector< std::vector< int > > > > maybeLayer_
Definition: LayerEncoding.h:48
const int layerIdKF(int binEta, int binZT, int binCot, int layerId) const
Class to calculate and provide dataformats used by Track Trigger emulator.
Definition: DataFormats.h:216
const DataFormat * zT_
Definition: LayerEncoding.h:42
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29