CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/SimCalorimetry/EcalElectronicsEmulation/src/EcalFEtoDigi.cc

Go to the documentation of this file.
00001 #include "CondFormats/EcalObjects/interface/EcalTPGLutIdMap.h"
00002 #include "CondFormats/EcalObjects/interface/EcalTPGLutGroup.h"
00003 #include "CondFormats/DataRecord/interface/EcalTPGLutIdMapRcd.h"
00004 #include "CondFormats/DataRecord/interface/EcalTPGLutGroupRcd.h"
00005 #include "SimCalorimetry/EcalElectronicsEmulation/interface/EcalFEtoDigi.h"
00006 
00007 EcalFEtoDigi::EcalFEtoDigi(const edm::ParameterSet& iConfig) {
00008   basename_           = iConfig.getUntrackedParameter<std::string>("FlatBaseName","ecal_tcc_");
00009   sm_                 = iConfig.getUntrackedParameter<int>("SuperModuleId",-1);
00010   fileEventOffset_    = iConfig.getUntrackedParameter<int>("FileEventOffset",0);
00011   useIdentityLUT_     = iConfig.getUntrackedParameter<bool>("UseIdentityLUT",false);
00012   debug_              = iConfig.getUntrackedParameter<bool>("debugPrintFlag", false);
00013 
00014   singlefile = (sm_==-1)?false:true;
00015 
00016   produces<EcalTrigPrimDigiCollection>();
00017   produces<EcalTrigPrimDigiCollection >("formatTCP");
00018  
00019 }
00020 
00022 void
00023 EcalFEtoDigi::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00024 {
00025 
00027   static int current_bx = -1;
00028   current_bx++;
00029 
00031   //readInput();
00032 
00033   if(debug_)
00034     std::cout << "[EcalFEtoDigi::produce] producing event " << current_bx << std::endl;
00035   
00036   std::auto_ptr<EcalTrigPrimDigiCollection>  
00037     e_tpdigis (new EcalTrigPrimDigiCollection);
00038   std::auto_ptr<EcalTrigPrimDigiCollection>  
00039     e_tpdigisTcp (new EcalTrigPrimDigiCollection);
00040 
00041   std::vector<TCCinput>::const_iterator it;
00042 
00043   for(int i=0; i<N_SM; i++) {
00044 
00045     if(!singlefile)
00046       sm_=i+1;
00047 
00048     for(it = inputdata_[i].begin(); it != inputdata_[i].end(); it++) {
00049 
00050       if(!(*it).is_current(current_bx+fileEventOffset_)) 
00051         continue;
00052       else
00053         if(debug_ && (*it).input!=0 )
00054           std::cout << "[EcalFEtoDigi] " 
00055                << "\tsupermodule:" << sm_ 
00056                << "\tevent: "      << current_bx 
00057                << "\tbx: "         << (*it).bunchCrossing
00058                << "\tvalue:0x"     << std::setfill('0') << std::setw(4) 
00059                << std::hex << (*it).input << std::setfill(' ') << std::dec 
00060                << std::endl;
00061 
00062       
00064       const EcalTrigTowerDetId  e_id = create_TTDetId(*it);
00065       
00066       //EcalElectronicsMapping theMapping;
00067       //const EcalTrigTowerDetId  e_id 
00068       //= theMapping.getTrigTowerDetId(SMidToTCCid(sm_),(*it).tower);
00069       //EcalElectronicsMapping::getTrigTowerDetId(int TCCid, int iTT)
00070       
00072       EcalTriggerPrimitiveDigi *e_digi = new EcalTriggerPrimitiveDigi(e_id);
00073       EcalTriggerPrimitiveDigi *e_digiTcp = new EcalTriggerPrimitiveDigi(e_id);
00074       
00076       EcalTriggerPrimitiveSample e_sample = create_TPSample(*it, iSetup);
00077       EcalTriggerPrimitiveSample e_sampleTcp = create_TPSampleTcp(*it, iSetup);
00078 
00080       e_digi->setSize(1); //set sampleOfInterest to 0
00081       e_digi->setSample(0,e_sample);
00082       
00084       e_tpdigis->push_back(*e_digi);
00085     
00087       e_digiTcp->setSize(1); //set sampleOfInterest to 0
00088       e_digiTcp->setSample(0,e_sampleTcp);
00089       
00091       e_tpdigisTcp->push_back(*e_digiTcp);
00092 
00093       if(debug_) 
00094         outfile << (*it).tower << '\t' 
00095                 << (*it).bunchCrossing << '\t'<< std::setfill('0') << std::hex
00096                 << "0x" << std::setw(4) << (*it).input << '\t'
00097                 << "0"  << std::dec << std::setfill(' ') 
00098                 << std::endl;
00099 
00101       if(debug_ && (*it).input!=0 )
00102         std::cout << "[EcalFEtoDigi] debug id: " << e_digi->id() << "\n\t" 
00103                   << std::dec 
00104                   << "\tieta: "     << e_digi->id().ieta()
00105                   << "\tiphi: "     << e_digi->id().iphi()
00106                   << "\tsize: "     << e_digi->size()
00107                   << "\tfg: "       <<(e_digi->fineGrain()?1:0)    
00108                   << std::hex    
00109                   << "\tEt: 0x"     << e_digi->compressedEt() 
00110                   << " (0x"         << (*it).get_energy() << ")" 
00111                   << "\tttflag: 0x" << e_digi->ttFlag()
00112                   << std::dec
00113                   << std::endl;
00114 
00115       delete e_digi;
00116       delete e_digiTcp;
00117 
00118     }
00119     
00120     if(singlefile)
00121       break;
00122   }
00123 
00125   if(e_tpdigis->size()==0) {
00126     std::cout << "[EcalFEtoDigi] creating empty collection for the event!\n";
00127     EcalTriggerPrimitiveDigi *e_digi = new EcalTriggerPrimitiveDigi();
00128     e_tpdigis->push_back(*e_digi);
00129   }
00130 
00131   iEvent.put(e_tpdigis);
00132   iEvent.put(e_tpdigisTcp,"formatTCP");
00133 
00134 }
00135 
00136 
00138 void 
00139 EcalFEtoDigi::readInput() {
00140 
00141   if(debug_)
00142     std::cout << "\n[EcalFEtoDigi::readInput] Reading input data\n";
00143   
00144   if(!singlefile)
00145     sm_=-1;
00146   for(int i=0; i<N_SM; i++)
00147     inputdata_[i].clear();
00148 
00149   std::stringstream s;
00150   int tcc;
00151 
00152   for (int i=0; i<N_SM; i++) {
00153 
00154     tcc = (sm_==-1)?SMidToTCCid(i+1):SMidToTCCid(sm_);
00155 
00156     s.str("");
00157     s << basename_ << tcc << ".txt"; 
00158 
00159     std::ifstream f(s.str().c_str());
00160     
00161     if(debug_) {
00162       std::cout << "  opening " << s.str().c_str() << "..." << std::endl;
00163       if(!f.good())
00164         std::cout << " skipped!"; 
00165       std::cout << std::endl;       
00166     }
00167     //if (!f.good() || f.eof()) 
00168     //  throw cms::Exception("BadInputFile") 
00169     //  << "EcalFEtoDigi: cannot open file " << s.str().c_str() << std::endl; 
00170     
00171     int n_bx=0;
00172     int tt; int bx; unsigned val; int dummy;
00173 
00174     while(f.good()) {
00175       if(f.eof()) break;
00176       tt=0; bx=-1; val=0x0; dummy=0;
00177       f >> tt >> bx >> std::hex >> val >> std::dec >> dummy;
00178       if(bx==-1 || bx < fileEventOffset_ ) continue;
00179       if( !n_bx || (bx!=(inputdata_[i].back()).bunchCrossing) )
00180         n_bx++;
00181       TCCinput ttdata(tt,bx,val);
00182       inputdata_[i].push_back(ttdata);
00183       
00184       if(debug_&&val!=0)
00185         printf("\treading tower:%d  bx:%d input:0x%x dummy:%2d\n", 
00186                tt, bx, val, dummy);
00187     } 
00188     
00189     f.close();
00190     
00191     if(sm_!=-1)
00192       break;    
00193   }
00194 
00195   if(debug_)
00196     std::cout << "[EcalFEtoDigi::readInput] Done reading." << std::endl;
00197 
00198   return; 
00199 }
00200 
00202 EcalTrigTowerDetId 
00203 EcalFEtoDigi::create_TTDetId(TCCinput data) {
00204 
00205   // (EcalBarrel only)
00206   static const int kTowersInPhi = 4;
00207   
00208   int iTT   = data.tower;
00209   int zside = (sm_>18)?-1:+1;
00210   int SMid  = sm_;
00211 
00212   int jtower = iTT-1;
00213   int etaTT  = jtower / kTowersInPhi +1;
00214   int phiTT;
00215   if (zside < 0) 
00216     phiTT = (SMid-19) * kTowersInPhi + jtower % kTowersInPhi;
00217   else 
00218     phiTT = (SMid- 1) * kTowersInPhi + kTowersInPhi-(jtower % kTowersInPhi)-1;
00219 
00220   phiTT ++;
00221   //needed as phi=0 (iphi=1) is at middle of lower SMs (1 and 19), need shift by 2
00222   phiTT = phiTT -2; 
00223   if (phiTT <= 0) phiTT = 72+phiTT;
00224 
00226   if(debug_&&data.get_energy()!=0)
00227     printf("[EcalFEtoDigi] Creating EcalTrigTowerDetId (SMid,itt)=(%d,%d)->(eta,phi)=(%d,%d) \n", SMid, iTT, etaTT, phiTT);
00228   
00229   EcalTrigTowerDetId 
00230     e_id( zside , EcalBarrel, etaTT, phiTT, 0);
00231   
00232   return e_id;
00233 }
00234 
00236 EcalTriggerPrimitiveSample 
00237 EcalFEtoDigi::create_TPSample(TCCinput data, const edm::EventSetup& evtSetup) {
00238 
00239   int tower      = create_TTDetId(data).rawId() ; 
00240   int  Et        = data.get_energy();
00241   bool tt_fg     = data.get_fg();
00242   //unsigned input = data.input;
00243   //int  Et    = input & 0x3ff; //get bits 0-9
00244   //bool tt_fg = input & 0x400; //get bit number 10
00245 
00247   unsigned int lut_[1024] ;
00248   if(!useIdentityLUT_)
00249     getLUT(lut_, tower, evtSetup) ;
00250   else
00251     for(int i=0; i<1024; i++) lut_[i] = i ; //identity lut!
00252   
00254   int lut_out = lut_[Et];
00255   int ttFlag  = (lut_out & 0x700) >> 8;
00256   int cEt     = (lut_out & 0xff );
00257 
00259   if(debug_&&data.get_energy()!=0)
00260     printf("[EcalFEtoDigi] Creating sample; input:0x%X (Et:0x%x) cEt:0x%x fg:%d ttflag:0x%x \n",
00261            data.input, Et, cEt, tt_fg, ttFlag);
00262   
00263   EcalTriggerPrimitiveSample e_sample(cEt, tt_fg, ttFlag);
00264   
00265   return e_sample;
00266 }
00267 
00269 EcalTriggerPrimitiveSample 
00270 EcalFEtoDigi::create_TPSampleTcp(TCCinput data, const edm::EventSetup& evtSetup) {
00271 
00272   int tower      = create_TTDetId(data).rawId() ; 
00273   int  Et        = data.get_energy();
00274   bool tt_fg     = data.get_fg();
00275 
00277   unsigned int lut_[1024] ;
00278   if(!useIdentityLUT_)
00279     getLUT(lut_, tower, evtSetup) ;
00280   else
00281     for(int i=0; i<1024; i++) lut_[i] = i ; //identity lut!
00282   
00283   int lut_out = lut_[Et];
00284   int ttFlag  = (lut_out & 0x700) >> 8;
00285   int tcpdata = ((ttFlag&0x7)<<11) | ((tt_fg & 0x1)<<10) |  (Et & 0x3ff) ;
00286 
00287   EcalTriggerPrimitiveSample e_sample(tcpdata);
00288   
00289   return e_sample;
00290 }
00291 
00292 
00294 void 
00295 EcalFEtoDigi::beginJob(){
00296 
00299   if(sm_!=-1 && (sm_<1 || sm_>36)) 
00300     throw cms::Exception("EcalFEtoDigiInvalidDetId") 
00301       << "EcalFEtoDigi: Adapt SM numbering convention.\n";
00302 
00304   if(debug_)
00305     outfile.open("inputcopy.txt");
00306 
00307   readInput();
00308   
00309 }  
00310 
00311 
00313 void 
00314 EcalFEtoDigi::endJob() {
00315   if(outfile.is_open())
00316     outfile.close();
00317 }
00318 
00320 int 
00321 EcalFEtoDigi::SMidToTCCid( const int smid ) const {
00322 
00323   return (smid<=18) ? smid+55-1 : smid+37-19;
00324 
00325 }
00326 
00328 void EcalFEtoDigi::getLUT(unsigned int * lut, const int towerId,  const edm::EventSetup& evtSetup) const 
00329 {
00330   edm::ESHandle<EcalTPGLutGroup> lutGrpHandle;
00331   evtSetup.get<EcalTPGLutGroupRcd>().get( lutGrpHandle );
00332   const EcalTPGGroups::EcalTPGGroupsMap & lutGrpMap = lutGrpHandle.product()->getMap() ;  
00333   EcalTPGGroups::EcalTPGGroupsMapItr itgrp = lutGrpMap.find(towerId) ;
00334   uint32_t lutGrp = 999 ;
00335   if (itgrp != lutGrpMap.end()) lutGrp = itgrp->second ;
00336   
00337   edm::ESHandle<EcalTPGLutIdMap> lutMapHandle;
00338   evtSetup.get<EcalTPGLutIdMapRcd>().get( lutMapHandle );
00339   const EcalTPGLutIdMap::EcalTPGLutMap & lutMap = lutMapHandle.product()->getMap() ;  
00340   EcalTPGLutIdMap::EcalTPGLutMapItr itLut = lutMap.find(lutGrp) ;
00341   if (itLut != lutMap.end()) {
00342     const unsigned int * theLut = (itLut->second).getLut() ;
00343     for (unsigned int i=0 ; i<1024 ; i++) lut[i] = theLut[i] ;
00344   }
00345 }