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(void) {};
00046
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
00066
00067
00068
00069
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);
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);
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
00136
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
00154
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())
00165 rdata = raw>>1;
00166 L1GctEmCand cand(rdata,it->isolated());
00167 data->push_back(cand);
00168 }
00169
00170
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())
00181 rdata = raw>>1;
00182 L1GctJetCand cand(rdata,it->isTau(),it->isForward());
00183 data->push_back(cand);
00184 }
00185
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
00195 unsigned phi = it->phi_packed();
00196 if(phi>90 && phi<110)
00197 cand.setPtPacked( (it->pt_packed())>>1 );
00198
00199
00200
00201 data->push_back(cand);
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
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();
00236 unsigned qua = it->quality();
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
00245
00246
00247
00248
00249
00250
00251
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();
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
00303
00304 }
00305 }
00306
00307 template <> inline void
00308 L1EmulBias::ModifyCollection(std::auto_ptr<L1MuGMTCandCollection>& data,
00309 const edm::Handle<L1MuGMTCandCollection> emul) {
00310
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
00320
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));
00377 for(unsigned i=0; i<old_rpcb.size(); i++)
00378 rec.setInputCand(i+ 4,new_rpcb->at(i));
00379 for(unsigned i=0; i<old_csc .size(); i++)
00380 rec.setInputCand(i+ 8,new_csc ->at(i));
00381 for(unsigned i=0; i<old_rpcf.size(); i++)
00382 rec.setInputCand(i+12,new_rpcf->at(i));
00383
00384 data->addRecord(rec);
00385 }
00386
00387 }
00388
00389 template <> inline void
00390 L1EmulBias::ModifyCollection(std::auto_ptr<CSCCorrelatedLCTDigiCollection>& data,
00391 const edm::Handle<CSCCorrelatedLCTDigiCollection> emul) {
00392
00393 typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt;
00394 typedef CSCCorrelatedLCTDigiCollection::const_iterator vecIt;
00395
00396 for (mapIt mit = emul->begin(); mit != emul->end(); mit++) {
00397
00398 CSCDetId did = (*mit).first;
00399
00400
00401
00402
00403 for (vecIt vit = emul->get((*mit).first).first;
00404 vit != emul->get((*mit).first).second; vit++) {
00406 CSCCorrelatedLCTDigi dg = *vit;
00407
00408 uint16_t tn = dg.getTrknmb();
00409 if(tn==2) tn--;
00410 dg.setTrknmb(tn);
00411
00412
00413
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
00424
00425
00426 typedef CSCCorrelatedLCTDigiCollection::DigiRangeIterator mapIt;
00427 typedef CSCCorrelatedLCTDigiCollection::const_iterator vecIt;
00428 CSCCorrelatedLCTDigiCollection_ ctf_trk_data_v, ctf_trk_emul_v;
00429
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
00436 std::auto_ptr<CSCCorrelatedLCTDigiCollection> dgcoll(new CSCCorrelatedLCTDigiCollection);
00437 CSCCorrelatedLCTDigiCollection ldc = tcit->second;
00438
00439
00440 for (mapIt mit = ldc.begin(); mit != ldc.end(); mit++) {
00441
00442
00443
00444 CSCDetId did = (*mit).first;
00445
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