CMS 3D CMS Logo

EMTFBlockME.cc
Go to the documentation of this file.
1 // Code to unpack the "ME Data Record"
2 
4 
5 #include "EMTFCollections.h"
6 #include "EMTFUnpackerTools.h"
7 
8 // This is the "header" - no EMTFBlockME.h file is needed
9 namespace l1t {
10  namespace stage2 {
11  namespace emtf {
12 
13  class MEBlockUnpacker : public Unpacker { // "MEBlockUnpacker" inherits from "Unpacker"
14  public:
15  virtual int checkFormat(const Block& block);
16  bool unpack(const Block& block, UnpackerCollections *coll) override; // Apparently it's always good to use override in C++
17  // virtual bool packBlock(const Block& block, UnpackerCollections *coll) override;
18  };
19 
20  // class MEBlockPacker : public Packer { // "MEBlockPacker" inherits from "Packer"
21  // public:
22  // virtual bool unpack(const Block& block, UnpackerCollections *coll) override; // Apparently it's always good to use override in C++
23  // };
24 
25  }
26  }
27 }
28 
29 namespace l1t {
30  namespace stage2 {
31  namespace emtf {
32 
34 
35  auto payload = block.payload();
36  int errors = 0;
37 
38  // Check the number of 16-bit words
39  if (payload.size() != 4) { errors += 1;
40  edm::LogError("L1T|EMTF") << "Payload size in 'ME Data Record' is different than expected"; }
41 
42  // Check that each word is 16 bits
43  for (unsigned int i = 0; i < 4; i++) {
44  if (GetHexBits(payload[i], 16, 31) != 0) { errors += 1;
45  edm::LogError("L1T|EMTF") << "Payload[" << i << "] has more than 16 bits in 'ME Data Record'"; }
46  }
47 
48  uint16_t MEa = payload[0];
49  uint16_t MEb = payload[1];
50  uint16_t MEc = payload[2];
51  uint16_t MEd = payload[3];
52 
53  //Check Format
54  if (GetHexBits(MEa, 15, 15) != 1) { errors += 1;
55  edm::LogError("L1T|EMTF") << "Format identifier bits in MEa are incorrect"; }
56  if (GetHexBits(MEb, 15, 15) != 1) { errors += 1;
57  edm::LogError("L1T|EMTF") << "Format identifier bits in MEb are incorrect"; }
58  if (GetHexBits(MEc, 15, 15) != 0) { errors += 1;
59  edm::LogError("L1T|EMTF") << "Format identifier bits in MEc are incorrect"; }
60  if (GetHexBits(MEd, 15, 15) != 0) { errors += 1;
61  edm::LogError("L1T|EMTF") << "Format identifier bits in MEd are incorrect"; }
62 
63  return errors;
64 
65  }
66 
67  // Converts station, CSC_ID, sector, subsector, and neighbor from the ME output
68  std::vector<int> convert_ME_location(int _station, int _csc_ID, int _sector, bool _csc_ID_shift = false) {
69  int new_sector = _sector;
70  int new_csc_ID = _csc_ID;
71  if (_csc_ID_shift) new_csc_ID += 1; // Before FW update on 05.05.16, shift by +1 from 0,1,2... convention to 1,2,3...
72  if (_station == 0) { int arr[] = { 1, new_csc_ID, new_sector, 1, 0}; std::vector<int> vec(arr, arr+5); return vec; }
73  else if (_station == 1) { int arr[] = { 1, new_csc_ID, new_sector, 2, 0}; std::vector<int> vec(arr, arr+5); return vec; }
74  else if (_station <= 4) { int arr[] = {_station, new_csc_ID, new_sector, -1, 0}; std::vector<int> vec(arr, arr+5); return vec; }
75  else if (_station == 5) new_sector = (_sector != 1) ? _sector-1 : 6; // Indicates neighbor chamber, don't return yet
76  else { int arr[] = {_station, _csc_ID, _sector, -99, -99}; std::vector<int> vec(arr, arr+5); return vec; }
77 
78  // Mapping for chambers from neighboring sector
79  if (new_csc_ID == 1) { int arr[] = {1, 3, new_sector, 2, 1}; std::vector<int> vec(arr, arr+5); return vec; }
80  else if (new_csc_ID == 2) { int arr[] = {1, 6, new_sector, 2, 1}; std::vector<int> vec(arr, arr+5); return vec; }
81  else if (new_csc_ID == 3) { int arr[] = {1, 9, new_sector, 2, 1}; std::vector<int> vec(arr, arr+5); return vec; }
82  else if (new_csc_ID == 4) { int arr[] = {2, 3, new_sector, -1, 1}; std::vector<int> vec(arr, arr+5); return vec; }
83  else if (new_csc_ID == 5) { int arr[] = {2, 9, new_sector, -1, 1}; std::vector<int> vec(arr, arr+5); return vec; }
84  else if (new_csc_ID == 6) { int arr[] = {3, 3, new_sector, -1, 1}; std::vector<int> vec(arr, arr+5); return vec; }
85  else if (new_csc_ID == 7) { int arr[] = {3, 9, new_sector, -1, 1}; std::vector<int> vec(arr, arr+5); return vec; }
86  else if (new_csc_ID == 8) { int arr[] = {4, 3, new_sector, -1, 1}; std::vector<int> vec(arr, arr+5); return vec; }
87  else if (new_csc_ID == 9) { int arr[] = {4, 9, new_sector, -1, 1}; std::vector<int> vec(arr, arr+5); return vec; }
88  else { int arr[] = {_station, _csc_ID, _sector, -99, -99}; std::vector<int> vec(arr, arr+5); return vec; }
89  }
90 
91 
93 
94  // std::cout << "Inside EMTFBlockME.cc: unpack" << std::endl;
95 
96  // Get the payload for this block, made up of 16-bit words (0xffff)
97  // Format defined in MTF7Payload::getBlock() in src/Block.cc
98  // payload[0] = bits 0-15, payload[1] = 16-31, payload[3] = 32-47, etc.
99  auto payload = block.payload();
100 
101  // Assign payload to 16-bit words
102  uint16_t MEa = payload[0];
103  uint16_t MEb = payload[1];
104  uint16_t MEc = payload[2];
105  uint16_t MEd = payload[3];
106 
107  // Check Format of Payload
108  l1t::emtf::ME ME_;
109  for (int err = 0; err < checkFormat(block); err++) ME_.add_format_error();
110 
111  // res is a pointer to a collection of EMTFDaqOut class objects
112  // There is one EMTFDaqOut for each MTF7 (60 deg. sector) in the event
114  res = static_cast<EMTFCollections*>(coll)->getEMTFDaqOuts();
115  int iOut = res->size() - 1;
116 
117  EMTFHitCollection* res_hit;
118  res_hit = static_cast<EMTFCollections*>(coll)->getEMTFHits();
119  EMTFHit Hit_;
120 
122  res_LCT = static_cast<EMTFCollections*>(coll)->getEMTFLCTs();
123 
125  // Unpack the ME Data Record
127 
128  ME_.set_clct_pattern ( GetHexBits(MEa, 0, 3) );
129  ME_.set_quality ( GetHexBits(MEa, 4, 7) );
130  ME_.set_wire ( GetHexBits(MEa, 8, 14) );
131 
132  ME_.set_strip ( GetHexBits(MEb, 0, 7) );
133  ME_.set_csc_ID ( GetHexBits(MEb, 8, 11) );
134  ME_.set_lr ( GetHexBits(MEb, 12, 12) );
135  ME_.set_bxe ( GetHexBits(MEb, 13, 13) );
136  ME_.set_bc0 ( GetHexBits(MEb, 14, 14) );
137 
138  ME_.set_me_bxn ( GetHexBits(MEc, 0, 11) );
139  ME_.set_nit ( GetHexBits(MEc, 12, 12) );
140  ME_.set_cik ( GetHexBits(MEc, 13, 13) );
141  ME_.set_afff ( GetHexBits(MEc, 14, 14) );
142 
143  ME_.set_tbin ( GetHexBits(MEd, 0, 2) );
144  ME_.set_vp ( GetHexBits(MEd, 3, 3) );
145  ME_.set_station ( GetHexBits(MEd, 4, 6) );
146  ME_.set_af ( GetHexBits(MEd, 7, 7) );
147  ME_.set_epc ( GetHexBits(MEd, 8, 11) );
148  ME_.set_sm ( GetHexBits(MEd, 12, 12) );
149  ME_.set_se ( GetHexBits(MEd, 13, 13) );
150  ME_.set_afef ( GetHexBits(MEd, 14, 14) );
151 
152  // ME_.set_dataword ( uint64_t dataword);
153 
154  // Convert specially-encoded ME quantities
155  bool csc_ID_shift = (getAlgoVersion() <= 8348); // For FW versions <= 28.04.2016, shift by +1 from 0,1,2... convention to 1,2,3...
156  // Computed as (Year - 2000)*2^9 + Month*2^5 + Day (see Block.cc and EMTFBlockTrailers.cc)
157  std::vector<int> conv_vals = convert_ME_location( ME_.Station(), ME_.CSC_ID(), (res->at(iOut)).PtrEventHeader()->Sector(), csc_ID_shift );
158 
159  Hit_.set_station ( conv_vals.at(0) );
160  Hit_.set_csc_ID ( conv_vals.at(1) );
161  Hit_.set_sector ( conv_vals.at(2) );
162  Hit_.set_subsector ( conv_vals.at(3) );
163  Hit_.set_neighbor ( conv_vals.at(4) );
164 
165  if (Hit_.Station() < 1 || Hit_.Station() > 4) edm::LogWarning("L1T|EMTF") << "EMTF unpacked LCT station = " << Hit_.Station()
166  << ", outside proper [1, 4] range" << std::endl;
167  if (Hit_.CSC_ID() < 1 || Hit_.CSC_ID() > 9) edm::LogWarning("L1T|EMTF") << "EMTF unpacked LCT CSC ID = " << Hit_.CSC_ID()
168  << ", outside proper [1, 9] range" << std::endl;
169  if (Hit_.Sector() < 1 || Hit_.Sector() > 6) edm::LogWarning("L1T|EMTF") << "EMTF unpacked LCT sector = " << Hit_.Sector()
170  << ", outside proper [1, 6] range" << std::endl;
171 
172  // Fill the EMTFHit
173  ImportME( Hit_, ME_, (res->at(iOut)).PtrEventHeader()->Endcap(), (res->at(iOut)).PtrEventHeader()->Sector() );
174 
175 
176  // Set the stub number for this hit
177  // Each chamber can send up to 2 stubs per BX
178  ME_.set_stub_num(0);
179  Hit_.set_stub_num(0);
180  // See if matching hit is already in event record (from neighboring sector)
181  bool duplicate_hit_exists = false;
182  for (auto const & iHit : *res_hit) {
183 
184  if ( iHit.Is_CSC() == 1 &&
185  Hit_.BX() == iHit.BX() &&
186  Hit_.Endcap() == iHit.Endcap() &&
187  Hit_.Station() == iHit.Station() &&
188  Hit_.Chamber() == iHit.Chamber() &&
189  (Hit_.Ring() % 3) == (iHit.Ring() % 3) ) { // ME1/1a and ME1/1b (rings "4" and 1) are the same chamber
190 
191  if ( Hit_.Neighbor() == iHit.Neighbor() ) {
192  ME_.set_stub_num( ME_.Stub_num() + 1 );
193  Hit_.set_stub_num( Hit_.Stub_num() + 1); }
194  else if ( Hit_.Ring() == iHit.Ring() &&
195  Hit_.Strip() == iHit.Strip() &&
196  Hit_.Wire() == iHit.Wire() )
197  duplicate_hit_exists = true;
198  }
199  } // End loop: for (auto const & iHit : *res_hit)
200 
201  (res->at(iOut)).push_ME(ME_);
202  res_hit->push_back(Hit_);
203  if (not duplicate_hit_exists) // Don't write duplicate LCTs from adjacent sectors
204  res_LCT->insertDigi( Hit_.CSC_DetId(), Hit_.CreateCSCCorrelatedLCTDigi() );
205 
206  // Finished with unpacking one ME Data Record
207  return true;
208 
209  } // End bool MEBlockUnpacker::unpack
210 
211  // bool MEBlockPacker::pack(const Block& block, UnpackerCollections *coll) {
212  // std::cout << "Inside MEBlockPacker::pack" << std::endl;
213  // return true;
214  // } // End bool MEBlockPacker::pack
215 
216  } // End namespace emtf
217  } // End namespace stage2
218 } // End namespace l1t
219 
221 // DEFINE_L1T_PACKER(l1t::stage2::MEBlockPacker);
void set_bxe(int bits)
Definition: ME.h:30
void set_neighbor(int bits)
Definition: EMTFHit.h:93
void set_af(int bits)
Definition: ME.h:42
void set_bc0(int bits)
Definition: ME.h:29
const std::vector< uint32_t > & payload() const
Definition: Block.h:60
int Strip() const
Definition: EMTFHit.h:158
unsigned int getAlgoVersion()
Definition: Unpacker.h:18
void ImportME(EMTFHit &_hit, const l1t::emtf::ME _ME, const int _endcap, const int _evt_sector)
void set_stub_num(int bits)
Definition: EMTFHit.h:111
void set_station(int bits)
Definition: EMTFHit.h:82
void set_vp(int bits)
Definition: ME.h:44
Definition: Event.h:15
delete x;
Definition: CaloConfig.h:22
int CSC_ID() const
Definition: ME.h:56
void set_epc(int bits)
Definition: ME.h:41
void set_wire(int bits)
Definition: ME.h:26
std::vector< int > convert_ME_location(int _station, int _csc_ID, int _sector, bool _csc_ID_shift=false)
Definition: EMTFBlockME.cc:68
Definition: Electron.h:6
int Chamber() const
Definition: EMTFHit.h:147
int Wire() const
Definition: EMTFHit.h:157
void add_format_error()
Definition: ME.h:47
int Sector() const
Definition: EMTFHit.h:142
void set_tbin(int bits)
Definition: ME.h:45
virtual int checkFormat(const Block &block)
Definition: EMTFBlockME.cc:33
bool unpack(const Block &block, UnpackerCollections *coll) override
Definition: EMTFBlockME.cc:92
void set_se(int bits)
Definition: ME.h:39
void set_stub_num(int bits)
Definition: ME.h:46
int BX() const
Definition: EMTFHit.h:168
void set_csc_ID(int bits)
Definition: EMTFHit.h:90
void set_quality(int bits)
Definition: ME.h:27
std::vector< EMTFHit > EMTFHitCollection
Definition: EMTFHit.h:270
std::vector< EMTFDaqOut > EMTFDaqOutCollection
Definition: EMTFDaqOut.h:130
void set_csc_ID(int bits)
Definition: ME.h:32
void set_afef(int bits)
Definition: ME.h:38
CSCDetId CSC_DetId() const
Definition: EMTFHit.h:76
int Ring() const
Definition: EMTFHit.h:141
CSCCorrelatedLCTDigi CreateCSCCorrelatedLCTDigi() const
Definition: EMTFHit.cc:18
void set_afff(int bits)
Definition: ME.h:34
void set_nit(int bits)
Definition: ME.h:36
void set_strip(int bits)
Definition: ME.h:33
void set_lr(int bits)
Definition: ME.h:31
int Stub_num() const
Definition: ME.h:70
void set_sector(int bits)
Definition: EMTFHit.h:84
JetCorrectorParametersCollection coll
Definition: classes.h:10
int Station() const
Definition: EMTFHit.h:140
void set_cik(int bits)
Definition: ME.h:35
int Station() const
Definition: ME.h:67
void set_station(int bits)
Definition: ME.h:43
#define DEFINE_L1T_UNPACKER(type)
int Endcap() const
Definition: EMTFHit.h:139
Definition: errors.py:1
int Stub_num() const
Definition: EMTFHit.h:169
void set_me_bxn(int bits)
Definition: ME.h:37
uint16_t GetHexBits(uint16_t word, uint16_t lowBit, uint16_t highBit)
void set_sm(int bits)
Definition: ME.h:40
void set_clct_pattern(int bits)
Definition: ME.h:28
int Neighbor() const
Definition: EMTFHit.h:151
void set_subsector(int bits)
Definition: EMTFHit.h:87
int CSC_ID() const
Definition: EMTFHit.h:148