CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/L1Trigger/HardwareValidation/plugins/L1EmulBias.h

Go to the documentation of this file.
00001 #ifndef L1_EMUL_BIAS_H
00002 #define L1_EMUL_BIAS_H
00003 
00004 /*\class L1EmulBias
00005  *\description produces modified emulator data to mimmic hw problems
00006  *\usage l1 monitoring software validation
00007  *\author Nuno Leonardo (CERN)
00008  *\date 07.07
00009  */
00010 
00011 // system includes
00012 #include <memory>
00013 #include <string>
00014 #include <iostream>
00015 #include <fstream>
00016 #include <iomanip>
00017 #include <vector>
00018 #include <algorithm>
00019 
00020 // common includes
00021 #include "FWCore/ServiceRegistry/interface/Service.h"
00022 #include "FWCore/Framework/interface/Frameworkfwd.h"
00023 #include "FWCore/Framework/interface/EDProducer.h"
00024 #include "FWCore/Framework/interface/Event.h"
00025 #include "FWCore/Framework/interface/MakerMacros.h"
00026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00028 
00029 // l1 dataformats, d|e record includes
00030 #include "L1Trigger/HardwareValidation/interface/DEtrait.h"
00031 
00032 // random generation
00033 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00034 #include "CLHEP/Random/RandFlat.h"
00035 #include "CLHEP/Random/RandGaussQ.h"
00036 
00037 
00038 class L1EmulBias : public edm::EDProducer {
00039   
00040  public:
00041   explicit L1EmulBias(const edm::ParameterSet&);
00042   ~L1EmulBias();
00043   
00044  protected:
00045   virtual void beginJob(void) {};
00046   //virtual void beginRun(edm::Run&, const edm::EventSetup&);
00047   virtual void produce(edm::Event&, const edm::EventSetup&);
00048   virtual void endJob(void) {};
00049 
00050  public:
00051   template <class T> 
00052     void ModifyCollection ( std::auto_ptr<T>& data, const edm::Handle<T> emul); 
00053   
00054  private:
00055   int verbose_;
00056   int verbose() {return verbose_;}
00057   edm::InputTag m_DEsource[dedefs::DEnsys][2];
00058   bool m_doSys[dedefs::DEnsys];
00059   std::string instName[dedefs::DEnsys][5];
00060   CLHEP::RandFlat   *rndFlat_;
00061   CLHEP::RandGaussQ *rndGaus_;
00062   
00063 };
00064 
00065 /* Notes:
00066    .by default data is make identical to emul
00067    .biasing is to be implemented via specialization
00068    .bias may be defined for each data type, eg data word bit-shifting
00069    .keep template function specialization in header file
00070 */
00071 
00072 template <class T> 
00073 void L1EmulBias::ModifyCollection(std::auto_ptr<T>& data, const edm::Handle<T> emul) {
00074   data = (std::auto_ptr<T>)(const_cast<T*>(emul.product()));
00075 } 
00076 
00077 template <> inline void 
00078 L1EmulBias::ModifyCollection(std::auto_ptr<EcalTrigPrimDigiCollection>& data, 
00079                            const edm::Handle<EcalTrigPrimDigiCollection> emul) {
00080   typedef EcalTrigPrimDigiCollection::const_iterator col_cit;
00081   for(col_cit it = emul->begin(); it!=emul->end(); it++) {
00082     EcalTriggerPrimitiveDigi col(*it);    
00083     int iphi = it->id().iphi();
00084     bool reset = (iphi>18 && iphi<39);//remove few supermodules
00085     for(int s=0; s<5; s++) {
00086       uint16_t sample = it->sample(s).raw(); 
00087       if(sample==0) continue;
00088       uint16_t tmp = reset?0:sample; 
00089       if(reset)
00090         tmp = sample>>1; 
00091       col.setSampleValue(s,tmp);
00092       if(verbose() && sample!=0)
00093         std::cout << "[emulbias] etp " << *it << "\t sample: " << s << "  "
00094                   << std::hex << sample << " -> " << col.sample(s).raw()
00095                   << std::dec << std::endl;
00096     }
00097     data->push_back(col);
00098   }  
00099 }
00100 
00101 template <> inline void 
00102 L1EmulBias::ModifyCollection(std::auto_ptr<HcalTrigPrimDigiCollection>& data, 
00103                            const edm::Handle<HcalTrigPrimDigiCollection> emul) {
00104   typedef HcalTrigPrimDigiCollection::const_iterator col_cit;
00105   for(col_cit it = emul->begin(); it!=emul->end(); it++) {
00106     HcalTriggerPrimitiveDigi col(*it);    
00107     int iphi = it->id().iphi();
00108     bool reset = (iphi>18 && iphi<27);//remove few supermodules
00109     for(int s=0; s<5; s++) {
00110       uint16_t sample = it->sample(s).raw(); 
00111       if(sample==0) continue;
00112       uint16_t tmp = reset?0:sample; 
00113       if(reset)
00114         tmp = sample>>1; 
00115       col.setSample(s,tmp);
00116     }
00117     data->push_back(col);
00118   }
00119 }
00120 
00121 template <> inline void 
00122 L1EmulBias::ModifyCollection(std::auto_ptr<L1CaloEmCollection>& data, 
00123                            const edm::Handle<L1CaloEmCollection> emul) {
00124   typedef L1CaloEmCollection::const_iterator col_cit;
00125   for(col_cit it = emul->begin(); it!=emul->end(); it++) {
00126     unsigned crate = it->rctCrate();
00127     unsigned raw   = it->raw();
00128     bool     iso   = it->isolated();  
00129     unsigned rdata = raw;
00130     if(crate<4*rndFlat_->fire())
00131       rdata = raw>>1;
00132     L1CaloEmCand cand(rdata,crate,iso,it->index(),it->bx(),false);    
00133     data->push_back(cand);
00134   }  
00135   //L1CaloEmCand(uint16_t data, unsigned crate, bool iso);
00136   //L1CaloEmCand(uint16_t data, unsigned crate, bool iso, uint16_t index, int16_t bx, bool dummy);
00137 }
00138 
00139 
00140 template <> inline void 
00141 L1EmulBias::ModifyCollection(std::auto_ptr<L1CaloRegionCollection>& data, 
00142                            const edm::Handle<L1CaloRegionCollection> emul) {
00143   typedef L1CaloRegionCollection::const_iterator col_cit;
00144   for(col_cit it = emul->begin(); it!=emul->end(); it++) {
00145     unsigned crate = it->rctCrate();
00146     unsigned raw = it->et();
00147     uint16_t rdata = raw;
00148     if(crate<4*rndFlat_->fire())
00149       rdata = raw>>1;
00150     L1CaloRegion cand(rdata,it->gctEta(),it->gctPhi(),it->bx());    
00151     data->push_back(cand);
00152   }  
00153   //L1CaloRegion(uint16_t data, unsigned ieta, unsigned iphi, int16_t bx);
00154   //Note: raw data accessor missing in dataformats!
00155 }
00156 
00157 template <> inline void 
00158 L1EmulBias::ModifyCollection(std::auto_ptr<L1GctEmCandCollection>& data, 
00159                            const edm::Handle<L1GctEmCandCollection>emul) {
00160   typedef L1GctEmCandCollection::const_iterator col_cit;
00161   for(col_cit it = emul->begin(); it!=emul->end(); it++) {
00162     unsigned raw = it->raw();
00163     uint16_t rdata = raw;
00164     if(it->phiIndex()<4*rndFlat_->fire()) //0-17
00165       rdata = raw>>1;
00166     L1GctEmCand cand(rdata,it->isolated());
00167     data->push_back(cand);
00168   }  
00169   //etaIndex(), etaSign() : -6 to -0, +0 to +6
00170   //L1GctEmCand(uint16_t data, bool iso);
00171 }
00172 
00173 template <> inline void 
00174 L1EmulBias::ModifyCollection(std::auto_ptr<L1GctJetCandCollection>& data, 
00175                            const edm::Handle<L1GctJetCandCollection> emul) {
00176   typedef L1GctJetCandCollection::const_iterator col_cit;
00177   for(col_cit it = emul->begin(); it!=emul->end(); it++) {
00178     unsigned raw = it->raw();
00179     uint16_t rdata = raw;
00180     if(it->phiIndex()<4*rndFlat_->fire()) //0-17
00181       rdata = raw>>1;
00182     L1GctJetCand cand(rdata,it->isTau(),it->isForward());
00183     data->push_back(cand);
00184   }
00185   //L1GctJetCand(uint16_t data, bool isTau, bool isFor);
00186 }
00187 
00188 template <> inline void 
00189 L1EmulBias::ModifyCollection ( std::auto_ptr<L1MuRegionalCandCollection>& data, 
00190                              const edm::Handle<L1MuRegionalCandCollection> emul) {
00191   typedef L1MuRegionalCandCollection::const_iterator col_cit;
00192   for(col_cit it = emul->begin(); it!=emul->end(); it++) {
00193     L1MuRegionalCand cand(*it);    
00194     //unsigned raw = it->getDataWord();
00195     unsigned phi = it->phi_packed();
00196     if(phi>90 && phi<110)
00197       cand.setPtPacked( (it->pt_packed())>>1 );
00198       //raw = (raw>>2);
00199       //L1MuRegionalCand cand(raw); 
00200       //cand.setType(it->type_idx());
00201     data->push_back(cand);
00202   }
00203   /* few alternatives...
00204   unsigned pt= it->pt_packed(); //0..31
00205   unsigned int qua = it->quality(); //0..7
00206   if(qua<4){cand.setPtPacked((pt>>2)&0x1f);cand.setQualityPacked((qua<<1)&0x07);}
00207   double rnd = rndGaus_->fire();
00208   if(rnd>0.7) {
00209     raw_=(raw>>1);
00210     cand.setDataWord(raw_);
00211   } else if (rnd>0.3) {
00212     pt_ *= (int)(1+0.3*rndFlat_->fire());
00213     cand.setPtPacked(pt_);
00214   } else 
00215     cand.reset();
00216   unsigned raw = it->getDataWord();
00217   if(2.5<fabs(it->phiValue())<3.0)
00218     rdata = raw>>1;
00219   L1MuRegionalCand cand(rdata,it->bx());    
00220   */
00221   //L1MuRegionalCand(unsigned dataword = 0, int bx = 0); 
00222   //L1MuRegionalCand(unsigned type_idx, unsigned phi, unsigned eta, unsigned pt, unsigned charge, unsigned ch_valid, unsigned finehalo, unsigned quality, int bx);
00223 }
00224 
00225 template <> inline void 
00226 L1EmulBias::ModifyCollection(std::auto_ptr<L1MuDTTrackContainer>& data, 
00227                            const edm::Handle<L1MuDTTrackContainer> emul) {
00228   typedef std::vector<L1MuDTTrackCand>  TrackContainer;
00229   typedef TrackContainer::const_iterator col_cit;
00230   TrackContainer* tracks_in = emul->getContainer();
00231   TrackContainer tracks;
00232   for(col_cit it = tracks_in->begin(); it!=tracks_in->end(); it++) {
00233     L1MuDTTrackCand cand(*it);    
00234     cand.setType(it->type_idx());
00235     unsigned pt = it->pt_packed(); //0..31
00236     unsigned qua = it->quality(); //0..7
00237     if(qua<4) {
00238       cand.setPtPacked((pt>>2)&0x1f);
00239       cand.setQualityPacked((qua<<1)&0x07);
00240     }
00241     tracks.push_back(cand);
00242   }
00243   data->setContainer(tracks);
00244   /*   few alternatives...
00245   unsigned phip = it->phi_packed();
00246   unsigned raw = it->getDataWord();
00247   uint16_t rdata = raw;
00248   if(2.5<fabs(it->phiValue())<3.0)
00249     rdata = raw>>1;
00250   L1MuRegionalCand cand(rdata,it->bx());    
00251   double rnd    = rndFlat_->fire();
00252   */
00253 }
00254 
00255 template <> inline void 
00256 L1EmulBias::ModifyCollection(std::auto_ptr<L1MuDTChambPhContainer>& data, 
00257                            const edm::Handle<L1MuDTChambPhContainer> emul) {
00258   typedef std::vector<L1MuDTChambPhDigi> Phi_Container;
00259   typedef Phi_Container::const_iterator  col_it;
00260   Phi_Container* tracks_in = emul->getContainer(); 
00261   Phi_Container tracks(tracks_in->size());
00262   int uqua;
00263   for(col_it it=tracks_in->begin(); it!=tracks_in->end(); it++) {
00264     uqua = it->code(); // (int)(10*rndFlat_->fire());
00265     uqua = (uqua<2?uqua+1:uqua);
00266     L1MuDTChambPhDigi 
00267       cand(it->bxNum(),it->whNum(),it->scNum(),it->stNum(),
00268            it->phi(),it->phiB(),uqua,it->Ts2Tag(),it->BxCnt() );
00269     tracks.push_back(cand);
00270   }
00271   data->setContainer(tracks);
00272 }
00273 
00274 template <> inline void 
00275 L1EmulBias::ModifyCollection(std::auto_ptr<L1MuDTChambThContainer>& data, 
00276                            const edm::Handle<L1MuDTChambThContainer> emul) {
00277   typedef std::vector<L1MuDTChambThDigi> Thi_Container;
00278   typedef Thi_Container::const_iterator  col_cit;
00279   Thi_Container* tracks_in = emul->getContainer(); 
00280   Thi_Container tracks(tracks_in->size());
00281   int uos[7],uqa[7];
00282   for(col_cit it=tracks_in->begin(); it!=tracks_in->end(); it++) {
00283     for(int j=0; j<7; j++) {
00284       uos[j]=(it->position(j)?0:1);
00285       uqa[j]=(it->quality (j)?0:1);
00286     }
00287     int stnum = it->stNum();
00288     stnum = (stnum>2?stnum-1:stnum);
00289     L1MuDTChambThDigi 
00290       cand(it->bxNum(),it->whNum(),it->scNum(),stnum,uos,uqa);
00291     tracks.push_back(cand);
00292   }
00293   data->setContainer(tracks);
00294 }
00295 
00296 template <> inline void 
00297 L1EmulBias::ModifyCollection(std::auto_ptr<LTCDigiCollection>& data, 
00298                            const edm::Handle<LTCDigiCollection> emul) {
00299   typedef std::vector<LTCDigi>::const_iterator col_cit;
00300   for(col_cit it=emul->begin(); it!=emul->end(); it++) {
00301     data->push_back(*it);
00302     //note: raw data accessor missing in dataformats!
00303     //data->push_back(LTCDigi(it->data()>>1));
00304   }
00305 }
00306 
00307 template <> inline void 
00308 L1EmulBias::ModifyCollection(std::auto_ptr<L1MuGMTCandCollection>& data, 
00309                            const edm::Handle<L1MuGMTCandCollection> emul) {
00310   //typedef std::vector<L1MuGMTCand>          L1MuGMTCandCollection;
00311   typedef std::vector<L1MuGMTCand>::const_iterator col_cit;
00312   for(col_cit it=emul->begin(); it!=emul->end(); it++) {
00313     float phiv = it->phiValue();
00314     unsigned dword = it->getDataWord();
00315     if(phiv>2. && phiv<4.) 
00316       dword = dword>>2;
00317     L1MuGMTCand cand(dword,it->bx());
00318     data->push_back(cand);
00319     //cand.setPtPacked(cand.ptIndex()>>1);
00320     //data->push_back(L1MuGMTCand((it->getDataWord()>>1),it->bx()));
00321   }
00322 }
00323 
00324 template <> inline void 
00325 L1EmulBias::ModifyCollection(std::auto_ptr<L1MuGMTReadoutCollection>& data, 
00326                            const edm::Handle<L1MuGMTReadoutCollection> emul) {
00327   typedef std::vector<L1MuGMTReadoutRecord>::const_iterator col_cit;
00328   std::vector<L1MuGMTReadoutRecord> col = emul->getRecords();
00329   for(col_cit it = col.begin(); it!=col.end(); it++) {
00330     L1MuGMTReadoutRecord rec(it->getBxInEvent());    
00331     rec.setBxNr (it->getBxNr ());
00332     rec.setEvNr (it->getEvNr ());
00333     rec.setBCERR(it->getBCERR());
00334     
00335     std::auto_ptr<L1MuRegionalCandCollection> new_dttf(new L1MuRegionalCandCollection);
00336     std::auto_ptr<L1MuRegionalCandCollection> new_rpcb(new L1MuRegionalCandCollection);
00337     std::auto_ptr<L1MuRegionalCandCollection> new_csc (new L1MuRegionalCandCollection);
00338     std::auto_ptr<L1MuRegionalCandCollection> new_rpcf(new L1MuRegionalCandCollection);
00339   
00340     L1MuRegionalCandCollection old_dttf = it->getDTBXCands();
00341     L1MuRegionalCandCollection old_rpcb = it->getBrlRPCCands();
00342     L1MuRegionalCandCollection old_csc  = it->getCSCCands();
00343     L1MuRegionalCandCollection old_rpcf = it->getFwdRPCCands();
00344 
00345     typedef L1MuRegionalCandCollection::const_iterator ait;
00346     for(ait it = old_dttf.begin(); it!=old_dttf.end(); it++) {
00347       L1MuRegionalCand cand(*it);    
00348       if(it->quality()<4)
00349         cand.setPtPacked((it->pt_packed()>>2)&0x1f);
00350       cand.setType(it->type_idx());
00351       new_dttf->push_back(cand);
00352     }
00353     for(ait it = old_rpcb.begin(); it!=old_rpcb.end(); it++) {
00354       L1MuRegionalCand cand(*it);    
00355       if(it->quality()<4)
00356         cand.setPtPacked((it->pt_packed()>>2)&0x1f);
00357       cand.setType(it->type_idx());
00358       new_rpcb->push_back(cand);
00359     }
00360     for(ait it = old_csc.begin(); it!=old_csc.end(); it++) {
00361       L1MuRegionalCand cand(*it);    
00362       if(it->quality()<4)
00363         cand.setPtPacked((it->pt_packed()>>2)&0x1f);
00364       cand.setType(it->type_idx());
00365       new_csc->push_back(cand);
00366     }
00367     for(ait it = old_rpcf.begin(); it!=old_rpcf.end(); it++) {
00368       L1MuRegionalCand cand(*it);    
00369       if(it->quality()<4)
00370         cand.setPtPacked((it->pt_packed()>>2)&0x1f);
00371       cand.setType(it->type_idx());
00372       new_rpcf->push_back(cand);
00373     }
00374   
00375     for(unsigned i=0; i<old_dttf.size(); i++)
00376       rec.setInputCand(i   ,new_dttf->at(i));//dt  : 0..3
00377     for(unsigned i=0; i<old_rpcb.size(); i++)
00378       rec.setInputCand(i+ 4,new_rpcb->at(i));//rpcb: 4..7
00379     for(unsigned i=0; i<old_csc .size(); i++)
00380       rec.setInputCand(i+ 8,new_csc ->at(i));//csc : 8..11
00381     for(unsigned i=0; i<old_rpcf.size(); i++)
00382       rec.setInputCand(i+12,new_rpcf->at(i));//rpcf:12..15
00383 
00384     data->addRecord(rec);
00385   }
00386   //void addRecord(L1MuGMTReadoutRecord const& rec) {
00387 }
00388 
00389 template <> inline void 
00390 L1EmulBias::ModifyCollection(std::auto_ptr<CSCCorrelatedLCTDigiCollection>& data, 
00391                            const edm::Handle<CSCCorrelatedLCTDigiCollection> emul) {
00392   //typedef MuonDigiCollection<CSCDetId,CSCCorrelatedLCTDigi> CSCCorrelatedLCTDigiCollection;
00393   typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt;//map iterator
00394   typedef CSCCorrelatedLCTDigiCollection::const_iterator    vecIt;//vec iterator
00395   //loop over data (map<idx,vec_digi>)
00396   for (mapIt mit = emul->begin(); mit != emul->end(); mit++) {
00397     //get detector index
00398     CSCDetId did = (*mit).first;
00399     //get vec_digi range(pair)  corresponding to idx of map
00400     //CSCCorrelatedLCTDigiCollection::Range ctpRange = emul->get(did)
00401     //loop over digi vector (ie between begin and end pointers in range)
00402     //for (vecIt vit = ctpRange.first; vit != ctpRange.second; vit++) {
00403     for (vecIt vit = emul->get((*mit).first).first; 
00404          vit != emul->get((*mit).first).second; vit++) {
00406       CSCCorrelatedLCTDigi dg = *vit; 
00407       //dg.clear;
00408       uint16_t tn = dg.getTrknmb();
00409       if(tn==2) tn--;
00410       dg.setTrknmb(tn);
00411       //dg.setTrknmb   (dg.getTrknmb   ());
00412       //dg.setMPCLink  (dg.getMPCLink  ());
00413       //dg.setWireGroup(dg.getWireGroup());
00415       data->insertDigi(did,dg);
00416     }
00417   }
00418 }
00419 
00420 template <> inline void L1EmulBias::ModifyCollection(std::auto_ptr<L1CSCTrackCollection>& data, 
00421                                                    const edm::Handle<L1CSCTrackCollection> emul) {
00422   typedef L1CSCTrackCollection::const_iterator col_cit;
00423   //typedef std::vector<L1CSCTrack> L1CSCTrackCollection;
00424   //typedef std::pair<csc::L1Track,CSCCorrelatedLCTDigiCollection> L1CSCTrack;
00425   //typedef MuonDigiCollection<CSCDetId,CSCCorrelatedLCTDigi> CSCCorrelatedLCTDigiCollection;
00426   typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt;//map iterator
00427   typedef CSCCorrelatedLCTDigiCollection::const_iterator    vecIt;//vec iterator
00428   CSCCorrelatedLCTDigiCollection_ ctf_trk_data_v, ctf_trk_emul_v; //vector
00429   //loop over csc-tracks (ie pairs<l1track,digi_vec>)
00430   for(col_cit tcit=emul->begin(); tcit!=emul->end(); tcit++) {
00431     csc::L1Track l1trk = tcit->first;
00432     if(l1trk.quality()<4)
00433       l1trk.setPtPacked((l1trk.pt_packed()>>2)&0x1f);
00434     l1trk.setType(l1trk.type_idx());
00435     //L1MuRegionalCand reg(tcit->first.getDataWord(), tcit->first.bx());
00436     std::auto_ptr<CSCCorrelatedLCTDigiCollection> dgcoll(new CSCCorrelatedLCTDigiCollection);
00437     CSCCorrelatedLCTDigiCollection ldc = tcit->second; //muondigicollection=map
00438     //get the lct-digi-collection (ie muon-digi-collection)
00439     //loop over data (map<idx,vec_digi>)
00440     for (mapIt mit = ldc.begin(); mit != ldc.end(); mit++) {
00441       //get vec_digi range(pair)  corresponding to idx of map
00442       //loop over digi vector (ie between begin and end pointers in range)
00443       //CSCCorrelatedLCTDigiCollection::Range ctpRange = ctp_lct_data_->get((*mit).first)
00444       CSCDetId did = (*mit).first;
00445       //for (vecIt vit = ctpRange.first; vit != ctpRange.second; vit++) {
00446       for (vecIt vit = ldc.get((*mit).first).first; 
00447            vit != ldc.get((*mit).first).second; vit++) {
00448         CSCCorrelatedLCTDigi dg = *vit; 
00449         uint16_t tn = dg.getTrknmb();
00450         if(tn==2) tn--;
00451         dg.setTrknmb(tn);
00452         dgcoll->insertDigi(did,dg);
00453       }
00454     }
00455     L1CSCTrack l1csctrk = std::make_pair(l1trk,*dgcoll);
00456     data->push_back(l1csctrk);
00457   }
00458 }
00459 
00460 #endif