CMS 3D CMS Logo

LateralityCoarsedProvider.cc
Go to the documentation of this file.
2 #include <cmath>
3 #include <memory>
4 
5 using namespace edm;
6 using namespace std;
7 using namespace cmsdt;
8 // ============================================================================
9 // Constructors and destructor
10 // ============================================================================
12  : LateralityProvider(pset, iC),
13  debug_(pset.getUntrackedParameter<bool>("debug")),
14  laterality_filename_(pset.getParameter<edm::FileInPath>("laterality_filename")) {
15  if (debug_)
16  LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider: constructor";
17 
19 }
20 
22  if (debug_)
23  LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider: destructor";
24 }
25 
26 // ============================================================================
27 // Main methods (initialise, run, finish)
28 // ============================================================================
30  if (debug_)
31  LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider::initialiase";
32 }
33 
35  const edm::EventSetup &iEventSetup,
36  MuonPathPtrs &muonpaths,
37  std::vector<lat_vector> &lateralities) {
38  if (debug_)
39  LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider: run";
40 
41  // fit per SL (need to allow for multiple outputs for a single mpath)
42  for (auto &muonpath : muonpaths) {
43  analyze(muonpath, lateralities);
44  }
45 }
46 
48  if (debug_)
49  LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider: finish";
50 };
51 
52 //------------------------------------------------------------------
53 //--- Metodos privados
54 //------------------------------------------------------------------
55 
56 void LateralityCoarsedProvider::analyze(MuonPathPtr &inMPath, std::vector<lat_vector> &lateralities) {
57  if (debug_)
58  LogDebug("LateralityCoarsedProvider") << "DTp2:analyze \t\t\t\t starts";
59 
60  auto coarsified_times = coarsify_times(inMPath);
61 
62  for (auto &lat_combination : lat_combinations) {
63  if (inMPath->missingLayer() == lat_combination.missing_layer &&
64  inMPath->cellLayout()[0] == lat_combination.cellLayout[0] &&
65  inMPath->cellLayout()[1] == lat_combination.cellLayout[1] &&
66  inMPath->cellLayout()[2] == lat_combination.cellLayout[2] &&
67  inMPath->cellLayout()[3] == lat_combination.cellLayout[3] &&
68  coarsified_times[0] == lat_combination.coarsed_times[0] &&
69  coarsified_times[1] == lat_combination.coarsed_times[1] &&
70  coarsified_times[2] == lat_combination.coarsed_times[2] &&
71  coarsified_times[3] == lat_combination.coarsed_times[3]) {
72  lateralities.push_back(lat_combination.latcombs);
73  return;
74  }
75  }
76  lateralities.push_back(LAT_VECTOR_NULL);
77  return;
78 }
79 
81  int max_time = -999;
82  // obtain the maximum time to do the coarsification
83  for (int layer = 0; layer < cmsdt::NUM_LAYERS; layer++) {
84  if (inMPath->missingLayer() == layer)
85  continue;
86  if (inMPath->primitive(layer)->tdcTimeStamp() > max_time)
87  max_time = inMPath->primitive(layer)->tdcTimeStamp();
88  }
89 
90  // do the coarsification
91  std::vector<short> coarsified_times;
92  for (int layer = 0; layer < cmsdt::NUM_LAYERS; layer++) {
93  if (inMPath->missingLayer() == layer) {
94  coarsified_times.push_back(-1);
95  continue;
96  }
97  auto coarsified_time = max_time - inMPath->primitive(layer)->tdcTimeStamp();
98  // transform into tdc counts
99  coarsified_time = (int)round(((float)TIME_TO_TDC_COUNTS / (float)LHC_CLK_FREQ) * coarsified_time);
100  // keep the LAT_MSB_BITS
101  coarsified_time = coarsified_time >> (LAT_TOTAL_BITS - LAT_MSB_BITS);
102 
103  if (inMPath->missingLayer() == -1) { // 4-hit candidates
104  if (coarsified_time <= LAT_P0_4H)
105  coarsified_times.push_back(0);
106  else if (coarsified_time <= LAT_P1_4H)
107  coarsified_times.push_back(1);
108  else if (coarsified_time <= LAT_P2_4H)
109  coarsified_times.push_back(2);
110  else
111  coarsified_times.push_back(3);
112  } else { // 3-hit candidates
113  if (coarsified_time <= LAT_P0_3H)
114  coarsified_times.push_back(0);
115  else if (coarsified_time <= LAT_P1_3H)
116  coarsified_times.push_back(1);
117  else if (coarsified_time <= LAT_P2_3H)
118  coarsified_times.push_back(2);
119  else
120  coarsified_times.push_back(3);
121  }
122  }
123  return coarsified_times;
124 }
125 
127  std::ifstream latFile(laterality_filename_.fullPath()); // Open file
128  if (latFile.fail()) {
129  throw cms::Exception("Missing Input File")
130  << "LateralityCoarsedProvider::fill_lat_combinations() - Cannot find " << laterality_filename_.fullPath();
131  return;
132  }
133 
135 
136  short line_counter = 0; // Line counter
137 
138  // Bit masks for every parameter
139  int _12bitMask = 0xFFF; // 12 bits
140  int _layoutMask = 0xE00; // 3 bits
141  int _is4HitMask = 0x100; // 1 bit
142  int _coarsedMask = 0xFF; // 8 bits
143  int _layerMask = 0xC0; // 2 bits
144 
145  while (std::getline(latFile, line)) {
146  if (line == "000000000000") {
147  line_counter++;
148  continue;
149  } //skip zeros
150 
151  if (line.size() == 12) {
152  std::vector<std::vector<short>> transformedVector = convertString(line);
153  latcomb lat0 = {
154  transformedVector[0][0], transformedVector[0][1], transformedVector[0][2], transformedVector[0][3]};
155  latcomb lat1 = {
156  transformedVector[1][0], transformedVector[1][1], transformedVector[1][2], transformedVector[1][3]};
157  latcomb lat2 = {
158  transformedVector[2][0], transformedVector[2][1], transformedVector[2][2], transformedVector[2][3]};
159 
160  //Transforming line number to binary
161  short address = line_counter & _12bitMask; // 12 bits
162 
163  short layout =
164  (address & _layoutMask) >> 9; //Doing AND and displacing 9 bits to the right to obtain 3 bits of layout
165  short is4Hit = (address & _is4HitMask) >> 8;
166  short coarsed = address & _coarsedMask;
167 
168  short bit1Layout = (layout & (1));
169  short bit2Layout = (layout & (1 << 1)) >> 1;
170  short bit3Layout = (layout & (1 << 2)) >> 2;
171 
172  //Logic implementation
173  short missingLayer = -1;
174  short layout_comb[NUM_LAYERS] = {bit3Layout, bit2Layout, bit1Layout, -1};
175  short coarsedTimes[NUM_LAYERS] = {0, 0, 0, 0};
176 
177  if (is4Hit != 1) { //3 hit case
178  missingLayer =
179  (coarsed & _layerMask) >> 6; //Missing layer is given by the two most significative bits of coarsed vector
180  coarsedTimes[missingLayer] = -1; //Missing layer set to -1
181  }
182 
183  // Filling coarsedTimes vector without the missing layer
184  if (missingLayer != -1) {
185  switch (missingLayer) {
186  case 0:
187  coarsedTimes[1] = (coarsed & 0x30) >> 4;
188  coarsedTimes[2] = (coarsed & 0x0C) >> 2;
189  coarsedTimes[3] = coarsed & 0x03;
190  lat0 = {-1, transformedVector[0][1], transformedVector[0][2], transformedVector[0][3]};
191  lat1 = {-1, transformedVector[1][1], transformedVector[1][2], transformedVector[1][3]};
192  lat2 = {-1, transformedVector[2][1], transformedVector[2][2], transformedVector[2][3]};
193  break;
194  case 1:
195  coarsedTimes[0] = (coarsed & 0x30) >> 4;
196  coarsedTimes[2] = (coarsed & 0x0C) >> 2;
197  coarsedTimes[3] = coarsed & 0x03;
198  lat0 = {transformedVector[0][0], -1, transformedVector[0][2], transformedVector[0][3]};
199  lat1 = {transformedVector[1][0], -1, transformedVector[1][2], transformedVector[1][3]};
200  lat2 = {transformedVector[2][0], -1, transformedVector[2][2], transformedVector[2][3]};
201  break;
202  case 2:
203  coarsedTimes[0] = (coarsed & 0x30) >> 4;
204  coarsedTimes[1] = (coarsed & 0x0C) >> 2;
205  coarsedTimes[3] = coarsed & 0x03;
206  lat0 = {transformedVector[0][0], transformedVector[0][1], -1, transformedVector[0][3]};
207  lat1 = {transformedVector[1][0], transformedVector[1][1], -1, transformedVector[1][3]};
208  lat2 = {transformedVector[2][0], transformedVector[2][1], -1, transformedVector[2][3]};
209  break;
210  case 3:
211  coarsedTimes[0] = (coarsed & 0x30) >> 4;
212  coarsedTimes[1] = (coarsed & 0x0C) >> 2;
213  coarsedTimes[2] = coarsed & 0x03;
214  lat0 = {transformedVector[0][0], transformedVector[0][1], transformedVector[0][2], -1};
215  lat1 = {transformedVector[1][0], transformedVector[1][1], transformedVector[1][2], -1};
216  lat2 = {transformedVector[2][0], transformedVector[2][1], transformedVector[2][2], -1};
217  break;
218 
219  default:
220  break;
221  }
222 
223  } else { //4 hit case
224  coarsedTimes[0] = (coarsed & 0xC0) >> 6;
225  coarsedTimes[1] = (coarsed & 0x30) >> 4;
226  coarsedTimes[2] = (coarsed & 0x0C) >> 2;
227  coarsedTimes[3] = coarsed & 0x03;
228  }
229 
230  lat_coarsed_combination lat_temp = {missingLayer,
231  {layout_comb[0], layout_comb[1], layout_comb[2], layout_comb[3]},
232  {coarsedTimes[0], coarsedTimes[1], coarsedTimes[2], coarsedTimes[3]},
233  {lat0, lat1, lat2}};
234  lat_combinations.push_back(lat_temp);
235 
236  } else { //size different from 12
237  std::cerr << "Error: line " << line_counter << " does not contain 12 bits." << std::endl;
238  }
239  line_counter++;
240  };
241 
242  //closing lateralities file
243  latFile.close();
244 };
245 
246 // Function to convert a 12 bit string in a a vector of 4 bit vectors
247 std::vector<std::vector<short>> LateralityCoarsedProvider::convertString(std::string chain) {
248  std::vector<std::vector<short>> result;
249 
250  for (size_t i = 0; i < chain.size(); i += 4) {
251  std::vector<short> group;
252  for (size_t j = 0; j < 4; j++) {
253  group.push_back(chain[i + j] - '0'); // Convert the character to integer
254  }
255  result.push_back(group);
256  }
257 
258  return result;
259 }
constexpr int LAT_P0_3H
Definition: constants.h:241
constexpr int LAT_P1_4H
Definition: constants.h:238
constexpr int LAT_P1_3H
Definition: constants.h:242
std::vector< MuonPathPtr > MuonPathPtrs
Definition: MuonPath.h:132
constexpr int LAT_P2_3H
Definition: constants.h:243
void run(edm::Event &iEvent, const edm::EventSetup &iEventSetup, MuonPathPtrs &inMpath, std::vector< lat_vector > &lateralities) override
std::vector< short > latcomb
constexpr int LAT_P0_4H
Definition: constants.h:237
int iEvent
Definition: GenABIO.cc:224
void analyze(MuonPathPtr &inMPath, std::vector< lat_vector > &lateralities)
constexpr int LAT_P2_4H
Definition: constants.h:239
std::vector< lat_coarsed_combination > lat_combinations
LateralityCoarsedProvider(const edm::ParameterSet &pset, edm::ConsumesCollector &iC)
constexpr int TIME_TO_TDC_COUNTS
Definition: constants.h:235
std::vector< short > coarsify_times(MuonPathPtr &inMPath)
constexpr int LHC_CLK_FREQ
Definition: constants.h:222
HLT enums.
std::shared_ptr< MuonPath > MuonPathPtr
Definition: MuonPath.h:131
constexpr int LAT_TOTAL_BITS
Definition: constants.h:233
constexpr int NUM_LAYERS
Definition: constants.h:358
const std::string & fullPath() const
Definition: FileInPath.cc:144
std::vector< std::vector< short > > convertString(std::string chain)
void initialise(const edm::EventSetup &iEventSetup) override
#define LogDebug(id)
constexpr int LAT_MSB_BITS
Definition: constants.h:234
short cellLayout[cmsdt::NUM_LAYERS]