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