CMS 3D CMS Logo

L1EmulBias.h
Go to the documentation of this file.
1 #ifndef L1_EMUL_BIAS_H
2 #define L1_EMUL_BIAS_H
3 
4 /*\class L1EmulBias
5  *\description produces modified emulator data to mimmic hw problems
6  *\usage l1 monitoring software validation
7  *\author Nuno Leonardo (CERN)
8  *\date 07.07
9  */
10 
11 // system includes
12 #include <memory>
13 #include <string>
14 #include <iostream>
15 #include <fstream>
16 #include <iomanip>
17 #include <vector>
18 #include <algorithm>
19 
20 // common includes
27 
28 // l1 dataformats, d|e record includes
32 
33 // random generation
34 #include "CLHEP/Random/RandomEngine.h"
35 #include "CLHEP/Random/RandGaussQ.h"
36 
38 public:
39  explicit L1EmulBias(const edm::ParameterSet&);
40  ~L1EmulBias() override;
41 
42 protected:
43  //virtual void beginRun(edm::Run&, const edm::EventSetup&);
44  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
45 
46 public:
47  template <class T>
48  void ModifyCollection(std::unique_ptr<T>& data, const edm::Handle<T> emul, CLHEP::HepRandomEngine*) const;
49 
50 private:
51  int verbose_;
52  int verbose() const { return verbose_; }
56 };
57 
58 /* Notes:
59  .by default data is make identical to emul
60  .biasing is to be implemented via specialization
61  .bias may be defined for each data type, eg data word bit-shifting
62  .keep template function specialization in header file
63 */
64 
65 template <class T>
66 void L1EmulBias::ModifyCollection(std::unique_ptr<T>& data, const edm::Handle<T> emul, CLHEP::HepRandomEngine*) const {
67  data = (std::unique_ptr<T>)(const_cast<T*>(emul.product()));
68 }
69 
70 template <>
71 inline void L1EmulBias::ModifyCollection(std::unique_ptr<EcalTrigPrimDigiCollection>& data,
73  CLHEP::HepRandomEngine*) const {
75  for (col_cit it = emul->begin(); it != emul->end(); it++) {
77  int iphi = it->id().iphi();
78  bool reset = (iphi > 18 && iphi < 39); //remove few supermodules
79  for (int s = 0; s < 5; s++) {
80  uint16_t sample = it->sample(s).raw();
81  if (sample == 0)
82  continue;
83  uint16_t tmp = reset ? 0 : sample;
84  if (reset)
85  tmp = sample >> 1;
86  col.setSampleValue(s, tmp);
87  if (verbose() && sample != 0)
88  std::cout << "[emulbias] etp " << *it << "\t sample: " << s << " " << std::hex << sample << " -> "
89  << col.sample(s).raw() << std::dec << std::endl;
90  }
91  data->push_back(col);
92  }
93 }
94 
95 template <>
96 inline void L1EmulBias::ModifyCollection(std::unique_ptr<HcalTrigPrimDigiCollection>& data,
98  CLHEP::HepRandomEngine*) const {
100  for (col_cit it = emul->begin(); it != emul->end(); it++) {
102  int iphi = it->id().iphi();
103  bool reset = (iphi > 18 && iphi < 27); //remove few supermodules
104  for (int s = 0; s < 5; s++) {
105  uint16_t sample = it->sample(s).raw();
106  if (sample == 0)
107  continue;
108  uint16_t tmp = reset ? 0 : sample;
109  if (reset)
110  tmp = sample >> 1;
111  col.setSample(s, tmp);
112  }
113  data->push_back(col);
114  }
115 }
116 
117 template <>
118 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1CaloEmCollection>& data,
120  CLHEP::HepRandomEngine* engine) const {
121  typedef L1CaloEmCollection::const_iterator col_cit;
122  for (col_cit it = emul->begin(); it != emul->end(); it++) {
123  unsigned crate = it->rctCrate();
124  unsigned raw = it->raw();
125  bool iso = it->isolated();
126  unsigned rdata = raw;
127  if (crate < 4 * engine->flat())
128  rdata = raw >> 1;
129  L1CaloEmCand cand(rdata, crate, iso, it->index(), it->bx(), false);
130  data->push_back(cand);
131  }
132  //L1CaloEmCand(uint16_t data, unsigned crate, bool iso);
133  //L1CaloEmCand(uint16_t data, unsigned crate, bool iso, uint16_t index, int16_t bx, bool dummy);
134 }
135 
136 template <>
137 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1CaloRegionCollection>& data,
139  CLHEP::HepRandomEngine* engine) const {
140  typedef L1CaloRegionCollection::const_iterator col_cit;
141  for (col_cit it = emul->begin(); it != emul->end(); it++) {
142  unsigned crate = it->rctCrate();
143  unsigned raw = it->et();
144  uint16_t rdata = raw;
145  if (crate < 4 * engine->flat())
146  rdata = raw >> 1;
147  L1CaloRegion cand(rdata, it->gctEta(), it->gctPhi(), it->bx());
148  data->push_back(cand);
149  }
150  //L1CaloRegion(uint16_t data, unsigned ieta, unsigned iphi, int16_t bx);
151  //Note: raw data accessor missing in dataformats!
152 }
153 
154 template <>
155 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1GctEmCandCollection>& data,
157  CLHEP::HepRandomEngine* engine) const {
158  typedef L1GctEmCandCollection::const_iterator col_cit;
159  for (col_cit it = emul->begin(); it != emul->end(); it++) {
160  unsigned raw = it->raw();
161  uint16_t rdata = raw;
162  if (it->phiIndex() < 4 * engine->flat()) //0-17
163  rdata = raw >> 1;
164  L1GctEmCand cand(rdata, it->isolated());
165  data->push_back(cand);
166  }
167  //etaIndex(), etaSign() : -6 to -0, +0 to +6
168  //L1GctEmCand(uint16_t data, bool iso);
169 }
170 
171 template <>
172 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1GctJetCandCollection>& data,
174  CLHEP::HepRandomEngine* engine) const {
175  typedef L1GctJetCandCollection::const_iterator col_cit;
176  for (col_cit it = emul->begin(); it != emul->end(); it++) {
177  unsigned raw = it->raw();
178  uint16_t rdata = raw;
179  if (it->phiIndex() < 4 * engine->flat()) //0-17
180  rdata = raw >> 1;
181  L1GctJetCand cand(rdata, it->isTau(), it->isForward());
182  data->push_back(cand);
183  }
184  //L1GctJetCand(uint16_t data, bool isTau, bool isFor);
185 }
186 
187 template <>
188 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuRegionalCandCollection>& data,
190  CLHEP::HepRandomEngine* engine) const {
191  typedef L1MuRegionalCandCollection::const_iterator col_cit;
192  for (col_cit it = emul->begin(); it != emul->end(); it++) {
193  L1MuRegionalCand cand(*it);
194  //unsigned raw = it->getDataWord();
195  unsigned phi = it->phi_packed();
196  if (phi > 90 && phi < 110)
197  cand.setPtPacked((it->pt_packed()) >> 1);
198  //raw = (raw>>2);
199  //L1MuRegionalCand cand(raw);
200  //cand.setType(it->type_idx());
201  data->push_back(cand);
202  }
203  /* few alternatives...
204  unsigned pt= it->pt_packed(); //0..31
205  unsigned int qua = it->quality(); //0..7
206  if(qua<4){cand.setPtPacked((pt>>2)&0x1f);cand.setQualityPacked((qua<<1)&0x07);}
207  double rnd = CLHEP::RandGaussQ::shoot(engine);
208  if(rnd>0.7) {
209  raw_=(raw>>1);
210  cand.setDataWord(raw_);
211  } else if (rnd>0.3) {
212  pt_ *= (int)(1+0.3*engine->flat());
213  cand.setPtPacked(pt_);
214  } else
215  cand.reset();
216  unsigned raw = it->getDataWord();
217  if(2.5<fabs(it->phiValue())<3.0)
218  rdata = raw>>1;
219  L1MuRegionalCand cand(rdata,it->bx());
220  */
221  //L1MuRegionalCand(unsigned dataword = 0, int bx = 0);
222  //L1MuRegionalCand(unsigned type_idx, unsigned phi, unsigned eta, unsigned pt, unsigned charge, unsigned ch_valid, unsigned finehalo, unsigned quality, int bx);
223 }
224 
225 template <>
226 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuDTTrackContainer>& data,
228  CLHEP::HepRandomEngine* engine) const {
229  typedef std::vector<L1MuDTTrackCand> TrackContainer;
230  typedef TrackContainer::const_iterator col_cit;
231  TrackContainer const* tracks_in = emul->getContainer();
233  for (col_cit it = tracks_in->begin(); it != tracks_in->end(); it++) {
234  L1MuDTTrackCand cand(*it);
235  cand.setType(it->type_idx());
236  unsigned pt = it->pt_packed(); //0..31
237  unsigned qua = it->quality(); //0..7
238  if (qua < 4) {
239  cand.setPtPacked((pt >> 2) & 0x1f);
240  cand.setQualityPacked((qua << 1) & 0x07);
241  }
242  tracks.push_back(cand);
243  }
244  data->setContainer(tracks);
245  /* few alternatives...
246  unsigned phip = it->phi_packed();
247  unsigned raw = it->getDataWord();
248  uint16_t rdata = raw;
249  if(2.5<fabs(it->phiValue())<3.0)
250  rdata = raw>>1;
251  L1MuRegionalCand cand(rdata,it->bx());
252  double rnd = engine->flat();
253  */
254 }
255 
256 template <>
257 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuDTChambPhContainer>& data,
259  CLHEP::HepRandomEngine* engine) const {
260  typedef std::vector<L1MuDTChambPhDigi> Phi_Container;
261  typedef Phi_Container::const_iterator col_it;
262  Phi_Container const* tracks_in = emul->getContainer();
263  Phi_Container tracks(tracks_in->size());
264  int uqua;
265  for (col_it it = tracks_in->begin(); it != tracks_in->end(); it++) {
266  uqua = it->code(); // (int)(10*engine->flat());
267  uqua = (uqua < 2 ? uqua + 1 : uqua);
269  it->bxNum(), it->whNum(), it->scNum(), it->stNum(), it->phi(), it->phiB(), uqua, it->Ts2Tag(), it->BxCnt());
270  tracks.push_back(cand);
271  }
272  data->setContainer(tracks);
273 }
274 
275 template <>
276 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuDTChambThContainer>& data,
278  CLHEP::HepRandomEngine*) const {
279  typedef std::vector<L1MuDTChambThDigi> Thi_Container;
280  typedef Thi_Container::const_iterator col_cit;
281  Thi_Container const* tracks_in = emul->getContainer();
282  Thi_Container tracks(tracks_in->size());
283  int uos[7], uqa[7];
284  for (col_cit it = tracks_in->begin(); it != tracks_in->end(); it++) {
285  for (int j = 0; j < 7; j++) {
286  uos[j] = (it->position(j) ? 0 : 1);
287  uqa[j] = (it->quality(j) ? 0 : 1);
288  }
289  int stnum = it->stNum();
290  stnum = (stnum > 2 ? stnum - 1 : stnum);
291  L1MuDTChambThDigi cand(it->bxNum(), it->whNum(), it->scNum(), stnum, uos, uqa);
292  tracks.push_back(cand);
293  }
294  data->setContainer(tracks);
295 }
296 
297 template <>
298 inline void L1EmulBias::ModifyCollection(std::unique_ptr<LTCDigiCollection>& data,
300  CLHEP::HepRandomEngine*) const {
301  typedef std::vector<LTCDigi>::const_iterator col_cit;
302  for (col_cit it = emul->begin(); it != emul->end(); it++) {
303  data->push_back(*it);
304  //note: raw data accessor missing in dataformats!
305  //data->push_back(LTCDigi(it->data()>>1));
306  }
307 }
308 
309 template <>
310 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuGMTCandCollection>& data,
312  CLHEP::HepRandomEngine*) const {
313  //typedef std::vector<L1MuGMTCand> L1MuGMTCandCollection;
314  typedef std::vector<L1MuGMTCand>::const_iterator col_cit;
315  for (col_cit it = emul->begin(); it != emul->end(); it++) {
316  float phiv = it->phiValue();
317  unsigned dword = it->getDataWord();
318  if (phiv > 2. && phiv < 4.)
319  dword = dword >> 2;
320  L1MuGMTCand cand(dword, it->bx());
321  data->push_back(cand);
322  //cand.setPtPacked(cand.ptIndex()>>1);
323  //data->push_back(L1MuGMTCand((it->getDataWord()>>1),it->bx()));
324  }
325 }
326 
327 template <>
328 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuGMTReadoutCollection>& data,
330  CLHEP::HepRandomEngine*) const {
331  typedef std::vector<L1MuGMTReadoutRecord>::const_iterator col_cit;
332  std::vector<L1MuGMTReadoutRecord> col = emul->getRecords();
333  for (col_cit it = col.begin(); it != col.end(); it++) {
334  L1MuGMTReadoutRecord rec(it->getBxInEvent());
335  rec.setBxNr(it->getBxNr());
336  rec.setEvNr(it->getEvNr());
337  rec.setBCERR(it->getBCERR());
338 
339  std::unique_ptr<L1MuRegionalCandCollection> new_dttf(new L1MuRegionalCandCollection);
340  std::unique_ptr<L1MuRegionalCandCollection> new_rpcb(new L1MuRegionalCandCollection);
341  std::unique_ptr<L1MuRegionalCandCollection> new_csc(new L1MuRegionalCandCollection);
342  std::unique_ptr<L1MuRegionalCandCollection> new_rpcf(new L1MuRegionalCandCollection);
343 
344  L1MuRegionalCandCollection old_dttf = it->getDTBXCands();
345  L1MuRegionalCandCollection old_rpcb = it->getBrlRPCCands();
346  L1MuRegionalCandCollection old_csc = it->getCSCCands();
347  L1MuRegionalCandCollection old_rpcf = it->getFwdRPCCands();
348 
349  typedef L1MuRegionalCandCollection::const_iterator ait;
350  for (ait it = old_dttf.begin(); it != old_dttf.end(); it++) {
351  L1MuRegionalCand cand(*it);
352  if (it->quality() < 4)
353  cand.setPtPacked((it->pt_packed() >> 2) & 0x1f);
354  cand.setType(it->type_idx());
355  new_dttf->push_back(cand);
356  }
357  for (ait it = old_rpcb.begin(); it != old_rpcb.end(); it++) {
358  L1MuRegionalCand cand(*it);
359  if (it->quality() < 4)
360  cand.setPtPacked((it->pt_packed() >> 2) & 0x1f);
361  cand.setType(it->type_idx());
362  new_rpcb->push_back(cand);
363  }
364  for (ait it = old_csc.begin(); it != old_csc.end(); it++) {
365  L1MuRegionalCand cand(*it);
366  if (it->quality() < 4)
367  cand.setPtPacked((it->pt_packed() >> 2) & 0x1f);
368  cand.setType(it->type_idx());
369  new_csc->push_back(cand);
370  }
371  for (ait it = old_rpcf.begin(); it != old_rpcf.end(); it++) {
372  L1MuRegionalCand cand(*it);
373  if (it->quality() < 4)
374  cand.setPtPacked((it->pt_packed() >> 2) & 0x1f);
375  cand.setType(it->type_idx());
376  new_rpcf->push_back(cand);
377  }
378 
379  for (unsigned i = 0; i < old_dttf.size(); i++)
380  rec.setInputCand(i, new_dttf->at(i)); //dt : 0..3
381  for (unsigned i = 0; i < old_rpcb.size(); i++)
382  rec.setInputCand(i + 4, new_rpcb->at(i)); //rpcb: 4..7
383  for (unsigned i = 0; i < old_csc.size(); i++)
384  rec.setInputCand(i + 8, new_csc->at(i)); //csc : 8..11
385  for (unsigned i = 0; i < old_rpcf.size(); i++)
386  rec.setInputCand(i + 12, new_rpcf->at(i)); //rpcf:12..15
387 
388  data->addRecord(rec);
389  }
390  //void addRecord(L1MuGMTReadoutRecord const& rec) {
391 }
392 
393 template <>
394 inline void L1EmulBias::ModifyCollection(std::unique_ptr<CSCCorrelatedLCTDigiCollection>& data,
396  CLHEP::HepRandomEngine*) const {
397  //typedef MuonDigiCollection<CSCDetId,CSCCorrelatedLCTDigi> CSCCorrelatedLCTDigiCollection;
398  typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt; //map iterator
399  typedef CSCCorrelatedLCTDigiCollection::const_iterator vecIt; //vec iterator
400  //loop over data (map<idx,vec_digi>)
401  for (mapIt mit = emul->begin(); mit != emul->end(); mit++) {
402  //get detector index
403  CSCDetId did = (*mit).first;
404  //get vec_digi range(pair) corresponding to idx of map
405  //CSCCorrelatedLCTDigiCollection::Range ctpRange = emul->get(did)
406  //loop over digi vector (ie between begin and end pointers in range)
407  //for (vecIt vit = ctpRange.first; vit != ctpRange.second; vit++) {
408  for (vecIt vit = emul->get((*mit).first).first; vit != emul->get((*mit).first).second; vit++) {
410  CSCCorrelatedLCTDigi dg = *vit;
411  //dg.clear;
412  uint16_t tn = dg.getTrknmb();
413  if (tn == 2)
414  tn--;
415  dg.setTrknmb(tn);
416  //dg.setTrknmb (dg.getTrknmb ());
417  //dg.setMPCLink (dg.getMPCLink ());
418  //dg.setWireGroup(dg.getWireGroup());
420  data->insertDigi(did, dg);
421  }
422  }
423 }
424 
425 template <>
426 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1CSCTrackCollection>& data,
428  CLHEP::HepRandomEngine*) const {
429  typedef L1CSCTrackCollection::const_iterator col_cit;
430  //typedef std::vector<L1CSCTrack> L1CSCTrackCollection;
431  //typedef std::pair<csc::L1Track,CSCCorrelatedLCTDigiCollection> L1CSCTrack;
432  //typedef MuonDigiCollection<CSCDetId,CSCCorrelatedLCTDigi> CSCCorrelatedLCTDigiCollection;
433  typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt; //map iterator
434  typedef CSCCorrelatedLCTDigiCollection::const_iterator vecIt; //vec iterator
435  CSCCorrelatedLCTDigiCollection_ ctf_trk_data_v, ctf_trk_emul_v; //vector
436  //loop over csc-tracks (ie pairs<l1track,digi_vec>)
437  for (col_cit tcit = emul->begin(); tcit != emul->end(); tcit++) {
438  csc::L1Track l1trk = tcit->first;
439  if (l1trk.quality() < 4)
440  l1trk.setPtPacked((l1trk.pt_packed() >> 2) & 0x1f);
441  l1trk.setType(l1trk.type_idx());
442  //L1MuRegionalCand reg(tcit->first.getDataWord(), tcit->first.bx());
443  std::unique_ptr<CSCCorrelatedLCTDigiCollection> dgcoll(new CSCCorrelatedLCTDigiCollection);
444  CSCCorrelatedLCTDigiCollection ldc = tcit->second; //muondigicollection=map
445  //get the lct-digi-collection (ie muon-digi-collection)
446  //loop over data (map<idx,vec_digi>)
447  for (mapIt mit = ldc.begin(); mit != ldc.end(); mit++) {
448  //get vec_digi range(pair) corresponding to idx of map
449  //loop over digi vector (ie between begin and end pointers in range)
450  //CSCCorrelatedLCTDigiCollection::Range ctpRange = ctp_lct_data_->get((*mit).first)
451  CSCDetId did = (*mit).first;
452  //for (vecIt vit = ctpRange.first; vit != ctpRange.second; vit++) {
453  for (vecIt vit = ldc.get((*mit).first).first; vit != ldc.get((*mit).first).second; vit++) {
454  CSCCorrelatedLCTDigi dg = *vit;
455  uint16_t tn = dg.getTrknmb();
456  if (tn == 2)
457  tn--;
458  dg.setTrknmb(tn);
459  dgcoll->insertDigi(did, dg);
460  }
461  }
462  L1CSCTrack l1csctrk = std::make_pair(l1trk, *dgcoll);
463  data->push_back(l1csctrk);
464  }
465 }
466 
467 #endif
~L1EmulBias() override
Definition: L1EmulBias.cc:139
const int DEnsys
Definition: DEtrait.h:38
bool m_doSys[dedefs::DEnsys]
Definition: L1EmulBias.h:54
std::pair< csc::L1Track, CSCCorrelatedLCTDigiCollection > L1CSCTrack
unsigned pt_packed() const
return pt packed as in hardware
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Definition: L1EmulBias.cc:142
unsigned int quality() const
return quality
std::vector< T >::const_iterator const_iterator
Level-1 Trigger jet candidate.
Definition: L1GctJetCand.h:17
Level-1 Region Calorimeter Trigger EM candidate.
Definition: L1CaloEmCand.h:17
void ModifyCollection(std::unique_ptr< T > &data, const edm::Handle< T > emul, CLHEP::HepRandomEngine *) const
Definition: L1EmulBias.h:66
L1EmulBias(const edm::ParameterSet &)
Definition: L1EmulBias.cc:9
Level-1 Trigger EM candidate at output of GCT.
Definition: L1GctEmCand.h:21
std::vector< TrackWithHistory * > TrackContainer
Definition: TrackContainer.h:8
void setType(unsigned type)
Set Type: 0 DT, 1 bRPC, 2 CSC, 3 fRPC.
edm::InputTag m_DEsource[dedefs::DEnsys][2]
Definition: L1EmulBias.h:53
int verbose() const
Definition: L1EmulBias.h:52
std::vector< L1MuRegionalCand > L1MuRegionalCandCollection
uint16_t getTrknmb() const
return track number
std::string instName[dedefs::DEnsys][5]
Definition: L1EmulBias.h:55
void setPtPacked(unsigned pt)
Set Pt: 0..31.
std::vector< CSCCorrelatedLCTDigi > CSCCorrelatedLCTDigiCollection_
Definition: DEtrait.h:97
int verbose_
Definition: L1EmulBias.h:51
std::vector< DigiType >::const_iterator const_iterator
void setBxNr(int bxnr)
set counters
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
col
Definition: cuy.py:1009
A calorimeter trigger region (sum of 4x4 trigger towers)
Definition: L1CaloRegion.h:21
tmp
align.sh
Definition: createJobs.py:716
void setTrknmb(const uint16_t number)
Set track number (1,2) after sorting LCTs.
void reset(double vett[256])
Definition: TPedValues.cc:11
unsigned type_idx() const
return type: 0 DT, 1 bRPC, 2 CSC, 3 fRPC