CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/FastSimulation/L1CaloTriggerProducer/src/FastL1GlobalAlgo.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    L1CaloTriggerProducer
00004 // Class:      FastL1GlobalAlgo
00005 // 
00013 //
00014 // Original Author:  Chi Nhan Nguyen
00015 //         Created:  Mon Feb 19 13:25:24 CST 2007
00016 // $Id: FastL1GlobalAlgo.cc,v 1.43 2011/10/04 21:28:04 aperrott Exp $
00017 //
00018 
00019 // No BitInfos for release versions
00020 
00021 #include "FastSimulation/L1CaloTriggerProducer/interface/FastL1GlobalAlgo.h"
00022 #include <iostream>
00023 #include <fstream>
00024 
00025 //
00026 // constructors and destructor
00027 //
00028 FastL1GlobalAlgo::FastL1GlobalAlgo(const edm::ParameterSet& iConfig)
00029 {  
00030   m_RMap = FastL1RegionMap::getFastL1RegionMap();
00031 
00032   // Get L1 config
00033   m_L1Config.DoEMCorr = iConfig.getParameter<bool>("DoEMCorr");
00034   m_L1Config.DoJetCorr = iConfig.getParameter<bool>("DoJetCorr");
00035   m_DoBitInfo = iConfig.getParameter<bool>("DoBitInfo");
00036   m_GctIso = iConfig.getParameter<bool>("GctIso");
00037   m_IsolationEt = iConfig.getParameter<double>("IsolationEt");
00038 
00039   // get uncompressed hcal et
00040   m_L1Config.HcalLUT = iConfig.getParameter<edm::FileInPath>("HcalLUT");
00041 
00042   m_L1Config.EMSeedEnThreshold = iConfig.getParameter<double>("EMSeedEnThreshold");
00043   m_L1Config.EMActiveLevel = iConfig.getParameter<double>("EMActiveLevel");
00044   m_L1Config.HadActiveLevel = iConfig.getParameter<double>("HadActiveLevel");
00045   m_L1Config.noTauVetoLevel = iConfig.getParameter<double>("noTauVetoLevel");   
00046   m_L1Config.hOeThreshold = iConfig.getParameter<double>("hOeThreshold");
00047   m_L1Config.FGEBThreshold = iConfig.getParameter<double>("FGEBThreshold");
00048   m_L1Config.FGEEThreshold = iConfig.getParameter<double>("FGEEThreshold");
00049 
00050   m_L1Config.MuonNoiseLevel = iConfig.getParameter<double>("MuonNoiseLevel");
00051   m_L1Config.EMNoiseLevel = iConfig.getParameter<double>("EMNoiseLevel");
00052   m_L1Config.HadNoiseLevel = iConfig.getParameter<double>("HadNoiseLevel");
00053   m_L1Config.QuietRegionThreshold = iConfig.getParameter<double>("QuietRegionThreshold");
00054   m_L1Config.JetSeedEtThreshold = iConfig.getParameter<double>("JetSeedEtThreshold");
00055 
00056   m_L1Config.TowerEMLSB = iConfig.getParameter<double>("TowerEMLSB");
00057   m_L1Config.TowerHadLSB = iConfig.getParameter<double>("TowerHadLSB");
00058   m_L1Config.EMLSB = iConfig.getParameter<double>("EMLSB");
00059   m_L1Config.JetLSB = iConfig.getParameter<double>("JetLSB");
00060 
00061   m_L1Config.CrystalEBThreshold = iConfig.getParameter<double>("CrystalEBThreshold");
00062   m_L1Config.CrystalEEThreshold = iConfig.getParameter<double>("CrystalEEThreshold");
00063 
00064   m_L1Config.TowerEBThreshold = iConfig.getParameter<double>("TowerEBThreshold");
00065   m_L1Config.TowerEEThreshold = iConfig.getParameter<double>("TowerEEThreshold");
00066   m_L1Config.TowerHBThreshold = iConfig.getParameter<double>("TowerHBThreshold");
00067   m_L1Config.TowerHEThreshold = iConfig.getParameter<double>("TowerHEThreshold");
00068 
00069   m_L1Config.TowerEBScale = iConfig.getParameter<double>("TowerEBScale");
00070   m_L1Config.TowerEEScale = iConfig.getParameter<double>("TowerEEScale");
00071   m_L1Config.TowerHBScale = iConfig.getParameter<double>("TowerHBScale");
00072   m_L1Config.TowerHEScale = iConfig.getParameter<double>("TowerHEScale");
00073 
00074   m_L1Config.noFGThreshold = iConfig.getParameter<double>("noFGThreshold");     
00075   
00076   m_L1Config.EmInputs = iConfig.getParameter <std::vector<edm::InputTag> >("EmInputs");
00077   m_L1Config.TowerInput = iConfig.getParameter<edm::InputTag>("TowerInput");
00078 
00079   m_L1Config.EcalTPInput = iConfig.getParameter<edm::InputTag>("EcalTPInput");
00080   m_L1Config.HcalTPInput = iConfig.getParameter<edm::InputTag>("HcalTPInput");
00081 
00082 
00083   //Load Hcal LUT file
00084   std::ifstream userfile;
00085   const std::string userfileName = m_L1Config.HcalLUT.fullPath();
00086   userfile.open(userfileName.c_str());
00087   static const int etabound = 32;
00088   static const int tpgmax = 256;
00089   for (int i=0; i<tpgmax; i++) { 
00090     for(int j = 1; j <=etabound; j++) {
00091       userfile >> m_hcaluncomp[j][i];
00092     }
00093   }
00094   userfile.close();  
00095   
00096 
00097   FastL1Region tr;
00098   for (int i=0;i<396;i++)
00099     m_Regions.push_back(tr);
00100 
00101 }
00102 
00103 FastL1GlobalAlgo::~FastL1GlobalAlgo()
00104 {
00105 }
00106 
00107 
00108 
00109 // ------------ Dump out CaloTower info  ------------
00110 void
00111 FastL1GlobalAlgo::CaloTowersDump(edm::Event const& e) {
00112   /*
00113     std::vector<edm::Handle<CaloTowerCollection> > prods;
00114     
00115     edm::LogInfo("FastL1GlobalAlgo::CaloTowersDump") << "Start!";
00116     e.getManyByType(prods);
00117     
00118     std::vector<edm::Handle<CaloTowerCollection> >::iterator i;
00119 
00120     for (i=prods.begin(); i!=prods.end(); i++) {
00121     const CaloTowerCollection& c=*(*i);
00122       
00123     for (CaloTowerCollection::const_iterator j=c.begin(); j!=c.end(); j++) {
00124     edm::LogInfo("FastL1GlobalAlgo::CaloTowersDump") << *j;
00125     }
00126     }
00127     edm::LogInfo("FastL1GlobalAlgo::CaloTowersDump") << "End!";
00128   */
00129 
00130 
00131   edm::Handle<CaloTowerCollection> input;
00132     
00133   edm::LogInfo("FastL1GlobalAlgo::CaloTowersDump") << "Start!";
00134   e.getByLabel(m_L1Config.TowerInput,input);
00135 
00136   for (CaloTowerCollection::const_iterator j=input->begin(); j!=input->end(); j++) {
00137     edm::LogInfo("FastL1GlobalAlgo::CaloTowersDump") << *j;
00138   }
00139 
00140   edm::LogInfo("FastL1GlobalAlgo::CaloTowersDump") << "End!";
00141 }
00142 
00143 
00144 // ------------ For sort()  ------------
00145 namespace myspace {
00146   bool 
00147   //FastL1GlobalAlgo::greaterEt( const reco::Candidate& a, const reco::Candidate& b ) {
00148   greaterEt( const reco::Candidate& a, const reco::Candidate& b ) {
00149     return (a.et()>b.et());
00150   }
00151 }
00152 
00153 // ------------ Find Jets  ------------
00154 void
00155 FastL1GlobalAlgo::findJets() {
00156 
00157   m_TauJets.clear();
00158   m_CenJets.clear();
00159   m_ForJets.clear();
00160   
00161   for (int i=0; i<396; i++) {
00162     // barrel/endcap part only:
00163     //if ((i%22)>3 && (i%22)<18) {
00164     std::pair<double, double> p = m_RMap->getRegionCenterEtaPhi(i);
00165     
00166     //if (m_Regions.at(i).SumEt()>0.)
00167     //  std::cout<<"******** TEST "<<i<<": "<<m_Regions.at(i).SumEt()<<std::endl;
00168 
00169     double eta   = p.first;
00170     double phi   = p.second;
00171     if (m_DoBitInfo){
00172       m_Regions[i].BitInfo.setEta(eta);
00173       m_Regions[i].BitInfo.setPhi(phi);
00174     }
00175 
00176     if (m_Regions.at(i).SumEt()>m_L1Config.JetSeedEtThreshold) {
00177       if (isMaxEtRgn_Window33(i)) {
00178         if (m_GctIso == true) { 
00179           if (TauIsolation(i) && (i%22)>3 && (i%22)<18 ) {
00180             addJet(i,true);    
00181           } else {
00182             addJet(i,false);    
00183           }
00184         } else {
00185           if (isTauJet(i) && (i%22)>3 && (i%22)<18 ) {
00186             addJet(i,true);    
00187           } else {
00188             addJet(i,false);    
00189           }
00190         }
00191         if (m_DoBitInfo) m_Regions[i].BitInfo.setMaxEt(true);
00192       }
00193       else {  
00194         if (m_DoBitInfo) m_Regions[i].BitInfo.setMaxEt(false);
00195       }
00196       if (m_DoBitInfo) m_Regions[i].BitInfo.setSumEtBelowThres (false);
00197     } else {
00198       if (m_DoBitInfo) m_Regions[i].BitInfo.setSumEtBelowThres (true);
00199     }
00200     //}
00201   }
00202   
00203 }
00204 
00205 
00206 // ------------ Add a jet  ------------
00207 void
00208 FastL1GlobalAlgo::addJet(int iRgn, bool taubit) {
00209   std::pair<double, double> p = m_RMap->getRegionCenterEtaPhi(iRgn);
00210 
00211   //double e     = m_Regions.at(iRgn).GetJetE();
00212   //double et = GCTEnergyTrunc(m_Regions.at(iRgn).GetJetEt(), m_L1Config.JetLSB, false);
00213   double et = m_Regions.at(iRgn).GetJetEt();
00214 
00215   double eta   = p.first;
00216   double phi   = p.second;
00217 
00218   if (m_L1Config.DoJetCorr) {
00219     et = GCTEnergyTrunc(corrJetEt(et,eta), m_L1Config.JetLSB, false);
00220   } else {
00221     et = GCTEnergyTrunc(et, m_L1Config.JetLSB, false);
00222   }
00223 
00224   double theta = 2.*atan(exp(-eta));
00225   double ex = et*cos(phi);
00226   double ey = et*sin(phi);
00227   //double ex = e*sin(theta)*cos(phi);
00228   //double ey = e*sin(theta)*sin(phi);
00229   double e = ex/sin(theta)/cos(phi);
00230   double ez = e*cos(theta);
00231 
00232   if (m_DoBitInfo){
00233     m_Regions[iRgn].BitInfo.setEt(et);
00234     m_Regions[iRgn].BitInfo.setEnergy(e);
00235   }
00236 
00237   reco::Particle::LorentzVector rp4(ex,ey,ez,e); 
00238   l1extra::L1JetParticle tjet(rp4);
00239   
00240   if (et>=5.) { 
00241     if (m_DoBitInfo) m_Regions[iRgn].BitInfo.setHard(true);
00242     if (m_DoBitInfo) m_Regions[iRgn].BitInfo.setSoft(false);
00243     if ((taubit || et>m_L1Config.noTauVetoLevel) && (std::abs(eta)<3.0) ) {
00244       m_TauJets.push_back(tjet);
00245       // sort by et 
00246       std::sort(m_TauJets.begin(),m_TauJets.end(), myspace::greaterEt);
00247     } else {
00248       if (std::abs(eta)<3.0) {
00249         m_CenJets.push_back(tjet);
00250         std::sort(m_CenJets.begin(),m_CenJets.end(), myspace::greaterEt);
00251       } else {
00252         m_ForJets.push_back(tjet);
00253         std::sort(m_ForJets.begin(),m_ForJets.end(), myspace::greaterEt);
00254       }
00255     }
00256   }
00257   else{  
00258     if (m_DoBitInfo) m_Regions[iRgn].BitInfo.setSoft(true);
00259     if (m_DoBitInfo) m_Regions[iRgn].BitInfo.setHard(false);
00260   } 
00261 }
00262 
00263 
00264 // ------------ Fill Egammas for TP input------------
00265 void
00266 FastL1GlobalAlgo::FillEgammasTP(edm::Event const& e) {
00267   m_Egammas.clear();
00268   m_isoEgammas.clear();
00269 
00270   l1extra::L1EmParticle* ph = new l1extra::L1EmParticle();
00271   //l1extra::L1EmParticle ph;
00272   for (int i=0; i<396; i++) { 
00273     CaloTowerCollection towers = m_Regions[i].GetCaloTowers();
00274 
00275     for (CaloTowerCollection::const_iterator cnd=towers.begin(); cnd!=towers.end(); cnd++) {
00276       if (cnd->emEt()<0.01 && cnd->hadEt()<0.01) continue;
00277       //if (cnd->emEt()<0.01) continue;
00278 
00279       reco::Particle::LorentzVector rp4(0.,0.,0.,0.);
00280       // l1extra::L1EmParticle* ph = new l1extra::L1EmParticle(rp4);
00281       *ph = l1extra::L1EmParticle(rp4);
00282 
00283       CaloTowerDetId cid   = cnd->id();
00284       
00285       int emTag = isEMCand(cid,ph,e);
00286       
00287       // 1 = non-iso EM, 2 = iso EM
00288       if (emTag==1) {
00289         m_Egammas.push_back(*ph);
00290       } else if (emTag==2) {
00291         m_isoEgammas.push_back(*ph);
00292       }
00293       
00294     }
00295     std::sort(m_Egammas.begin(),m_Egammas.end(), myspace::greaterEt);
00296     std::sort(m_isoEgammas.begin(),m_isoEgammas.end(), myspace::greaterEt);
00297   }
00298   delete ph;
00299 }
00300 
00301 
00302 // ------------ Fill Egammas for Tower input ------------
00303 void
00304 FastL1GlobalAlgo::FillEgammas(edm::Event const& e) {
00305   m_Egammas.clear();
00306   m_isoEgammas.clear();
00307 
00308   //std::vector< edm::Handle<CaloTowerCollection> > input;
00309   //e.getManyByType(input);
00310   edm::Handle<CaloTowerCollection> input;
00311   e.getByLabel(m_L1Config.TowerInput,input);
00312 
00313   //std::vector< edm::Handle<CaloTowerCollection> >::iterator j;
00314   //for (j=input.begin(); j!=input.end(); j++) {
00315   //  const CaloTowerCollection& c=*(*j);
00316   
00317   l1extra::L1EmParticle* ph = new l1extra::L1EmParticle();  
00318   //l1extra::L1EmParticle ph;
00319   //for (CaloTowerCollection::const_iterator cnd=c.begin(); cnd!=c.end(); cnd++) {
00320   for (CaloTowerCollection::const_iterator cnd=input->begin(); cnd!=input->end(); cnd++) {
00321     reco::Particle::LorentzVector rp4(0.,0.,0.,0.);
00322     //l1extra::L1EmParticle* ph = new l1extra::L1EmParticle(rp4);
00323     *ph = l1extra::L1EmParticle(rp4);
00324 
00325     CaloTowerDetId cid   = cnd->id();
00326 
00327     int emTag = isEMCand(cid,ph,e);
00328       
00329     // 1 = non-iso EM, 2 = iso EM
00330     if (emTag==1) {
00331       m_Egammas.push_back(*ph);
00332     } else if (emTag==2) {
00333       m_isoEgammas.push_back(*ph);
00334     }
00335 
00336   }
00337   //}
00338 
00339   std::sort(m_Egammas.begin(),m_Egammas.end(), myspace::greaterEt);
00340   std::sort(m_isoEgammas.begin(),m_isoEgammas.end(), myspace::greaterEt);
00341 
00342   delete ph;
00343 }
00344 
00345 // ------------ Fill MET 1: loop over towers ------------
00346 void
00347 FastL1GlobalAlgo::FillMET(edm::Event const& e) {
00348   m_METs.clear();
00349 
00350   //std::vector< edm::Handle<CaloTowerCollection> > input;
00351   //e.getManyByType(input);
00352   edm::Handle<CaloTowerCollection> input;
00353   e.getByLabel(m_L1Config.TowerInput,input);
00354 
00355   double sum_hade = 0.0;
00356   double sum_hadet = 0.0;
00357   double sum_hadex = 0.0;
00358   double sum_hadey = 0.0;
00359   double sum_hadez = 0.0;
00360   double sum_e = 0.0;
00361   double sum_et = 0.0;
00362   double sum_ex = 0.0;
00363   double sum_ey = 0.0;
00364   double sum_ez = 0.0;
00365 
00366   //std::vector< edm::Handle<CaloTowerCollection> >::iterator i;
00367 
00368   //for (i=input.begin(); i!=input.end(); i++) {
00369   //  const CaloTowerCollection& c=*(*i);
00370 
00371   //  for (CaloTowerCollection::const_iterator candidate=c.begin(); candidate!=c.end(); candidate++) {
00372   for (CaloTowerCollection::const_iterator candidate=input->begin(); candidate!=input->end(); candidate++) {
00373     //double eme    = candidate->emEnergy();
00374     //double hade    = candidate->hadEnergy();
00375     double eme    = candidate->emEt();
00376     double hade    = candidate->hadEt();
00377       
00378     double EThres = 0.;
00379     double HThres = 0.;
00380     double EBthres = m_L1Config.TowerEBThreshold;
00381     double HBthres = m_L1Config.TowerHBThreshold;
00382     double EEthres = m_L1Config.TowerEBThreshold;
00383     double HEthres = m_L1Config.TowerEEThreshold;
00384 
00385     //if(std::abs(candidate->eta())<1.479) {
00386     if(std::abs(candidate->eta())<2.322) {
00387       EThres = EBthres;
00388     } else {
00389       EThres = EEthres;
00390     }
00391     //if(std::abs(candidate->eta())<1.305) {
00392     if(std::abs(candidate->eta())<2.322) {
00393       HThres = HBthres;
00394     } else {
00395       HThres = HEthres;
00396     }
00397 
00398     // rescale energies
00399     double emScale = 1.0;
00400     double hadScale = 1.0;
00401     if (std::abs(candidate->eta()>1.3050) && std::abs(candidate->eta())<3.0) {
00402       hadScale = m_L1Config.TowerHEScale;
00403       emScale = m_L1Config.TowerEEScale;
00404     }
00405     if (std::abs(candidate->eta()<1.3050)) {
00406       hadScale = m_L1Config.TowerHBScale;
00407       emScale = m_L1Config.TowerEBScale;
00408     }
00409     eme    *= emScale;
00410     hade   *= hadScale;
00411       
00412     if (eme>=EThres || hade>=HThres) {
00413       double phi   = candidate->phi();
00414       double eta = candidate->eta();
00415       //double et    = candidate->et();
00416       //double e     = candidate->energy();
00417       double theta = 2.*atan(exp(-eta));
00418       double et    = 0.;
00419       double e     = 0.;
00420       double had_et    = 0.;
00421       double had_e     = 0.;
00422 
00423       if (eme>=EThres) {
00424         et    += candidate->emEt();
00425         e    += candidate->emEnergy();
00426       }
00427       if (hade>=HThres) {
00428         et    += candidate->hadEt();
00429         e    += candidate->hadEnergy();
00430         had_et  += candidate->hadEt();
00431         had_e   += candidate->hadEnergy();
00432       }
00433 
00434       //sum_et += et;
00435       sum_et += RCTEnergyTrunc(et,1.0,1024);
00436       sum_ex += et*cos(phi);
00437       sum_ey += et*sin(phi); 
00438       //sum_ex += e*sin(theta)*cos(phi);
00439       //sum_ey += e*sin(theta)*sin(phi); 
00440       //sum_e += e;
00441       sum_e += et/sin(theta);
00442       sum_ez += et*cos(theta)/sin(theta);
00443 
00444       sum_hadet += had_et;
00445       sum_hadex += had_et*cos(phi);
00446       sum_hadey += had_et*sin(phi); 
00447       //sum_hadex += had_e*sin(theta)*cos(phi);
00448       //sum_hadey += had_e*sin(theta)*sin(phi); 
00449       //sum_hade += had_e;
00450       sum_hade += had_et/sin(theta);
00451       sum_hadez += had_et*cos(theta)/sin(theta);
00452     }
00453   }
00454   //}
00455 
00456   reco::Particle::LorentzVector rp4(-sum_ex,-sum_ey,0.,std::sqrt(sum_ex*sum_ex + sum_ey*sum_ey));
00457   //m_MET = l1extra::L1EtMissParticle(rp4,sum_et,0.);  
00458   m_METs.push_back(l1extra::L1EtMissParticle(rp4,l1extra::L1EtMissParticle::kMET,sum_et));  
00459 }
00460 
00461 // ------------ Fill MET 2: loop over regions ------------
00462 void
00463 FastL1GlobalAlgo::FillMET() {
00464   m_METs.clear();
00465 
00466   //double sum_e = 0.0;
00467   double sum_et = 0.0;
00468   double sum_ex = 0.0;
00469   double sum_ey = 0.0;
00470   //double sum_ez = 0.0;
00471 
00472   for (int i=0; i<396; i++) { 
00473     std::pair<double, double> etaphi = m_RMap->getRegionCenterEtaPhi(i);
00474     double phi   = etaphi.second;
00475     //double eta = etaphi.first;
00476     //double theta = 2.*atan(exp(-eta));
00477     
00478     double et = m_Regions[i].SumEt();
00479     //double e = m_Regions[i].SumE();
00480 
00481     //sum_et += et;
00482     //sum_et += RCTEnergyTrunc(et,0.5,2048);
00483     //sum_et += et;
00484     //sum_ex += RCTEnergyTrunc(et*cos(phi),0.5,2048);
00485     //sum_ey += RCTEnergyTrunc(et*sin(phi),0.5,2048); 
00486     sum_ex += et*cos(phi);
00487     sum_ey += et*sin(phi); 
00488     //sum_ex += e*sin(theta)*cos(phi);
00489     //sum_ey += e*sin(theta)*sin(phi); 
00490     //sum_e += e;
00491     //sum_e += RCTEnergyTrunc(et/sin(theta),0.5,2048);
00492     //sum_ez += RCTEnergyTrunc(et*cos(theta)/sin(theta),0.5,2048);
00493     //sum_e += et/sin(theta);
00494     //sum_ez += et*cos(theta)/sin(theta);
00495     //sum_ez += e*cos(theta);
00496     //sum_et += e*sin(theta);
00497   }
00498   
00499   //sum_et = RCTEnergyTrunc(std::sqrt(sum_ex*sum_ex + sum_ey*sum_ey),0.5,2048);
00500   sum_et = std::sqrt(sum_ex*sum_ex + sum_ey*sum_ey);
00501     
00502   //reco::Particle::LorentzVector rp4(-sum_ex,-sum_ey,-sum_ez,sum_e);
00503   reco::Particle::LorentzVector rp4(-sum_ex,-sum_ey,0.,std::sqrt(sum_ex*sum_ex + sum_ey*sum_ey));
00504   //edm::LogInfo("********** FastL1GlobalAlgo::FillMET()")<<rp4.mass()<<std::endl; 
00505   //m_MET = l1extra::L1EtMissParticle(rp4,sum_et,0.);  
00506   m_METs.push_back(l1extra::L1EtMissParticle(rp4,l1extra::L1EtMissParticle::kMET,sum_et));  
00507 
00508 }
00509 
00510 void 
00511 FastL1GlobalAlgo::InitL1Regions() 
00512 {
00513   m_Regions.clear();
00514   m_Regions = std::vector<FastL1Region>(396); // ieta: 1-22, iphi: 1-18
00515 
00516   // init regions
00517   for (int i=0; i<396; i++) {
00518     m_Regions[i].SetParameters(m_L1Config);
00519     m_Regions[i].SetDoBitInfo(m_DoBitInfo);
00520     std::pair<int, int> p = m_RMap->getRegionEtaPhiIndex(i);
00521     m_Regions[i].SetEtaPhiIndex(p.first,p.second,i);
00522     CaloTower c;
00523     for (int twrid=0; twrid<16; twrid++) {
00524       m_Regions[i].FillTowerZero(c,twrid);
00525     }
00526   }  
00527 }
00528 
00529 // ------------ Fill L1 Regions on Trigger Primitives------------
00530 void 
00531 FastL1GlobalAlgo::FillL1RegionsTP(edm::Event const& e, const edm::EventSetup& s) 
00532 {
00533   //edm::ESHandle<CaloTowerConstituentsMap> cttopo;
00534   //s.get<IdealGeometryRecord>().get(cttopo);
00535   //const CaloTowerConstituentsMap* theTowerConstituentsMap = cttopo.product();
00536 
00537   InitL1Regions();
00538 
00539   edm::ESHandle<CaloGeometry> cGeom;
00540   //c.get<IdealGeometryRecord>().get(cGeom);
00541   s.get<CaloGeometryRecord>().get(cGeom);    
00542 
00543   edm::Handle<EcalTrigPrimDigiCollection> ETPinput;
00544   e.getByLabel(m_L1Config.EcalTPInput,ETPinput);
00545 
00546   edm::Handle<HcalTrigPrimDigiCollection> HTPinput;
00547   e.getByLabel(m_L1Config.HcalTPInput,HTPinput);
00548 
00549   //CaloTowerCollection towers;
00550   int hEtV  [396][16] = {{0}}; 
00551   //  int hFGV  [396][16] = {{0}};
00552   int hiEtaV[396][16] = {{0}};
00553   int hiPhiV[396][16] = {{0}};
00554   for (HcalTrigPrimDigiCollection::const_iterator hTP=HTPinput->begin(); 
00555        hTP!=HTPinput->end(); hTP++) {
00556     
00557     int rgnid = 999;
00558     int twrid = 999;
00559 
00560     int hiphi = hTP->id().iphi();
00561     int hieta = hTP->id().ieta();
00562 
00563     /*
00564       if(abs(hieta) > 20 && hiphi%2 == 0) hiphi--;
00565       std::pair<int, int> prim_tower_feed; // prim tower indeces iEta +/- 1~32, iPhi (+) 1~72
00566       prim_tower_feed.first = hieta;
00567       prim_tower_feed.second = hiphi;
00568       std::pair<int, int> rct_index = m_RMap-> getRegionEtaPhiIndex(prim_tower_feed); // convert prim tower indeces into RCT indeces ieta 0~21, iphi 0~17
00569       rgnid = rct_index.second*22 + rct_index.first; // converting fastL1 obsolete RCT numbering
00570     */
00571 
00572     rgnid  = m_RMap->getRegionIndex(hieta,hiphi);
00573     twrid = m_RMap->getRegionTowerIndex(hieta,hiphi);
00574 
00575     /*
00576       if (std::abs(htwrid.ieta())>28) {
00577       std::cout<<htwrid.ieta()<<" "<<htwrid.iphi()<<" "<<rgnid<<" "<<twrid<<" "<<std::endl;
00578       }
00579     */
00580 
00581     /*
00582     if (hTP->SOI_compressedEt()>0) {
00583       std::cout<<"hcalTP >>> "<<hTP->SOI_compressedEt()<<" "<<hTP->SOI_fineGrain()<<" "
00584                <<rgnid<<" "<<twrid<<std::endl;
00585     }
00586     */
00587     if(rgnid < 396 && twrid < 16){
00588       hEtV[rgnid][twrid] = (int)hTP->SOI_compressedEt();
00589       //      hFGV[rgnid][twrid] = (int)hTP->SOI_fineGrain();
00590       hiEtaV[rgnid][twrid] = hieta;
00591       hiPhiV[rgnid][twrid] = hiphi;
00592     }
00593   }
00594   
00595 
00596   int emEtV  [396][16] = {{0}};
00597   int emFGV  [396][16] = {{0}};
00598   int emiEtaV[396][16] = {{0}};
00599   int emiPhiV[396][16] = {{0}};
00600   //double emEtaV[396][16] = {0.};
00601   for (EcalTrigPrimDigiCollection::const_iterator eTP=ETPinput->begin(); 
00602        eTP!=ETPinput->end(); eTP++) {
00603    
00604     int eieta = eTP->id().ieta();
00605 
00606     if(abs(eieta)> 28) continue; // no crystal in HF
00607     else{
00608       int eiphi = eTP->id().iphi();
00609       //int teiphi = eiphi;
00610       
00611       int rgnid = 999;
00612       int twrid = 999;
00613       
00614       //if(abs(eieta) > 20 && eiphi%2 == 0) teiphi=eiphi-1;
00615       
00616       /*
00617         if(abs(eieta) > 20 && eiphi%2 == 0) eiphi--;
00618         std::pair<int, int> prim_tower_feed; // prim tower indeces iEta +/- 1~28, iPhi (+) 1~72
00619         prim_tower_feed.first = eieta;
00620         prim_tower_feed.second = eiphi;
00621         std::pair<int, int> rct_index = m_RMap-> getRegionEtaPhiIndex(prim_tower_feed); // convert prim tower indeces into RCT indeces ieta 0~21, iphi 0~17
00622         rgnid = rct_index.second*22 + rct_index.first; // converting fastL1 obsolete RCT numbering
00623       */
00624       
00625       /*  
00626       rgnid  = m_RMap->getRegionIndex(eieta,eiphi);
00627       twrid = m_RMap->getRegionTowerIndex(eieta,eiphi);
00628       
00629       if (eTP->compressedEt()>0) {
00630         std::cout<<"ecalTP *** "<<eTP->compressedEt()<<" "<<eTP->fineGrain()<<" "
00631                  <<rgnid<<" "<<twrid<<std::endl;
00632       }
00633       */
00634       if(rgnid < 396 && twrid < 16){
00635         emEtV[rgnid][twrid] = (int)eTP->compressedEt();
00636         emFGV[rgnid][twrid] = (int)eTP->fineGrain();
00637         emiEtaV[rgnid][twrid] = eieta;
00638         emiPhiV[rgnid][twrid] = eiphi;
00639         
00640         //edm::ESHandle<CaloGeometry> cGeom; 
00641         //s.get<IdealGeometryRecord>().get(cGeom);    
00642       
00643         //CaloTowerDetId  towerDetId = CaloTowerDetId( eieta, teiphi); 
00644         //CaloTowerDetId  towerDetId2 = CaloTowerDetId( eieta, eiphi); 
00645         //const GlobalPoint gP1 = cGeom->getPosition(towerDetId);
00646         //const GlobalPoint gP12 = cGeom->getPosition(towerDetId2);
00647         //double eta = gP1.eta();  
00648 
00649         //if(abs(eieta) > 20) 
00650         //std::cout<<eiphi<<" "<<gP1.phi()<<" "<<eieta<<" "<<gP1.eta()<<" "<<std::endl;
00651 
00652         //double phi = gP1.phi();    
00653         //emEtaV[rgnid][twrid] = eta;
00654       }
00655     } // else
00656   } // ecalTP
00657   
00658   
00659   for (int i=0; i<396; i++) {
00660     for (int j=0; j<16; j++) {
00661       if (emEtV[i][j]>0 || hEtV[i][j]>0) {
00662         
00663 
00664         std::pair<double, double> etaphi 
00665           = m_RMap->getRegionCenterEtaPhi(i);
00666         double eta = etaphi.first;
00667         double phi = etaphi.second;
00668    
00669 
00670         double emEt = ((double) emEtV[i][j]) * m_L1Config.EMLSB;
00671         //double hadEt = ((double )hEtV[i][j]) * m_L1Config.JetLSB * cos(2.*atan(exp(-hiEtaV[i][j])));
00672         int iAbsTwrEta = std::abs(hiEtaV[i][j]);
00673         //double hadEt = ((double )hcaletValue(iAbsTwrEta, hEtV[i][j])) * m_L1Config.JetLSB;
00674         double hadEt = ((double )hcaletValue(iAbsTwrEta, hEtV[i][j]));
00675         //double hadEt = hEtV[i][j] * m_L1Config.JetLSB;
00676    
00677         if (m_L1Config.DoEMCorr) {
00678           //emEt = corrEmEt(emEt,emEtaV[i][j]);
00679           emEt = corrEmEt(emEt,std::abs(emiEtaV[i][j]));
00680         }
00681 
00682         double et = emEt + hadEt;
00683         edm::ESHandle<CaloGeometry> cGeom; 
00684         //s.get<IdealGeometryRecord>().get(cGeom);    
00685         s.get<CaloGeometryRecord>().get(cGeom);    
00686 
00687 
00688         //math::RhoEtaPhiVector lvec(et,eta,phi);
00689         math::XYZTLorentzVector lvec(et,eta,phi,0.);
00690         //math::PtEtaPhiMLorentzVector lvec(et,eta,phi,0.);
00691         
00692         CaloTowerDetId towerDetId;  
00693         if (emEtV[i][j]>0) 
00694           towerDetId = CaloTowerDetId(emiEtaV[i][j],emiPhiV[i][j]); 
00695         else
00696           towerDetId = CaloTowerDetId(hiEtaV[i][j],hiPhiV[i][j]); 
00697         
00698         GlobalPoint gP = cGeom->getPosition(towerDetId);
00699         CaloTower t = CaloTower(towerDetId,  
00700                                 emEt, hadEt, 0.,
00701                                 0, 0, lvec,
00702                                 gP, gP);
00703     
00704         //m_Regions[i].FillTower_Scaled(t,j,false);
00705         m_Regions[i].FillTower_Scaled(t,j,true,cGeom);
00706     
00707           /*
00708             if (et>0) {
00709             std::cout<<"+++ "<<emEt<<" "<<hadEt<<" "
00710             <<i<<" "<<j<<" "
00711             <<std::endl<<"-- "
00712             <<t.emEt()<<" "<<t.hadEt()<<" "
00713             <<t.eta()<<" "<<t.phi()<<" "
00714             <<std::endl;
00715             }
00716           */
00717 
00718 
00719           // Explicitely take care of integer boolean conversion
00720           if (emEt > 3.0 && emEt < m_L1Config.noFGThreshold && (int)emFGV[i][j]!=0) 
00721             m_Regions[i].SetFGBit(j,true);
00722           else 
00723             m_Regions[i].SetFGBit(j,false);
00724         
00725           if (emEt > 3.0){
00726             if (emEt < 60. && hadEt/emEt >  m_L1Config.hOeThreshold)  
00727               m_Regions[i].SetHOEBit(j,true);
00728             else
00729               m_Regions[i].SetHOEBit(j,false);
00730           }
00731           else
00732             m_Regions[i].SetHOEBit(j,false);
00733 
00734           m_Regions[i].SetRegionBits(e);
00735       }
00736       
00737 
00738     }
00739   }
00740 
00741 
00742 
00743 
00744   
00745 
00746 }
00747 
00748 // ------------ Fill L1 Regions on Towers and RecHits------------
00749 void 
00750 FastL1GlobalAlgo::FillL1Regions(edm::Event const& e, const edm::EventSetup& c) 
00751 {
00752   InitL1Regions();
00753   
00754   edm::Handle<CaloTowerCollection> input;
00755   e.getByLabel(m_L1Config.TowerInput,input);
00756   
00757   edm::ESHandle<CaloTowerConstituentsMap> cttopo;
00758   c.get<IdealGeometryRecord>().get(cttopo);
00759   const CaloTowerConstituentsMap* theTowerConstituentsMap = cttopo.product();
00760   
00761   edm::ESHandle<CaloTopology> calotopo;
00762   c.get<CaloTopologyRecord>().get(calotopo);
00763   
00764   edm::ESHandle<CaloGeometry> cGeom;
00765   //c.get<IdealGeometryRecord>().get(cGeom);
00766   c.get<CaloGeometryRecord>().get(cGeom);    
00767 
00768   edm::Handle<EcalRecHitCollection> ec1;
00769   e.getByLabel(m_L1Config.EmInputs.at(1),ec1);
00770   
00771   edm::Handle<EcalRecHitCollection> ec0;
00772   e.getByLabel(m_L1Config.EmInputs.at(0),ec0);
00773   
00774   // ascii visualisation of mapping
00775   //m_RMap->display();
00776   
00777  
00778   // works for barrel/endcap region only right now!
00779   //std::vector< edm::Handle<CaloTowerCollection> > input;
00780   //e.getManyByType(input);
00781   //edm::Handle<CaloTowerCollection> input;
00782   //e.getByLabel(m_L1Config.TowerInput,input);
00783 
00784   //std::vector< edm::Handle<CaloTowerCollection> >::iterator j;
00785   //for (j=input.begin(); j!=input.end(); j++) {
00786   //  const CaloTowerCollection& c=*(*j);
00787     
00788   //  for (CaloTowerCollection::const_iterator cnd=c.begin(); cnd!=c.end(); cnd++) {
00789   for (CaloTowerCollection::const_iterator cnd=input->begin(); cnd!=input->end(); cnd++) {
00790 
00791     CaloTowerDetId cid   = cnd->id();
00792     std::pair<int, int> pep = m_RMap->getRegionEtaPhiIndex(cid);
00793  
00794     int rgnid = 999;
00795     int twrid = 999;
00796       
00797     if (std::abs(pep.first)<=22) {
00798       rgnid = pep.second*22 + pep.first;
00799       twrid = m_RMap->getRegionTowerIndex(cid);
00800       //std::cout << rgnid << " " << twrid << std::endl;
00801       //std::cout << cnd->emEt() << " " << cnd->hadEt() << std::endl;
00802     } 
00803 
00804     if (rgnid<396 && twrid<16) {
00805       m_Regions[rgnid].FillTower_Scaled(*cnd,twrid,true,cGeom);
00806       m_Regions[rgnid].SetRegionBits(e);
00807     }
00808 
00809   }
00810 
00811   //}
00812   
00813   // Fill EM Crystals
00814   for (int i=0; i<396; i++) {
00815 
00816     //m_Regions[i].FillEMCrystals(e,iConfig,m_RMap);
00817     m_Regions[i].FillEMCrystals(theTowerConstituentsMap,
00818                                 &(*calotopo),
00819                                 &(*cGeom),
00820                                 &(*ec0), &(*ec1),
00821                                 m_RMap);
00822     
00823   }
00824 
00825   //checkMapping();
00826   
00827 }
00828 
00829 
00830 // Fill Bitwords
00831 void 
00832 FastL1GlobalAlgo::FillBitInfos() {
00833   if (m_DoBitInfo){
00834     m_BitInfos.clear();
00835     for (int i=0; i<396; i++) {
00836       m_BitInfos.push_back(m_Regions[i].getBitInfo());
00837     }
00838   }
00839 }
00840 
00841 // ------------ Check if jet is taujet ------------
00842 bool 
00843 FastL1GlobalAlgo::isTauJet(int cRgn) {
00844 
00845   // Barrel and Endcap only
00846   if ((cRgn%22)<4 || (cRgn%22)>17)  
00847     return false;
00848 
00849   int shower_shape = 0; 
00850   int et_isolation = 0;
00851 
00852   if (m_Regions[cRgn].GetTauBit()) {
00853     shower_shape = 1;
00854     //if (m_DoBitInfo) m_Regions[cRgn].BitInfo.setTauVeto(true);         
00855   } else {
00856     shower_shape = 0;
00857     //if (m_DoBitInfo) m_Regions[cRgn].BitInfo.setTauVeto(false);        
00858   }
00859 
00860   int nwid = m_Regions[cRgn].GetNWId();
00861   int nid = m_Regions[cRgn].GetNorthId();
00862   int neid = m_Regions[cRgn].GetNEId();
00863   int wid = m_Regions[cRgn].GetWestId();
00864   int eid = m_Regions[cRgn].GetEastId();
00865   int swid = m_Regions[cRgn].GetSWId();
00866   int sid = m_Regions[cRgn].GetSouthId();
00867   int seid = m_Regions[cRgn].GetSEId();
00868 
00869   //Use 3x2 window at eta borders!
00870   // west border:
00871   if ((cRgn%22)==4) { 
00872     if (m_Regions[nid].GetTauBit()  ||
00873         m_Regions[neid].GetTauBit() ||
00874         m_Regions[eid].GetTauBit()  ||
00875         m_Regions[seid].GetTauBit() ||
00876         m_Regions[sid].GetTauBit() ) {
00877       et_isolation = 1;
00878     }    
00879   }
00880   
00881   // east border:
00882   if ((cRgn%22)==17) { 
00883     if (m_Regions[nid].GetTauBit()  ||
00884         m_Regions[nwid].GetTauBit() ||
00885         m_Regions[wid].GetTauBit()  ||
00886         m_Regions[swid].GetTauBit() ||
00887         m_Regions[sid].GetTauBit() ) {
00888       et_isolation = 1;
00889     }    
00890   }
00891 
00892   if ( (cRgn%22)>4 && (cRgn%22)<17){ // non-border
00893     if (nwid==999 || neid==999 || nid==999 || swid==999 || seid==999 || sid==999 || wid==999 || 
00894         eid==999 ) { 
00895       return false;
00896     }
00897     
00898     if (m_Regions[nwid].GetTauBit() ||
00899         m_Regions[nid].GetTauBit()  ||
00900         m_Regions[neid].GetTauBit() ||
00901         m_Regions[wid].GetTauBit()  ||
00902         m_Regions[eid].GetTauBit()  ||
00903         m_Regions[swid].GetTauBit() ||
00904         m_Regions[seid].GetTauBit() ||
00905         m_Regions[sid].GetTauBit()  ) {
00906       et_isolation = 1; 
00907     }    
00908   } // non-border
00909 
00910   if (m_DoBitInfo) {
00911     if (et_isolation == 1) {
00912       //std::cout<<"*********************************"<<std::endl;
00913       //std::cout<<"Rgn iso veto: "<<et_isolation<<std::endl;
00914       m_Regions[cRgn].BitInfo.setIsolationVeto(true);   
00915     } else {
00916       m_Regions[cRgn].BitInfo.setIsolationVeto(false);  
00917     }
00918     //    if (shower_shape == 1) {
00919     //std::cout<<"###############################"<<std::endl;
00920     //std::cout<<"Rgn shower veto: "<<shower_shape<<std::endl;
00921     //}
00922   }
00923   
00924   if (et_isolation == 1 || shower_shape == 1) return false;
00925   else return true;
00926 
00927 }
00928 
00929 // ------------ Check if tower is emcand ------------
00930 // returns 1 = non-iso EM, 2 = iso EM, 0 = no EM
00931 int
00932 FastL1GlobalAlgo::isEMCand(CaloTowerDetId cid, l1extra::L1EmParticle* ph,const edm::Event& iEvent) {
00933 
00934   // center tower
00935   int crgn = m_RMap->getRegionIndex(cid);
00936   int ctwr = m_RMap->getRegionTowerIndex(cid);
00937 
00938   // crystals only in barrel/endcap part
00939   if ((crgn%22)<4 || (crgn%22)>17) return 0;
00940   if (crgn>395 || crgn < 0 || ctwr > 15 || ctwr < 0) return 0;
00941 
00942   CaloTowerCollection c = m_Regions.at(crgn).GetCaloTowers();
00943   double cenEt = c[ctwr].et();
00944   //double cenEt = RCTEnergyTrunc(c[ctwr].et(),0.5,64);
00945   //double cenE = c[ctwr].emEnergy();
00946   
00947   // Using region position rather than tower position
00948   std::pair<double, double> crpos = m_RMap->getRegionCenterEtaPhi(crgn);
00949   //double cenEta = c[ctwr].eta();
00950   //double cenPhi = c[ctwr].phi();
00951   double cenEta = crpos.first;
00952   double cenPhi = crpos.second;
00953 
00954   double cenFGbit = m_Regions.at(crgn).GetFGBit(ctwr);
00955   double cenHOEbit = m_Regions.at(crgn).GetHOEBit(ctwr);
00956 
00957   if (cenEt<m_L1Config.TowerEBThreshold) return 0;
00958 
00959   // check fine grain bit
00960   if (cenFGbit) return 0;
00961 
00962   // check H/E bit
00963   if (cenHOEbit) return 0;
00964 
00965   // check neighbours
00966   std::pair<int, int> no = m_RMap->GetTowerNorthEtaPhi(cid.ieta(),cid.iphi()); 
00967   std::pair<int, int> so = m_RMap->GetTowerSouthEtaPhi(cid.ieta(),cid.iphi()); 
00968   std::pair<int, int> we = m_RMap->GetTowerWestEtaPhi(cid.ieta(),cid.iphi()); 
00969   std::pair<int, int> ea = m_RMap->GetTowerEastEtaPhi(cid.ieta(),cid.iphi()); 
00970   std::pair<int, int> nw = m_RMap->GetTowerNWEtaPhi(cid.ieta(),cid.iphi()); 
00971   std::pair<int, int> ne = m_RMap->GetTowerNEEtaPhi(cid.ieta(),cid.iphi()); 
00972   std::pair<int, int> sw = m_RMap->GetTowerSWEtaPhi(cid.ieta(),cid.iphi()); 
00973   std::pair<int, int> se = m_RMap->GetTowerSEEtaPhi(cid.ieta(),cid.iphi()); 
00974   if (no.first>29 || no.first<-29 || no.second>72 || no.second<0) return 0;
00975   if (so.first>29 || so.first<-29 || so.second>72 || so.second<0) return 0;
00976   if (we.first>29 || we.first<-29 || we.second>72 || we.second<0) return 0;
00977   if (ea.first>29 || ea.first<-29 || ea.second>72 || ea.second<0) return 0;
00978   if (nw.first>29 || nw.first<-29 || nw.second>72 || nw.second<0) return 0;
00979   if (ne.first>29 || ne.first<-29 || ne.second>72 || ne.second<0) return 0;
00980   if (sw.first>29 || sw.first<-29 || sw.second>72 || sw.second<0) return 0;
00981   if (se.first>29 || se.first<-29 || se.second>72 || se.second<0) return 0;
00982 
00983   int notwr = m_RMap->getRegionTowerIndex(no);
00984   int norgn = m_RMap->getRegionIndex(no.first,no.second);
00985   int sotwr = m_RMap->getRegionTowerIndex(so);
00986   int sorgn = m_RMap->getRegionIndex(so.first,so.second);
00987   int wetwr = m_RMap->getRegionTowerIndex(we);
00988   int wergn = m_RMap->getRegionIndex(we.first,we.second);
00989   int eatwr = m_RMap->getRegionTowerIndex(ea);
00990   int eargn = m_RMap->getRegionIndex(ea.first,ea.second);
00991   int setwr = m_RMap->getRegionTowerIndex(se);
00992   int sergn = m_RMap->getRegionIndex(se.first,sw.second);
00993   int swtwr = m_RMap->getRegionTowerIndex(sw);
00994   int swrgn = m_RMap->getRegionIndex(sw.first,sw.second);
00995   int netwr = m_RMap->getRegionTowerIndex(ne);
00996   int nergn = m_RMap->getRegionIndex(ne.first,ne.second);
00997   int nwtwr = m_RMap->getRegionTowerIndex(nw);
00998   int nwrgn = m_RMap->getRegionIndex(nw.first,nw.second);
00999   
01000   //
01001   if (norgn>395 || norgn < 0 || notwr > 15 || notwr < 0) return 0;
01002   c = m_Regions[norgn].GetCaloTowers();
01003   double noEt = c[notwr].et();
01004   //double noEt = RCTEnergyTrunc(c[notwr].et(),0.5,64);
01005   //double noE = c[notwr].emEnergy();
01006   // check fine grain bit
01007   bool noFGbit = m_Regions[norgn].GetFGBit(notwr);
01008   // check H/E bit
01009   bool noHOEbit = m_Regions[norgn].GetHOEBit(notwr);
01010 
01011   //
01012   if (sorgn>395 || sorgn < 0 || sotwr > 15 || sotwr < 0) return 0;
01013   c = m_Regions[sorgn].GetCaloTowers();
01014   double soEt = c[sotwr].et();
01015   //double soEt = RCTEnergyTrunc(c[sotwr].et(),0.5,64);
01016   //double soE = c[sotwr].emEnergy();
01017   // check fine grain bit
01018   bool soFGbit = m_Regions[sorgn].GetFGBit(sotwr);
01019   // check H/E bit
01020   bool soHOEbit = m_Regions[sorgn].GetHOEBit(sotwr);
01021 
01022   //
01023   if (wergn>395 || wergn < 0 || wetwr > 15 || wetwr < 0) return 0;
01024   c = m_Regions[wergn].GetCaloTowers();
01025   double weEt = c[wetwr].et();
01026   //double weEt = RCTEnergyTrunc(c[wetwr].et(),0.5,64);
01027   //double weE = c[wetwr].emEnergy();
01028   // check fine grain bit
01029   bool weFGbit = m_Regions[wergn].GetFGBit(wetwr);
01030   // check H/E bit
01031   bool weHOEbit = m_Regions[wergn].GetHOEBit(wetwr);
01032 
01033   //
01034   if (eargn>395 || eargn < 0 || eatwr > 15 || eatwr < 0) return 0;
01035   c = m_Regions[eargn].GetCaloTowers();
01036   double eaEt = c[eatwr].et();
01037   //double eaEt = RCTEnergyTrunc(c[eatwr].et(),0.5,64);
01038   //double eaE = c[eatwr].emEnergy();
01039   // check fine grain bit
01040   bool eaFGbit = m_Regions[eargn].GetFGBit(eatwr);
01041   // check H/E bit
01042   bool eaHOEbit = m_Regions[eargn].GetHOEBit(eatwr);
01043 
01044   //
01045   if (nwrgn>395 || nwrgn < 0 || nwtwr > 15 || nwtwr < 0) return 0;
01046   c = m_Regions[nwrgn].GetCaloTowers();
01047   double nwEt = c[nwtwr].et();
01048   //double nwEt = RCTEnergyTrunc(c[nwtwr].et(),0.5,64);
01049   //double nwE = c[nwtwr].emEnergy();
01050   // check fine grain bit
01051   bool nwFGbit = m_Regions[nwrgn].GetFGBit(nwtwr);
01052   // check H/E bit
01053   bool nwHOEbit = m_Regions[nwrgn].GetHOEBit(nwtwr);
01054 
01055   //
01056   if (nergn>395 || nergn < 0 || netwr > 15 || netwr < 0) return 0;
01057   c = m_Regions[nergn].GetCaloTowers();
01058   double neEt = c[netwr].et();
01059   //double neEt = RCTEnergyTrunc(c[netwr].et(),0.5,64);
01060   //double neE = c[netwr].emEnergy();
01061   // check fine grain bit
01062   bool neFGbit = m_Regions[nergn].GetFGBit(netwr);
01063   // check H/E bit
01064   bool neHOEbit = m_Regions[nergn].GetHOEBit(netwr);
01065 
01066   //
01067   if (swrgn>395 || swrgn < 0 || swtwr > 15 || swtwr < 0) return 0;
01068   c = m_Regions[swrgn].GetCaloTowers();
01069   double swEt = c[swtwr].et();
01070   //double swEt = RCTEnergyTrunc(c[swtwr].et(),0.5,64);
01071   //double swE = c[swtwr].emEnergy();
01072   // check fine grain bit
01073   bool swFGbit = m_Regions[swrgn].GetFGBit(swtwr);
01074   // check H/E bit
01075   bool swHOEbit = m_Regions[swrgn].GetHOEBit(swtwr);
01076 
01077   //
01078   if (sergn>395 || sergn < 0 || setwr > 15 || setwr < 0) return 0;
01079   c = m_Regions[sergn].GetCaloTowers();
01080   double seEt = c[setwr].et();
01081   //double seEt = RCTEnergyTrunc(c[setwr].et(),0.5,64);
01082   //double seE = c[setwr].emEnergy();
01083   // check fine grain bit
01084   bool seFGbit = m_Regions[sergn].GetFGBit(setwr);
01085   // check H/E bit
01086   bool seHOEbit = m_Regions[sergn].GetHOEBit(setwr);
01087 
01088   
01089   // check if highest et tower
01090   bool isHit = ( cenEt > noEt && cenEt >= soEt && cenEt > weEt &&
01091                  cenEt >= eaEt && cenEt > nwEt && cenEt > neEt &&
01092                  cenEt >= swEt && cenEt >= seEt );
01093   if (!isHit) return 0;
01094 
01095   // find highest neighbour
01096   double hitEt = cenEt;
01097   //double hitE = cenE;
01098   double maxEt = std::max(noEt,std::max(soEt,std::max(weEt,eaEt)));
01099   //double maxE = std::max(noE,std::max(soE,std::max(weE,eaE)));
01100 
01101   // check 2 tower Et
01102   float emEtThres = m_L1Config.EMSeedEnThreshold;
01103   
01104   // at this point candidate is at least non-iso Egamma
01105   //double eme = (hitE+maxE);
01106   //double eme = (hitE+maxE);
01107   double emet = (hitEt+maxEt);
01108 
01109   /*
01110     if (m_L1Config.DoEMCorr) {
01111     emet = GCTEnergyTrunc(corrEmEt(emet,cenEta),m_L1Config.EMLSB, true);
01112     //emet = GCTEnergyTrunc(emet,m_L1Config.EMLSB, true);
01113     } else {
01114     emet = GCTEnergyTrunc(emet,m_L1Config.EMLSB, true);
01115     }
01116   */
01117   emet = GCTEnergyTrunc(emet,m_L1Config.EMLSB, true);
01118 
01119   if ((emet)<emEtThres) return 0;
01120 
01121   double emtheta = 2.*atan(exp(-cenEta));
01122   //double emet = eme*sin(emtheta);
01123   double emex = emet*cos(cenPhi);
01124   double emey = emet*sin(cenPhi);
01125   //double emex = eme*sin(emtheta)*cos(cenPhi);
01126   //double emey = eme*sin(emtheta)*sin(cenPhi);
01127   double eme = emex/sin(emtheta)/cos(cenPhi);
01128   double emez = eme*cos(emtheta);
01129 
01130 
01131   reco::Particle::LorentzVector rp4(emex,emey,emez,eme);
01132   //reco::Particle::Point rp3(0.,0.,0.);
01133   //reco::Particle::Charge q(0);
01134   //*ph = reco::Photon(q,rp4,rp3);
01135   *ph = l1extra::L1EmParticle(rp4);
01136   //ph = l1extra::L1EmParticle(rp4);
01137 
01138   //std::cout<<"EM eme     : "<<eme<<std::endl;
01139   //std::cout<<"EM rp4.et(): "<<rp4.Et()<<std::endl;
01140   //std::cout<<"EM ph->et() : "<<ph->et()<<std::endl;
01141  
01142   //if (emet>0.) {
01143   //  std::cout << "em region et, eta, phi: "<< emet<<" "<< cenEta<<" "<< cenPhi<<" " << std::endl;
01144   //  std::cout << "em lv et, eta, phi: "<< ph->et()<<" "<< ph->eta()<<" "<< ph->phi()<<" " << std::endl;
01145   //}
01146 
01147   // check isolation FG bits
01148   if (noFGbit || soFGbit || weFGbit || eaFGbit || 
01149       nwFGbit || neFGbit || swFGbit || seFGbit ||
01150       noHOEbit || soHOEbit || weHOEbit || eaHOEbit || 
01151       nwHOEbit || neHOEbit || swHOEbit || seHOEbit)
01152     return 1;
01153   
01154   // check isolation corners
01155   //double corThres = 0.4;
01156   //double quietThres = m_L1Config.EMNoiseLevel;
01157   double quietThres = m_L1Config.QuietRegionThreshold;
01158   bool isoVeto1 = false,isoVeto2 = false,isoVeto3 = false,isoVeto4 = false;
01159   if (swEt>quietThres || weEt>quietThres || nwEt>quietThres || noEt>quietThres || neEt>quietThres ) {
01160     //if ((swEt + weEt + nwEt + noEt + neEt)/cenEt > corThres) 
01161     isoVeto1 = true;
01162   }
01163   if (neEt>quietThres || eaEt>quietThres || seEt>quietThres || soEt>quietThres || swEt>quietThres ) {
01164     //if ((neEt + eaEt + seEt + soEt + swEt)/cenEt > corThres) 
01165     isoVeto2 = true;
01166   }
01167   if (nwEt>quietThres || noEt>quietThres || neEt>quietThres || eaEt>quietThres || seEt>quietThres ) {
01168     //if ((nwEt + noEt + neEt + eaEt + seEt)/cenEt > corThres) 
01169     isoVeto3 = true;
01170   }
01171   if (seEt>quietThres || soEt>quietThres || swEt>quietThres || weEt>quietThres || nwEt>quietThres ) {
01172     //if ((seEt + soEt + swEt + weEt + nwEt)/cenEt > corThres) 
01173     isoVeto4 = true;
01174   }
01175   if (isoVeto1 && isoVeto2 && isoVeto3 && isoVeto4)
01176     return 1;
01177   
01178   return 2;    
01179 }
01180 
01181 
01182 // is central region the highest Et Region?
01183 bool 
01184 FastL1GlobalAlgo::isMaxEtRgn_Window33(int crgn) {
01185 
01186   int nwid = m_Regions.at(crgn).GetNWId();
01187   int nid = m_Regions.at(crgn).GetNorthId();
01188   int neid = m_Regions.at(crgn).GetNEId();
01189   int wid = m_Regions.at(crgn).GetWestId();
01190   int eid = m_Regions.at(crgn).GetEastId();
01191   int swid = m_Regions.at(crgn).GetSWId();
01192   int sid = m_Regions.at(crgn).GetSouthId();
01193   int seid = m_Regions.at(crgn).GetSEId();
01194 
01195 
01196   //Use 3x2 window at eta borders!
01197   // east border:
01198   if ((crgn%22)==21) { 
01199     
01200     if (nwid==999 || nid==999 || swid==999 || sid==999 || wid==999 ) { 
01201       return false;
01202     }
01203 
01204     double cenet = m_Regions.at(crgn).SumEt();
01205     double nwet =  m_Regions[nwid].SumEt();
01206     double noet = m_Regions[nid].SumEt();
01207     double weet = m_Regions[wid].SumEt();
01208     double swet = m_Regions[swid].SumEt();
01209     double soet = m_Regions[sid].SumEt();
01210     
01211     if ( cenet > nwet &&  cenet > noet &&
01212          cenet >= weet &&  cenet >= soet &&
01213          cenet >= swet ) 
01214       {
01215 
01216         double cene = m_Regions.at(crgn).SumE();
01217         double nwe =  m_Regions[nwid].SumE();
01218         double noe = m_Regions[nid].SumE();
01219         double wee = m_Regions[wid].SumE();
01220         double swe = m_Regions[swid].SumE();
01221         double soe = m_Regions[sid].SumE();
01222         
01223         // if region is central: jet energy is sum of 3x3 region
01224         // surrounding the central region
01225         double jE = cene + nwe + noe + wee + swe + soe;
01226         double jEt = cenet + nwet + noet + weet + swet + soet;
01227         
01228 
01229         m_Regions.at(crgn).SetJetE(jE);
01230         m_Regions.at(crgn).SetJetEt(jEt);
01231         
01232         m_Regions.at(crgn).SetJetE3x3(cene);
01233         m_Regions.at(crgn).SetJetEt3x3(cenet);
01234         
01235         return true;
01236       } else { return false; }
01237     
01238   }
01239 
01240 
01241   // west border:
01242   if ((crgn%22)==0) { 
01243     
01244     if (neid==999 || nid==999 || seid==999 || sid==999 || eid==999 ) { 
01245       return false;
01246     }
01247 
01248     double cenet = m_Regions.at(crgn).SumEt();
01249     double neet =  m_Regions[neid].SumEt();
01250     double noet = m_Regions[nid].SumEt();
01251     double eaet = m_Regions[eid].SumEt();
01252     double seet = m_Regions[seid].SumEt();
01253     double soet = m_Regions[sid].SumEt();
01254     
01255     if ( cenet > neet &&  cenet > noet &&
01256          cenet >= eaet &&  cenet >= soet &&
01257          cenet >= seet ) 
01258       {
01259 
01260         double cene = m_Regions.at(crgn).SumE();
01261         double nee =  m_Regions[neid].SumE();
01262         double noe = m_Regions[nid].SumE();
01263         double eae = m_Regions[eid].SumE();
01264         double see = m_Regions[seid].SumE();
01265         double soe = m_Regions[sid].SumE();
01266         
01267         // if region is central: jet energy is sum of 3x3 region
01268         // surrounding the central region
01269         double jE = cene + nee + noe + eae + see + soe;
01270         double jEt = cenet + neet + noet + eaet + seet + soet;
01271         
01272         m_Regions.at(crgn).SetJetE(jE);
01273         m_Regions.at(crgn).SetJetEt(jEt);
01274         
01275         m_Regions.at(crgn).SetJetE3x3(cene);
01276         m_Regions.at(crgn).SetJetEt3x3(cenet);
01277         
01278         return true;
01279       } else { return false; }
01280     
01281   }
01282 
01283 
01284   if (nwid==999 || neid==999 || nid==999 || swid==999 || seid==999 || sid==999 || wid==999 || 
01285       eid==999 ) { 
01286     //std::cerr << "FastL1GlobalAlgo::isMaxEtRgn_Window33(): RegionId out of bounds: " << std::endl
01287     //      << nwid << " " << nid << " "  << neid << " " << std::endl
01288     //      << wid << " " << crgn << " "  << eid << " " << std::endl
01289     //      << swid << " " << sid << " "  << seid << " " << std::endl;    
01290     return false;
01291   }
01292 
01293 
01294   double cenet = m_Regions.at(crgn).SumEt();
01295   double nwet =  m_Regions[nwid].SumEt();
01296   double noet = m_Regions[nid].SumEt();
01297   double neet = m_Regions[neid].SumEt();
01298   double weet = m_Regions[wid].SumEt();
01299   double eaet = m_Regions[eid].SumEt();
01300   double swet = m_Regions[swid].SumEt();
01301   double soet = m_Regions[sid].SumEt();
01302   double seet = m_Regions[seid].SumEt();
01303 
01304   if ( cenet > nwet &&  cenet > noet &&
01305        cenet > neet &&  cenet >= eaet &&
01306        cenet > weet &&  cenet >= soet &&
01307        cenet >= swet &&  cenet >= seet ) 
01308     {
01309 
01310       double cene = m_Regions.at(crgn).SumE();
01311       double nwe =  m_Regions[nwid].SumE();
01312       double noe = m_Regions[nid].SumE();
01313       double nee = m_Regions[neid].SumE();
01314       double wee = m_Regions[wid].SumE();
01315       double eae = m_Regions[eid].SumE();
01316       double swe = m_Regions[swid].SumE();
01317       double soe = m_Regions[sid].SumE();
01318       double see = m_Regions[seid].SumE();
01319 
01320       // if region is central: jet energy is sum of 3x3 region
01321       // surrounding the central region
01322       double jE = cene + nwe + noe + nee + wee + eae + swe + soe + see;
01323       double jEt = cenet + nwet + noet + neet + weet + eaet + swet + soet + seet;
01324 
01325 
01326       m_Regions.at(crgn).SetJetE(jE);
01327       m_Regions.at(crgn).SetJetEt(jEt);
01328 
01329       m_Regions.at(crgn).SetJetE3x3(cene);
01330       m_Regions.at(crgn).SetJetEt3x3(cenet);
01331       
01332       return true;
01333     } else { return false; }
01334 
01335 }
01336 
01337 
01338 void
01339 FastL1GlobalAlgo::checkMapping() {
01340 
01341   // loop over towers ieta,iphi
01342   for (int j=1;j<=72;j++) {
01343     for (int i=-28; i<=28; i++) {
01344       if (i==0) continue;
01345       int iRgn =  m_RMap->getRegionIndex(i,j);
01346       std::pair<double, double> RgnEtaPhi = m_RMap->getRegionCenterEtaPhi(iRgn);
01347       //int iTwr = m_RMap->getRegionTowerIndex(i,j);
01348       std::pair<int, int> iRgnEtaPhi = m_RMap->getRegionEtaPhiIndex(iRgn);
01349       std::pair<int, int> iRgnEtaPhi2 = m_RMap->getRegionEtaPhiIndex(std::pair<int, int>(i,j));
01350 
01351       std::cout<<"---------------------------------------------------------------------------"<<std::endl;
01352       std::cout<<"Region:   "<<iRgn<<" | "<<RgnEtaPhi.first<<", "<<RgnEtaPhi.second*180./3.141<<std::endl;
01353       std::cout<<"   -      "<<iRgnEtaPhi.first<<", "<<iRgnEtaPhi.second<<std::endl;
01354       std::cout<<"   -      "<<iRgnEtaPhi2.first<<", "<<iRgnEtaPhi2.second<<std::endl;
01355       std::cout<<" Tower:   "<<i<<", "<<m_RMap->convertFromECal_to_HCal_iphi(j)<<std::endl;
01356       std::cout<<" TowerId: "<<m_RMap->getRegionTowerIndex(i,j)<<std::endl;
01357 
01358     }
01359   }
01360 
01361 }
01362 
01363 double
01364 FastL1GlobalAlgo::hcaletValue(const int ieta,const int compET) {
01365   double etvalue = m_hcaluncomp[ieta][compET];//*cos(eta_ave);
01366   return etvalue;
01367 }
01368 
01369 // Et Check      
01370 bool     
01371 FastL1GlobalAlgo::TauIsolation(int cRgn) {       
01372                  
01373   if ((cRgn%22)<4 || (cRgn%22)>17) return false;         
01374                  
01375   double iso_threshold =  m_IsolationEt; // arbitrarily set      
01376   int shower_shape = 0;          
01377   int et_isolation = 0;          
01378   unsigned int iso_count = 0;    
01379                  
01380   int nwid = m_Regions[cRgn].GetNWId();          
01381   int nid = m_Regions[cRgn].GetNorthId();        
01382   int neid = m_Regions[cRgn].GetNEId();          
01383   int wid = m_Regions[cRgn].GetWestId();         
01384   int eid = m_Regions[cRgn].GetEastId();         
01385   int swid = m_Regions[cRgn].GetSWId();          
01386   int sid = m_Regions[cRgn].GetSouthId();        
01387   int seid = m_Regions[cRgn].GetSEId();          
01388                  
01389   if (m_Regions[cRgn].GetTauBit()) {  // check center 
01390     shower_shape = 1;    
01391     //if (m_DoBitInfo) m_Regions[cRgn].BitInfo.setTauVeto(true);         
01392   } 
01393 
01394   if((cRgn%22)==4  || (cRgn%22)==17 ) {          
01395     // west border       
01396     if ((cRgn%22)==4) {          
01397       if( m_Regions[neid].SumEt() > iso_threshold){      
01398         iso_count ++;    
01399         if (m_Regions[neid].GetTauBit()) iso_count++;    
01400       }          
01401       if( m_Regions[nid].SumEt() > iso_threshold){       
01402         iso_count ++;    
01403         if (m_Regions[nid].GetTauBit()) iso_count++;     
01404       }          
01405       if( m_Regions[eid].SumEt() > iso_threshold){       
01406         iso_count ++;    
01407         if (m_Regions[eid].GetTauBit()) iso_count++;     
01408       }          
01409       if( m_Regions[seid].SumEt() > iso_threshold){      
01410         iso_count ++;    
01411         if (m_Regions[seid].GetTauBit()) iso_count++;    
01412       }          
01413       if( m_Regions[sid].SumEt() > iso_threshold){       
01414         iso_count ++;    
01415         if (m_Regions[sid].GetTauBit()) iso_count++;     
01416       }          
01417     } // west bd         
01418                  
01419     // east border:      
01420     if ((cRgn%22)==17) {         
01421       if( m_Regions[nwid].SumEt() > iso_threshold){      
01422         iso_count ++;    
01423         if (m_Regions[nwid].GetTauBit()) iso_count++;    
01424       }          
01425       if( m_Regions[nid].SumEt() > iso_threshold){       
01426         iso_count ++;    
01427         if (m_Regions[nid].GetTauBit()) iso_count++;     
01428       }          
01429       if( m_Regions[wid].SumEt() > iso_threshold){       
01430         iso_count ++;    
01431         if (m_Regions[wid].GetTauBit()) iso_count++;     
01432       }          
01433       if( m_Regions[swid].SumEt() > iso_threshold){      
01434         iso_count ++;    
01435         if (m_Regions[swid].GetTauBit()) iso_count++;    
01436       }          
01437       if( m_Regions[sid].SumEt() > iso_threshold){       
01438         iso_count ++;    
01439         if (m_Regions[sid].GetTauBit()) iso_count++;     
01440       }          
01441     } // east bd         
01442                  
01443   }      
01444                  
01445   if ( (cRgn%22)>4 && (cRgn%22)<17){ // non-border       
01446     if (nwid==999 || neid==999 || nid==999 || swid==999 || seid==999 || sid==999 || wid==999 ||          
01447         eid==999 ) {     
01448       return false;      
01449     }    
01450                  
01451     if( m_Regions[neid].SumEt() > iso_threshold){        
01452       iso_count ++;      
01453       if (m_Regions[neid].GetTauBit()) iso_count++;      
01454     }    
01455     if( m_Regions[nid].SumEt() > iso_threshold){         
01456       iso_count ++;      
01457       if (m_Regions[nid].GetTauBit()) iso_count++;       
01458     }    
01459     if( m_Regions[eid].SumEt() > iso_threshold){         
01460       iso_count ++;      
01461       if (m_Regions[eid].GetTauBit()) iso_count++;       
01462     }    
01463     if( m_Regions[seid].SumEt() > iso_threshold){        
01464       iso_count ++;      
01465       if (m_Regions[seid].GetTauBit()) iso_count++;      
01466     }    
01467     if( m_Regions[sid].SumEt() > iso_threshold){         
01468       iso_count ++;      
01469       if (m_Regions[sid].GetTauBit()) iso_count++;       
01470     }    
01471     if( m_Regions[nwid].SumEt() > iso_threshold){        
01472       iso_count ++;      
01473       if (m_Regions[nwid].GetTauBit()) iso_count++;      
01474     }    
01475     if( m_Regions[wid].SumEt() > iso_threshold){         
01476       iso_count ++;      
01477       if (m_Regions[wid].GetTauBit()) iso_count++;       
01478     }    
01479     if( m_Regions[swid].SumEt() > iso_threshold){        
01480       iso_count ++;      
01481       if (m_Regions[swid].GetTauBit()) iso_count++;      
01482     }    
01483   }// non-border         
01484                  
01485                  
01486   if (iso_count >= 2 ){          
01487     et_isolation = 1;    
01488   }      
01489   else {
01490     et_isolation = 0;
01491   }      
01492                  
01493   if (m_DoBitInfo){      
01494     if (et_isolation == 1) 
01495       m_Regions[cRgn].BitInfo.setIsolationVeto (true);   
01496     else
01497       m_Regions[cRgn].BitInfo.setIsolationVeto (false);          
01498   }      
01499 
01500   if (et_isolation == 1 || shower_shape == 1) return false; 
01501   else return true;
01502   
01503 }//
01504