00001 #ifndef L1_EMUL_BIAS_H
00002 #define L1_EMUL_BIAS_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <memory>
00013 #include <string>
00014 #include <iostream>
00015 #include <fstream>
00016 #include <iomanip>
00017 #include <vector>
00018 #include <algorithm>
00019
00020
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
00030 #include "L1Trigger/HardwareValidation/interface/DEtrait.h"
00031
00032
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
00065
00066
00067
00068
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);
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);
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
00135
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
00153
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())
00164 rdata = raw>>1;
00165 L1GctEmCand cand(rdata,it->isolated());
00166 data->push_back(cand);
00167 }
00168
00169
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())
00180 rdata = raw>>1;
00181 L1GctJetCand cand(rdata,it->isTau(),it->isForward());
00182 data->push_back(cand);
00183 }
00184
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
00194 unsigned phi = it->phi_packed();
00195 if(phi>90 && phi<110)
00196 cand.setPtPacked( (it->pt_packed())>>1 );
00197
00198
00199
00200 data->push_back(cand);
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
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();
00235 unsigned qua = it->quality();
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
00244
00245
00246
00247
00248
00249
00250
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();
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
00302
00303 }
00304 }
00305
00306 template <> inline void
00307 L1EmulBias::ModifyCollection(std::auto_ptr<L1MuGMTCandCollection>& data,
00308 const edm::Handle<L1MuGMTCandCollection> emul) {
00309
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
00319
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));
00376 for(unsigned i=0; i<old_rpcb.size(); i++)
00377 rec.setInputCand(i+ 4,new_rpcb->at(i));
00378 for(unsigned i=0; i<old_csc .size(); i++)
00379 rec.setInputCand(i+ 8,new_csc ->at(i));
00380 for(unsigned i=0; i<old_rpcf.size(); i++)
00381 rec.setInputCand(i+12,new_rpcf->at(i));
00382
00383 data->addRecord(rec);
00384 }
00385
00386 }
00387
00388 template <> inline void
00389 L1EmulBias::ModifyCollection(std::auto_ptr<CSCCorrelatedLCTDigiCollection>& data,
00390 const edm::Handle<CSCCorrelatedLCTDigiCollection> emul) {
00391
00392 typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt;
00393 typedef CSCCorrelatedLCTDigiCollection::const_iterator vecIt;
00394
00395 for (mapIt mit = emul->begin(); mit != emul->end(); mit++) {
00396
00397 CSCDetId did = (*mit).first;
00398
00399
00400
00401
00402 for (vecIt vit = emul->get((*mit).first).first;
00403 vit != emul->get((*mit).first).second; vit++) {
00405 CSCCorrelatedLCTDigi dg = *vit;
00406
00407 uint16_t tn = dg.getTrknmb();
00408 if(tn==2) tn--;
00409 dg.setTrknmb(tn);
00410
00411
00412
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
00423
00424
00425 typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt;
00426 typedef CSCCorrelatedLCTDigiCollection::const_iterator vecIt;
00427 CSCCorrelatedLCTDigiCollection_ ctf_trk_data_v, ctf_trk_emul_v;
00428
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
00435 std::auto_ptr<CSCCorrelatedLCTDigiCollection> dgcoll(new CSCCorrelatedLCTDigiCollection);
00436 CSCCorrelatedLCTDigiCollection ldc = tcit->second;
00437
00438
00439 for (mapIt mit = ldc.begin(); mit != ldc.end(); mit++) {
00440
00441
00442
00443 CSCDetId did = (*mit).first;
00444
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