CMS 3D CMS Logo

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

Generated on Tue Jun 9 17:40:15 2009 for CMSSW by  doxygen 1.5.4