Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:22:27

0001 #ifndef L1_EMUL_BIAS_H
0002 #define L1_EMUL_BIAS_H
0003 
0004 /*\class L1EmulBias
0005  *\description produces modified emulator data to mimmic hw problems
0006  *\usage l1 monitoring software validation
0007  *\author Nuno Leonardo (CERN)
0008  *\date 07.07
0009  */
0010 
0011 // system includes
0012 #include <memory>
0013 #include <string>
0014 #include <iostream>
0015 #include <fstream>
0016 #include <iomanip>
0017 #include <vector>
0018 #include <algorithm>
0019 
0020 // common includes
0021 #include "FWCore/Framework/interface/Frameworkfwd.h"
0022 #include "FWCore/Framework/interface/EDProducer.h"
0023 #include "FWCore/Framework/interface/Event.h"
0024 #include "FWCore/Framework/interface/MakerMacros.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0027 
0028 // l1 dataformats, d|e record includes
0029 #include "DataFormats/HcalDigi/interface/HcalTriggerPrimitiveDigi.h"
0030 #include "DataFormats/L1CSCTrackFinder/interface/L1Track.h"
0031 #include "L1Trigger/HardwareValidation/interface/DEtrait.h"
0032 
0033 // random generation
0034 #include "CLHEP/Random/RandomEngine.h"
0035 #include "CLHEP/Random/RandGaussQ.h"
0036 
0037 class L1EmulBias : public edm::EDProducer {
0038 public:
0039   explicit L1EmulBias(const edm::ParameterSet&);
0040   ~L1EmulBias() override;
0041 
0042 protected:
0043   void beginJob(void) override{};
0044   //virtual void beginRun(edm::Run&, const edm::EventSetup&);
0045   void produce(edm::Event&, const edm::EventSetup&) override;
0046   void endJob(void) override{};
0047 
0048 public:
0049   template <class T>
0050   void ModifyCollection(std::unique_ptr<T>& data, const edm::Handle<T> emul, CLHEP::HepRandomEngine*);
0051 
0052 private:
0053   int verbose_;
0054   int verbose() { return verbose_; }
0055   edm::InputTag m_DEsource[dedefs::DEnsys][2];
0056   bool m_doSys[dedefs::DEnsys];
0057   std::string instName[dedefs::DEnsys][5];
0058 };
0059 
0060 /* Notes:
0061    .by default data is make identical to emul
0062    .biasing is to be implemented via specialization
0063    .bias may be defined for each data type, eg data word bit-shifting
0064    .keep template function specialization in header file
0065 */
0066 
0067 template <class T>
0068 void L1EmulBias::ModifyCollection(std::unique_ptr<T>& data, const edm::Handle<T> emul, CLHEP::HepRandomEngine*) {
0069   data = (std::unique_ptr<T>)(const_cast<T*>(emul.product()));
0070 }
0071 
0072 template <>
0073 inline void L1EmulBias::ModifyCollection(std::unique_ptr<EcalTrigPrimDigiCollection>& data,
0074                                          const edm::Handle<EcalTrigPrimDigiCollection> emul,
0075                                          CLHEP::HepRandomEngine*) {
0076   typedef EcalTrigPrimDigiCollection::const_iterator col_cit;
0077   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0078     EcalTriggerPrimitiveDigi col(*it);
0079     int iphi = it->id().iphi();
0080     bool reset = (iphi > 18 && iphi < 39);  //remove few supermodules
0081     for (int s = 0; s < 5; s++) {
0082       uint16_t sample = it->sample(s).raw();
0083       if (sample == 0)
0084         continue;
0085       uint16_t tmp = reset ? 0 : sample;
0086       if (reset)
0087         tmp = sample >> 1;
0088       col.setSampleValue(s, tmp);
0089       if (verbose() && sample != 0)
0090         std::cout << "[emulbias] etp " << *it << "\t sample: " << s << "  " << std::hex << sample << " -> "
0091                   << col.sample(s).raw() << std::dec << std::endl;
0092     }
0093     data->push_back(col);
0094   }
0095 }
0096 
0097 template <>
0098 inline void L1EmulBias::ModifyCollection(std::unique_ptr<HcalTrigPrimDigiCollection>& data,
0099                                          const edm::Handle<HcalTrigPrimDigiCollection> emul,
0100                                          CLHEP::HepRandomEngine*) {
0101   typedef HcalTrigPrimDigiCollection::const_iterator col_cit;
0102   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0103     HcalTriggerPrimitiveDigi col(*it);
0104     int iphi = it->id().iphi();
0105     bool reset = (iphi > 18 && iphi < 27);  //remove few supermodules
0106     for (int s = 0; s < 5; s++) {
0107       uint16_t sample = it->sample(s).raw();
0108       if (sample == 0)
0109         continue;
0110       uint16_t tmp = reset ? 0 : sample;
0111       if (reset)
0112         tmp = sample >> 1;
0113       col.setSample(s, tmp);
0114     }
0115     data->push_back(col);
0116   }
0117 }
0118 
0119 template <>
0120 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1CaloEmCollection>& data,
0121                                          const edm::Handle<L1CaloEmCollection> emul,
0122                                          CLHEP::HepRandomEngine* engine) {
0123   typedef L1CaloEmCollection::const_iterator col_cit;
0124   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0125     unsigned crate = it->rctCrate();
0126     unsigned raw = it->raw();
0127     bool iso = it->isolated();
0128     unsigned rdata = raw;
0129     if (crate < 4 * engine->flat())
0130       rdata = raw >> 1;
0131     L1CaloEmCand cand(rdata, crate, iso, it->index(), it->bx(), false);
0132     data->push_back(cand);
0133   }
0134   //L1CaloEmCand(uint16_t data, unsigned crate, bool iso);
0135   //L1CaloEmCand(uint16_t data, unsigned crate, bool iso, uint16_t index, int16_t bx, bool dummy);
0136 }
0137 
0138 template <>
0139 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1CaloRegionCollection>& data,
0140                                          const edm::Handle<L1CaloRegionCollection> emul,
0141                                          CLHEP::HepRandomEngine* engine) {
0142   typedef L1CaloRegionCollection::const_iterator col_cit;
0143   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0144     unsigned crate = it->rctCrate();
0145     unsigned raw = it->et();
0146     uint16_t rdata = raw;
0147     if (crate < 4 * engine->flat())
0148       rdata = raw >> 1;
0149     L1CaloRegion cand(rdata, it->gctEta(), it->gctPhi(), it->bx());
0150     data->push_back(cand);
0151   }
0152   //L1CaloRegion(uint16_t data, unsigned ieta, unsigned iphi, int16_t bx);
0153   //Note: raw data accessor missing in dataformats!
0154 }
0155 
0156 template <>
0157 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1GctEmCandCollection>& data,
0158                                          const edm::Handle<L1GctEmCandCollection> emul,
0159                                          CLHEP::HepRandomEngine* engine) {
0160   typedef L1GctEmCandCollection::const_iterator col_cit;
0161   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0162     unsigned raw = it->raw();
0163     uint16_t rdata = raw;
0164     if (it->phiIndex() < 4 * engine->flat())  //0-17
0165       rdata = raw >> 1;
0166     L1GctEmCand cand(rdata, it->isolated());
0167     data->push_back(cand);
0168   }
0169   //etaIndex(), etaSign() : -6 to -0, +0 to +6
0170   //L1GctEmCand(uint16_t data, bool iso);
0171 }
0172 
0173 template <>
0174 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1GctJetCandCollection>& data,
0175                                          const edm::Handle<L1GctJetCandCollection> emul,
0176                                          CLHEP::HepRandomEngine* engine) {
0177   typedef L1GctJetCandCollection::const_iterator col_cit;
0178   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0179     unsigned raw = it->raw();
0180     uint16_t rdata = raw;
0181     if (it->phiIndex() < 4 * engine->flat())  //0-17
0182       rdata = raw >> 1;
0183     L1GctJetCand cand(rdata, it->isTau(), it->isForward());
0184     data->push_back(cand);
0185   }
0186   //L1GctJetCand(uint16_t data, bool isTau, bool isFor);
0187 }
0188 
0189 template <>
0190 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuRegionalCandCollection>& data,
0191                                          const edm::Handle<L1MuRegionalCandCollection> emul,
0192                                          CLHEP::HepRandomEngine* engine) {
0193   typedef L1MuRegionalCandCollection::const_iterator col_cit;
0194   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0195     L1MuRegionalCand cand(*it);
0196     //unsigned raw = it->getDataWord();
0197     unsigned phi = it->phi_packed();
0198     if (phi > 90 && phi < 110)
0199       cand.setPtPacked((it->pt_packed()) >> 1);
0200     //raw = (raw>>2);
0201     //L1MuRegionalCand cand(raw);
0202     //cand.setType(it->type_idx());
0203     data->push_back(cand);
0204   }
0205   /* few alternatives...
0206   unsigned pt= it->pt_packed(); //0..31
0207   unsigned int qua = it->quality(); //0..7
0208   if(qua<4){cand.setPtPacked((pt>>2)&0x1f);cand.setQualityPacked((qua<<1)&0x07);}
0209   double rnd = CLHEP::RandGaussQ::shoot(engine);
0210   if(rnd>0.7) {
0211     raw_=(raw>>1);
0212     cand.setDataWord(raw_);
0213   } else if (rnd>0.3) {
0214     pt_ *= (int)(1+0.3*engine->flat());
0215     cand.setPtPacked(pt_);
0216   } else 
0217     cand.reset();
0218   unsigned raw = it->getDataWord();
0219   if(2.5<fabs(it->phiValue())<3.0)
0220     rdata = raw>>1;
0221   L1MuRegionalCand cand(rdata,it->bx());    
0222   */
0223   //L1MuRegionalCand(unsigned dataword = 0, int bx = 0);
0224   //L1MuRegionalCand(unsigned type_idx, unsigned phi, unsigned eta, unsigned pt, unsigned charge, unsigned ch_valid, unsigned finehalo, unsigned quality, int bx);
0225 }
0226 
0227 template <>
0228 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuDTTrackContainer>& data,
0229                                          const edm::Handle<L1MuDTTrackContainer> emul,
0230                                          CLHEP::HepRandomEngine* engine) {
0231   typedef std::vector<L1MuDTTrackCand> TrackContainer;
0232   typedef TrackContainer::const_iterator col_cit;
0233   TrackContainer const* tracks_in = emul->getContainer();
0234   TrackContainer tracks;
0235   for (col_cit it = tracks_in->begin(); it != tracks_in->end(); it++) {
0236     L1MuDTTrackCand cand(*it);
0237     cand.setType(it->type_idx());
0238     unsigned pt = it->pt_packed();  //0..31
0239     unsigned qua = it->quality();   //0..7
0240     if (qua < 4) {
0241       cand.setPtPacked((pt >> 2) & 0x1f);
0242       cand.setQualityPacked((qua << 1) & 0x07);
0243     }
0244     tracks.push_back(cand);
0245   }
0246   data->setContainer(tracks);
0247   /*   few alternatives...
0248   unsigned phip = it->phi_packed();
0249   unsigned raw = it->getDataWord();
0250   uint16_t rdata = raw;
0251   if(2.5<fabs(it->phiValue())<3.0)
0252     rdata = raw>>1;
0253   L1MuRegionalCand cand(rdata,it->bx());    
0254   double rnd    = engine->flat();
0255   */
0256 }
0257 
0258 template <>
0259 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuDTChambPhContainer>& data,
0260                                          const edm::Handle<L1MuDTChambPhContainer> emul,
0261                                          CLHEP::HepRandomEngine* engine) {
0262   typedef std::vector<L1MuDTChambPhDigi> Phi_Container;
0263   typedef Phi_Container::const_iterator col_it;
0264   Phi_Container const* tracks_in = emul->getContainer();
0265   Phi_Container tracks(tracks_in->size());
0266   int uqua;
0267   for (col_it it = tracks_in->begin(); it != tracks_in->end(); it++) {
0268     uqua = it->code();  // (int)(10*engine->flat());
0269     uqua = (uqua < 2 ? uqua + 1 : uqua);
0270     L1MuDTChambPhDigi cand(
0271         it->bxNum(), it->whNum(), it->scNum(), it->stNum(), it->phi(), it->phiB(), uqua, it->Ts2Tag(), it->BxCnt());
0272     tracks.push_back(cand);
0273   }
0274   data->setContainer(tracks);
0275 }
0276 
0277 template <>
0278 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuDTChambThContainer>& data,
0279                                          const edm::Handle<L1MuDTChambThContainer> emul,
0280                                          CLHEP::HepRandomEngine*) {
0281   typedef std::vector<L1MuDTChambThDigi> Thi_Container;
0282   typedef Thi_Container::const_iterator col_cit;
0283   Thi_Container const* tracks_in = emul->getContainer();
0284   Thi_Container tracks(tracks_in->size());
0285   int uos[7], uqa[7];
0286   for (col_cit it = tracks_in->begin(); it != tracks_in->end(); it++) {
0287     for (int j = 0; j < 7; j++) {
0288       uos[j] = (it->position(j) ? 0 : 1);
0289       uqa[j] = (it->quality(j) ? 0 : 1);
0290     }
0291     int stnum = it->stNum();
0292     stnum = (stnum > 2 ? stnum - 1 : stnum);
0293     L1MuDTChambThDigi cand(it->bxNum(), it->whNum(), it->scNum(), stnum, uos, uqa);
0294     tracks.push_back(cand);
0295   }
0296   data->setContainer(tracks);
0297 }
0298 
0299 template <>
0300 inline void L1EmulBias::ModifyCollection(std::unique_ptr<LTCDigiCollection>& data,
0301                                          const edm::Handle<LTCDigiCollection> emul,
0302                                          CLHEP::HepRandomEngine*) {
0303   typedef std::vector<LTCDigi>::const_iterator col_cit;
0304   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0305     data->push_back(*it);
0306     //note: raw data accessor missing in dataformats!
0307     //data->push_back(LTCDigi(it->data()>>1));
0308   }
0309 }
0310 
0311 template <>
0312 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuGMTCandCollection>& data,
0313                                          const edm::Handle<L1MuGMTCandCollection> emul,
0314                                          CLHEP::HepRandomEngine*) {
0315   //typedef std::vector<L1MuGMTCand>          L1MuGMTCandCollection;
0316   typedef std::vector<L1MuGMTCand>::const_iterator col_cit;
0317   for (col_cit it = emul->begin(); it != emul->end(); it++) {
0318     float phiv = it->phiValue();
0319     unsigned dword = it->getDataWord();
0320     if (phiv > 2. && phiv < 4.)
0321       dword = dword >> 2;
0322     L1MuGMTCand cand(dword, it->bx());
0323     data->push_back(cand);
0324     //cand.setPtPacked(cand.ptIndex()>>1);
0325     //data->push_back(L1MuGMTCand((it->getDataWord()>>1),it->bx()));
0326   }
0327 }
0328 
0329 template <>
0330 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1MuGMTReadoutCollection>& data,
0331                                          const edm::Handle<L1MuGMTReadoutCollection> emul,
0332                                          CLHEP::HepRandomEngine*) {
0333   typedef std::vector<L1MuGMTReadoutRecord>::const_iterator col_cit;
0334   std::vector<L1MuGMTReadoutRecord> col = emul->getRecords();
0335   for (col_cit it = col.begin(); it != col.end(); it++) {
0336     L1MuGMTReadoutRecord rec(it->getBxInEvent());
0337     rec.setBxNr(it->getBxNr());
0338     rec.setEvNr(it->getEvNr());
0339     rec.setBCERR(it->getBCERR());
0340 
0341     std::unique_ptr<L1MuRegionalCandCollection> new_dttf(new L1MuRegionalCandCollection);
0342     std::unique_ptr<L1MuRegionalCandCollection> new_rpcb(new L1MuRegionalCandCollection);
0343     std::unique_ptr<L1MuRegionalCandCollection> new_csc(new L1MuRegionalCandCollection);
0344     std::unique_ptr<L1MuRegionalCandCollection> new_rpcf(new L1MuRegionalCandCollection);
0345 
0346     L1MuRegionalCandCollection old_dttf = it->getDTBXCands();
0347     L1MuRegionalCandCollection old_rpcb = it->getBrlRPCCands();
0348     L1MuRegionalCandCollection old_csc = it->getCSCCands();
0349     L1MuRegionalCandCollection old_rpcf = it->getFwdRPCCands();
0350 
0351     typedef L1MuRegionalCandCollection::const_iterator ait;
0352     for (ait it = old_dttf.begin(); it != old_dttf.end(); it++) {
0353       L1MuRegionalCand cand(*it);
0354       if (it->quality() < 4)
0355         cand.setPtPacked((it->pt_packed() >> 2) & 0x1f);
0356       cand.setType(it->type_idx());
0357       new_dttf->push_back(cand);
0358     }
0359     for (ait it = old_rpcb.begin(); it != old_rpcb.end(); it++) {
0360       L1MuRegionalCand cand(*it);
0361       if (it->quality() < 4)
0362         cand.setPtPacked((it->pt_packed() >> 2) & 0x1f);
0363       cand.setType(it->type_idx());
0364       new_rpcb->push_back(cand);
0365     }
0366     for (ait it = old_csc.begin(); it != old_csc.end(); it++) {
0367       L1MuRegionalCand cand(*it);
0368       if (it->quality() < 4)
0369         cand.setPtPacked((it->pt_packed() >> 2) & 0x1f);
0370       cand.setType(it->type_idx());
0371       new_csc->push_back(cand);
0372     }
0373     for (ait it = old_rpcf.begin(); it != old_rpcf.end(); it++) {
0374       L1MuRegionalCand cand(*it);
0375       if (it->quality() < 4)
0376         cand.setPtPacked((it->pt_packed() >> 2) & 0x1f);
0377       cand.setType(it->type_idx());
0378       new_rpcf->push_back(cand);
0379     }
0380 
0381     for (unsigned i = 0; i < old_dttf.size(); i++)
0382       rec.setInputCand(i, new_dttf->at(i));  //dt  : 0..3
0383     for (unsigned i = 0; i < old_rpcb.size(); i++)
0384       rec.setInputCand(i + 4, new_rpcb->at(i));  //rpcb: 4..7
0385     for (unsigned i = 0; i < old_csc.size(); i++)
0386       rec.setInputCand(i + 8, new_csc->at(i));  //csc : 8..11
0387     for (unsigned i = 0; i < old_rpcf.size(); i++)
0388       rec.setInputCand(i + 12, new_rpcf->at(i));  //rpcf:12..15
0389 
0390     data->addRecord(rec);
0391   }
0392   //void addRecord(L1MuGMTReadoutRecord const& rec) {
0393 }
0394 
0395 template <>
0396 inline void L1EmulBias::ModifyCollection(std::unique_ptr<CSCCorrelatedLCTDigiCollection>& data,
0397                                          const edm::Handle<CSCCorrelatedLCTDigiCollection> emul,
0398                                          CLHEP::HepRandomEngine*) {
0399   //typedef MuonDigiCollection<CSCDetId,CSCCorrelatedLCTDigi> CSCCorrelatedLCTDigiCollection;
0400   typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt;  //map iterator
0401   typedef CSCCorrelatedLCTDigiCollection::const_iterator vecIt;     //vec iterator
0402   //loop over data (map<idx,vec_digi>)
0403   for (mapIt mit = emul->begin(); mit != emul->end(); mit++) {
0404     //get detector index
0405     CSCDetId did = (*mit).first;
0406     //get vec_digi range(pair)  corresponding to idx of map
0407     //CSCCorrelatedLCTDigiCollection::Range ctpRange = emul->get(did)
0408     //loop over digi vector (ie between begin and end pointers in range)
0409     //for (vecIt vit = ctpRange.first; vit != ctpRange.second; vit++) {
0410     for (vecIt vit = emul->get((*mit).first).first; vit != emul->get((*mit).first).second; vit++) {
0411       ///modify digi
0412       CSCCorrelatedLCTDigi dg = *vit;
0413       //dg.clear;
0414       uint16_t tn = dg.getTrknmb();
0415       if (tn == 2)
0416         tn--;
0417       dg.setTrknmb(tn);
0418       //dg.setTrknmb   (dg.getTrknmb   ());
0419       //dg.setMPCLink  (dg.getMPCLink  ());
0420       //dg.setWireGroup(dg.getWireGroup());
0421       ///append digi
0422       data->insertDigi(did, dg);
0423     }
0424   }
0425 }
0426 
0427 template <>
0428 inline void L1EmulBias::ModifyCollection(std::unique_ptr<L1CSCTrackCollection>& data,
0429                                          const edm::Handle<L1CSCTrackCollection> emul,
0430                                          CLHEP::HepRandomEngine*) {
0431   typedef L1CSCTrackCollection::const_iterator col_cit;
0432   //typedef std::vector<L1CSCTrack> L1CSCTrackCollection;
0433   //typedef std::pair<csc::L1Track,CSCCorrelatedLCTDigiCollection> L1CSCTrack;
0434   //typedef MuonDigiCollection<CSCDetId,CSCCorrelatedLCTDigi> CSCCorrelatedLCTDigiCollection;
0435   typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt;  //map iterator
0436   typedef CSCCorrelatedLCTDigiCollection::const_iterator vecIt;     //vec iterator
0437   CSCCorrelatedLCTDigiCollection_ ctf_trk_data_v, ctf_trk_emul_v;   //vector
0438   //loop over csc-tracks (ie pairs<l1track,digi_vec>)
0439   for (col_cit tcit = emul->begin(); tcit != emul->end(); tcit++) {
0440     csc::L1Track l1trk = tcit->first;
0441     if (l1trk.quality() < 4)
0442       l1trk.setPtPacked((l1trk.pt_packed() >> 2) & 0x1f);
0443     l1trk.setType(l1trk.type_idx());
0444     //L1MuRegionalCand reg(tcit->first.getDataWord(), tcit->first.bx());
0445     std::unique_ptr<CSCCorrelatedLCTDigiCollection> dgcoll(new CSCCorrelatedLCTDigiCollection);
0446     CSCCorrelatedLCTDigiCollection ldc = tcit->second;  //muondigicollection=map
0447     //get the lct-digi-collection (ie muon-digi-collection)
0448     //loop over data (map<idx,vec_digi>)
0449     for (mapIt mit = ldc.begin(); mit != ldc.end(); mit++) {
0450       //get vec_digi range(pair)  corresponding to idx of map
0451       //loop over digi vector (ie between begin and end pointers in range)
0452       //CSCCorrelatedLCTDigiCollection::Range ctpRange = ctp_lct_data_->get((*mit).first)
0453       CSCDetId did = (*mit).first;
0454       //for (vecIt vit = ctpRange.first; vit != ctpRange.second; vit++) {
0455       for (vecIt vit = ldc.get((*mit).first).first; vit != ldc.get((*mit).first).second; vit++) {
0456         CSCCorrelatedLCTDigi dg = *vit;
0457         uint16_t tn = dg.getTrknmb();
0458         if (tn == 2)
0459           tn--;
0460         dg.setTrknmb(tn);
0461         dgcoll->insertDigi(did, dg);
0462       }
0463     }
0464     L1CSCTrack l1csctrk = std::make_pair(l1trk, *dgcoll);
0465     data->push_back(l1csctrk);
0466   }
0467 }
0468 
0469 #endif