CMS 3D CMS Logo

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